Fix issue going out of the range with back()

This commit is contained in:
Eugen Wissner 2016-12-22 22:01:45 +01:00
parent b41dcc9f37
commit 8bd6a14988

View File

@ -14,11 +14,11 @@ import core.exception;
import std.algorithm.comparison; import std.algorithm.comparison;
import std.range.primitives; import std.range.primitives;
import std.traits; import std.traits;
public import tanya.enums : IL;
import tanya.memory; import tanya.memory;
version (unittest) version (unittest)
{ {
import tanya.traits;
struct TestA struct TestA
{ {
~this() @nogc ~this() @nogc
@ -186,16 +186,13 @@ template Vector(T)
if (isStaticArray!U) if (isStaticArray!U)
in in
{ {
assert(allocator !is null);
static assert(init.length > 0); static assert(init.length > 0);
} }
body body
{ {
this(allocator); this(allocator);
vector = cast(T[]) allocator.allocate(init.length * T.sizeof); allocator.resize!(T, false)(vector, init.length);
if (vector is null)
{
onOutOfMemoryError();
}
vector[0 .. $] = init[0 .. $]; vector[0 .. $] = init[0 .. $];
length_ = init.length; length_ = init.length;
} }
@ -205,18 +202,17 @@ template Vector(T)
if (isStaticArray!U) if (isStaticArray!U)
in in
{ {
assert(allocator !is null);
static assert(init.length > 0); static assert(init.length > 0);
} }
body body
{ {
allocator_ = cast(const shared Allocator) allocator; allocator_ = cast(const shared Allocator) allocator;
auto buf = cast(T[]) allocator.allocate(init.length * T.sizeof);
if (buf is null) T[] buf;
{ allocator.resize!(T, false)(buf, init.length);
onOutOfMemoryError();
}
buf[0 .. $] = init[0 .. $]; buf[0 .. $] = init[0 .. $];
vector = cast(const (T[])) buf; vector = cast(const(T[])) buf;
length_ = init.length; length_ = init.length;
} }
@ -225,16 +221,15 @@ template Vector(T)
if (isStaticArray!U) if (isStaticArray!U)
in in
{ {
assert(allocator !is null);
static assert(init.length > 0); static assert(init.length > 0);
} }
body body
{ {
allocator_ = cast(immutable Allocator) allocator; allocator_ = cast(immutable Allocator) allocator;
auto buf = cast(T[]) allocator.allocate(init.length * T.sizeof);
if (buf is null) T[] buf;
{ allocator.resize!(T, false)(buf, init.length);
onOutOfMemoryError();
}
buf[0 .. $] = init[0 .. $]; buf[0 .. $] = init[0 .. $];
vector = cast(immutable(T[])) buf; vector = cast(immutable(T[])) buf;
length_ = init.length; length_ = init.length;
@ -593,8 +588,10 @@ template Vector(T)
} }
/** /**
* Returns: The value on index $(D_PARAM pos) or a range that iterates over * Params:
* elements of the vector, in forward order. * pos = Index.
*
* Returns: The value at a specified index.
* *
* Precondition: $(D_INLINECODE length > pos) * Precondition: $(D_INLINECODE length > pos)
*/ */
@ -608,7 +605,10 @@ template Vector(T)
return vector[pos]; return vector[pos];
} }
/// Ditto. /**
* Returns: Random access range that iterates over elements of the vector, in
* forward order.
*/
Range!Vector opIndex() Range!Vector opIndex()
{ {
return typeof(return)(this, 0, length); return typeof(return)(this, 0, length);
@ -827,7 +827,7 @@ template Vector(T)
} }
body body
{ {
return vector[$ - 1]; return vector[length_ - 1];
} }
/// ///