diff options
| author | Eugen Wissner <belka@caraus.de> | 2017-03-09 06:07:23 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2017-03-09 06:07:23 +0100 |
| commit | f4145abfd1f46f07c0da58b029b29c294f394882 (patch) | |
| tree | 7b49107984c0c0ecf3fd8ec87ab37d4478f697ea /source | |
| parent | 093d499729c23ca64d0742092993a8849992cc4b (diff) | |
| download | tanya-f4145abfd1f46f07c0da58b029b29c294f394882.tar.gz | |
Add SList constructors
Diffstat (limited to 'source')
| -rw-r--r-- | source/tanya/container/list.d | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/source/tanya/container/list.d b/source/tanya/container/list.d index e318a27..52706ac 100644 --- a/source/tanya/container/list.d +++ b/source/tanya/container/list.d @@ -191,6 +191,63 @@ struct SList(T) } /** + * Initializes this list 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: + * init = Source list. + * allocator = Allocator. + */ + this(ref SList init, shared Allocator allocator = defaultAllocator) + { + this(init[], allocator); + } + + /// Ditto. + this(SList init, shared Allocator allocator = defaultAllocator) @trusted + { + this(allocator); + if (allocator is init.allocator) + { + head = init.head; + init.head = null; + } + else + { + Entry* next; + for (auto current = init.head; current !is null; current = current.next) + { + if (head is null) + { + head = allocator.make!Entry(move(current.content)); + next = head; + } + else + { + next.next = allocator.make!Entry(move(current.content)); + next = next.next; + } + } + } + } + + /// + @safe @nogc unittest + { + auto l1 = SList!int([5, 1, 234]); + auto l2 = SList!int(l1); + assert(l1 == l2); + } + + /** * Removes all elements from the list. */ ~this() |
