Skip to content

Commit

Permalink
fix index types to size_t
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed Apr 10, 2016
1 parent ef26d53 commit d4fa64b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
16 changes: 7 additions & 9 deletions std/algorithm/iteration.d
Expand Up @@ -3029,10 +3029,9 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)
Range _input;
Separator _separator;
// Do we need hasLength!Range? popFront uses _input.length...
alias IndexType = typeof(unsigned(_input.length));
enum IndexType _unComputed = IndexType.max - 1, _atEnd = IndexType.max;
IndexType _frontLength = _unComputed;
IndexType _backLength = _unComputed;
enum size_t _unComputed = size_t.max - 1, _atEnd = size_t.max;
size_t _frontLength = _unComputed;
size_t _backLength = _unComputed;

static if (isNarrowString!Range)
{
Expand All @@ -3045,7 +3044,7 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)

static if (isBidirectionalRange!Range)
{
static IndexType lastIndexOf(Range haystack, Separator needle)
static size_t lastIndexOf(Range haystack, Separator needle)
{
import std.range : retro;
auto r = haystack.retro().find!pred(needle);
Expand Down Expand Up @@ -3312,11 +3311,10 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
private:
Range _input;
Separator _separator;
alias RIndexType = typeof(unsigned(_input.length));
// _frontLength == size_t.max means empty
RIndexType _frontLength = RIndexType.max;
size_t _frontLength = size_t.max;
static if (isBidirectionalRange!Range)
RIndexType _backLength = RIndexType.max;
size_t _backLength = size_t.max;

@property auto separatorLength() { return _separator.length; }

Expand Down Expand Up @@ -3367,7 +3365,7 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
{
@property bool empty()
{
return _frontLength == RIndexType.max && _input.empty;
return _frontLength == size_t.max && _input.empty;
}
}

Expand Down
40 changes: 24 additions & 16 deletions std/range/package.d
Expand Up @@ -222,9 +222,7 @@ if (isBidirectionalRange!(Unqual!Range))

static if (hasLength!R)
{
private alias IndexType = CommonType!(size_t, typeof(source.length));

IndexType retroIndex(IndexType n)
size_t retroIndex(size_t n)
{
return source.length - n - 1;
}
Expand Down Expand Up @@ -274,26 +272,26 @@ if (isBidirectionalRange!(Unqual!Range))

static if (isRandomAccessRange!(R) && hasLength!(R))
{
auto ref opIndex(IndexType n) { return source[retroIndex(n)]; }
auto ref opIndex(size_t n) { return source[retroIndex(n)]; }

static if (hasAssignableElements!R)
{
void opIndexAssign(ElementType!R val, IndexType n)
void opIndexAssign(ElementType!R val, size_t n)
{
source[retroIndex(n)] = val;
}
}

static if (is(typeof(.moveAt(source, 0))))
{
ElementType!R moveAt(IndexType index)
ElementType!R moveAt(size_t index)
{
return .moveAt(source, retroIndex(index));
}
}

static if (hasSlicing!R)
typeof(this) opSlice(IndexType a, IndexType b)
typeof(this) opSlice(size_t a, size_t b)
{
return typeof(this)(source[source.length - b .. source.length - a]);
}
Expand Down Expand Up @@ -4800,16 +4798,22 @@ body
cast(Value)(pastLast - (length - upper) * step),
step);
}
@property IndexType length() const
@property size_t length() const
{
IndexType ret;
if (step > 0)
{
return unsigned((pastLast - current) / step);
ret = unsigned((pastLast - current) / step);
}
else
{
return unsigned((current - pastLast) / -step);
ret = unsigned((current - pastLast) / -step);
}
static if (!is(IndexType : size_t))
{
assert(ret <= size_t.max);
}
return cast(size_t) ret;
}

alias opDollar = length;
Expand Down Expand Up @@ -4861,7 +4865,7 @@ if (isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))

@property auto save() { return this; }

inout(Value) opIndex(ulong n) inout
inout(Value) opIndex(size_t n) inout
{
assert(n < this.length);

Expand All @@ -4877,9 +4881,14 @@ if (isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))
return cast(inout Result)Result(cast(Value)(current + lower),
cast(Value)(pastLast - (length - upper)));
}
@property IndexType length() const
@property size_t length() const
{
return unsigned(pastLast - current);
immutable ret = unsigned(pastLast - current);
static if (!is(IndexType : size_t))
{
assert(ret <= size_t.max);
}
return cast(size_t) ret;
}

alias opDollar = length;
Expand Down Expand Up @@ -8552,8 +8561,7 @@ public:
Only defined if $(D hasMobileElements!R) and $(D isRandomAccessRange!R)
are $(D true).
+/
static if(hasMobileElements!R && isRandomAccessRange!R) auto moveAt(IndexType)(IndexType index)
if(is(typeof((*_range).moveAt(index))))
static if(hasMobileElements!R && isRandomAccessRange!R) auto moveAt(size_t index)
{
return (*_range).moveAt(index);
}
Expand Down Expand Up @@ -9557,7 +9565,7 @@ auto padRight(R, E)(R r, E e, size_t n) if (

static if (isRandomAccessRange!R)
{
inout(E) opIndex(size_t index) inout
E opIndex(size_t index)
{
assert(index <= this.length, "Index out of bounds");
return (index > data.length && index <= maxSize) ? element :
Expand Down

0 comments on commit d4fa64b

Please sign in to comment.