From 8725ec5f20356a31b5292abb9eb3a18ec9a7d93c Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 19 Apr 2017 13:49:44 +0200 Subject: [PATCH 1/2] Make Integer representation little endian --- source/tanya/math/mp.d | 246 ++++++++++++++---------------------- source/tanya/math/package.d | 10 +- 2 files changed, 103 insertions(+), 153 deletions(-) diff --git a/source/tanya/math/mp.d b/source/tanya/math/mp.d index 57a8d5c..024a0e4 100644 --- a/source/tanya/math/mp.d +++ b/source/tanya/math/mp.d @@ -129,12 +129,8 @@ struct Integer * Precondition: $(D_INLINECODE allocator !is null) */ this(R)(const Sign sign, R value, shared Allocator allocator = defaultAllocator) + @trusted if (isInputRange!R && hasLength!R && is(Unqual!(ElementType!R) == ubyte)) - out - { - assert(this.size == 0 || this.rep[0] > 0); - } - body { this(allocator); while (!value.empty && value.front == 0) @@ -144,7 +140,14 @@ struct Integer this.rep = allocator.resize(this.rep[0 .. this.size], value.length).ptr; this.size = value.length; this.sign = sign; - value.copy(this.rep[0 .. this.size]); + value.copy(this.rep[0 .. this.size].retro); + } + + private @nogc unittest + { + ubyte[5] range = [ 0x02, 0x11, 0x00, 0x00, 0x01 ]; + auto integer = Integer(Sign.positive, range[]); + assert(equal(range[].retro, integer.rep[0 .. integer.size])); } /** @@ -205,9 +208,9 @@ struct Integer big-endian format. */ mask = 0xff; ubyte shift; - for (auto i = this.size; i; --i, mask <<= 8, shift += 8) + for (size_t i; i < this.size; ++i, mask <<= 8, shift += 8) { - this.rep[i - 1] = cast(ubyte) ((absolute & mask) >> shift); + this.rep[i] = cast(ubyte) ((absolute & mask) >> shift); } } @@ -263,15 +266,15 @@ struct Integer { auto integer = Integer(1019); assert(integer.length == 2); - assert(integer.rep[0] == 0b00000011 && integer.rep[1] == 0b11111011); + assert(integer.rep[1] == 0b00000011 && integer.rep[0] == 0b11111011); integer = 3337; assert(integer.length == 2); - assert(integer.rep[0] == 0b00001101 && integer.rep[1] == 0b00001001); + assert(integer.rep[1] == 0b00001101 && integer.rep[0] == 0b00001001); integer = 688; assert(integer.length == 2); - assert(integer.rep[0] == 0b00000010 && integer.rep[1] == 0b10110000); + assert(integer.rep[1] == 0b00000010 && integer.rep[0] == 0b10110000); integer = 0; assert(integer.length == 0); @@ -283,10 +286,8 @@ struct Integer private void expand() nothrow @trusted @nogc { rep = allocator.resize(this.rep[0 .. this.size], this.size + 1).ptr; + this.rep[this.size] = 0x01; ++this.size; - auto target = this.rep[1 .. this.size].retro; - this.rep[0 .. this.size - 1].retro.copy(target); - this.rep[0] = 0x01; } /* @@ -295,11 +296,12 @@ struct Integer */ private void contract() nothrow @trusted @nogc { - const i = this.rep[0 .. this.size].countUntil!((const ref a) => a != 0); + const i = this.rep[0 .. this.size] + .retro + .countUntil!((const ref a) => a != 0); if (i > 0) { - this.rep[i .. this.size].copy(this.rep[0 .. this.size - i]); this.rep = allocator.resize(this.rep[0 .. this.size], this.size - i).ptr; this.size -= i; } @@ -315,28 +317,22 @@ struct Integer { if (summand.length > this.length) { - const delta = summand.size - this.size; - auto tmp = this.rep[0 .. this.size]; + this.rep = allocator.resize!ubyte(this.rep[0 .. this.size], summand.size).ptr; + this.rep[this.size .. summand.size].initializeAll(); - this.rep = allocator.resize!ubyte(null, summand.size).ptr; - tmp.copy(this.rep[delta .. summand.size]); - this.rep[0 .. delta].initializeAll(); - - allocator.deallocate(tmp[0 .. this.size]); this.size = summand.size; } bool carry; - size_t i = this.size; - size_t j = summand.size; + size_t i; + size_t j; do { uint sum; - --i; - if (j) + if (j < summand.size) { - --j; sum = this.rep[i] + summand.rep[j] + carry; + ++j; } else { @@ -346,7 +342,7 @@ struct Integer carry = sum > 0xff; this.rep[i] = cast(ubyte) sum; } - while (i); + while (++i < this.size); if (carry) { @@ -357,19 +353,18 @@ struct Integer private void subtract(const ref Integer subtrahend) nothrow @trusted @nogc { - size_t i = this.size; - size_t j = subtrahend.size; + size_t i; + size_t j; bool borrow; - while (i) + while (i < this.size) { int difference; - --i; - if (j) + if (j < subtrahend.size) { - --j; difference = this.rep[i] - subtrahend.rep[j] - borrow; + ++j; } else { @@ -377,29 +372,30 @@ struct Integer } borrow = difference < 0; this.rep[i] = cast(ubyte) difference; + + ++i; } - if (borrow && i > 0) + if (borrow && i < this.size && this.rep[i]) { - if (this.rep[i - 1]) // Don't borrow i - { - --this.rep[i - 1]; - } + --this.rep[i]; } contract(); } private int compare(const ref Integer that) const nothrow @trusted @nogc { - if (this.length > that.length) + if (length > that.length) { return 1; } - else if (this.length < that.length) + else if (length < that.length) { return -1; } - return this.rep[0 .. this.size].cmp(that.rep[0 .. that.size]); + return this.rep[0 .. this.size] + .retro + .cmp(that.rep[0 .. that.size].retro); } /** @@ -508,11 +504,6 @@ struct Integer * Returns: $(D_KEYWORD this). */ ref Integer opOpAssign(string op : "+")(const auto ref Integer operand) - out - { - assert(this.size == 0 || this.rep[0] > 0); - } - body { if (this.sign == operand.sign) { @@ -544,19 +535,19 @@ struct Integer auto h2 = Integer(3337); h1 += h2; assert(h1.length == 2); - assert(h1.rep[0] == 0x11 && h1.rep[1] == 0x04); + assert(h1.rep[1] == 0x11 && h1.rep[0] == 0x04); } { auto h1 = Integer(4356); auto h2 = Integer(2_147_483_647); - ubyte[4] expected = [0x80, 0x00, 0x11, 0x03]; + ubyte[4] expected = [ 0x03, 0x11, 0x00, 0x80 ]; h1 += h2; assert(h1.rep[0 .. h1.size] == expected); } { auto h1 = Integer(2147488003L); auto h2 = Integer(2_147_483_647); - ubyte[5] expected = [0x01, 0x00, 0x00, 0x11, 0x02]; + ubyte[5] expected = [ 0x02, 0x11, 0x00, 0x00, 0x01 ]; h1 += h2; assert(h1.rep[0 .. h1.size] == expected); } @@ -564,11 +555,6 @@ struct Integer /// Ditto. ref Integer opOpAssign(string op : "-")(const auto ref Integer operand) - out - { - assert(this.size == 0 || this.rep[0] > 0); - } - body { if (operand.sign == this.sign) { @@ -608,12 +594,12 @@ struct Integer h1 -= h2; assert(h1.length == 1); assert(h1.rep[0] == 0x01); - assert(h1.sign); + assert(h1.sign == Sign.negative); } { auto h1 = Integer(8589934590L); auto h2 = Integer(2147483647); - ubyte[5] expected = [0x01, 0x7f, 0xff, 0xff, 0xff]; + ubyte[5] expected = [ 0xff, 0xff, 0xff, 0x7f, 0x01 ]; h1 -= h2; assert(h1.rep[0 .. h1.size] == expected); @@ -621,7 +607,7 @@ struct Integer { auto h1 = Integer(6442450943); auto h2 = Integer(4294967294); - ubyte[4] expected = [0x80, 0x00, 0x00, 0x01]; + ubyte[4] expected = [ 0x01, 0x00, 0x00, 0x80 ]; h1 -= h2; assert(h1.rep[0 .. h1.size] == expected); } @@ -635,18 +621,13 @@ struct Integer /// Ditto. ref Integer opOpAssign(string op : "*")(const auto ref Integer operand) @trusted - out { - assert(this.size == 0 || this.rep[0] > 0); - } - body - { - size_t i = operand.size; + size_t i; if (this.length == 0) { return this; } - else if (i == 0) + else if (operand.length == 0) { this = 0; return this; @@ -657,7 +638,6 @@ struct Integer this = 0; do { - --i; for (ubyte mask = 0x01; mask; mask <<= 1) { if (mask & operand.rep[i]) @@ -666,8 +646,9 @@ struct Integer } temp <<= 1; } + ++i; } - while (i); + while (i < operand.size); this.sign = sign ? Sign.negative : Sign.positive; @@ -691,20 +672,14 @@ struct Integer /// Ditto. ref Integer opOpAssign(string op : "^^")(const auto ref Integer operand) @trusted - out { - assert(this.size == 0 || this.rep[0] > 0); - } - body - { - size_t i = operand.size; + size_t i; auto tmp1 = Integer(this, this.allocator); this = 1; do { - --i; for (ubyte mask = 0x01; mask; mask <<= 1) { if (operand.rep[i] & mask) @@ -715,8 +690,9 @@ struct Integer auto tmp2 = tmp1; tmp1 *= tmp2; } + ++i; } - while (i); + while (i < operand.size); return this; } @@ -732,7 +708,7 @@ struct Integer h1 = Integer(2342); h1 ^^= h2; - ubyte[6] expected = [0x1b, 0x5c, 0xab, 0x9c, 0x31, 0x10]; + ubyte[6] expected = [ 0x10, 0x31, 0x9c, 0xab, 0x5c, 0x1b ]; assert(h1.rep[0 .. h1.size] == expected); } @@ -743,10 +719,6 @@ struct Integer { assert(operand.length > 0, "Division by zero."); } - out - { - assert(this.size == 0 || this.rep[0] > 0); - } body { auto divisor = Integer(operand, this.allocator); @@ -774,7 +746,7 @@ struct Integer subtract(divisor); // dividend -= divisor static if (op == "/") { - quotient[bitPosition / 8] |= 0x80 >> (bitPosition % 8); + quotient[$ - 1 - bitPosition / 8] |= 0x80 >> (bitPosition % 8); } } if (bitSize != 0) @@ -823,11 +795,6 @@ struct Integer /// Ditto. ref Integer opOpAssign(string op : ">>")(const size_t operand) @trusted - out - { - assert(this.size == 0 || this.rep[0] > 0); - } - body { const step = operand / 8; @@ -843,20 +810,18 @@ struct Integer const bit = operand % 8; const delta = 8 - bit; - carry = cast(ubyte) (this.rep[0] << delta); - this.rep[0] = this.rep[0] >> bit; - if (rep[0]) + for (j = step; j < length - 1; ++i, ++j) { - ++j; + carry = cast(ubyte) (this.rep[i + 1] << delta); + this.rep[i] = (this.rep[i] >> bit) | carry; } - for (i = 1; i < this.length; ++i) + + this.rep[i] = this.rep[j] >> bit; + size_t newSize = length - step; + if (this.rep[i] == 0) { - immutable oldCarry = carry; - carry = cast(ubyte) (this.rep[i] << delta); - this.rep[j] = this.rep[i] >> bit | oldCarry; - ++j; + --newSize; } - const newSize = this.length - operand / 8 - (i == j ? 0 : 1); this.rep = allocator.resize(this.rep[0 .. this.size], newSize).ptr; this.size = newSize; @@ -868,21 +833,21 @@ struct Integer auto integer = Integer(4294967294); integer >>= 10; assert(integer.length == 3); - assert(integer.rep[0] == 0x3f && integer.rep[1] == 0xff && integer.rep[2] == 0xff); + assert(integer.rep[2] == 0x3f && integer.rep[1] == 0xff && integer.rep[0] == 0xff); integer = 27336704; integer >>= 1; assert(integer.length == 3); - assert(integer.rep[0] == 0xd0 && integer.rep[1] == 0x90 && integer.rep[2] == 0x00); + assert(integer.rep[2] == 0xd0 && integer.rep[1] == 0x90 && integer.rep[0] == 0x00); integer = 4294967294; integer >>= 20; assert(integer.length == 2); - assert(integer.rep[0] == 0x0f && integer.rep[1] == 0xff); + assert(integer.rep[1] == 0x0f && integer.rep[0] == 0xff); integer >>= 0; assert(integer.length == 2); - assert(integer.rep[0] == 0x0f && integer.rep[1] == 0xff); + assert(integer.rep[1] == 0x0f && integer.rep[0] == 0xff); integer >>= 20; assert(integer.length == 0); @@ -893,7 +858,7 @@ struct Integer integer = 1431655765; integer >>= 16; assert(integer.length == 2); - assert(integer.rep[0] == 0x55 && integer.rep[1] == 0x55); + assert(integer.rep[1] == 0x55 && integer.rep[0] == 0x55); integer >>= 16; assert(integer.length == 0); @@ -901,50 +866,45 @@ struct Integer /// Ditto. ref Integer opOpAssign(string op : "<<")(const size_t operand) @trusted - out { - assert(this.size == 0 || this.rep[0] > 0); - } - body - { - ubyte carry; auto i = this.length; size_t j; + size_t newSize; const bit = operand % 8; const delta = 8 - bit; + const step = operand / 8; - if (cast(ubyte) (this.rep[0] >> delta)) + auto carry = cast(ubyte) (this.rep[this.size - 1] >> delta); + if (carry != 0) { - const newSize = i + operand / 8 + 1; + newSize = length + step + 1; this.rep = allocator.resize(this.rep[0 .. this.size], newSize).ptr; this.size = newSize; - j = i + 1; + j = newSize - 1; + this.rep[j] = carry; } else { - const newSize = i + operand / 8; + newSize = length + step; this.rep = allocator.resize(this.rep[0 .. this.size], newSize).ptr; - j = i; + this.size = j = newSize; } - do + + --i, --j; + for (; i > 0; --i, --j) { - --i, --j; - const oldCarry = carry; - carry = rep[i] >> delta; - rep[j] = cast(ubyte) ((this.rep[i] << bit) | oldCarry); - } - while (i); - if (carry) - { - rep[0] = carry; + this.rep[i] = cast(ubyte) (this.rep[j] << bit) | (this.rep[j - 1] >> delta); } + this.rep[i] = cast(ubyte) (this.rep[j] << bit); + this.rep[0 .. step].fill(cast(ubyte) 0); + return this; } private @nogc unittest { auto integer = Integer(4294967295); - ubyte[5] expected = [0x01, 0xff, 0xff, 0xff, 0xfe]; + ubyte[5] expected = [ 0xfe, 0xff, 0xff, 0xff, 0x01 ]; integer <<= 1; assert(integer.rep[0 .. integer.size] == expected); } @@ -956,10 +916,9 @@ struct Integer } body { - auto p = this.rep + this.size; - while (p != this.rep) + for (ubyte* p = this.rep; p < this.rep + this.size; ++p) { - --p, --*p; + --*p; if (*p != ubyte.max) { break; @@ -970,16 +929,16 @@ struct Integer private void increment() nothrow @trusted @nogc { - auto p = this.rep + this.size; - while (p != this.rep) + ubyte* p; + for (p = this.rep; p < this.rep + this.size; ++p) { - --p, ++*p; + ++*p; if (*p > 0) { return; } } - if (p == this.rep) + if (p == this.rep + this.size) { expand(); } @@ -1054,11 +1013,6 @@ struct Integer /// Ditto. ref Integer opUnary(string op : "++")() - out - { - assert(this.size == 0 || this.rep[0] > 0); - } - body { if (this.sign) { @@ -1077,11 +1031,6 @@ struct Integer /// Ditto. ref Integer opUnary(string op : "--")() - out - { - assert(this.size == 0 || this.rep[0] > 0); - } - body { if (this.sign) { @@ -1108,11 +1057,11 @@ struct Integer integer = 511; ++integer; assert(integer.length == 2); - assert(integer.rep[0] == 0x02 && integer.rep[1] == 0x00); + assert(integer.rep[1] == 0x02 && integer.rep[0] == 0x00); --integer; assert(integer.length == 2); - assert(integer.rep[0] == 0x01 && integer.rep[1] == 0xff); + assert(integer.rep[1] == 0x01 && integer.rep[0] == 0xff); integer = 79; ++integer; @@ -1126,11 +1075,11 @@ struct Integer integer = 65535; ++integer; assert(integer.length == 3); - assert(integer.rep[0] == 0x01 && integer.rep[1] == 0x00 && integer.rep[2] == 0x00); + assert(integer.rep[2] == 0x01 && integer.rep[1] == 0x00 && integer.rep[0] == 0x00); --integer; assert(integer.length == 2); - assert(integer.rep[0] == 0xff && integer.rep[1] == 0xff); + assert(integer.rep[1] == 0xff && integer.rep[0] == 0xff); integer = -2; ++integer; @@ -1217,10 +1166,11 @@ struct Integer return 0; } T ret; - const(ubyte)* src = this.rep + this.size - 1; - for (ubyte shift; src >= this.rep && shift <= T.sizeof * 8; --src, shift += 8) + const(ubyte)* src = this.rep; + ubyte shift; + for (; src < this.rep + this.size && shift <= T.sizeof * 8; ++src, shift += 8) { - ret |= cast(T) (*src) << shift; + ret |= (cast(T) *src) << shift; } return ret; } diff --git a/source/tanya/math/package.d b/source/tanya/math/package.d index 994d5c7..fd5f2a2 100644 --- a/source/tanya/math/package.d +++ b/source/tanya/math/package.d @@ -87,21 +87,20 @@ in } body { - size_t i = y.length; + size_t i; auto tmp1 = Integer(x, x.allocator); auto result = Integer(x.allocator); - if (x.length == 0 && i != 0) + if (x.length == 0 && y.length != 0) { - i = 0; + i = y.length; } else { result = 1; } - while (i) + while (i < y.length) { - --i; for (ubyte mask = 0x01; mask; mask <<= 1) { if (y.rep[i] & mask) @@ -113,6 +112,7 @@ body tmp1 *= tmp2; tmp1 %= z; } + ++i; } return result; } From 4635835a991164a54b425ee9b35427957623589e Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 20 Apr 2017 17:32:16 +0200 Subject: [PATCH 2/2] Rename Vector range to Slice --- source/tanya/container/package.d | 2 +- source/tanya/container/slice.d | 1657 ++++++++++++++++++++++++++++++ source/tanya/container/vector.d | 1642 +---------------------------- 3 files changed, 1659 insertions(+), 1642 deletions(-) create mode 100644 source/tanya/container/slice.d diff --git a/source/tanya/container/package.d b/source/tanya/container/package.d index 8012ffe..171f660 100644 --- a/source/tanya/container/package.d +++ b/source/tanya/container/package.d @@ -14,5 +14,5 @@ module tanya.container; public import tanya.container.buffer; public import tanya.container.list; -public import tanya.container.vector; +public import tanya.container.slice; public import tanya.container.queue; diff --git a/source/tanya/container/slice.d b/source/tanya/container/slice.d new file mode 100644 index 0000000..e0b2e5f --- /dev/null +++ b/source/tanya/container/slice.d @@ -0,0 +1,1657 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Single-dimensioned array. + * + * Copyright: Eugene Wissner 2016-2017. + * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, + * Mozilla Public License, v. 2.0). + * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) + */ +module tanya.container.slice; + +import core.checkedint; +import core.exception; +import std.algorithm.comparison; +import std.algorithm.mutation; +import std.conv; +import std.range.primitives; +import std.meta; +import std.traits; +import tanya.memory; + +/** + * Random-access range for the $(D_PSYMBOL Vector). + * + * Params: + * T = Element type. + */ +struct Slice(T) + if (!is(Unqual!T == char)) +{ + private T* begin, end; + private alias ContainerType = CopyConstness!(T, Vector!(Unqual!T)); + private ContainerType* vector; + + invariant + { + assert(this.begin <= this.end); + assert(this.vector !is null); + assert(this.begin >= this.vector.data); + assert(this.end <= this.vector.data + this.vector.length); + } + + private this(ref ContainerType vector, T* begin, T* end) @trusted + in + { + assert(begin <= end); + assert(begin >= vector.data); + assert(end <= vector.data + vector.length); + } + body + { + this.vector = &vector; + this.begin = begin; + this.end = end; + } + + @disable this(); + + @property Slice save() + { + return this; + } + + @property bool empty() const + { + return this.begin == this.end; + } + + @property size_t length() const + { + return this.end - this.begin; + } + + alias opDollar = length; + + @property ref inout(T) front() inout + in + { + assert(!empty); + } + body + { + return *this.begin; + } + + @property ref inout(T) back() inout @trusted + in + { + assert(!empty); + } + body + { + return *(this.end - 1); + } + + void popFront() @trusted + in + { + assert(!empty); + } + body + { + ++this.begin; + } + + void popBack() @trusted + in + { + assert(!empty); + } + body + { + --this.end; + } + + ref inout(T) opIndex(const size_t i) inout @trusted + in + { + assert(i < length); + } + body + { + return *(this.begin + i); + } + + Slice opIndex() + { + return typeof(return)(*this.vector, this.begin, this.end); + } + + Slice!(const T) opIndex() const + { + return typeof(return)(*this.vector, this.begin, this.end); + } + + Slice opSlice(const size_t i, const size_t j) @trusted + in + { + assert(i <= j); + assert(j <= length); + } + body + { + return typeof(return)(*this.vector, this.begin + i, this.begin + j); + } + + Slice!(const T) opSlice(const size_t i, const size_t j) const @trusted + in + { + assert(i <= j); + assert(j <= length); + } + body + { + return typeof(return)(*this.vector, this.begin + i, this.begin + j); + } + + inout(T[]) get() inout @trusted + { + return this.begin[0 .. length]; + } +} + +/** + * One dimensional array. + * + * Params: + * T = Content type. + */ +struct Vector(T) + if (!is(T == char)) +{ + private size_t length_; + private T* data; + private size_t capacity_; + + invariant + { + assert(this.length_ <= this.capacity_); + assert(this.capacity_ == 0 || this.data !is null); + } + + /** + * Creates a new $(D_PSYMBOL Vector) with the elements from a static array. + * + * Params: + * R = Static array size. + * init = Values to initialize the vector with. + * allocator = Allocator. + */ + this(size_t R)(T[R] init, shared Allocator allocator = defaultAllocator) + { + this(allocator); + insertBack!(T[])(init[]); + } + + /** + * Creates a new $(D_PSYMBOL Vector) with the elements from an input range. + * + * Params: + * R = Type of the initial range. + * init = Values to initialize the vector with. + * allocator = Allocator. + */ + this(R)(R init, shared Allocator allocator = defaultAllocator) + if (!isInfinite!R + && isInputRange!R + && isImplicitlyConvertible!(ElementType!R, T)) + { + this(allocator); + insertBack(init); + } + + /** + * Initializes this vector from another one. + * + * If $(D_PARAM init) is passed by value, it won't be copied, but moved. + * If the allocator of ($D_PARAM init) matches $(D_PARAM allocator), + * $(D_KEYWORD this) will just take the ownership over $(D_PARAM init)'s + * storage, otherwise, the storage will be allocated with + * $(D_PARAM allocator) and all elements will be moved; + * $(D_PARAM init) will be destroyed at the end. + * + * If $(D_PARAM init) is passed by reference, it will be copied. + * + * Params: + * R = Vector type. + * init = Source vector. + * allocator = Allocator. + */ + this(R)(const ref R init, shared Allocator allocator = defaultAllocator) + if (is(Unqual!R == Vector)) + { + this(allocator); + insertBack(init[]); + } + + /// Ditto. + this(R)(R init, shared Allocator allocator = defaultAllocator) @trusted + if (is(R == Vector)) + { + this(allocator); + if (allocator is init.allocator) + { + // Just steal all references and the allocator. + this.data = init.data; + this.length_ = init.length_; + this.capacity_ = init.capacity_; + + // Reset the source vector, so it can't destroy the moved storage. + init.length_ = init.capacity_ = 0; + init.data = null; + } + else + { + // Move each element. + reserve(init.length_); + moveEmplaceAll(init.data[0 .. init.length_], this.data[0 .. init.length_]); + this.length_ = init.length_; + // Destructor of init should destroy it here. + } + } + + /// + @trusted @nogc unittest + { + auto v1 = Vector!int([1, 2, 3]); + auto v2 = Vector!int(v1); + assert(v1 == v2); + + auto v3 = Vector!int(Vector!int([1, 2, 3])); + assert(v1 == v3); + assert(v3.length == 3); + assert(v3.capacity == 3); + } + + private @trusted @nogc unittest // const constructor tests + { + auto v1 = const Vector!int([1, 2, 3]); + auto v2 = Vector!int(v1); + assert(v1.data !is v2.data); + assert(v1 == v2); + + auto v3 = const Vector!int(Vector!int([1, 2, 3])); + assert(v1 == v3); + assert(v3.length == 3); + assert(v3.capacity == 3); + } + + /** + * Creates a new $(D_PSYMBOL Vector). + * + * Params: + * len = Initial length of the vector. + * init = Initial value to fill the vector with. + * allocator = Allocator. + */ + this(const size_t len, T init, shared Allocator allocator = defaultAllocator) @trusted + { + this(allocator); + reserve(len); + uninitializedFill(this.data[0 .. len], init); + length_ = len; + } + + /// Ditto. + this(const size_t len, shared Allocator allocator = defaultAllocator) + { + this(allocator); + length = len; + } + + /// Ditto. + this(shared Allocator allocator) + in + { + assert(allocator !is null); + } + body + { + allocator_ = allocator; + } + + /// + unittest + { + auto v = Vector!int([3, 8, 2]); + + assert(v.capacity == 3); + assert(v.length == 3); + assert(v[0] == 3 && v[1] == 8 && v[2] == 2); + } + + /// + unittest + { + auto v = Vector!int(3, 5); + + assert(v.capacity == 3); + assert(v.length == 3); + assert(v[0] == 5 && v[1] == 5 && v[2] == 5); + } + + @safe unittest + { + auto v1 = Vector!int(defaultAllocator); + } + + /** + * Destroys this $(D_PSYMBOL Vector). + */ + ~this() @trusted + { + clear(); + allocator.deallocate(this.data[0 .. capacity]); + } + + /** + * Copies the vector. + */ + this(this) + { + auto buf = this.data[0 .. this.length_]; + this.length_ = capacity_ = 0; + this.data = null; + insertBack(buf); + } + + /** + * Removes all elements. + */ + void clear() + { + length = 0; + } + + /// + unittest + { + auto v = Vector!int([18, 20, 15]); + v.clear(); + assert(v.length == 0); + assert(v.capacity == 3); + } + + /** + * Returns: How many elements the vector can contain without reallocating. + */ + @property size_t capacity() const + { + return capacity_; + } + + /// + @safe @nogc unittest + { + auto v = Vector!int(4); + assert(v.capacity == 4); + } + + /** + * Returns: Vector length. + */ + @property size_t length() const + { + return length_; + } + + /// Ditto. + size_t opDollar() const + { + return length; + } + + /** + * Expands/shrinks the vector. + * + * Params: + * len = New length. + */ + @property void length(const size_t len) @trusted + { + if (len == length) + { + return; + } + else if (len > length) + { + reserve(len); + initializeAll(this.data[length_ .. len]); + } + else + { + static if (hasElaborateDestructor!T) + { + const T* end = this.data + length_ - 1; + for (T* e = this.data + len; e != end; ++e) + { + destroy(*e); + } + } + } + length_ = len; + } + + /// + unittest + { + Vector!int v; + + v.length = 5; + assert(v.length == 5); + assert(v.capacity == 5); + + v.length = 7; + assert(v.length == 7); + assert(v.capacity == 7); + + assert(v[$ - 1] == 0); + v[$ - 1] = 3; + assert(v[$ - 1] == 3); + + v.length = 0; + assert(v.length == 0); + assert(v.capacity == 7); + } + + /** + * Reserves space for $(D_PARAM size) elements. + * + * If $(D_PARAM size) is less than or equal to the $(D_PSYMBOL capacity), the + * function call does not cause a reallocation and the vector capacity is not + * affected. + * + * Params: + * size = Desired size. + */ + void reserve(const size_t size) @trusted + { + if (capacity_ >= size) + { + return; + } + bool overflow; + immutable byteSize = mulu(size, T.sizeof, overflow); + assert(!overflow); + + void[] buf = this.data[0 .. this.capacity_]; + if (!allocator.reallocateInPlace(buf, byteSize)) + { + buf = allocator.allocate(byteSize); + if (buf is null) + { + onOutOfMemoryErrorNoGC(); + } + scope (failure) + { + allocator.deallocate(buf); + } + const T* end = this.data + this.length_; + for (T* src = this.data, dest = cast(T*) buf; src != end; ++src, ++dest) + { + moveEmplace(*src, *dest); + static if (hasElaborateDestructor!T) + { + destroy(*src); + } + } + allocator.deallocate(this.data[0 .. this.capacity_]); + this.data = cast(T*) buf; + } + this.capacity_ = size; + } + + /// + @nogc @safe unittest + { + Vector!int v; + assert(v.capacity == 0); + assert(v.length == 0); + + v.reserve(3); + assert(v.capacity == 3); + assert(v.length == 0); + } + + /** + * Requests the vector to reduce its capacity to fit the $(D_PARAM size). + * + * The request is non-binding. The vector won't become smaller than the + * $(D_PARAM length). + * + * Params: + * size = Desired size. + */ + void shrink(const size_t size) @trusted + { + if (capacity <= size) + { + return; + } + immutable n = max(length, size); + void[] buf = this.data[0 .. this.capacity_]; + if (allocator.reallocateInPlace(buf, n * T.sizeof)) + { + this.capacity_ = n; + } + } + + /// + @nogc @safe unittest + { + Vector!int v; + assert(v.capacity == 0); + assert(v.length == 0); + + v.reserve(5); + v.insertBack(1); + v.insertBack(3); + assert(v.capacity == 5); + assert(v.length == 2); + } + + /** + * Returns: $(D_KEYWORD true) if the vector is empty. + */ + @property bool empty() const + { + return length == 0; + } + + /** + * Removes the value at the back of the vector. + * + * Returns: The number of elements removed + * + * Precondition: $(D_INLINECODE !empty). + */ + void removeBack() + in + { + assert(!empty); + } + body + { + length = length - 1; + } + + /** + * Removes $(D_PARAM howMany) elements from the vector. + * + * This method doesn't fail if it could not remove $(D_PARAM howMany) + * elements. Instead, if $(D_PARAM howMany) is greater than the vector + * length, all elements are removed. + * + * Params: + * howMany = How many elements should be removed. + * + * Returns: The number of elements removed + */ + size_t removeBack(const size_t howMany) + out (removed) + { + assert(removed <= howMany); + } + body + { + immutable toRemove = min(howMany, length); + + length = length - toRemove; + + return toRemove; + } + + /// + unittest + { + auto v = Vector!int([5, 18, 17]); + + assert(v.removeBack(0) == 0); + assert(v.removeBack(2) == 2); + assert(v.removeBack(3) == 1); + assert(v.removeBack(3) == 0); + } + + /** + * Remove all elements beloning to $(D_PARAM r). + * + * Params: + * r = Range originally obtained from this vector. + * + * Returns: A range spanning the remaining elements in the array that + * initially were right after $(D_PARAM r). + * + * Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this). + */ + Slice!T remove(Slice!T r) @trusted + in + { + assert(r.vector is &this); + assert(r.begin >= this.data); + assert(r.end <= this.data + length); + } + body + { + auto end = this.data + this.length; + moveAll(Slice!T(this, r.end, end), Slice!T(this, r.begin, end)); + length = length - r.length; + return Slice!T(this, r.begin, this.data + length); + } + + /// + @safe @nogc unittest + { + auto v = Vector!int([5, 18, 17, 2, 4, 6, 1]); + + assert(v.remove(v[1 .. 3]).length == 4); + assert(v[0] == 5 && v[1] == 2 && v[2] == 4 && v[3] == 6 && v[4] == 1); + assert(v.length == 5); + + assert(v.remove(v[4 .. 4]).length == 1); + assert(v[0] == 5 && v[1] == 2 && v[2] == 4 && v[3] == 6 && v[4] == 1); + assert(v.length == 5); + + assert(v.remove(v[4 .. 5]).length == 0); + assert(v[0] == 5 && v[1] == 2 && v[2] == 4 && v[3] == 6); + assert(v.length == 4); + + assert(v.remove(v[]).length == 0); + + } + + private void moveBack(R)(ref R el) @trusted + if (isImplicitlyConvertible!(R, T)) + { + reserve(this.length + 1); + moveEmplace(el, *(this.data + this.length_)); + ++this.length_; + } + + /** + * Inserts the $(D_PARAM el) into the vector. + * + * Params: + * R = Type of the inserted value(s) (single value, range or static array). + * el = Value(s) should be inserted. + * + * Returns: The number of elements inserted. + */ + size_t insertBack(R)(R el) + if (isImplicitlyConvertible!(R, T)) + { + moveBack(el); + return 1; + } + + /// Ditto. + size_t insertBack(R)(ref R el) @trusted + if (isImplicitlyConvertible!(R, T)) + { + reserve(this.length_ + 1); + emplace(this.data + this.length_, el); + ++this.length_; + return 1; + } + + /// Ditto. + size_t insertBack(R)(R el) + if (!isInfinite!R + && isInputRange!R + && isImplicitlyConvertible!(ElementType!R, T)) + { + static if (hasLength!R) + { + reserve(length + el.length); + } + size_t retLength; + foreach (e; el) + { + retLength += insertBack(e); + } + return retLength; + } + + /// Ditto. + size_t insertBack(size_t R)(T[R] el) + { + return insertBack!(T[])(el[]); + } + + /// Ditto. + alias insert = insertBack; + + /// + unittest + { + struct TestRange + { + int counter = 6; + + int front() + { + return counter; + } + + void popFront() + { + counter -= 2; + } + + bool empty() + { + return counter == 0; + } + } + + Vector!int v1; + + assert(v1.insertBack(5) == 1); + assert(v1.length == 1); + assert(v1.capacity == 1); + assert(v1.back == 5); + + assert(v1.insertBack(TestRange()) == 3); + assert(v1.length == 4); + assert(v1.capacity == 4); + assert(v1[0] == 5 && v1[1] == 6 && v1[2] == 4 && v1[3] == 2); + + assert(v1.insertBack([34, 234]) == 2); + assert(v1.length == 6); + assert(v1.capacity == 6); + assert(v1[4] == 34 && v1[5] == 234); + } + + /** + * Inserts $(D_PARAM el) before or after $(D_PARAM r). + * + * Params: + * R = Type of the inserted value(s) (single value, range or static array). + * r = Range originally obtained from this vector. + * el = Value(s) should be inserted. + * + * Returns: The number of elements inserted. + * + * Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this). + */ + size_t insertAfter(R)(Slice!T r, R el) + if (!isInfinite!R + && isInputRange!R + && isImplicitlyConvertible!(ElementType!R, T)) + in + { + assert(r.vector is &this); + assert(r.begin >= this.data); + assert(r.end <= this.data + length); + } + body + { + immutable oldLen = length; + immutable offset = r.end - this.data; + immutable inserted = insertBack(el); + bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]); + return inserted; + } + + /// Ditto. + size_t insertAfter(size_t R)(Slice!T r, T[R] el) + in + { + assert(r.vector is &this); + assert(r.begin >= this.data); + assert(r.end <= this.data + length); + } + body + { + return insertAfter!(T[])(r, el[]); + } + + /// Ditto. + size_t insertAfter(R)(Slice!T r, auto ref R el) + if (isImplicitlyConvertible!(R, T)) + in + { + assert(r.vector is &this); + assert(r.begin >= this.data); + assert(r.end <= this.data + length); + } + body + { + immutable oldLen = length; + immutable offset = r.end - this.data; + + static if (__traits(isRef, el)) + { + insertBack(el); + } + else + { + moveBack(el); + } + bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]); + + return 1; + } + + /// Ditto. + size_t insertBefore(R)(Slice!T r, R el) + if (!isInfinite!R + && isInputRange!R + && isImplicitlyConvertible!(ElementType!R, T)) + in + { + assert(r.vector is &this); + assert(r.begin >= this.data); + assert(r.end <= this.data + length); + } + body + { + return insertAfter(Slice!T(this, this.data, r.begin), el); + } + + /// Ditto. + size_t insertBefore(size_t R)(Slice!T r, T[R] el) + in + { + assert(r.vector is &this); + assert(r.begin >= this.data); + assert(r.end <= this.data + length); + } + body + { + return insertBefore!(T[])(r, el[]); + } + + /// Ditto. + size_t insertBefore(R)(Slice!T r, auto ref R el) + if (isImplicitlyConvertible!(R, T)) + in + { + assert(r.vector is &this); + assert(r.begin >= this.data); + assert(r.end <= this.data + length); + } + body + { + immutable oldLen = length; + immutable offset = r.begin - this.data; + + static if (__traits(isRef, el)) + { + insertBack(el); + } + else + { + moveBack(el); + } + bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]); + + return 1; + } + + /// + unittest + { + Vector!int v1; + v1.insertAfter(v1[], [2, 8]); + assert(v1[0] == 2); + assert(v1[1] == 8); + assert(v1.length == 2); + + v1.insertAfter(v1[], [1, 2]); + assert(v1[0] == 2); + assert(v1[1] == 8); + assert(v1[2] == 1); + assert(v1[3] == 2); + assert(v1.length == 4); + + v1.insertAfter(v1[0 .. 0], [1, 2]); + assert(v1[0] == 1); + assert(v1[1] == 2); + assert(v1[2] == 2); + assert(v1[3] == 8); + assert(v1[4] == 1); + assert(v1[5] == 2); + assert(v1.length == 6); + + v1.insertAfter(v1[0 .. 4], 9); + assert(v1[0] == 1); + assert(v1[1] == 2); + assert(v1[2] == 2); + assert(v1[3] == 8); + assert(v1[4] == 9); + assert(v1[5] == 1); + assert(v1[6] == 2); + assert(v1.length == 7); + } + + /// + unittest + { + Vector!int v1; + v1.insertBefore(v1[], [2, 8]); + assert(v1[0] == 2); + assert(v1[1] == 8); + assert(v1.length == 2); + + v1.insertBefore(v1[], [1, 2]); + assert(v1[0] == 1); + assert(v1[1] == 2); + assert(v1[2] == 2); + assert(v1[3] == 8); + assert(v1.length == 4); + + v1.insertBefore(v1[0 .. 1], [1, 2]); + assert(v1[0] == 1); + assert(v1[1] == 2); + assert(v1[2] == 1); + assert(v1[3] == 2); + assert(v1[4] == 2); + assert(v1[5] == 8); + assert(v1.length == 6); + + v1.insertBefore(v1[2 .. $], 9); + assert(v1[0] == 1); + assert(v1[1] == 2); + assert(v1[2] == 9); + assert(v1[3] == 1); + assert(v1[4] == 2); + assert(v1[5] == 2); + assert(v1[6] == 8); + assert(v1.length == 7); + } + + /** + * Assigns a value to the element with the index $(D_PARAM pos). + * + * Params: + * value = Value. + * pos = Position. + * + * Returns: Assigned value. + * + * Precondition: $(D_INLINECODE length > pos). + */ + ref T opIndexAssign(ref T value, const size_t pos) + { + return opIndex(pos) = value; + } + + @safe unittest + { + Vector!int a = Vector!int(1); + a[0] = 5; + assert(a[0] == 5); + } + + /// Ditto. + T opIndexAssign(T value, const size_t pos) + { + return opIndexAssign(value, pos); + } + + /// Ditto. + Slice!T opIndexAssign(T value) + { + return opSliceAssign(value, 0, length); + } + + /// Ditto. + Slice!T opIndexAssign(ref T value) + { + return opSliceAssign(value, 0, length); + } + + /** + * Assigns a range or a static array. + * + * Params: + * R = Range type or static array length. + * value = Value. + * + * Returns: Assigned value. + * + * Precondition: $(D_INLINECODE length == value.length). + */ + Slice!T opIndexAssign(R)(R value) + if (!isInfinite!R && isInputRange!R + && isImplicitlyConvertible!(ElementType!R, T)) + { + return opSliceAssign!R(value, 0, length); + } + + /// Ditto. + Slice!T opIndexAssign(size_t R)(T[R] value) + { + return opSliceAssign!R(value, 0, length); + } + + /// + @nogc unittest + { + auto v1 = Vector!int([12, 1, 7]); + + v1[] = 3; + assert(v1[0] == 3); + assert(v1[1] == 3); + assert(v1[2] == 3); + + v1[] = [7, 1, 12]; + assert(v1[0] == 7); + assert(v1[1] == 1); + assert(v1[2] == 12); + } + + /** + * Params: + * pos = Index. + * + * Returns: The value at a specified index. + * + * Precondition: $(D_INLINECODE length > pos). + */ + ref inout(T) opIndex(const size_t pos) inout @trusted + in + { + assert(length > pos); + } + body + { + return *(this.data + pos); + } + + /** + * Returns: Random access range that iterates over elements of the vector, in + * forward order. + */ + Slice!T opIndex() @trusted + { + return typeof(return)(this, this.data, this.data + length); + } + + /// Ditto. + Slice!(const T) opIndex() const @trusted + { + return typeof(return)(this, this.data, this.data + length); + } + + /// + unittest + { + const v1 = Vector!int([6, 123, 34, 5]); + + assert(v1[0] == 6); + assert(v1[1] == 123); + assert(v1[2] == 34); + assert(v1[3] == 5); + static assert(is(typeof(v1[0]) == const(int))); + static assert(is(typeof(v1[]))); + } + + /** + * Comparison for equality. + * + * Params: + * that = The vector to compare with. + * + * Returns: $(D_KEYWORD true) if the vectors are equal, $(D_KEYWORD false) + * otherwise. + */ + bool opEquals()(auto ref typeof(this) that) @trusted + { + return equal(this.data[0 .. length], that.data[0 .. that.length]); + } + + /// Ditto. + bool opEquals()(const auto ref typeof(this) that) const @trusted + { + return equal(this.data[0 .. length], that.data[0 .. that.length]); + } + + /// Ditto. + bool opEquals(Slice!T that) + { + return equal(opIndex(), that); + } + + /** + * Comparison for equality. + * + * Params: + * R = Right hand side type. + * that = Right hand side vector range. + * + * Returns: $(D_KEYWORD true) if the vector and the range are equal, + * $(D_KEYWORD false) otherwise. + */ + bool opEquals(R)(Slice!R that) const + if (is(Unqual!R == T)) + { + return equal(opIndex(), that); + } + + /// + unittest + { + Vector!int v1, v2; + assert(v1 == v2); + + v1.length = 1; + v2.length = 2; + assert(v1 != v2); + + v1.length = 2; + v1[0] = v2[0] = 2; + v1[1] = 3; + v2[1] = 4; + assert(v1 != v2); + + v2[1] = 3; + assert(v1 == v2); + } + + /** + * Returns: The first element. + * + * Precondition: $(D_INLINECODE !empty). + */ + @property ref inout(T) front() inout + in + { + assert(!empty); + } + body + { + return *this.data; + } + + /// + @safe unittest + { + auto v = Vector!int([5]); + + assert(v.front == 5); + + v.length = 2; + v[1] = 15; + assert(v.front == 5); + } + + /** + * Returns: The last element. + * + * Precondition: $(D_INLINECODE !empty). + */ + @property ref inout(T) back() inout @trusted + in + { + assert(!empty); + } + body + { + return *(this.data + length - 1); + } + + /// + unittest + { + auto v = Vector!int([5]); + + assert(v.back == 5); + + v.length = 2; + v[1] = 15; + assert(v.back == 15); + } + + /** + * Params: + * i = Slice start. + * j = Slice end. + * + * Returns: A range that iterates over elements of the container from + * index $(D_PARAM i) up to (excluding) index $(D_PARAM j). + * + * Precondition: $(D_INLINECODE i <= j && j <= length). + */ + Slice!T opSlice(const size_t i, const size_t j) @trusted + in + { + assert(i <= j); + assert(j <= length); + } + body + { + return typeof(return)(this, this.data + i, this.data + j); + } + + /// Ditto. + Slice!(const T) opSlice(const size_t i, const size_t j) const @trusted + in + { + assert(i <= j); + assert(j <= length); + } + body + { + return typeof(return)(this, this.data + i, this.data + j); + } + + /// + unittest + { + Vector!int v; + auto r = v[]; + assert(r.length == 0); + assert(r.empty); + } + + /// + unittest + { + auto v = Vector!int([1, 2, 3]); + auto r = v[]; + + assert(r.front == 1); + assert(r.back == 3); + + r.popFront(); + assert(r.front == 2); + + r.popBack(); + assert(r.back == 2); + + assert(r.length == 1); + } + + /// + unittest + { + auto v = Vector!int([1, 2, 3, 4]); + auto r = v[1 .. 4]; + assert(r.length == 3); + assert(r[0] == 2); + assert(r[1] == 3); + assert(r[2] == 4); + + r = v[0 .. 0]; + assert(r.length == 0); + + r = v[4 .. 4]; + assert(r.length == 0); + } + + /** + * Slicing assignment. + * + * Params: + * R = Type of the assigned slice or length of the static array should be + * assigned. + * value = New value (single value, input range or static array). + * i = Slice start. + * j = Slice end. + * + * Returns: Slice with the assigned part of the vector. + * + * Precondition: $(D_INLINECODE i <= j && j <= length + * && value.length == j - i) + */ + Slice!T opSliceAssign(R)(R value, const size_t i, const size_t j) @trusted + if (!isInfinite!R + && isInputRange!R + && isImplicitlyConvertible!(ElementType!R, T)) + in + { + assert(i <= j); + assert(j <= length); + assert(j - i == walkLength(value)); + } + body + { + copy(value, this.data[i .. j]); + return opSlice(i, j); + } + + /// Ditto. + Slice!T opSliceAssign(size_t R)(T[R] value, const size_t i, const size_t j) + { + return opSliceAssign!(T[])(value[], i, j); + } + + /// Ditto. + Slice!T opSliceAssign(ref T value, const size_t i, const size_t j) @trusted + in + { + assert(i <= j); + assert(j <= length); + } + body + { + fill(this.data[i .. j], value); + return opSlice(i, j); + } + + /// Ditto. + Slice!T opSliceAssign(T value, const size_t i, const size_t j) + { + return opSliceAssign(value, i, j); + } + + /// + @nogc @safe unittest + { + auto v1 = Vector!int([3, 3, 3]); + auto v2 = Vector!int([1, 2]); + + v1[0 .. 2] = 286; + assert(v1[0] == 286); + assert(v1[1] == 286); + assert(v1[2] == 3); + + v2[0 .. $] = v1[1 .. 3]; + assert(v2[0] == 286); + assert(v2[1] == 3); + + v1[0 .. 2] = [5, 8]; + assert(v1[0] == 5); + assert(v1[1] == 8); + assert(v1[2] == 3); + } + + /** + * Returns an array used internally by the vector to store its owned elements. + * The length of the returned array may differ from the size of the allocated + * memory for the vector: the array contains only initialized elements, but + * not the reserved memory. + * + * Returns: The array with elements of this vector. + */ + inout(T[]) get() inout @trusted + { + return this.data[0 .. length]; + } + + /// + unittest + { + auto v = Vector!int([1, 2, 4]); + auto data = v.get(); + + assert(data[0] == 1); + assert(data[1] == 2); + assert(data[2] == 4); + assert(data.length == 3); + + data = v[1 .. 2].get(); + assert(data[0] == 2); + assert(data.length == 1); + } + + /** + * Assigns another vector. + * + * If $(D_PARAM that) is passed by value, it won't be copied, but moved. + * This vector will take the ownership over $(D_PARAM that)'s storage and + * the allocator. + * + * If $(D_PARAM that) is passed by reference, it will be copied. + * + * Params: + * R = Content type. + * that = The value should be assigned. + * + * Returns: $(D_KEYWORD this). + */ + ref typeof(this) opAssign(R)(const ref R that) + if (is(Unqual!R == Vector)) + { + return this = that[]; + } + + /// Ditto. + ref typeof(this) opAssign(R)(R that) @trusted + if (is(R == Vector)) + { + swap(this.data, that.data); + swap(this.length_, that.length_); + swap(this.capacity_, that.capacity_); + swap(this.allocator_, that.allocator_); + return this; + } + + /** + * Assigns a range to the vector. + * + * Params: + * R = Content type. + * that = The value should be assigned. + * + * Returns: $(D_KEYWORD this). + */ + ref typeof(this) opAssign(R)(R that) + if (!isInfinite!R + && isInputRange!R + && isImplicitlyConvertible!(ElementType!R, T)) + { + length = 0; + insertBack(that); + return this; + } + + /// + @safe @nogc unittest + { + auto v1 = const Vector!int([5, 15, 8]); + Vector!int v2; + v2 = v1; + assert(v1 == v2); + } + + /// + @safe @nogc unittest + { + auto v1 = const Vector!int([5, 15, 8]); + Vector!int v2; + v2 = v1[0 .. 2]; + assert(equal(v1[0 .. 2], v2[])); + } + + // Move assignment. + private @safe @nogc unittest + { + Vector!int v1; + v1 = Vector!int([5, 15, 8]); + } + + /** + * Assigns a static array. + * + * Params: + * R = Static array size. + * that = Values to initialize the vector with. + * + * Returns: $(D_KEYWORD this). + */ + ref typeof(this) opAssign(size_t R)(T[R] that) + { + return opAssign!(T[])(that[]); + } + + /// + @safe @nogc unittest + { + auto v1 = Vector!int([5, 15, 8]); + Vector!int v2; + + v2 = [5, 15, 8]; + assert(v1 == v2); + } + + mixin DefaultAllocator; +} + +/// +unittest +{ + auto v = Vector!int([5, 15, 8]); + + assert(v.front == 5); + assert(v[1] == 15); + assert(v.back == 8); + + auto r = v[]; + r[0] = 7; + assert(r.front == 7); + assert(r.front == v.front); +} + +@nogc unittest +{ + const v1 = Vector!int(); + const Vector!int v2; + const v3 = Vector!int([1, 5, 8]); + static assert(is(PointerTarget!(typeof(v3.data)) == const(int))); +} + +@nogc unittest +{ + // Test that const vectors return usable ranges. + auto v = const Vector!int([1, 2, 4]); + auto r1 = v[]; + + assert(r1.back == 4); + r1.popBack(); + assert(r1.back == 2); + r1.popBack(); + assert(r1.back == 1); + r1.popBack(); + assert(r1.length == 0); + + static assert(!is(typeof(r1[0] = 5))); + static assert(!is(typeof(v[0] = 5))); + + const r2 = r1[]; + static assert(is(typeof(r2[]))); +} + +@nogc unittest +{ + Vector!int v1; + const Vector!int v2; + + auto r1 = v1[]; + auto r2 = v1[]; + + assert(r1.length == 0); + assert(r2.empty); + assert(r1 == r2); + + v1.insertBack([1, 2, 4]); + assert(v1[] == v1); + assert(v2[] == v2); + assert(v2[] != v1); + assert(v1[] != v2); + assert(v1[].equal(v1[])); + assert(v2[].equal(v2[])); + assert(!v1[].equal(v2[])); +} + +@nogc unittest +{ + struct MutableEqualsStruct + { + int opEquals(typeof(this) that) @nogc + { + return true; + } + } + struct ConstEqualsStruct + { + int opEquals(const typeof(this) that) const @nogc + { + return true; + } + } + auto v1 = Vector!ConstEqualsStruct(); + auto v2 = Vector!ConstEqualsStruct(); + assert(v1 == v2); + assert(v1[] == v2); + assert(v1 == v2[]); + assert(v1[].equal(v2[])); + + auto v3 = const Vector!ConstEqualsStruct(); + auto v4 = const Vector!ConstEqualsStruct(); + assert(v3 == v4); + assert(v3[] == v4); + assert(v3 == v4[]); + assert(v3[].equal(v4[])); + + auto v7 = Vector!MutableEqualsStruct(1, MutableEqualsStruct()); + auto v8 = Vector!MutableEqualsStruct(1, MutableEqualsStruct()); + assert(v7 == v8); + assert(v7[] == v8); + assert(v7 == v8[]); + assert(v7[].equal(v8[])); +} + +@nogc unittest +{ + struct SWithDtor + { + ~this() @nogc + { + } + } + auto v = Vector!SWithDtor(); // Destructor can destroy empty vectors. +} + +private unittest +{ + class A + { + } + A a1, a2; + auto v1 = Vector!A([a1, a2]); +} + +private @safe @nogc unittest +{ + auto v = Vector!int([5, 15, 8]); + { + size_t i; + + foreach (e; v) + { + assert(i != 0 || e == 5); + assert(i != 1 || e == 15); + assert(i != 2 || e == 8); + ++i; + } + assert(i == 3); + } + { + size_t i = 3; + + foreach_reverse (e; v) + { + --i; + assert(i != 2 || e == 8); + assert(i != 1 || e == 15); + assert(i != 0 || e == 5); + } + assert(i == 0); + } +} diff --git a/source/tanya/container/vector.d b/source/tanya/container/vector.d index 7f56bf0..8e4fa01 100644 --- a/source/tanya/container/vector.d +++ b/source/tanya/container/vector.d @@ -12,1644 +12,4 @@ */ module tanya.container.vector; -import core.checkedint; -import core.exception; -import std.algorithm.comparison; -import std.algorithm.mutation; -import std.conv; -import std.range.primitives; -import std.meta; -import std.traits; -import tanya.memory; - -/** - * Random-access range for the $(D_PSYMBOL Vector). - * - * Params: - * E = Element type. - */ -struct Range(E) -{ - private E* begin, end; - private alias ContainerType = CopyConstness!(E, Vector!(Unqual!E)); - private ContainerType* vector; - - invariant - { - assert(this.begin <= this.end); - assert(this.vector !is null); - assert(this.begin >= this.vector.data); - assert(this.end <= this.vector.data + this.vector.length); - } - - private this(ref ContainerType vector, E* begin, E* end) @trusted - in - { - assert(begin <= end); - assert(begin >= vector.data); - assert(end <= vector.data + vector.length); - } - body - { - this.vector = &vector; - this.begin = begin; - this.end = end; - } - - @disable this(); - - @property Range save() - { - return this; - } - - @property bool empty() const - { - return this.begin == this.end; - } - - @property size_t length() const - { - return this.end - this.begin; - } - - alias opDollar = length; - - @property ref inout(E) front() inout - in - { - assert(!empty); - } - body - { - return *this.begin; - } - - @property ref inout(E) back() inout @trusted - in - { - assert(!empty); - } - body - { - return *(this.end - 1); - } - - void popFront() @trusted - in - { - assert(!empty); - } - body - { - ++this.begin; - } - - void popBack() @trusted - in - { - assert(!empty); - } - body - { - --this.end; - } - - ref inout(E) opIndex(const size_t i) inout @trusted - in - { - assert(i < length); - } - body - { - return *(this.begin + i); - } - - Range opIndex() - { - return typeof(return)(*this.vector, this.begin, this.end); - } - - Range!(const E) opIndex() const - { - return typeof(return)(*this.vector, this.begin, this.end); - } - - Range opSlice(const size_t i, const size_t j) @trusted - in - { - assert(i <= j); - assert(j <= length); - } - body - { - return typeof(return)(*this.vector, this.begin + i, this.begin + j); - } - - Range!(const E) opSlice(const size_t i, const size_t j) const @trusted - in - { - assert(i <= j); - assert(j <= length); - } - body - { - return typeof(return)(*this.vector, this.begin + i, this.begin + j); - } - - inout(E[]) get() inout @trusted - { - return this.begin[0 .. length]; - } -} - -/** - * One dimensional array. - * - * Params: - * T = Content type. - */ -struct Vector(T) -{ - private size_t length_; - private T* data; - private size_t capacity_; - - invariant - { - assert(this.length_ <= this.capacity_); - assert(this.capacity_ == 0 || this.data !is null); - } - - /** - * Creates a new $(D_PSYMBOL Vector) with the elements from a static array. - * - * Params: - * R = Static array size. - * init = Values to initialize the vector with. - * allocator = Allocator. - */ - this(size_t R)(T[R] init, shared Allocator allocator = defaultAllocator) - { - this(allocator); - insertBack!(T[])(init[]); - } - - /** - * Creates a new $(D_PSYMBOL Vector) with the elements from an input range. - * - * Params: - * R = Type of the initial range. - * init = Values to initialize the vector with. - * allocator = Allocator. - */ - this(R)(R init, shared Allocator allocator = defaultAllocator) - if (!isInfinite!R - && isInputRange!R - && isImplicitlyConvertible!(ElementType!R, T)) - { - this(allocator); - insertBack(init); - } - - /** - * Initializes this vector from another one. - * - * If $(D_PARAM init) is passed by value, it won't be copied, but moved. - * If the allocator of ($D_PARAM init) matches $(D_PARAM allocator), - * $(D_KEYWORD this) will just take the ownership over $(D_PARAM init)'s - * storage, otherwise, the storage will be allocated with - * $(D_PARAM allocator) and all elements will be moved; - * $(D_PARAM init) will be destroyed at the end. - * - * If $(D_PARAM init) is passed by reference, it will be copied. - * - * Params: - * R = Vector type. - * init = Source vector. - * allocator = Allocator. - */ - this(R)(const ref R init, shared Allocator allocator = defaultAllocator) - if (is(Unqual!R == Vector)) - { - this(allocator); - insertBack(init[]); - } - - /// Ditto. - this(R)(R init, shared Allocator allocator = defaultAllocator) @trusted - if (is(R == Vector)) - { - this(allocator); - if (allocator is init.allocator) - { - // Just steal all references and the allocator. - this.data = init.data; - this.length_ = init.length_; - this.capacity_ = init.capacity_; - - // Reset the source vector, so it can't destroy the moved storage. - init.length_ = init.capacity_ = 0; - init.data = null; - } - else - { - // Move each element. - reserve(init.length_); - moveEmplaceAll(init.data[0 .. init.length_], this.data[0 .. init.length_]); - this.length_ = init.length_; - // Destructor of init should destroy it here. - } - } - - /// - @trusted @nogc unittest - { - auto v1 = Vector!int([1, 2, 3]); - auto v2 = Vector!int(v1); - assert(v1 == v2); - - auto v3 = Vector!int(Vector!int([1, 2, 3])); - assert(v1 == v3); - assert(v3.length == 3); - assert(v3.capacity == 3); - } - - private @trusted @nogc unittest // const constructor tests - { - auto v1 = const Vector!int([1, 2, 3]); - auto v2 = Vector!int(v1); - assert(v1.data !is v2.data); - assert(v1 == v2); - - auto v3 = const Vector!int(Vector!int([1, 2, 3])); - assert(v1 == v3); - assert(v3.length == 3); - assert(v3.capacity == 3); - } - - /** - * Creates a new $(D_PSYMBOL Vector). - * - * Params: - * len = Initial length of the vector. - * init = Initial value to fill the vector with. - * allocator = Allocator. - */ - this(const size_t len, T init, shared Allocator allocator = defaultAllocator) @trusted - { - this(allocator); - reserve(len); - uninitializedFill(this.data[0 .. len], init); - length_ = len; - } - - /// Ditto. - this(const size_t len, shared Allocator allocator = defaultAllocator) - { - this(allocator); - length = len; - } - - /// Ditto. - this(shared Allocator allocator) - in - { - assert(allocator !is null); - } - body - { - allocator_ = allocator; - } - - /// - unittest - { - auto v = Vector!int([3, 8, 2]); - - assert(v.capacity == 3); - assert(v.length == 3); - assert(v[0] == 3 && v[1] == 8 && v[2] == 2); - } - - /// - unittest - { - auto v = Vector!int(3, 5); - - assert(v.capacity == 3); - assert(v.length == 3); - assert(v[0] == 5 && v[1] == 5 && v[2] == 5); - } - - @safe unittest - { - auto v1 = Vector!int(defaultAllocator); - } - - /** - * Destroys this $(D_PSYMBOL Vector). - */ - ~this() @trusted - { - clear(); - allocator.deallocate(this.data[0 .. capacity]); - } - - /** - * Copies the vector. - */ - this(this) - { - auto buf = this.data[0 .. this.length_]; - this.length_ = capacity_ = 0; - this.data = null; - insertBack(buf); - } - - /** - * Removes all elements. - */ - void clear() - { - length = 0; - } - - /// - unittest - { - auto v = Vector!int([18, 20, 15]); - v.clear(); - assert(v.length == 0); - assert(v.capacity == 3); - } - - /** - * Returns: How many elements the vector can contain without reallocating. - */ - @property size_t capacity() const - { - return capacity_; - } - - /// - @safe @nogc unittest - { - auto v = Vector!int(4); - assert(v.capacity == 4); - } - - /** - * Returns: Vector length. - */ - @property size_t length() const - { - return length_; - } - - /// Ditto. - size_t opDollar() const - { - return length; - } - - /** - * Expands/shrinks the vector. - * - * Params: - * len = New length. - */ - @property void length(const size_t len) @trusted - { - if (len == length) - { - return; - } - else if (len > length) - { - reserve(len); - initializeAll(this.data[length_ .. len]); - } - else - { - static if (hasElaborateDestructor!T) - { - const T* end = this.data + length_ - 1; - for (T* e = this.data + len; e != end; ++e) - { - destroy(*e); - } - } - } - length_ = len; - } - - /// - unittest - { - Vector!int v; - - v.length = 5; - assert(v.length == 5); - assert(v.capacity == 5); - - v.length = 7; - assert(v.length == 7); - assert(v.capacity == 7); - - assert(v[$ - 1] == 0); - v[$ - 1] = 3; - assert(v[$ - 1] == 3); - - v.length = 0; - assert(v.length == 0); - assert(v.capacity == 7); - } - - /** - * Reserves space for $(D_PARAM size) elements. - * - * If $(D_PARAM size) is less than or equal to the $(D_PSYMBOL capacity), the - * function call does not cause a reallocation and the vector capacity is not - * affected. - * - * Params: - * size = Desired size. - */ - void reserve(const size_t size) @trusted - { - if (capacity_ >= size) - { - return; - } - bool overflow; - immutable byteSize = mulu(size, T.sizeof, overflow); - assert(!overflow); - - void[] buf = this.data[0 .. this.capacity_]; - if (!allocator.reallocateInPlace(buf, byteSize)) - { - buf = allocator.allocate(byteSize); - if (buf is null) - { - onOutOfMemoryErrorNoGC(); - } - scope (failure) - { - allocator.deallocate(buf); - } - const T* end = this.data + this.length_; - for (T* src = this.data, dest = cast(T*) buf; src != end; ++src, ++dest) - { - moveEmplace(*src, *dest); - static if (hasElaborateDestructor!T) - { - destroy(*src); - } - } - allocator.deallocate(this.data[0 .. this.capacity_]); - this.data = cast(T*) buf; - } - this.capacity_ = size; - } - - /// - @nogc @safe unittest - { - Vector!int v; - assert(v.capacity == 0); - assert(v.length == 0); - - v.reserve(3); - assert(v.capacity == 3); - assert(v.length == 0); - } - - /** - * Requests the vector to reduce its capacity to fit the $(D_PARAM size). - * - * The request is non-binding. The vector won't become smaller than the - * $(D_PARAM length). - * - * Params: - * size = Desired size. - */ - void shrink(const size_t size) @trusted - { - if (capacity <= size) - { - return; - } - immutable n = max(length, size); - void[] buf = this.data[0 .. this.capacity_]; - if (allocator.reallocateInPlace(buf, n * T.sizeof)) - { - this.capacity_ = n; - } - } - - /// - @nogc @safe unittest - { - Vector!int v; - assert(v.capacity == 0); - assert(v.length == 0); - - v.reserve(5); - v.insertBack(1); - v.insertBack(3); - assert(v.capacity == 5); - assert(v.length == 2); - } - - /** - * Returns: $(D_KEYWORD true) if the vector is empty. - */ - @property bool empty() const - { - return length == 0; - } - - /** - * Removes the value at the back of the vector. - * - * Returns: The number of elements removed - * - * Precondition: $(D_INLINECODE !empty). - */ - void removeBack() - in - { - assert(!empty); - } - body - { - length = length - 1; - } - - /** - * Removes $(D_PARAM howMany) elements from the vector. - * - * This method doesn't fail if it could not remove $(D_PARAM howMany) - * elements. Instead, if $(D_PARAM howMany) is greater than the vector - * length, all elements are removed. - * - * Params: - * howMany = How many elements should be removed. - * - * Returns: The number of elements removed - */ - size_t removeBack(const size_t howMany) - out (removed) - { - assert(removed <= howMany); - } - body - { - immutable toRemove = min(howMany, length); - - length = length - toRemove; - - return toRemove; - } - - /// - unittest - { - auto v = Vector!int([5, 18, 17]); - - assert(v.removeBack(0) == 0); - assert(v.removeBack(2) == 2); - assert(v.removeBack(3) == 1); - assert(v.removeBack(3) == 0); - } - - /** - * Remove all elements beloning to $(D_PARAM r). - * - * Params: - * r = Range originally obtained from this vector. - * - * Returns: A range spanning the remaining elements in the array that - * initially were right after $(D_PARAM r). - * - * Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this). - */ - Range!T remove(Range!T r) @trusted - in - { - assert(r.vector is &this); - assert(r.begin >= this.data); - assert(r.end <= this.data + length); - } - body - { - auto end = this.data + this.length; - moveAll(Range!T(this, r.end, end), Range!T(this, r.begin, end)); - length = length - r.length; - return Range!T(this, r.begin, this.data + length); - } - - /// - @safe @nogc unittest - { - auto v = Vector!int([5, 18, 17, 2, 4, 6, 1]); - - assert(v.remove(v[1 .. 3]).length == 4); - assert(v[0] == 5 && v[1] == 2 && v[2] == 4 && v[3] == 6 && v[4] == 1); - assert(v.length == 5); - - assert(v.remove(v[4 .. 4]).length == 1); - assert(v[0] == 5 && v[1] == 2 && v[2] == 4 && v[3] == 6 && v[4] == 1); - assert(v.length == 5); - - assert(v.remove(v[4 .. 5]).length == 0); - assert(v[0] == 5 && v[1] == 2 && v[2] == 4 && v[3] == 6); - assert(v.length == 4); - - assert(v.remove(v[]).length == 0); - - } - - private void moveBack(R)(ref R el) @trusted - if (isImplicitlyConvertible!(R, T)) - { - reserve(this.length + 1); - moveEmplace(el, *(this.data + this.length_)); - ++this.length_; - } - - /** - * Inserts the $(D_PARAM el) into the vector. - * - * Params: - * R = Type of the inserted value(s) (single value, range or static array). - * el = Value(s) should be inserted. - * - * Returns: The number of elements inserted. - */ - size_t insertBack(R)(R el) - if (isImplicitlyConvertible!(R, T)) - { - moveBack(el); - return 1; - } - - /// Ditto. - size_t insertBack(R)(ref R el) @trusted - if (isImplicitlyConvertible!(R, T)) - { - reserve(this.length_ + 1); - emplace(this.data + this.length_, el); - ++this.length_; - return 1; - } - - /// Ditto. - size_t insertBack(R)(R el) - if (!isInfinite!R - && isInputRange!R - && isImplicitlyConvertible!(ElementType!R, T)) - { - static if (hasLength!R) - { - reserve(length + el.length); - } - size_t retLength; - foreach (e; el) - { - retLength += insertBack(e); - } - return retLength; - } - - /// Ditto. - size_t insertBack(size_t R)(T[R] el) - { - return insertBack!(T[])(el[]); - } - - /// Ditto. - alias insert = insertBack; - - /// - unittest - { - struct TestRange - { - int counter = 6; - - int front() - { - return counter; - } - - void popFront() - { - counter -= 2; - } - - bool empty() - { - return counter == 0; - } - } - - Vector!int v1; - - assert(v1.insertBack(5) == 1); - assert(v1.length == 1); - assert(v1.capacity == 1); - assert(v1.back == 5); - - assert(v1.insertBack(TestRange()) == 3); - assert(v1.length == 4); - assert(v1.capacity == 4); - assert(v1[0] == 5 && v1[1] == 6 && v1[2] == 4 && v1[3] == 2); - - assert(v1.insertBack([34, 234]) == 2); - assert(v1.length == 6); - assert(v1.capacity == 6); - assert(v1[4] == 34 && v1[5] == 234); - } - - /** - * Inserts $(D_PARAM el) before or after $(D_PARAM r). - * - * Params: - * R = Type of the inserted value(s) (single value, range or static array). - * r = Range originally obtained from this vector. - * el = Value(s) should be inserted. - * - * Returns: The number of elements inserted. - * - * Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this). - */ - size_t insertAfter(R)(Range!T r, R el) - if (!isInfinite!R - && isInputRange!R - && isImplicitlyConvertible!(ElementType!R, T)) - in - { - assert(r.vector is &this); - assert(r.begin >= this.data); - assert(r.end <= this.data + length); - } - body - { - immutable oldLen = length; - immutable offset = r.end - this.data; - immutable inserted = insertBack(el); - bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]); - return inserted; - } - - /// Ditto. - size_t insertAfter(size_t R)(Range!T r, T[R] el) - in - { - assert(r.vector is &this); - assert(r.begin >= this.data); - assert(r.end <= this.data + length); - } - body - { - return insertAfter!(T[])(r, el[]); - } - - /// Ditto. - size_t insertAfter(R)(Range!T r, auto ref R el) - if (isImplicitlyConvertible!(R, T)) - in - { - assert(r.vector is &this); - assert(r.begin >= this.data); - assert(r.end <= this.data + length); - } - body - { - immutable oldLen = length; - immutable offset = r.end - this.data; - - static if (__traits(isRef, el)) - { - insertBack(el); - } - else - { - moveBack(el); - } - bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]); - - return 1; - } - - /// Ditto. - size_t insertBefore(R)(Range!T r, R el) - if (!isInfinite!R - && isInputRange!R - && isImplicitlyConvertible!(ElementType!R, T)) - in - { - assert(r.vector is &this); - assert(r.begin >= this.data); - assert(r.end <= this.data + length); - } - body - { - return insertAfter(Range!T(this, this.data, r.begin), el); - } - - /// Ditto. - size_t insertBefore(size_t R)(Range!T r, T[R] el) - in - { - assert(r.vector is &this); - assert(r.begin >= this.data); - assert(r.end <= this.data + length); - } - body - { - return insertBefore!(T[])(r, el[]); - } - - /// Ditto. - size_t insertBefore(R)(Range!T r, auto ref R el) - if (isImplicitlyConvertible!(R, T)) - in - { - assert(r.vector is &this); - assert(r.begin >= this.data); - assert(r.end <= this.data + length); - } - body - { - immutable oldLen = length; - immutable offset = r.begin - this.data; - - static if (__traits(isRef, el)) - { - insertBack(el); - } - else - { - moveBack(el); - } - bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]); - - return 1; - } - - /// - unittest - { - Vector!int v1; - v1.insertAfter(v1[], [2, 8]); - assert(v1[0] == 2); - assert(v1[1] == 8); - assert(v1.length == 2); - - v1.insertAfter(v1[], [1, 2]); - assert(v1[0] == 2); - assert(v1[1] == 8); - assert(v1[2] == 1); - assert(v1[3] == 2); - assert(v1.length == 4); - - v1.insertAfter(v1[0 .. 0], [1, 2]); - assert(v1[0] == 1); - assert(v1[1] == 2); - assert(v1[2] == 2); - assert(v1[3] == 8); - assert(v1[4] == 1); - assert(v1[5] == 2); - assert(v1.length == 6); - - v1.insertAfter(v1[0 .. 4], 9); - assert(v1[0] == 1); - assert(v1[1] == 2); - assert(v1[2] == 2); - assert(v1[3] == 8); - assert(v1[4] == 9); - assert(v1[5] == 1); - assert(v1[6] == 2); - assert(v1.length == 7); - } - - /// - unittest - { - Vector!int v1; - v1.insertBefore(v1[], [2, 8]); - assert(v1[0] == 2); - assert(v1[1] == 8); - assert(v1.length == 2); - - v1.insertBefore(v1[], [1, 2]); - assert(v1[0] == 1); - assert(v1[1] == 2); - assert(v1[2] == 2); - assert(v1[3] == 8); - assert(v1.length == 4); - - v1.insertBefore(v1[0 .. 1], [1, 2]); - assert(v1[0] == 1); - assert(v1[1] == 2); - assert(v1[2] == 1); - assert(v1[3] == 2); - assert(v1[4] == 2); - assert(v1[5] == 8); - assert(v1.length == 6); - - v1.insertBefore(v1[2 .. $], 9); - assert(v1[0] == 1); - assert(v1[1] == 2); - assert(v1[2] == 9); - assert(v1[3] == 1); - assert(v1[4] == 2); - assert(v1[5] == 2); - assert(v1[6] == 8); - assert(v1.length == 7); - } - - /** - * Assigns a value to the element with the index $(D_PARAM pos). - * - * Params: - * value = Value. - * pos = Position. - * - * Returns: Assigned value. - * - * Precondition: $(D_INLINECODE length > pos). - */ - ref T opIndexAssign(ref T value, const size_t pos) - { - return opIndex(pos) = value; - } - - @safe unittest - { - Vector!int a = Vector!int(1); - a[0] = 5; - assert(a[0] == 5); - } - - /// Ditto. - T opIndexAssign(T value, const size_t pos) - { - return opIndexAssign(value, pos); - } - - /// Ditto. - Range!T opIndexAssign(T value) - { - return opSliceAssign(value, 0, length); - } - - /// Ditto. - Range!T opIndexAssign(ref T value) - { - return opSliceAssign(value, 0, length); - } - - /** - * Assigns a range or a static array. - * - * Params: - * R = Range type or static array length. - * value = Value. - * - * Returns: Assigned value. - * - * Precondition: $(D_INLINECODE length == value.length). - */ - Range!T opIndexAssign(R)(R value) - if (!isInfinite!R && isInputRange!R - && isImplicitlyConvertible!(ElementType!R, T)) - { - return opSliceAssign!R(value, 0, length); - } - - /// Ditto. - Range!T opIndexAssign(size_t R)(T[R] value) - { - return opSliceAssign!R(value, 0, length); - } - - /// - @nogc unittest - { - auto v1 = Vector!int([12, 1, 7]); - - v1[] = 3; - assert(v1[0] == 3); - assert(v1[1] == 3); - assert(v1[2] == 3); - - v1[] = [7, 1, 12]; - assert(v1[0] == 7); - assert(v1[1] == 1); - assert(v1[2] == 12); - } - - /** - * Params: - * pos = Index. - * - * Returns: The value at a specified index. - * - * Precondition: $(D_INLINECODE length > pos). - */ - ref inout(T) opIndex(const size_t pos) inout @trusted - in - { - assert(length > pos); - } - body - { - return *(this.data + pos); - } - - /** - * Returns: Random access range that iterates over elements of the vector, in - * forward order. - */ - Range!T opIndex() @trusted - { - return typeof(return)(this, this.data, this.data + length); - } - - /// Ditto. - Range!(const T) opIndex() const @trusted - { - return typeof(return)(this, this.data, this.data + length); - } - - /// - unittest - { - const v1 = Vector!int([6, 123, 34, 5]); - - assert(v1[0] == 6); - assert(v1[1] == 123); - assert(v1[2] == 34); - assert(v1[3] == 5); - static assert(is(typeof(v1[0]) == const(int))); - static assert(is(typeof(v1[]))); - } - - /** - * Comparison for equality. - * - * Params: - * that = The vector to compare with. - * - * Returns: $(D_KEYWORD true) if the vectors are equal, $(D_KEYWORD false) - * otherwise. - */ - bool opEquals()(auto ref typeof(this) that) @trusted - { - return equal(this.data[0 .. length], that.data[0 .. that.length]); - } - - /// Ditto. - bool opEquals()(const auto ref typeof(this) that) const @trusted - { - return equal(this.data[0 .. length], that.data[0 .. that.length]); - } - - /// Ditto. - bool opEquals(Range!T that) - { - return equal(opIndex(), that); - } - - /** - * Comparison for equality. - * - * Params: - * R = Right hand side type. - * that = Right hand side vector range. - * - * Returns: $(D_KEYWORD true) if the vector and the range are equal, - * $(D_KEYWORD false) otherwise. - */ - bool opEquals(R)(Range!R that) const - if (is(Unqual!R == T)) - { - return equal(opIndex(), that); - } - - /// - unittest - { - Vector!int v1, v2; - assert(v1 == v2); - - v1.length = 1; - v2.length = 2; - assert(v1 != v2); - - v1.length = 2; - v1[0] = v2[0] = 2; - v1[1] = 3; - v2[1] = 4; - assert(v1 != v2); - - v2[1] = 3; - assert(v1 == v2); - } - - /** - * Returns: The first element. - * - * Precondition: $(D_INLINECODE !empty). - */ - @property ref inout(T) front() inout - in - { - assert(!empty); - } - body - { - return *this.data; - } - - /// - @safe unittest - { - auto v = Vector!int([5]); - - assert(v.front == 5); - - v.length = 2; - v[1] = 15; - assert(v.front == 5); - } - - /** - * Returns: The last element. - * - * Precondition: $(D_INLINECODE !empty). - */ - @property ref inout(T) back() inout @trusted - in - { - assert(!empty); - } - body - { - return *(this.data + length - 1); - } - - /// - unittest - { - auto v = Vector!int([5]); - - assert(v.back == 5); - - v.length = 2; - v[1] = 15; - assert(v.back == 15); - } - - /** - * Params: - * i = Slice start. - * j = Slice end. - * - * Returns: A range that iterates over elements of the container from - * index $(D_PARAM i) up to (excluding) index $(D_PARAM j). - * - * Precondition: $(D_INLINECODE i <= j && j <= length). - */ - Range!T opSlice(const size_t i, const size_t j) @trusted - in - { - assert(i <= j); - assert(j <= length); - } - body - { - return typeof(return)(this, this.data + i, this.data + j); - } - - /// Ditto. - Range!(const T) opSlice(const size_t i, const size_t j) const @trusted - in - { - assert(i <= j); - assert(j <= length); - } - body - { - return typeof(return)(this, this.data + i, this.data + j); - } - - /// - unittest - { - Vector!int v; - auto r = v[]; - assert(r.length == 0); - assert(r.empty); - } - - /// - unittest - { - auto v = Vector!int([1, 2, 3]); - auto r = v[]; - - assert(r.front == 1); - assert(r.back == 3); - - r.popFront(); - assert(r.front == 2); - - r.popBack(); - assert(r.back == 2); - - assert(r.length == 1); - } - - /// - unittest - { - auto v = Vector!int([1, 2, 3, 4]); - auto r = v[1 .. 4]; - assert(r.length == 3); - assert(r[0] == 2); - assert(r[1] == 3); - assert(r[2] == 4); - - r = v[0 .. 0]; - assert(r.length == 0); - - r = v[4 .. 4]; - assert(r.length == 0); - } - - /** - * Slicing assignment. - * - * Params: - * R = Type of the assigned slice or length of the static array should be - * assigned. - * value = New value (single value, input range or static array). - * i = Slice start. - * j = Slice end. - * - * Returns: Slice with the assigned part of the vector. - * - * Precondition: $(D_INLINECODE i <= j && j <= length - * && value.length == j - i) - */ - Range!T opSliceAssign(R)(R value, const size_t i, const size_t j) @trusted - if (!isInfinite!R - && isInputRange!R - && isImplicitlyConvertible!(ElementType!R, T)) - in - { - assert(i <= j); - assert(j <= length); - assert(j - i == walkLength(value)); - } - body - { - copy(value, this.data[i .. j]); - return opSlice(i, j); - } - - /// Ditto. - Range!T opSliceAssign(size_t R)(T[R] value, const size_t i, const size_t j) - { - return opSliceAssign!(T[])(value[], i, j); - } - - /// Ditto. - Range!T opSliceAssign(ref T value, const size_t i, const size_t j) @trusted - in - { - assert(i <= j); - assert(j <= length); - } - body - { - fill(this.data[i .. j], value); - return opSlice(i, j); - } - - /// Ditto. - Range!T opSliceAssign(T value, const size_t i, const size_t j) - { - return opSliceAssign(value, i, j); - } - - /// - @nogc @safe unittest - { - auto v1 = Vector!int([3, 3, 3]); - auto v2 = Vector!int([1, 2]); - - v1[0 .. 2] = 286; - assert(v1[0] == 286); - assert(v1[1] == 286); - assert(v1[2] == 3); - - v2[0 .. $] = v1[1 .. 3]; - assert(v2[0] == 286); - assert(v2[1] == 3); - - v1[0 .. 2] = [5, 8]; - assert(v1[0] == 5); - assert(v1[1] == 8); - assert(v1[2] == 3); - } - - /** - * Returns an array used internally by the vector to store its owned elements. - * The length of the returned array may differ from the size of the allocated - * memory for the vector: the array contains only initialized elements, but - * not the reserved memory. - * - * Returns: The array with elements of this vector. - */ - inout(T[]) get() inout @trusted - { - return this.data[0 .. length]; - } - - /// - unittest - { - auto v = Vector!int([1, 2, 4]); - auto data = v.get(); - - assert(data[0] == 1); - assert(data[1] == 2); - assert(data[2] == 4); - assert(data.length == 3); - - data = v[1 .. 2].get(); - assert(data[0] == 2); - assert(data.length == 1); - } - - /** - * Assigns another vector. - * - * If $(D_PARAM that) is passed by value, it won't be copied, but moved. - * This vector will take the ownership over $(D_PARAM that)'s storage and - * the allocator. - * - * If $(D_PARAM that) is passed by reference, it will be copied. - * - * Params: - * R = Content type. - * that = The value should be assigned. - * - * Returns: $(D_KEYWORD this). - */ - ref typeof(this) opAssign(R)(const ref R that) - if (is(Unqual!R == Vector)) - { - return this = that[]; - } - - /// Ditto. - ref typeof(this) opAssign(R)(R that) @trusted - if (is(R == Vector)) - { - swap(this.data, that.data); - swap(this.length_, that.length_); - swap(this.capacity_, that.capacity_); - swap(this.allocator_, that.allocator_); - return this; - } - - /** - * Assigns a range to the vector. - * - * Params: - * R = Content type. - * that = The value should be assigned. - * - * Returns: $(D_KEYWORD this). - */ - ref typeof(this) opAssign(R)(R that) - if (!isInfinite!R - && isInputRange!R - && isImplicitlyConvertible!(ElementType!R, T)) - { - length = 0; - insertBack(that); - return this; - } - - /// - @safe @nogc unittest - { - auto v1 = const Vector!int([5, 15, 8]); - Vector!int v2; - v2 = v1; - assert(v1 == v2); - } - - /// - @safe @nogc unittest - { - auto v1 = const Vector!int([5, 15, 8]); - Vector!int v2; - v2 = v1[0 .. 2]; - assert(equal(v1[0 .. 2], v2[])); - } - - // Move assignment. - private @safe @nogc unittest - { - Vector!int v1; - v1 = Vector!int([5, 15, 8]); - } - - /** - * Assigns a static array. - * - * Params: - * R = Static array size. - * that = Values to initialize the vector with. - * - * Returns: $(D_KEYWORD this). - */ - ref typeof(this) opAssign(size_t R)(T[R] that) - { - return opAssign!(T[])(that[]); - } - - /// - @safe @nogc unittest - { - auto v1 = Vector!int([5, 15, 8]); - Vector!int v2; - - v2 = [5, 15, 8]; - assert(v1 == v2); - } - - mixin DefaultAllocator; -} - -/// -unittest -{ - auto v = Vector!int([5, 15, 8]); - - assert(v.front == 5); - assert(v[1] == 15); - assert(v.back == 8); - - auto r = v[]; - r[0] = 7; - assert(r.front == 7); - assert(r.front == v.front); -} - -@nogc unittest -{ - const v1 = Vector!int(); - const Vector!int v2; - const v3 = Vector!int([1, 5, 8]); - static assert(is(PointerTarget!(typeof(v3.data)) == const(int))); -} - -@nogc unittest -{ - // Test that const vectors return usable ranges. - auto v = const Vector!int([1, 2, 4]); - auto r1 = v[]; - - assert(r1.back == 4); - r1.popBack(); - assert(r1.back == 2); - r1.popBack(); - assert(r1.back == 1); - r1.popBack(); - assert(r1.length == 0); - - static assert(!is(typeof(r1[0] = 5))); - static assert(!is(typeof(v[0] = 5))); - - const r2 = r1[]; - static assert(is(typeof(r2[]))); -} - -@nogc unittest -{ - Vector!int v1; - const Vector!int v2; - - auto r1 = v1[]; - auto r2 = v1[]; - - assert(r1.length == 0); - assert(r2.empty); - assert(r1 == r2); - - v1.insertBack([1, 2, 4]); - assert(v1[] == v1); - assert(v2[] == v2); - assert(v2[] != v1); - assert(v1[] != v2); - assert(v1[].equal(v1[])); - assert(v2[].equal(v2[])); - assert(!v1[].equal(v2[])); -} - -@nogc unittest -{ - struct MutableEqualsStruct - { - int opEquals(typeof(this) that) @nogc - { - return true; - } - } - struct ConstEqualsStruct - { - int opEquals(const typeof(this) that) const @nogc - { - return true; - } - } - auto v1 = Vector!ConstEqualsStruct(); - auto v2 = Vector!ConstEqualsStruct(); - assert(v1 == v2); - assert(v1[] == v2); - assert(v1 == v2[]); - assert(v1[].equal(v2[])); - - auto v3 = const Vector!ConstEqualsStruct(); - auto v4 = const Vector!ConstEqualsStruct(); - assert(v3 == v4); - assert(v3[] == v4); - assert(v3 == v4[]); - assert(v3[].equal(v4[])); - - auto v7 = Vector!MutableEqualsStruct(1, MutableEqualsStruct()); - auto v8 = Vector!MutableEqualsStruct(1, MutableEqualsStruct()); - assert(v7 == v8); - assert(v7[] == v8); - assert(v7 == v8[]); - assert(v7[].equal(v8[])); -} - -@nogc unittest -{ - struct SWithDtor - { - ~this() @nogc - { - } - } - auto v = Vector!SWithDtor(); // Destructor can destroy empty vectors. -} - -private unittest -{ - class A - { - } - A a1, a2; - auto v1 = Vector!A([a1, a2]); -} - -private @safe @nogc unittest -{ - auto v = Vector!int([5, 15, 8]); - { - size_t i; - - foreach (e; v) - { - assert(i != 0 || e == 5); - assert(i != 1 || e == 15); - assert(i != 2 || e == 8); - ++i; - } - assert(i == 3); - } - { - size_t i = 3; - - foreach_reverse (e; v) - { - --i; - assert(i != 2 || e == 8); - assert(i != 1 || e == 15); - assert(i != 0 || e == 5); - } - assert(i == 0); - } -} +public import tanya.container.slice;