Unlisted bug in Map.save and a few stylistic changes

This commit is contained in:
Andrei Alexandrescu 2010-07-04 21:04:48 +00:00
parent 875d77e732
commit 4347cae9f5

View file

@ -53,7 +53,6 @@ import std.array, std.container, std.contracts, std.conv, std.date,
version(unittest) version(unittest)
{ {
import std.random, std.stdio, std.string; import std.random, std.stdio, std.string;
mixin(dummyRanges); mixin(dummyRanges);
} }
@ -123,15 +122,15 @@ struct Map(alias fun, Range) if (isInputRange!(Range))
// and wasted space when 99% of the time this range will only be iterated // and wasted space when 99% of the time this range will only be iterated
// over in the forward direction. Use a bool to determine whether cache // over in the forward direction. Use a bool to determine whether cache
// is front or back instead. // is front or back instead.
bool cacheIsBack; bool cacheIsBack_;
private void fillCacheBack() { private void fillCacheBack() {
if (!_input.empty) _cache = fun(_input.back); if (!_input.empty) _cache = fun(_input.back);
cacheIsBack = true; cacheIsBack_ = true;
} }
@property ElementType back() { @property ElementType back() {
if(!cacheIsBack) { if (!cacheIsBack_) {
fillCacheBack(); fillCacheBack();
} }
return _cache; return _cache;
@ -147,7 +146,7 @@ struct Map(alias fun, Range) if (isInputRange!(Range))
if (!_input.empty) _cache = fun(_input.front); if (!_input.empty) _cache = fun(_input.front);
static if(isBidirectionalRange!(Range)) { static if(isBidirectionalRange!(Range)) {
cacheIsBack = false; cacheIsBack_ = false;
} }
} }
@ -167,12 +166,12 @@ struct Map(alias fun, Range) if (isInputRange!(Range))
void popFront() { void popFront() {
_input.popFront; _input.popFront;
fillCache; fillCache();
} }
@property ElementType front() { @property ElementType front() {
static if (isBidirectionalRange!(Range)) { static if (isBidirectionalRange!(Range)) {
if(cacheIsBack) { if (cacheIsBack_) {
fillCache(); fillCache();
} }
} }
@ -202,10 +201,7 @@ struct Map(alias fun, Range) if (isInputRange!(Range))
static if (isForwardRange!Range) static if (isForwardRange!Range)
@property Map save() @property Map save()
{ {
Map result; return this;
result._input = _input.save;
result._cache = _cache;
return result;
} }
} }
@ -219,8 +215,10 @@ unittest
assert(equal(map!("a * a")(chain(arr1, arr2)), [ 1, 4, 9, 16, 25, 36 ][])); assert(equal(map!("a * a")(chain(arr1, arr2)), [ 1, 4, 9, 16, 25, 36 ][]));
// Test the caching stuff. // Test the caching stuff.
auto squares2 = squares; assert(squares.back == 16);
auto squares2 = squares.save;
assert(squares2.back == 16); assert(squares2.back == 16);
assert(squares2.front == 1); assert(squares2.front == 1);
squares2.popFront; squares2.popFront;
assert(squares2.front == 4); assert(squares2.front == 4);