1 module carbon.utils; 2 3 import std.algorithm; 4 import std.container; 5 import std.traits; 6 import std.stdio; 7 8 9 string toLiteral(string str) 10 { 11 import std..string : format; 12 13 return format("%s", [str])[1 .. $-1]; 14 } 15 16 unittest 17 { 18 scope(failure) {writefln("Unittest failure :%s(%s)", __FILE__, __LINE__); stdout.flush();} 19 scope(success) {writefln("Unittest success :%s(%s)", __FILE__, __LINE__); stdout.flush();} 20 21 assert("123".toLiteral == `"123"`); 22 assert("12\"3".toLiteral == `"12\"3"`); 23 } 24 25 26 struct Cache(AA) 27 if(isAssociativeArray!AA) 28 { 29 alias KeyType = typeof(AA.init.keys[0]); 30 alias ValueType = typeof(AA.init.values[0]); 31 32 ValueType opCall(KeyType key, lazy ValueType value) 33 { 34 if(auto p = key in _aa) 35 return *p; 36 else{ 37 ValueType v = value(); 38 _aa[key] = v; 39 return v; 40 } 41 } 42 43 private: 44 AA _aa; 45 } 46 47 unittest 48 { 49 scope(failure) {writefln("Unittest failure :%s(%s)", __FILE__, __LINE__); stdout.flush();} 50 scope(success) {writefln("Unittest success :%s(%s)", __FILE__, __LINE__); stdout.flush();} 51 52 Cache!(int[string]) c; 53 assert(c("foo", 1) == 1); 54 assert(c("bar", 2) == 2); 55 assert(c("foo", 3) == 1); 56 assert(c("bar", 4) == 2); 57 } 58 59 60 auto maybeModified(K, V)(V[K] aa) 61 { 62 static struct Result() 63 { 64 this(V[K] aa) 65 { 66 _keys = typeof(_keys)(aa.byKey); 67 68 _aa = aa; 69 } 70 71 72 int opApply(int delegate(K, ref V) dg) 73 { 74 foreach(k; _keys) 75 if(k in _aa) 76 if(auto res = dg(k, _aa[k])) 77 return res; 78 79 return 0; 80 } 81 82 private: 83 Array!K _keys; 84 V[K] _aa; 85 } 86 87 return Result!()(aa); 88 } 89 90 unittest 91 { 92 scope(failure) {writefln("Unittest failure :%s(%s)", __FILE__, __LINE__); stdout.flush();} 93 scope(success) {writefln("Unittest success :%s(%s)", __FILE__, __LINE__); stdout.flush();} 94 95 auto aa = ["a": 1, "b": 2, "c": 3]; 96 97 foreach(k, ref v; aa.maybeModified){ 98 aa[k ~ k] = v; 99 } 100 101 assert(aa.length == 6); 102 } 103 104 105 auto maybeModified(E)(E[] arr) 106 { 107 static struct Result() 108 { 109 this(E[] arr) 110 { 111 _dup = Array!E(arr); 112 } 113 114 115 int opApply(int delegate(E) dg) 116 { 117 foreach(e; _dup) 118 if(auto res = dg(e)) 119 return res; 120 121 return 0; 122 } 123 124 private: 125 Array!E _dup; 126 } 127 128 129 return Result!()(arr); 130 } 131 132 unittest 133 { 134 scope(failure) {writefln("Unittest failure :%s(%s)", __FILE__, __LINE__); stdout.flush();} 135 scope(success) {writefln("Unittest success :%s(%s)", __FILE__, __LINE__); stdout.flush();} 136 137 auto arr = [1, 2, 3]; 138 139 foreach(v; arr.maybeModified){ 140 arr ~= v; 141 } 142 143 assert(arr.length == 6); 144 assert(arr == [1, 2, 3, 1, 2, 3]); 145 } 146 147 148 /* 149 struct ShiftRegistor(E, bool isRingBuffer = true, bool containPointer = false) 150 { 151 152 153 154 private: 155 static if(containPointer) 156 alias C = E*; 157 else 158 alias C = E; 159 160 C[] _buffer; 161 } 162 163 164 struct ShiftRegistor(E, size_t N, bool isRingBuffer = true, bool containPointer = false) 165 { 166 167 } 168 169 170 struct ParallelShiftRegistor(E, size_t P, bool isRingBuffer = true, bool containPointer = false) 171 { 172 173 } 174 175 176 struct ParallelShiftRegistor(E, size_t P, bool isRingBuffer = true, bool containPointer = false) 177 */