vibe.internal.list 0/67(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
module vibe.internal.list; import core.atomic; struct CircularDList(T) { private { T m_pivot; } this(T pivot) { assert(pivot.prev is null && pivot.next is null); pivot.next = pivot.prev = pivot; m_pivot = pivot; assert(this.empty); } bool empty() const { return m_pivot.next is m_pivot; } @property T front() { return m_pivot.next is m_pivot ? null : m_pivot.next; } @property T back() { return m_pivot.prev is m_pivot ? null : m_pivot.prev; } void remove(T elem) { assert(elem !is m_pivot); elem.prev.next = elem.next; elem.next.prev = elem.prev; elem.prev = elem.next = null; } void insertBefore(T elem, T pivot) { assert(elem.prev is null && elem.next is null); elem.prev = pivot.prev; elem.next = pivot; pivot.prev.next = elem; pivot.prev = elem; } void insertAfter(T elem, T pivot) { assert(elem.prev is null && elem.next is null); elem.prev = pivot; elem.next = pivot.next; pivot.next.prev = elem; pivot.next = elem; } void insertFront(T elem) { insertAfter(elem, m_pivot); } void insertBack(T elem) { insertBefore(elem, m_pivot); } // NOTE: allowed to remove the current element int opApply(int delegate(T) @safe nothrow del) @safe nothrow { T prev = m_pivot; debug size_t counter = 0; while (prev.next !is m_pivot) { auto el = prev.next; if (auto ret = del(el)) return ret; if (prev.next is el) prev = prev.next; debug assert (++counter < 1_000_000, "Cycle in list?"); } return 0; } } unittest { static final class C { C prev, next; int i; this(int i) { this.i = i; } } alias L = CircularDList!C; auto l = L(new C(0)); assert(l.empty); auto c = new C(1); l.insertBack(c); assert(!l.empty); assert(l.front is c); assert(l.back is c); foreach (c; l) assert(c.i == 1); auto c2 = new C(2); l.insertFront(c2); assert(!l.empty); assert(l.front is c2); assert(l.back is c); foreach (c; l) assert(c.i == 1 || c.i == 2); l.remove(c); assert(!l.empty); assert(l.front is c2); assert(l.back is c2); foreach (c; l) assert(c.i == 2); l.remove(c2); assert(l.empty); } struct StackSList(T) { @safe nothrow: import core.atomic : cas; private T m_first; @property T first() { return m_first; } @property T front() { return m_first; } bool empty() const { return m_first is null; } void add(T elem) { debug iterate((el) { assert(el !is elem, "Double-insertion of list element."); return true; }); elem.next = m_first; m_first = elem; } bool remove(T elem) { debug uint counter = 0; T w = m_first, wp; while (w !is elem) { if (!w) return false; wp = w; w = w.next; debug assert(++counter < 1_000_000, "Cycle in linked list?"); } if (wp) wp.next = w.next; else m_first = w.next; return true; } void filter(scope bool delegate(T el) @safe nothrow del) { debug uint counter = 0; T w = m_first, pw; while (w !is null) { auto wnext = w.next; if (!del(w)) { if (pw) pw.next = wnext; else m_first = wnext; } else pw = w; w = wnext; debug assert(++counter < 1_000_000, "Cycle in linked list?"); } } void iterate(scope bool delegate(T el) @safe nothrow del) { debug uint counter = 0; T w = m_first; while (w !is null) { auto wnext = w.next; if (!del(w)) break; w = wnext; debug assert(++counter < 1_000_000, "Cycle in linked list?"); } } } unittest { static final class C { C next; int i; this(int i) { this.i = i; } } alias L = StackSList!C; L l; assert(l.empty); auto c = new C(1); l.add(c); assert(!l.empty); assert(l.front is c); l.iterate((el) { assert(el.i == 1); return true; }); auto c2 = new C(2); l.add(c2); assert(!l.empty); assert(l.front is c2); l.iterate((el) { assert(el.i == 1 || el.i == 2); return true; }); l.remove(c); assert(!l.empty); assert(l.front is c2); l.iterate((el) { assert(el.i == 2); return true; }); l.filter((el) => el.i == 0); assert(l.empty); }