From 4ea9c2b740fce773b8f7ba821700a5a1bdc53f2b Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 15 Jan 2017 08:38:19 +0100 Subject: [PATCH] Vector: Reuse available methods --- source/tanya/container/buffer.d | 2 +- source/tanya/container/entry.d | 2 +- source/tanya/container/list.d | 2 +- source/tanya/container/package.d | 2 +- source/tanya/container/vector.d | 44 ++++++++++---------------------- 5 files changed, 18 insertions(+), 34 deletions(-) diff --git a/source/tanya/container/buffer.d b/source/tanya/container/buffer.d index 5a9ad21..4d01ad5 100644 --- a/source/tanya/container/buffer.d +++ b/source/tanya/container/buffer.d @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** - * Copyright: Eugene Wissner 2016. + * 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) diff --git a/source/tanya/container/entry.d b/source/tanya/container/entry.d index 0515c54..54cedc9 100644 --- a/source/tanya/container/entry.d +++ b/source/tanya/container/entry.d @@ -5,7 +5,7 @@ /* * Internal package used by containers that rely on entries/nodes. * - * Copyright: Eugene Wissner 2016. + * 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) diff --git a/source/tanya/container/list.d b/source/tanya/container/list.d index d0691ee..0c916c1 100644 --- a/source/tanya/container/list.d +++ b/source/tanya/container/list.d @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** - * Copyright: Eugene Wissner 2016. + * 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) diff --git a/source/tanya/container/package.d b/source/tanya/container/package.d index eba95a7..db8f025 100644 --- a/source/tanya/container/package.d +++ b/source/tanya/container/package.d @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** - * Copyright: Eugene Wissner 2016. + * 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) diff --git a/source/tanya/container/vector.d b/source/tanya/container/vector.d index 2467325..c5d6d3e 100644 --- a/source/tanya/container/vector.d +++ b/source/tanya/container/vector.d @@ -402,18 +402,10 @@ struct Vector(T) * len = Initial length of the vector. * allocator = Allocator. */ - this(size_t len, shared Allocator allocator = defaultAllocator) @trusted + this(size_t len, shared Allocator allocator = defaultAllocator) { this(allocator); - - vector = cast(T*) allocator.allocate(len * T.sizeof); - if (len == 0) - { - return; - } - reserve(len); - initializeAll(vector[0 .. len]); - length_ = len; + length = len; } /** @@ -477,19 +469,10 @@ struct Vector(T) /** * Destroys this $(D_PSYMBOL Vector). */ - ~this() @trusted + ~this() { - static if (hasElaborateDestructor!T) - { - const T* end = vector + length_; - for (T* e = vector; e != end; ++e) - { - destroy(*e); - } - } - allocator.deallocate(cast(void[]) vector[0 .. capacity_]); - vector = null; - capacity_ = length_ = 0; + length = 0; + shrink(0); } /** @@ -695,7 +678,7 @@ struct Vector(T) */ @property bool empty() const { - return length_ == 0; + return length == 0; } /** @@ -812,16 +795,17 @@ struct Vector(T) && isInputRange!R && isImplicitlyConvertible!(ElementType!R, T)) { - immutable rLen = walkLength(el); - - reserve(length_ + rLen); - T* pos = vector + length_; + static if (hasLength!R) + { + reserve(length_ + el.length); + } + size_t retLength; foreach (e; el) { - emplace(pos, e); - ++length_, ++pos; + insertBack(e); + ++retLength; } - return rLen; + return retLength; } /// Ditto.