fix Issue 5939 - Cannot copy std.algorithm.map

Move out Voldemort types to modle level.
This commit is contained in:
k-hara 2012-07-29 19:16:34 +09:00
parent 0a5c88daf3
commit cedddcd23e
2 changed files with 298 additions and 301 deletions

View file

@ -377,17 +377,21 @@ template map(fun...) if (fun.length >= 1)
alias unaryFun!fun _fun; alias unaryFun!fun _fun;
} }
struct Result return MapResult!(_fun, Range)(r);
{ }
}
private struct MapResult(alias fun, Range)
{
alias Unqual!Range R; alias Unqual!Range R;
alias typeof(_fun(.ElementType!R.init)) ElementType; //alias typeof(fun(.ElementType!R.init)) ElementType;
R _input; R _input;
static if (isBidirectionalRange!R) static if (isBidirectionalRange!R)
{ {
@property auto ref back() @property auto ref back()
{ {
return _fun(_input.back); return fun(_input.back);
} }
void popBack() void popBack()
@ -421,14 +425,14 @@ template map(fun...) if (fun.length >= 1)
@property auto ref front() @property auto ref front()
{ {
return _fun(_input.front); return fun(_input.front);
} }
static if (isRandomAccessRange!R) static if (isRandomAccessRange!R)
{ {
auto ref opIndex(size_t index) auto ref opIndex(size_t index)
{ {
return _fun(_input[index]); return fun(_input[index]);
} }
} }
@ -457,10 +461,6 @@ template map(fun...) if (fun.length >= 1)
result._input = result._input.save; result._input = result._input.save;
return result; return result;
} }
}
return Result(r);
}
} }
unittest unittest
@ -1092,15 +1092,19 @@ template filter(alias pred) if (is(typeof(unaryFun!pred)))
{ {
auto filter(Range)(Range rs) if (isInputRange!(Unqual!Range)) auto filter(Range)(Range rs) if (isInputRange!(Unqual!Range))
{ {
struct FilteredRange return FilterResult!(unaryFun!pred, Range)(rs);
{ }
}
private struct FilterResult(alias pred, Range)
{
alias Unqual!Range R; alias Unqual!Range R;
R _input; R _input;
this(R r) this(R r)
{ {
_input = r; _input = r;
while (!_input.empty && !unaryFun!pred(_input.front)) while (!_input.empty && !pred(_input.front))
{ {
_input.popFront(); _input.popFront();
} }
@ -1122,7 +1126,7 @@ template filter(alias pred) if (is(typeof(unaryFun!pred)))
do do
{ {
_input.popFront(); _input.popFront();
} while (!_input.empty && !unaryFun!pred(_input.front)); } while (!_input.empty && !pred(_input.front));
} }
@property auto ref front() @property auto ref front()
@ -1137,10 +1141,6 @@ template filter(alias pred) if (is(typeof(unaryFun!pred)))
return typeof(this)(_input); return typeof(this)(_input);
} }
} }
}
return FilteredRange(rs);
}
} }
unittest unittest
@ -1256,17 +1256,20 @@ template filterBidirectional(alias pred)
{ {
auto filterBidirectional(Range)(Range r) if (isBidirectionalRange!(Unqual!Range)) auto filterBidirectional(Range)(Range r) if (isBidirectionalRange!(Unqual!Range))
{ {
struct Result return FilterBidiResult!(unaryFun!pred, Range)(r);
{ }
}
private struct FilterBidiResult(alias pred, Range)
{
alias Unqual!Range R; alias Unqual!Range R;
alias unaryFun!pred predFun;
R _input; R _input;
this(R r) this(R r)
{ {
_input = r; _input = r;
while (!_input.empty && !predFun(_input.front)) _input.popFront(); while (!_input.empty && !pred(_input.front)) _input.popFront();
while (!_input.empty && !predFun(_input.back)) _input.popBack(); while (!_input.empty && !pred(_input.back)) _input.popBack();
} }
@property bool empty() { return _input.empty; } @property bool empty() { return _input.empty; }
@ -1276,7 +1279,7 @@ template filterBidirectional(alias pred)
do do
{ {
_input.popFront(); _input.popFront();
} while (!_input.empty && !predFun(_input.front)); } while (!_input.empty && !pred(_input.front));
} }
@property auto ref front() @property auto ref front()
@ -1289,7 +1292,7 @@ template filterBidirectional(alias pred)
do do
{ {
_input.popBack(); _input.popBack();
} while (!_input.empty && !predFun(_input.back)); } while (!_input.empty && !pred(_input.back));
} }
@property auto ref back() @property auto ref back()
@ -1299,13 +1302,7 @@ template filterBidirectional(alias pred)
@property auto save() @property auto save()
{ {
Result result; return typeof(this)(_input.save);
result._input = _input.save;
return result;
}
}
return Result(r);
} }
} }
@ -2211,11 +2208,13 @@ unittest
auto splitter(alias isTerminator, Range)(Range input) auto splitter(alias isTerminator, Range)(Range input)
if (is(typeof(unaryFun!(isTerminator)(ElementType!(Range).init)))) if (is(typeof(unaryFun!(isTerminator)(ElementType!(Range).init))))
{ {
struct Result return SplitterResult!(unaryFun!isTerminator, Range)(input);
{ }
private struct SplitterResult(alias isTerminator, Range)
{
private Range _input; private Range _input;
private size_t _end; private size_t _end;
private alias unaryFun!isTerminator _isTerminator;
this(Range input) this(Range input)
{ {
@ -2227,7 +2226,7 @@ if (is(typeof(unaryFun!(isTerminator)(ElementType!(Range).init))))
else else
{ {
// Chase first terminator // Chase first terminator
while (_end < _input.length && !_isTerminator(_input[_end])) while (_end < _input.length && !isTerminator(_input[_end]))
{ {
++_end; ++_end;
} }
@ -2271,17 +2270,17 @@ if (is(typeof(unaryFun!(isTerminator)(ElementType!(Range).init))))
_end = _end.max; _end = _end.max;
return; return;
} }
if (!_isTerminator(_input.front)) if (!isTerminator(_input.front))
{ {
// Found a legit next field // Found a legit next field
break; break;
} }
_input.popFront(); _input.popFront();
} }
assert(!_input.empty && !_isTerminator(_input.front)); assert(!_input.empty && !isTerminator(_input.front));
// Prepare _end // Prepare _end
_end = 1; _end = 1;
while (_end < _input.length && !_isTerminator(_input[_end])) while (_end < _input.length && !isTerminator(_input[_end]))
{ {
++_end; ++_end;
} }
@ -2296,10 +2295,8 @@ if (is(typeof(unaryFun!(isTerminator)(ElementType!(Range).init))))
return ret; return ret;
} }
} }
}
return Result(input);
} }
unittest unittest
{ {
auto L = iota(1L, 10L); auto L = iota(1L, 10L);
@ -2639,8 +2636,11 @@ assert(equal(uniq(arr), [ 1, 2, 3, 4, 5 ][]));
auto uniq(alias pred = "a == b", Range)(Range r) auto uniq(alias pred = "a == b", Range)(Range r)
if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool)) if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))
{ {
struct Result return UniqResult!(binaryFun!pred, Range)(r);
{ }
private struct UniqResult(alias pred, Range)
{
Range _input; Range _input;
this(Range input) this(Range input)
@ -2660,7 +2660,7 @@ if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))
{ {
_input.popFront(); _input.popFront();
} }
while (!_input.empty && binaryFun!(pred)(last, _input.front)); while (!_input.empty && pred(last, _input.front));
} }
@property ElementType!Range front() { return _input.front; } @property ElementType!Range front() { return _input.front; }
@ -2674,7 +2674,7 @@ if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))
{ {
_input.popBack(); _input.popBack();
} }
while (!_input.empty && binaryFun!pred(last, _input.back)); while (!_input.empty && pred(last, _input.back));
} }
@property ElementType!Range back() { return _input.back; } @property ElementType!Range back() { return _input.back; }
@ -2695,9 +2695,6 @@ if (isInputRange!Range && is(typeof(binaryFun!pred(r.front, r.front)) == bool))
return typeof(this)(_input.save); return typeof(this)(_input.save);
} }
} }
}
return Result(r);
} }
unittest unittest

View file

@ -757,7 +757,7 @@ Example:
@trusted int sub(int a, int b) {return a-b;} @trusted int sub(int a, int b) {return a-b;}
@system int mul(int a, int b) {return a*b;} @system int mul(int a, int b) {return a*b;}
htatic assert( isSafe!add); static assert( isSafe!add);
static assert( isSafe!sub); static assert( isSafe!sub);
static assert(!isSafe!mul); static assert(!isSafe!mul);
-------------------- --------------------