Add alias opDollar and use $ in range slicing

This commit is contained in:
k-hara 2012-11-27 10:34:56 +09:00
parent 24b7d423d2
commit 07104b5fe6
5 changed files with 72 additions and 78 deletions

View file

@ -534,7 +534,7 @@ unittest
assert(squares[3] == 16);
// Test slicing.
auto squareSlice = squares[1..squares.length - 1];
auto squareSlice = squares[1..$-1];
assert(equal(squareSlice, [4, 9][]));
assert(squareSlice.back == 9);
assert(squareSlice[1] == 9);
@ -2111,7 +2111,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
}
else
{
_input = _input[_frontLength .. _input.length];
_input = _input[_frontLength .. $];
skipOver(_input, _separator) || assert(false);
_frontLength = _unComputed;
}
@ -2144,7 +2144,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
_backLength = _input.length - lastIndex - 1;
}
}
return _input[_input.length - _backLength .. _input.length];
return _input[$ - _backLength .. $];
}
void popBack()
@ -2164,7 +2164,7 @@ if (is(typeof(ElementType!Range.init == Separator.init))
}
else
{
_input = _input[0 .. _input.length - _backLength];
_input = _input[0 .. $ - _backLength];
if (!_input.empty && _input.back == _separator)
{
_input.popBack();
@ -2342,7 +2342,7 @@ if (is(typeof(Range.init.front == Separator.init.front) : bool)
{
// Special case: popping the first-to-last item; there is
// an empty item right after this.
_input = _input[_input.length .. _input.length];
_input = _input[$ .. $];
_frontLength = 0;
static if (isBidirectionalRange!Range)
_backLength = 0;
@ -2350,7 +2350,7 @@ if (is(typeof(Range.init.front == Separator.init.front) : bool)
}
// Normal case, pop one item and the separator, get ready for
// reading the next item
_input = _input[_frontLength + separatorLength .. _input.length];
_input = _input[_frontLength + separatorLength .. $];
// mark _frontLength as uninitialized
_frontLength = _frontLength.max;
}
@ -2371,7 +2371,7 @@ if (is(typeof(Range.init.front == Separator.init.front) : bool)
@property Range back()
{
ensureBackLength();
return _input[_input.length - _backLength .. _input.length];
return _input[$ - _backLength .. $];
}
void popBack()
@ -2395,7 +2395,7 @@ if (is(typeof(Range.init.front == Separator.init.front) : bool)
return;
}
// Normal case
_input = _input[0 .. _input.length - _backLength - separatorLength];
_input = _input[0 .. $ - _backLength - separatorLength];
_backLength = _backLength.max;
}
}
@ -2511,7 +2511,7 @@ private struct SplitterResult(alias isTerminator, Range)
return;
}
// Skip over existing word
_input = _input[_end .. _input.length];
_input = _input[_end .. $];
// Skip terminator
for (;;)
{
@ -3299,9 +3299,7 @@ if (isRandomAccessRange!R1 && isBidirectionalRange!R2
const needleLength = walkLength(needle.save);
if (needleLength > haystack.length)
{
// @@@BUG@@@
//return haystack[$ .. $];
return haystack[haystack.length .. haystack.length];
return haystack[$ .. $];
}
// @@@BUG@@@
// auto needleBack = moveBack(needle);
@ -3319,7 +3317,7 @@ if (isRandomAccessRange!R1 && isBidirectionalRange!R2
{
if (scout >= haystack.length)
{
return haystack[haystack.length .. haystack.length];
return haystack[$ .. $];
}
if (!binaryFun!pred(haystack[scout], needleBack))
{
@ -3327,7 +3325,7 @@ if (isRandomAccessRange!R1 && isBidirectionalRange!R2
continue;
}
// Found a match with the last element in the needle
auto cand = haystack[scout + 1 - needleLength .. haystack.length];
auto cand = haystack[scout + 1 - needleLength .. $];
if (startsWith!pred(cand, needle))
{
// found
@ -3476,9 +3474,8 @@ unittest
// Failed search
static if (hasLength!R1)
{
static if (is(typeof(haystack[haystack.length ..
haystack.length]) : R1))
return haystack[haystack.length .. haystack.length];
static if (is(typeof(haystack[$ .. $]) : R1))
return haystack[$ .. $];
else
return R1.init;
}
@ -3718,7 +3715,7 @@ is ignored.
return 0;
immutable delta = portion - ignore;
return equal(needle[needle.length - delta .. needle.length],
return equal(needle[$ - delta .. $],
needle[virtual_begin .. virtual_begin + delta]);
}
@ -3948,7 +3945,7 @@ if (isForwardRange!R1 && isForwardRange!R2)
immutable pos2 = balance.empty ? pos1 : pos1 + needle.length;
return tuple(haystack[0 .. pos1],
haystack[pos1 .. pos2],
haystack[pos2 .. haystack.length]);
haystack[pos2 .. $]);
}
else
{
@ -3987,7 +3984,7 @@ if (isForwardRange!R1 && isForwardRange!R2)
{
auto balance = find!pred(haystack, needle);
immutable pos = haystack.length - balance.length;
return tuple(haystack[0 .. pos], haystack[pos .. haystack.length]);
return tuple(haystack[0 .. pos], haystack[pos .. $]);
}
else
{
@ -4023,7 +4020,7 @@ if (isForwardRange!R1 && isForwardRange!R2)
{
auto balance = find!pred(haystack, needle);
immutable pos = balance.empty ? 0 : haystack.length - balance.length + needle.length;
return tuple(haystack[0 .. pos], haystack[pos .. haystack.length]);
return tuple(haystack[0 .. pos], haystack[pos .. $]);
}
else
{
@ -7024,8 +7021,8 @@ Range partition(alias predicate,
alias .partition!(pred, ss, Range) recurse;
auto lower = recurse(r[0 .. middle]);
auto upper = recurse(r[middle .. $]);
bringToFront(lower, r[middle .. r.length - upper.length]);
return r[r.length - lower.length - upper.length .. r.length];
bringToFront(lower, r[middle .. $ - upper.length]);
return r[$ - lower.length - upper.length .. $];
}
else static if (ss == SwapStrategy.semistable)
{
@ -7226,10 +7223,10 @@ if (ss == SwapStrategy.unstable && isRandomAccessRange!Range
auto swapLen = min(i, strictlyLess);
swapRanges(r[0 .. swapLen], r[j - swapLen .. j]);
swapLen = min(r.length - l, strictlyGreater);
swapRanges(r[k .. k + swapLen], r[r.length - swapLen .. r.length]);
swapRanges(r[k .. k + swapLen], r[$ - swapLen .. $]);
return tuple(r[0 .. strictlyLess],
r[strictlyLess .. r.length - strictlyGreater],
r[r.length - strictlyGreater .. r.length]);
r[strictlyLess .. $ - strictlyGreater],
r[$ - strictlyGreater .. $]);
}
unittest
@ -7486,7 +7483,7 @@ sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
enum maxLen = 8;
assert(isSorted!lessFun(r), text("Failed to sort range of type ",
Range.stringof, ". Actual result is: ",
r[0 .. r.length > maxLen ? maxLen : r.length ],
r[0 .. $ > maxLen ? maxLen : $],
r.length > maxLen ? "..." : ""));
}
else
@ -7762,7 +7759,7 @@ private void sortImpl(alias less, SwapStrategy ss, Range)(Range r)
}
swapAt(r, r.length - 1, lessI);
auto right = r[lessI + 1..r.length];
auto right = r[lessI + 1 .. $];
auto left = r[0..min(lessI, greaterI + 1)];
if (right.length > left.length)
@ -7798,7 +7795,7 @@ private void sortImpl(alias less, SwapStrategy ss, Range)(Range r)
}
else
{
.sortImpl!(less, ss, Range)(r[0 .. r.length - right.length]);
.sortImpl!(less, ss, Range)(r[0 .. $ - right.length]);
r = right;
}
}
@ -7925,7 +7922,7 @@ unittest
/**
Reorders the random-access range $(D r) such that the range $(D r[0
.. mid]) is the same as if the entire $(D r) were sorted, and leaves
the range $(D r[mid .. r.length]) in no particular order. Performs
the range $(D r[mid .. $(DOLLAR)]) in no particular order. Performs
$(BIGOH r.length * log(mid)) evaluations of $(D pred). The
implementation simply calls $(D topN!(less, ss)(r, n)) and then $(D
sort!(less, ss)(r[0 .. n])).