Split mutation algorithms into own submodule.

Clean up inter-package imports.
This commit is contained in:
H. S. Teoh 2015-01-17 21:50:11 -08:00
parent b476a82b5b
commit 5cae4f0cb5
7 changed files with 2148 additions and 2093 deletions

View file

@ -197,7 +197,7 @@ STD_REGEX_MODULES = $(addprefix std/regex/, package $(addprefix internal/, \
generator ir parser backtracking kickstart tests thompson)) generator ir parser backtracking kickstart tests thompson))
STD_ALGO_MODULES = $(addprefix std/algorithm/, package comparison iteration \ STD_ALGO_MODULES = $(addprefix std/algorithm/, package comparison iteration \
searching setops sorting) mutation searching setops sorting)
STD_RANGE_MODULES = $(addprefix std/range/, package primitives interfaces) STD_RANGE_MODULES = $(addprefix std/range/, package primitives interfaces)

View file

@ -877,7 +877,7 @@ struct Levenshtein(Range, alias equals, CostType = size_t)
EditOp[] path() EditOp[] path()
{ {
import std.algorithm : reverse; // FIXME import std.algorithm.mutation : reverse;
EditOp[] result; EditOp[] result;
size_t i = rows - 1, j = cols - 1; size_t i = rows - 1, j = cols - 1;

View file

@ -2534,7 +2534,7 @@ auto splitter(alias pred = "a == b", Range, Separator)(Range r, Separator s)
if (is(typeof(binaryFun!pred(r.front, s)) : bool) if (is(typeof(binaryFun!pred(r.front, s)) : bool)
&& ((hasSlicing!Range && hasLength!Range) || isNarrowString!Range)) && ((hasSlicing!Range && hasLength!Range) || isNarrowString!Range))
{ {
import std.algorithm : find; // FIXME import std.algorithm.searching : find;
import std.conv : unsigned; import std.conv : unsigned;
static struct Result static struct Result
@ -2815,7 +2815,7 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
&& isForwardRange!Separator && isForwardRange!Separator
&& (hasLength!Separator || isNarrowString!Separator)) && (hasLength!Separator || isNarrowString!Separator))
{ {
import std.algorithm : find; // FIXME import std.algorithm.searching : find;
import std.conv : unsigned; import std.conv : unsigned;
static struct Result static struct Result
@ -3139,7 +3139,7 @@ if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(input.front))))
private struct SplitterResult(alias isTerminator, Range) private struct SplitterResult(alias isTerminator, Range)
{ {
import std.algorithm : find; // FIXME import std.algorithm.searching : find;
enum fullSlicing = (hasLength!Range && hasSlicing!Range) || isSomeString!Range; enum fullSlicing = (hasLength!Range && hasSlicing!Range) || isSomeString!Range;
private Range _input; private Range _input;
@ -3353,7 +3353,7 @@ Returns:
auto splitter(C)(C[] s) auto splitter(C)(C[] s)
if (isSomeChar!C) if (isSomeChar!C)
{ {
import std.algorithm : find; // FIXME import std.algorithm.searching : find;
static struct Result static struct Result
{ {
private: private:
@ -3745,8 +3745,9 @@ if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))
/// ///
@safe unittest @safe unittest
{ {
import std.algorithm : copy; // FIXME import std.algorithm.mutation : copy;
import std.algorithm.comparison : equal; import std.algorithm.comparison : equal;
int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ]; int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ];
assert(equal(uniq(arr), [ 1, 2, 3, 4, 5 ][])); assert(equal(uniq(arr), [ 1, 2, 3, 4, 5 ][]));

2120
std/algorithm/mutation.d Normal file

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -73,7 +73,7 @@ auto cartesianProduct(R1, R2)(R1 range1, R2 range2)
/// ///
@safe unittest @safe unittest
{ {
import std.algorithm : canFind; // FIXME import std.algorithm.searching : canFind;
import std.range; import std.range;
import std.typecons : tuple; import std.typecons : tuple;
@ -90,7 +90,7 @@ auto cartesianProduct(R1, R2)(R1 range1, R2 range2)
/// ///
@safe unittest @safe unittest
{ {
import std.algorithm : canFind; // FIXME import std.algorithm.searching : canFind;
import std.typecons : tuple; import std.typecons : tuple;
auto B = [ 1, 2, 3 ]; auto B = [ 1, 2, 3 ];
@ -107,7 +107,7 @@ auto cartesianProduct(R1, R2)(R1 range1, R2 range2)
@safe unittest @safe unittest
{ {
// Test cartesian product of two infinite ranges // Test cartesian product of two infinite ranges
import std.algorithm : canFind; // FIXME import std.algorithm.searching : canFind;
import std.range; import std.range;
import std.typecons : tuple; import std.typecons : tuple;
@ -131,7 +131,7 @@ auto cartesianProduct(R1, R2)(R1 range1, R2 range2)
{ {
// Test cartesian product of an infinite input range and a finite forward // Test cartesian product of an infinite input range and a finite forward
// range. // range.
import std.algorithm : canFind; // FIXME import std.algorithm.searching : canFind;
import std.range; import std.range;
import std.typecons : tuple; import std.typecons : tuple;
@ -169,7 +169,7 @@ auto cartesianProduct(R1, R2)(R1 range1, R2 range2)
@safe unittest @safe unittest
{ {
import std.algorithm : canFind; // FIXME import std.algorithm.searching : canFind;
import std.typecons : tuple; import std.typecons : tuple;
// Test cartesian product of two finite ranges. // Test cartesian product of two finite ranges.
@ -196,7 +196,7 @@ auto cartesianProduct(R1, R2)(R1 range1, R2 range2)
@safe unittest @safe unittest
{ {
import std.algorithm : canFind; // FIXME import std.algorithm.searching : canFind;
import std.algorithm.comparison : equal; import std.algorithm.comparison : equal;
import std.algorithm.iteration : map; import std.algorithm.iteration : map;
import std.typecons : tuple; import std.typecons : tuple;
@ -416,7 +416,7 @@ auto cartesianProduct(R1, R2, RR...)(R1 range1, R2 range2, RR otherRanges)
@safe unittest @safe unittest
{ {
import std.algorithm : canFind; // FIXME import std.algorithm.searching : canFind;
import std.range; import std.range;
import std.typecons : tuple, Tuple; import std.typecons : tuple, Tuple;
@ -433,7 +433,7 @@ auto cartesianProduct(R1, R2, RR...)(R1 range1, R2 range2, RR otherRanges)
@safe unittest @safe unittest
{ {
import std.algorithm : canFind; // FIXME import std.algorithm.searching : canFind;
import std.range; import std.range;
import std.typecons : tuple, Tuple; import std.typecons : tuple, Tuple;
@ -722,7 +722,7 @@ struct NWayUnion(alias less, RangeOfRanges)
this(RangeOfRanges ror) this(RangeOfRanges ror)
{ {
import std.algorithm : remove, SwapStrategy; // FIXME import std.algorithm.mutation : remove, SwapStrategy;
// Preemptively get rid of all empty ranges in the input // Preemptively get rid of all empty ranges in the input
// No need for stability either // No need for stability either

View file

@ -1,7 +1,8 @@
// Written in the D programming language. // Written in the D programming language.
module std.algorithm.sorting; module std.algorithm.sorting;
import std.algorithm : SortOutput, SwapStrategy; // FIXME import std.algorithm : SortOutput; // FIXME
import std.algorithm.mutation : SwapStrategy;
import std.functional; // : unaryFun, binaryFun; import std.functional; // : unaryFun, binaryFun;
import std.range.primitives; import std.range.primitives;
// FIXME // FIXME
@ -669,6 +670,8 @@ template multiSort(less...) //if (less.length > 1)
private size_t getPivot(alias less, Range)(Range r) private size_t getPivot(alias less, Range)(Range r)
{ {
import std.algorithm.mutation : swapAt;
// This algorithm sorts the first, middle and last elements of r, // This algorithm sorts the first, middle and last elements of r,
// then returns the index of the middle element. In effect, it uses the // then returns the index of the middle element. In effect, it uses the
// median-of-three heuristic. // median-of-three heuristic.
@ -709,6 +712,8 @@ private size_t getPivot(alias less, Range)(Range r)
private void optimisticInsertionSort(alias less, Range)(Range r) private void optimisticInsertionSort(alias less, Range)(Range r)
{ {
import std.algorithm.mutation : swapAt;
alias pred = binaryFun!(less); alias pred = binaryFun!(less);
if (r.length < 2) if (r.length < 2)
{ {
@ -859,6 +864,7 @@ unittest
unittest unittest
{ {
import std.algorithm : rndstuff; // FIXME import std.algorithm : rndstuff; // FIXME
import std.algorithm : swapRanges; // FIXME
import std.random : Random, unpredictableSeed, uniform; import std.random : Random, unpredictableSeed, uniform;
import std.uni : toUpper; import std.uni : toUpper;
@ -918,8 +924,6 @@ unittest
// Unstable sort should complete without an excessive number of predicate calls // Unstable sort should complete without an excessive number of predicate calls
// This would suggest it's running in quadratic time // This would suggest it's running in quadratic time
import std.algorithm : swapRanges; // FIXME
// Compilation error if predicate is not static, i.e. a nested function // Compilation error if predicate is not static, i.e. a nested function
static uint comp; static uint comp;
static bool pred(size_t a, size_t b) static bool pred(size_t a, size_t b)
@ -959,27 +963,10 @@ unittest
} }
} }
//private
void swapAt(R)(R r, size_t i1, size_t i2)
{
import std.algorithm : swap; // FIXME
static if (is(typeof(&r[i1])))
{
swap(r[i1], r[i2]);
}
else
{
if (i1 == i2) return;
auto t1 = moveAt(r, i1);
auto t2 = moveAt(r, i2);
r[i2] = t1;
r[i1] = t2;
}
}
private void quickSortImpl(alias less, Range)(Range r, size_t depth) private void quickSortImpl(alias less, Range)(Range r, size_t depth)
{ {
import std.algorithm : swap; // FIXME import std.algorithm : swap; // FIXME
import std.algorithm.mutation : swapAt;
import std.algorithm.comparison : min; import std.algorithm.comparison : min;
alias Elem = ElementType!(Range); alias Elem = ElementType!(Range);
@ -1038,6 +1025,8 @@ private void quickSortImpl(alias less, Range)(Range r, size_t depth)
// Bottom-Up Heap-Sort Implementation // Bottom-Up Heap-Sort Implementation
private template HeapSortImpl(alias less, Range) private template HeapSortImpl(alias less, Range)
{ {
import std.algorithm.mutation : swapAt;
static assert(isRandomAccessRange!Range); static assert(isRandomAccessRange!Range);
static assert(hasLength!Range); static assert(hasLength!Range);
static assert(hasSwappableElements!Range || hasAssignableElements!Range); static assert(hasSwappableElements!Range || hasAssignableElements!Range);