SList.insertFront for ranges

This commit is contained in:
Eugen Wissner 2017-03-01 19:23:54 +01:00
parent 3c23aca6a6
commit c6a99b114e
2 changed files with 1885 additions and 1845 deletions

View File

@ -3,6 +3,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Linked list.
*
* Copyright: Eugene Wissner 2016-2017.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
@ -11,7 +13,9 @@
module tanya.container.list;
import std.algorithm.comparison;
import std.algorithm.mutation;
import std.algorithm.searching;
import std.range.primitives;
import std.traits;
import tanya.container.entry;
import tanya.memory;
@ -134,35 +138,71 @@ struct SList(T)
* Inserts a new element at the beginning.
*
* Params:
* R = Type of the inserted value(s).
* x = New element.
*
* Returns: The number of elements inserted.
*/
void insertFront(ref T x)
size_t insertFront(R)(ref R x) @trusted
if (isImplicitlyConvertible!(R, T))
{
auto temp = allocator.make!Entry;
temp.content = x;
temp.next = head;
head = temp;
head = allocator.make!Entry(x, head);
return 1;
}
/// Ditto.
void insertFront(T x)
size_t insertFront(R)(R x) @trusted
if (isImplicitlyConvertible!(R, T))
{
insertFront(x);
auto temp = cast(Entry*) allocator.allocate(Entry.sizeof);
x.moveEmplace(temp.content);
temp.next = head;
head = temp;
return 1;
}
/// Ditto.
size_t insertFront(R)(R el)
if (!isInfinite!R
&& isInputRange!R
&& isImplicitlyConvertible!(ElementType!R, T))
{
size_t retLength;
foreach (e; el)
{
retLength += insertFront(e);
}
return retLength;
}
/// Ditto.
size_t insertFront(size_t R)(T[R] el)
{
return insertFront!(T[])(el[]);
}
/// Ditto.
alias insert = insertFront;
///
unittest
@nogc @safe unittest
{
SList!int l;
SList!int l1;
l.insertFront(8);
assert(l.front == 8);
l.insertFront(9);
assert(l.front == 9);
assert(l1.insertFront(8) == 1);
assert(l1.front == 8);
assert(l1.insertFront(9) == 1);
assert(l1.front == 9);
SList!int l2;
assert(l2.insertFront([25, 30, 15]) == 3);
assert(l2.front == 15);
l2.insertFront(l1[]);
assert(l2.length == 5);
assert(l2.front == 8);
}
/**

View File

@ -190,7 +190,7 @@ struct Vector(T)
/**
* Initializes this vector from another one.
*
* If $(D_PARAM init) is passed by value, it won't be copied, but moved
* 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