| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.container.dlist; |
| 4 |
|
| 5 |
import std.algorithm.comparison : equal; |
| 6 |
import std.container : DList; |
| 7 |
|
| 8 |
auto s = DList!int(1, 2, 3); |
| 9 |
assert(equal(s[], [1, 2, 3])); |
| 10 |
|
| 11 |
s.removeFront(); |
| 12 |
assert(equal(s[], [2, 3])); |
| 13 |
s.removeBack(); |
| 14 |
assert(equal(s[], [2])); |
| 15 |
|
| 16 |
s.insertFront([4, 5]); |
| 17 |
assert(equal(s[], [4, 5, 2])); |
| 18 |
s.insertBack([6, 7]); |
| 19 |
assert(equal(s[], [4, 5, 2, 6, 7])); |
| 20 |
|
| 21 |
// If you want to apply range operations, simply slice it. |
| 22 |
import std.algorithm.searching : countUntil; |
| 23 |
import std.range : popFrontN, popBackN, walkLength; |
| 24 |
|
| 25 |
auto sl = DList!int([1, 2, 3, 4, 5]); |
| 26 |
assert(countUntil(sl[], 2) == 1); |
| 27 |
|
| 28 |
auto r = sl[]; |
| 29 |
popFrontN(r, 2); |
| 30 |
popBackN(r, 2); |
| 31 |
assert(r.equal([3])); |
| 32 |
assert(walkLength(r) == 1); |
| 33 |
|
| 34 |
// DList.Range can be used to remove elements from the list it spans |
| 35 |
auto nl = DList!int([1, 2, 3, 4, 5]); |
| 36 |
for (auto rn = nl[]; !rn.empty;) |
| 37 |
if (rn.front % 2 == 0) |
| 38 |
nl.popFirstOf(rn); |
| 39 |
else |
| 40 |
rn.popFront(); |
| 41 |
assert(equal(nl[], [1, 3, 5])); |
| 42 |
auto rs = nl[]; |
| 43 |
rs.popFront(); |
| 44 |
nl.remove(rs); |
| 45 |
assert(equal(nl[], [1])); |
| 46 |
} |
| 47 |
|