eventcore.internal.utils 20/101(19%) 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
254
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
822
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
10116
10216
10318
10416
10516
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
13816
1392
1401
1411
1421
1431
1441
1450
1460
1470
14817
1491
1501
1511
1520
1530
1541
1551
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
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
module eventcore.internal.utils; import taggedalgebraic; void print(ARGS...)(string str, ARGS args) @trusted @nogc nothrow { import std.format : formattedWrite; StdoutRange r; scope cb = () { scope (failure) assert(false); (&r).formattedWrite(str, args); }; (cast(void delegate() @nogc @safe nothrow)cb)(); r.put('\n'); } private extern(C) Throwable.TraceInfo _d_traceContext(void* ptr = null); void nogc_assert(bool cond, string message, string file = __FILE__, int line = __LINE__) @trusted nothrow { import core.stdc.stdlib : abort; import std.stdio : stderr; if (!cond) { scope (exit) { abort(); assert(false); } stderr.writefln("Assertion failure @%s(%s): %s", file, line, message); stderr.writeln("------------------------"); if (auto info = _d_traceContext(null)) { foreach (s; info) stderr.writeln(s); } else stderr.writeln("no stack trace available"); } } struct StdoutRange { @safe: @nogc: nothrow: import core.stdc.stdio; void put(string str) { () @trusted { fwrite(str.ptr, str.length, 1, stdout); } (); } void put(char ch) { () @trusted { fputc(ch, stdout); } (); } } struct ChoppedVector(T, size_t CHUNK_SIZE = 16*64*1024/nextPOT(T.sizeof)) { import core.memory : GC; static assert(nextPOT(CHUNK_SIZE) == CHUNK_SIZE, "CHUNK_SIZE must be a power of two for performance reasons."); @safe: nothrow: import core.stdc.stdlib : calloc, free, malloc, realloc; import std.traits : hasIndirections; alias chunkSize = CHUNK_SIZE; private { alias Chunk = T[chunkSize]; alias ChunkPtr = Chunk*; ChunkPtr[] m_chunks; size_t m_chunkCount; size_t m_length; } @disable this(this); ~this() @nogc { clear(); } @property size_t length() const @nogc { return m_length; } void clear() @nogc { () @trusted { foreach (i; 0 .. m_chunkCount) { destroy(m_chunks[i]); static if (hasIndirections!T) GC.removeRange(m_chunks[i]); free(m_chunks[i]); } free(m_chunks.ptr); } (); m_chunkCount = 0; m_length = 0; } ref T opIndex(size_t index) @nogc { auto chunk = index / chunkSize; auto subidx = index % chunkSize; if (index >= m_length) m_length = index+1; reserveChunk(chunk); return (*m_chunks[chunk])[subidx]; } int opApply(scope int delegate(size_t idx, ref T) @safe nothrow del) { size_t idx = 0; foreach (c; m_chunks) { if (c) { foreach (i, ref t; *c) if (auto ret = del(idx+i, t)) return ret; } idx += chunkSize; } return 0; } int opApply(scope int delegate(size_t idx, ref const(T)) @safe nothrow del) const { size_t idx = 0; foreach (c; m_chunks) { if (c) { foreach (i, ref t; *c) if (auto ret = del(idx+i, t)) return ret; } idx += chunkSize; } return 0; } private void reserveChunk(size_t chunkidx) @nogc { if (m_chunks.length <= chunkidx) { auto l = m_chunks.length == 0 ? 64 : m_chunks.length; while (l <= chunkidx) l *= 2; () @trusted { auto newptr = cast(ChunkPtr*)realloc(m_chunks.ptr, l * ChunkPtr.length); assert(newptr !is null, "Failed to allocate chunk index!"); m_chunks = newptr[0 .. l]; } (); } while (m_chunkCount <= chunkidx) { () @trusted { auto ptr = cast(ChunkPtr)calloc(chunkSize, T.sizeof); assert(ptr !is null, "Failed to allocate chunk!"); // FIXME: initialize with T.init instead of 0 static if (hasIndirections!T) GC.addRange(ptr, chunkSize * T.sizeof); m_chunks[m_chunkCount++] = ptr; } (); } } } struct AlgebraicChoppedVector(TCommon, TSpecific...) { import std.conv : to; import std.meta : AliasSeq; union U { typeof(null) none; mixin fields!0; } alias FieldType = TaggedAlgebraic!U; static struct FullField { TCommon common; FieldType specific; mixin(accessors()); } ChoppedVector!(FullField) items; alias items this; private static string accessors() { import std.format : format; string ret; foreach (i, U; TSpecific) ret ~= "@property ref TSpecific[%s] %s() nothrow @safe { return this.specific.get!(TSpecific[%s]); }\n" .format(i, U.Handle.name, i); return ret; } private mixin template fields(size_t i) { static if (i < TSpecific.length) { mixin("TSpecific["~i.to!string~"] "~TSpecific[i].Handle.name~";"); mixin fields!(i+1); } } } /** Efficient bit set of dynamic size. */ struct SmallIntegerSet(V : size_t) { private { uint[][4] m_bits; size_t m_count; } @property bool empty() const { return m_count == 0; } void insert(V i) { foreach (j; 0 .. m_bits.length) { uint b = 1u << (i%32); i /= 32; if (i >= m_bits[j].length) m_bits[j].length = nextPOT(i+1); if (j == 0 && !(m_bits[j][i] & b)) m_count++; m_bits[j][i] |= b; } } void remove(V i) { foreach (j; 0 .. m_bits.length) { uint b = 1u << (i%32); i /= 32; if (!m_bits[j][i]) break; if (j == 0 && m_bits[j][i] & b) m_count--; m_bits[j][i] &= ~b; if (m_bits[j][i]) break; } } bool contains(V i) const { return i/32 < m_bits[0].length && m_bits[0][i/32] & (1u<<(i%32)); } int opApply(scope int delegate(V) @safe nothrow del) const @safe { int rec(size_t depth, uint bi) { auto b = m_bits[depth][bi]; foreach (i; 0 .. 32) if (b & (1u << i)) { uint sbi = bi*32 + i; if (depth == 0) { if (auto ret = del(V(sbi))) return ret; } else rec(depth-1, sbi); } return 0; } foreach (i, b; m_bits[$-1]) if (b) { if (auto ret = rec(m_bits.length-1, cast(uint)i)) return ret; } return 0; } } unittest { uint[] ints = [0, 16, 31, 128, 4096, 65536]; SmallIntegerSet!uint set; bool[uint] controlset; assert(set.empty); foreach (i; ints) { set.insert(i); controlset[i] = true; } assert(!set.empty); foreach (jidx, j; ints) { size_t cnt = 0; bool[int] seen; foreach (i; set) { assert(i in controlset); assert(i !in seen); seen[i] = true; cnt++; } assert(cnt == ints.length - jidx); set.remove(j); controlset.remove(j); } assert(set.empty); foreach (i; set) assert(false); } @safe nothrow unittest { SmallIntegerSet!uint s; void testIter(scope uint[] seq...) nothrow { size_t cnt = 0; foreach (v; s) { assert(v == seq[cnt]); cnt++; } assert(cnt == seq.length); } testIter(); s.insert(1); assert(s.contains(1)); assert(!s.contains(2)); testIter(1); s.insert(3467); assert(s.contains(3467)); assert(!s.contains(300)); testIter(1, 3467); s.insert(2); testIter(1, 2, 3467); s.remove(1); testIter(2, 3467); s.remove(2); testIter(3467); s.remove(3467); testIter(); } private size_t nextPOT(size_t n) @safe nothrow @nogc { foreach_reverse (i; 0 .. size_t.sizeof*8) { size_t ni = cast(size_t)1 << i; if (n & ni) { return n & (ni-1) ? ni << 1 : ni; } } return 1; } unittest { assert(nextPOT(1) == 1); assert(nextPOT(2) == 2); assert(nextPOT(3) == 4); assert(nextPOT(4) == 4); assert(nextPOT(5) == 8); }