Vector: Reuse available methods

This commit is contained in:
Eugen Wissner 2017-01-15 08:38:19 +01:00
parent 48205b2fc9
commit 4ea9c2b740
5 changed files with 18 additions and 34 deletions

View File

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * 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/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)

View File

@ -5,7 +5,7 @@
/* /*
* Internal package used by containers that rely on entries/nodes. * 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/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)

View File

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * 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/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)

View File

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * 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/, * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0). * Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)

View File

@ -402,18 +402,10 @@ struct Vector(T)
* len = Initial length of the vector. * len = Initial length of the vector.
* allocator = Allocator. * allocator = Allocator.
*/ */
this(size_t len, shared Allocator allocator = defaultAllocator) @trusted this(size_t len, shared Allocator allocator = defaultAllocator)
{ {
this(allocator); this(allocator);
length = len;
vector = cast(T*) allocator.allocate(len * T.sizeof);
if (len == 0)
{
return;
}
reserve(len);
initializeAll(vector[0 .. len]);
length_ = len;
} }
/** /**
@ -477,19 +469,10 @@ struct Vector(T)
/** /**
* Destroys this $(D_PSYMBOL Vector). * Destroys this $(D_PSYMBOL Vector).
*/ */
~this() @trusted ~this()
{ {
static if (hasElaborateDestructor!T) length = 0;
{ shrink(0);
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;
} }
/** /**
@ -695,7 +678,7 @@ struct Vector(T)
*/ */
@property bool empty() const @property bool empty() const
{ {
return length_ == 0; return length == 0;
} }
/** /**
@ -812,16 +795,17 @@ struct Vector(T)
&& isInputRange!R && isInputRange!R
&& isImplicitlyConvertible!(ElementType!R, T)) && isImplicitlyConvertible!(ElementType!R, T))
{ {
immutable rLen = walkLength(el); static if (hasLength!R)
{
reserve(length_ + rLen); reserve(length_ + el.length);
T* pos = vector + length_; }
size_t retLength;
foreach (e; el) foreach (e; el)
{ {
emplace(pos, e); insertBack(e);
++length_, ++pos; ++retLength;
} }
return rLen; return retLength;
} }
/// Ditto. /// Ditto.