whitespace & std.algortihm style unification

This commit is contained in:
Dmitry Olshansky 2012-11-23 23:18:12 +04:00
parent 585a9a241a
commit dca2a007a2

View file

@ -555,7 +555,7 @@ unittest
auto intRange = map!"a"([1,2,3]); auto intRange = map!"a"([1,2,3]);
static assert(isRandomAccessRange!(typeof(intRange))); static assert(isRandomAccessRange!(typeof(intRange)));
foreach(DummyType; AllDummyRanges) foreach (DummyType; AllDummyRanges)
{ {
DummyType d; DummyType d;
auto m = map!"a * a"(d); auto m = map!"a * a"(d);
@ -666,7 +666,7 @@ template reduce(fun...) if (fun.length >= 1)
auto reduce(Args...)(Args args) auto reduce(Args...)(Args args)
if (Args.length > 0 && Args.length <= 2 && isIterable!(Args[$ - 1])) if (Args.length > 0 && Args.length <= 2 && isIterable!(Args[$ - 1]))
{ {
static if(isInputRange!(Args[$ - 1])) static if (isInputRange!(Args[$ - 1]))
{ {
static if (Args.length == 2) static if (Args.length == 2)
{ {
@ -724,9 +724,9 @@ template reduce(fun...) if (fun.length >= 1)
alias Args[$ - 1] R; alias Args[$ - 1] R;
alias ForeachType!R E; alias ForeachType!R E;
static if(args.length == 2) static if (args.length == 2)
{ {
static if(fun.length == 1) static if (fun.length == 1)
{ {
auto result = Tuple!(Unqual!(Args[0]))(args[0]); auto result = Tuple!(Unqual!(Args[0]))(args[0]);
} }
@ -737,7 +737,7 @@ template reduce(fun...) if (fun.length >= 1)
enum bool initialized = true; enum bool initialized = true;
} }
else static if(fun.length == 1) else static if (fun.length == 1)
{ {
Tuple!(typeof(binaryFun!fun(E.init, E.init))) result = void; Tuple!(typeof(binaryFun!fun(E.init, E.init))) result = void;
bool initialized = false; bool initialized = false;
@ -751,18 +751,18 @@ template reduce(fun...) if (fun.length >= 1)
// For now, just iterate using ref to avoid unnecessary copying. // For now, just iterate using ref to avoid unnecessary copying.
// When Bug 2443 is fixed, this may need to change. // When Bug 2443 is fixed, this may need to change.
foreach(ref elem; r) foreach (ref elem; r)
{ {
if(initialized) if (initialized)
{ {
foreach(i, T; result.Types) foreach (i, T; result.Types)
{ {
result[i] = binaryFun!(fun[i])(result[i], elem); result[i] = binaryFun!(fun[i])(result[i], elem);
} }
} }
else else
{ {
static if(is(typeof(&initialized))) static if (is(typeof(&initialized)))
{ {
initialized = true; initialized = true;
} }
@ -777,7 +777,7 @@ template reduce(fun...) if (fun.length >= 1)
enforce(initialized, enforce(initialized,
"Cannot reduce an empty iterable w/o an explicit seed value."); "Cannot reduce an empty iterable w/o an explicit seed value.");
static if(fun.length == 1) static if (fun.length == 1)
{ {
return result[0]; return result[0];
} }
@ -823,12 +823,12 @@ unittest
int opApply(int delegate(ref int) dg) int opApply(int delegate(ref int) dg)
{ {
int res; int res;
if(actEmpty) return res; if (actEmpty) return res;
foreach(i; 0..100) foreach (i; 0..100)
{ {
res = dg(i); res = dg(i);
if(res) break; if (res) break;
} }
return res; return res;
} }
@ -918,8 +918,8 @@ unittest
alias DummyRange!(ReturnBy.Reference, Length.No, RangeType.Input) InputRange; alias DummyRange!(ReturnBy.Reference, Length.No, RangeType.Input) InputRange;
enum filler = uint.max; enum filler = uint.max;
InputRange range; InputRange range;
fill(range,filler); fill(range, filler);
foreach(value;range.arr) foreach (value; range.arr)
assert(value == filler); assert(value == filler);
} }
unittest unittest
@ -975,13 +975,13 @@ void fill(Range1, Range2)(Range1 range, Range2 filler)
|| (isInputRange!Range2 && isInfinite!Range2)) || (isInputRange!Range2 && isInfinite!Range2))
&& is(typeof(Range1.init.front = Range2.init.front))) && is(typeof(Range1.init.front = Range2.init.front)))
{ {
static if(isInfinite!Range2) static if (isInfinite!Range2)
{ {
//Range2 is infinite, no need for bounds checking or saving //Range2 is infinite, no need for bounds checking or saving
static if(hasSlicing!Range2 && hasLength!Range1 static if (hasSlicing!Range2 && hasLength!Range1
&& is(typeof(filler[0 .. range.length]))) && is(typeof(filler[0 .. range.length])))
{ {
copy(filler[0..range.length], range); copy(filler[0 .. range.length], range);
} }
else else
{ {
@ -996,7 +996,7 @@ void fill(Range1, Range2)(Range1 range, Range2 filler)
{ {
enforce(!filler.empty, "Cannot fill range with an empty filler"); enforce(!filler.empty, "Cannot fill range with an empty filler");
static if(hasLength!Range1 && hasLength!Range2 static if (hasLength!Range1 && hasLength!Range2
&& is(typeof(range.length > filler.length))) && is(typeof(range.length > filler.length)))
{ {
//Case we have access to length //Case we have access to length
@ -1048,7 +1048,7 @@ unittest
alias DummyRange!(ReturnBy.Reference, Length.No, RangeType.Input) InputRange; alias DummyRange!(ReturnBy.Reference, Length.No, RangeType.Input) InputRange;
InputRange range; InputRange range;
fill(range,[1,2]); fill(range,[1,2]);
foreach(i,value;range.arr) foreach (i,value;range.arr)
assert(value == (i%2==0?1:2)); assert(value == (i%2==0?1:2));
//test with a input being a "reference forward" range //test with a input being a "reference forward" range
@ -1253,7 +1253,7 @@ private struct FilterResult(alias pred, Range)
return _input.front; return _input.front;
} }
static if(isForwardRange!R) static if (isForwardRange!R)
{ {
@property auto save() @property auto save()
{ {
@ -1285,7 +1285,7 @@ unittest
static assert(isInfinite!(typeof(infinite))); static assert(isInfinite!(typeof(infinite)));
static assert(isForwardRange!(typeof(infinite))); static assert(isForwardRange!(typeof(infinite)));
foreach(DummyType; AllDummyRanges) { foreach (DummyType; AllDummyRanges) {
DummyType d; DummyType d;
auto f = filter!"a & 1"(d); auto f = filter!"a & 1"(d);
assert(equal(f, [1,3,5,7,9])); assert(equal(f, [1,3,5,7,9]));
@ -1803,9 +1803,9 @@ if (isMutable!T && !is(typeof(T.init.proxySwap(T.init))))
//types and therefore all of their elements get copied as part of //types and therefore all of their elements get copied as part of
//assigning them, which would be assigning overlapping arrays if lhs //assigning them, which would be assigning overlapping arrays if lhs
//and rhs were the same array. //and rhs were the same array.
static if(isStaticArray!T) static if (isStaticArray!T)
{ {
if(lhs.ptr == rhs.ptr) if (lhs.ptr == rhs.ptr)
return; return;
} }
@ -1949,7 +1949,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
IndexType _frontLength = _unComputed; IndexType _frontLength = _unComputed;
IndexType _backLength = _unComputed; IndexType _backLength = _unComputed;
static if(isBidirectionalRange!Range) static if (isBidirectionalRange!Range)
{ {
static IndexType lastIndexOf(Range haystack, Separator needle) static IndexType lastIndexOf(Range haystack, Separator needle)
{ {
@ -2012,7 +2012,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
} }
} }
static if(isForwardRange!Range) static if (isForwardRange!Range)
{ {
@property typeof(this) save() @property typeof(this) save()
{ {
@ -2022,7 +2022,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
} }
} }
static if(isBidirectionalRange!Range) static if (isBidirectionalRange!Range)
{ {
@property Range back() @property Range back()
{ {
@ -2030,7 +2030,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
if (_backLength == _unComputed) if (_backLength == _unComputed)
{ {
immutable lastIndex = lastIndexOf(_input, _separator); immutable lastIndex = lastIndexOf(_input, _separator);
if(lastIndex == -1) if (lastIndex == -1)
{ {
_backLength = _input.length; _backLength = _input.length;
} }
@ -2123,8 +2123,8 @@ unittest
assert(split.front == "b "); assert(split.front == "b ");
assert(split.back == "r "); assert(split.back == "r ");
foreach(DummyType; AllDummyRanges) { // Bug 4408 foreach (DummyType; AllDummyRanges) { // Bug 4408
static if(isRandomAccessRange!DummyType) { static if (isRandomAccessRange!DummyType) {
static assert(isBidirectionalRange!DummyType); static assert(isBidirectionalRange!DummyType);
DummyType d; DummyType d;
auto s = splitter(d, 5); auto s = splitter(d, 5);
@ -2250,7 +2250,7 @@ if (is(typeof(Range.init.front == Separator.init.front) : bool)
_frontLength = _frontLength.max; _frontLength = _frontLength.max;
} }
static if(isForwardRange!Range) static if (isForwardRange!Range)
{ {
@property typeof(this) save() @property typeof(this) save()
{ {
@ -2432,7 +2432,7 @@ private struct SplitterResult(alias isTerminator, Range)
} }
} }
static if(isForwardRange!Range) static if (isForwardRange!Range)
{ {
@property typeof(this) save() @property typeof(this) save()
{ {
@ -2479,9 +2479,9 @@ unittest
static assert(isForwardRange!(typeof(splitter!"a == ' '"("ABC")))); static assert(isForwardRange!(typeof(splitter!"a == ' '"("ABC"))));
foreach(DummyType; AllDummyRanges) foreach (DummyType; AllDummyRanges)
{ {
static if(isRandomAccessRange!DummyType) static if (isRandomAccessRange!DummyType)
{ {
auto rangeSplit = splitter!"a == 5"(DummyType.init); auto rangeSplit = splitter!"a == 5"(DummyType.init);
assert(equal(rangeSplit.front, [1,2,3,4])); assert(equal(rangeSplit.front, [1,2,3,4]));
@ -2869,7 +2869,7 @@ unittest
assert(equal(r, [ 1, 2, 3, 4, 5 ][])); assert(equal(r, [ 1, 2, 3, 4, 5 ][]));
assert(equal(retro(r), retro([ 1, 2, 3, 4, 5 ][]))); assert(equal(retro(r), retro([ 1, 2, 3, 4, 5 ][])));
foreach(DummyType; AllDummyRanges) { foreach (DummyType; AllDummyRanges) {
DummyType d; DummyType d;
auto u = uniq(d); auto u = uniq(d);
assert(equal(u, [1,2,3,4,5,6,7,8,9,10])); assert(equal(u, [1,2,3,4,5,6,7,8,9,10]));
@ -2974,7 +2974,7 @@ unittest
tuple(4, 3u), tuple(5, 1u) ][])); tuple(4, 3u), tuple(5, 1u) ][]));
static assert(isForwardRange!(typeof(group(arr)))); static assert(isForwardRange!(typeof(group(arr))));
foreach(DummyType; AllDummyRanges) { foreach (DummyType; AllDummyRanges) {
DummyType d; DummyType d;
auto g = group(d); auto g = group(d);
@ -3568,7 +3568,7 @@ unittest
assert(find(a, b) == [ 1, 2, 3, 4, 5 ]); assert(find(a, b) == [ 1, 2, 3, 4, 5 ]);
assert(find(b, a).empty); assert(find(b, a).empty);
foreach(DummyType; AllDummyRanges) { foreach (DummyType; AllDummyRanges) {
DummyType d; DummyType d;
auto findRes = find(d, 5); auto findRes = find(d, 5);
assert(equal(findRes, [5,6,7,8,9,10])); assert(equal(findRes, [5,6,7,8,9,10]));
@ -4372,7 +4372,7 @@ if (isInputRange!R1 &&
alias doesThisStart haystack; alias doesThisStart haystack;
alias withThis needle; alias withThis needle;
static if(is(typeof(pred) : string)) static if (is(typeof(pred) : string))
enum isDefaultPred = pred == "a == b"; enum isDefaultPred = pred == "a == b";
else else
enum isDefaultPred = false; enum isDefaultPred = false;
@ -4464,7 +4464,7 @@ unittest
} }
} }
foreach(T; TypeTuple!(int, short)) foreach (T; TypeTuple!(int, short))
{ {
immutable arr = cast(T[])[0, 1, 2, 3, 4, 5]; immutable arr = cast(T[])[0, 1, 2, 3, 4, 5];
@ -4677,7 +4677,7 @@ if (isInputRange!R1 &&
alias doesThisEnd haystack; alias doesThisEnd haystack;
alias withThis needle; alias withThis needle;
static if(is(typeof(pred) : string)) static if (is(typeof(pred) : string))
enum isDefaultPred = pred == "a == b"; enum isDefaultPred = pred == "a == b";
else else
enum isDefaultPred = false; enum isDefaultPred = false;
@ -4783,7 +4783,7 @@ unittest
} }
} }
foreach(T; TypeTuple!(int, short)) foreach (T; TypeTuple!(int, short))
{ {
immutable arr = cast(T[])[0, 1, 2, 3, 4, 5]; immutable arr = cast(T[])[0, 1, 2, 3, 4, 5];
@ -5277,7 +5277,7 @@ if (isInputRange!R1 && isInputRange!R2 && !(isSomeString!R1 && isSomeString!R2))
// Specialization for strings (for speed purposes) // Specialization for strings (for speed purposes)
int cmp(alias pred = "a < b", R1, R2)(R1 r1, R2 r2) if (isSomeString!R1 && isSomeString!R2) int cmp(alias pred = "a < b", R1, R2)(R1 r1, R2 r2) if (isSomeString!R1 && isSomeString!R2)
{ {
static if(is(typeof(pred) : string)) static if (is(typeof(pred) : string))
enum isLessThan = pred == "a < b"; enum isLessThan = pred == "a < b";
else else
enum isLessThan = false; enum isLessThan = false;
@ -5403,7 +5403,7 @@ Returns the minimum of the passed-in values. The type of the result is
computed by using $(XREF traits, CommonType). computed by using $(XREF traits, CommonType).
*/ */
MinType!(T1, T2, T) min(T1, T2, T...)(T1 a, T2 b, T xs) MinType!(T1, T2, T) min(T1, T2, T...)(T1 a, T2 b, T xs)
if(is(typeof(a < b))) if (is(typeof(a < b)))
{ {
static if (T.length == 0) static if (T.length == 0)
{ {
@ -5494,7 +5494,7 @@ assert(e == 2);
---- ----
*/ */
MaxType!(T1, T2, T) max(T1, T2, T...)(T1 a, T2 b, T xs) MaxType!(T1, T2, T) max(T1, T2, T...)(T1 a, T2 b, T xs)
if(is(typeof(a < b))) if (is(typeof(a < b)))
{ {
static if (T.length == 0) static if (T.length == 0)
{ {
@ -7602,16 +7602,16 @@ 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)
{ {
alias binaryFun!(less) pred; alias binaryFun!(less) pred;
if(r.length < 2) { if (r.length < 2) {
return ; return ;
} }
immutable maxJ = r.length - 1; immutable maxJ = r.length - 1;
for(size_t i = r.length - 2; i != size_t.max; --i) { for (size_t i = r.length - 2; i != size_t.max; --i) {
size_t j = i; size_t j = i;
auto temp = r[i]; auto temp = r[i];
for(; j < maxJ && pred(r[j + 1], temp); ++j) { for (; j < maxJ && pred(r[j + 1], temp); ++j) {
r[j] = r[j + 1]; r[j] = r[j + 1];
} }
@ -7633,7 +7633,7 @@ unittest
assert(isSorted(a)); assert(isSorted(a));
} }
// private //private
void swapAt(R)(R r, size_t i1, size_t i2) void swapAt(R)(R r, size_t i1, size_t i2)
{ {
static if (is(typeof(&r[i1]))) static if (is(typeof(&r[i1])))
@ -8620,7 +8620,7 @@ void makeIndex(
alias Unqual!(ElementType!RangeIndex) I; alias Unqual!(ElementType!RangeIndex) I;
enforce(r.length == index.length, enforce(r.length == index.length,
"r and index must be same length for makeIndex."); "r and index must be same length for makeIndex.");
static if(I.sizeof < size_t.sizeof) static if (I.sizeof < size_t.sizeof)
{ {
enforce(r.length <= I.max, "Cannot create an index with " ~ enforce(r.length <= I.max, "Cannot create an index with " ~
"element type " ~ I.stringof ~ " with length " ~ "element type " ~ I.stringof ~ " with length " ~
@ -8628,7 +8628,7 @@ void makeIndex(
); );
} }
for(I i = 0; i < r.length; ++i) for (I i = 0; i < r.length; ++i)
{ {
index[cast(size_t) i] = i; index[cast(size_t) i] = i;
} }
@ -9112,7 +9112,7 @@ assert(!all!"a & 1"([1, 2, 3, 5, 7, 9]));
--- ---
*/ */
bool all(alias pred, R)(R range) bool all(alias pred, R)(R range)
if(isInputRange!R && is(typeof(unaryFun!pred(range.front)))) if (isInputRange!R && is(typeof(unaryFun!pred(range.front))))
{ {
return find!(not!(unaryFun!pred))(range).empty; return find!(not!(unaryFun!pred))(range).empty;
} }
@ -9303,12 +9303,12 @@ public:
assert(false); assert(false);
} }
static if(allSatisfy!(isForwardRange, Rs)) static if (allSatisfy!(isForwardRange, Rs))
{ {
@property typeof(this) save() @property typeof(this) save()
{ {
auto ret = this; auto ret = this;
foreach(ti, elem; _r) foreach (ti, elem; _r)
{ {
ret._r[ti] = elem.save; ret._r[ti] = elem.save;
} }
@ -9433,12 +9433,12 @@ public:
return _input[0].front; return _input[0].front;
} }
static if(allSatisfy!(isForwardRange, Rs)) static if (allSatisfy!(isForwardRange, Rs))
{ {
@property typeof(this) save() @property typeof(this) save()
{ {
auto ret = this; auto ret = this;
foreach(ti, elem; _input) foreach (ti, elem; _input)
{ {
ret._input[ti] = elem.save; ret._input[ti] = elem.save;
} }
@ -9536,7 +9536,7 @@ public:
return r1.front; return r1.front;
} }
static if(isForwardRange!R1 && isForwardRange!R2) static if (isForwardRange!R1 && isForwardRange!R2)
{ {
@property typeof(this) save() @property typeof(this) save()
{ {
@ -9646,7 +9646,7 @@ public:
return r2.front; return r2.front;
} }
static if(isForwardRange!R1 && isForwardRange!R2) static if (isForwardRange!R1 && isForwardRange!R2)
{ {
@property typeof(this) save() @property typeof(this) save()
{ {