diff options
| author | Eugen Wissner <belka@caraus.de> | 2018-04-12 19:35:59 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2018-04-14 16:15:35 +0200 |
| commit | d267a9cc645f1219203bd43e046dc07b4a1fc520 (patch) | |
| tree | b877a1353d05b0b8a1375f75c4334890e1d53f37 /source | |
| parent | ddb02e41ebc9470c183ccb0d4a256a4eb2f14968 (diff) | |
| download | tanya-d267a9cc645f1219203bd43e046dc07b4a1fc520.tar.gz | |
Implement SList.popFirstOf
Fix #36.
Slicing for the SList on top of the existing SRange would be inefficent.
There would be two cases:
- Range iterates till the end of the list.
- Range iterates till some element "end".
If both cases are implemented in the same range, this range should check
for both conditions (end of the list and "begin == end") instead of only
one (end of the list).
Introducing a different range is undesirable since all containers have
currently only one range.
Diffstat (limited to 'source')
| -rw-r--r-- | source/tanya/container/list.d | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/source/tanya/container/list.d b/source/tanya/container/list.d index cb3115f..9a5beb7 100644 --- a/source/tanya/container/list.d +++ b/source/tanya/container/list.d @@ -720,6 +720,50 @@ struct SList(T) } /** + * Removes the fron element of the $(D_PARAM range) from the list. + * + * Params: + * range = Range whose front element should be removed. + * + * Returns: $(D_PSYMBOL range) with the first element removed. + * + * Precondition: $(D_INLINECODE !range.empty). + * $(D_PARAM range) is extracted from this list. + */ + Range popFirstOf(Range range) + in + { + assert(!range.empty); + assert(checkRangeBelonging(range)); + } + do + { + auto next = (*range.head).next; + + allocator.dispose(*range.head); + *range.head = next; + + return range; + } + + /// + @nogc nothrow pure @safe unittest + { + auto list = SList!int([5, 234, 30]); + auto range = list[]; + + range.popFront(); + assert(list.popFirstOf(range).front == 30); + + range = list[]; + assert(range.front == 5); + range.popFront; + assert(range.front == 30); + range.popFront; + assert(range.empty); + } + + /** * Returns: Range that iterates over all elements of the container, in * forward order. */ |
