1 /** 2 Utility functions for circular array processing 3 Copyright: © 2012 RejectedSoftware e.K., © 2014-2015 Etienne Cimon 4 License: Subject to the terms of the MIT license, as written in the included LICENSE file. 5 Authors: Sönke Ludwig, Etienne Cimon 6 */ 7 module memutils.circularbuffer; 8 9 import memutils.allocators; 10 import memutils.constants; 11 import std.algorithm; 12 import std.traits : hasElaborateDestructor, isBasicType, isPointer; 13 import memutils.utils; 14 15 struct CircularBuffer(T, size_t N = 0, ALLOC = ThreadMem) { 16 @disable this(this); 17 18 private { 19 static if( N > 0 ) T[N] m_buffer; 20 else T[] m_buffer; 21 size_t m_start = 0; 22 size_t m_fill = 0; 23 } 24 static if( N == 0 ){ 25 this(size_t capacity) { m_buffer = allocArray!(T, ALLOC)(capacity); } 26 ~this() { if (m_buffer) freeArray!(T, ALLOC)(m_buffer, m_fill, m_start); } 27 } 28 else { 29 // clear ring buffer static fields upon removal (to run struct destructors, if T is a struct) 30 ~this() 31 { 32 // TODO: Test this 33 // destroy(m_buffer[m_start .. m_fill]); 34 } 35 } 36 @property bool empty() const { return m_fill == 0; } 37 @property bool full() const { return m_fill == m_buffer.length; } 38 @property size_t length() const { return m_fill; } 39 @property size_t freeSpace() const { return m_buffer.length - m_fill; } 40 @property size_t capacity() const { return m_buffer.length; } 41 static if( N == 0 ){ 42 @property void capacity(size_t new_size) 43 { 44 if (new_size <= length || new_size == capacity) return; 45 if( m_buffer.length ){ 46 auto temp = allocArray!(T, ALLOC)(new_size); 47 size_t tmp_fill = m_fill; 48 read(temp[0 .. m_fill]); 49 m_start = 0; 50 m_fill = tmp_fill; 51 freeArray!(T, ALLOC)(m_buffer, m_fill, m_start); 52 m_buffer = temp; 53 } else m_buffer = allocArray!(T, ALLOC)(new_size); 54 } 55 } 56 @property ref inout(T) front() inout { assert(!empty); return m_buffer[m_start]; } 57 @property ref inout(T) back() inout { assert(!empty); return m_buffer[mod(m_start+m_fill-1)]; } 58 void clear() 59 { 60 popFrontN(length); 61 assert(m_fill == 0); 62 m_start = 0; 63 } 64 void put()(T itm) { assert(m_fill < m_buffer.length); m_buffer[mod(m_start + m_fill++)] = itm; } 65 void forcePut()(T itm) { if (m_fill >= m_buffer.length) popBack(); m_buffer[mod(m_start + m_fill++)] = itm; } 66 void put(TC : T)(TC[] itms) 67 { 68 if( !itms.length ) return; 69 static if( N == 0 ) { 70 if (m_fill+itms.length > m_buffer.length) 71 capacity = capacity*2; 72 } else assert(m_fill+itms.length <= m_buffer.length, "Cannot write to buffer, it is full."); 73 if( mod(m_start+m_fill) >= mod(m_start+m_fill+itms.length) ){ 74 size_t chunk1 = m_buffer.length - (m_start+m_fill); 75 size_t chunk2 = itms.length - chunk1; 76 m_buffer[m_start+m_fill .. m_buffer.length] = itms[0 .. chunk1]; 77 m_buffer[0 .. chunk2] = itms[chunk1 .. $]; 78 } else { 79 m_buffer[mod(m_start+m_fill) .. mod(m_start+m_fill)+itms.length] = itms[]; 80 } 81 m_fill += itms.length; 82 } 83 void putN(size_t n) { assert(m_fill+n <= m_buffer.length); m_fill += n; } 84 void popFront() { assert(!empty); m_start = mod(m_start+1); m_fill--; } 85 void popFrontN(size_t n) { 86 import core.stdc..string : memset; 87 assert(length >= n); 88 m_start = mod(m_start + n); 89 m_fill -= n; 90 } 91 void popBack() { assert(!empty); m_fill--; } 92 void popBackN(size_t n) { assert(length >= n); m_fill -= n; } 93 94 // moves all the values from the buffer one step down at start of the reference range 95 void removeAt(Range r) 96 { 97 assert(r.m_buffer is m_buffer); 98 if( m_start + m_fill > m_buffer.length ){ 99 assert(r.m_start >= m_start && r.m_start < m_buffer.length || r.m_start < mod(m_start+m_fill)); 100 if( r.m_start > m_start ){ 101 foreach(i; r.m_start .. m_buffer.length-1) 102 m_buffer[i] = m_buffer[i+1]; 103 m_buffer[$-1] = m_buffer[0]; 104 foreach(i; 0 .. mod(m_start + m_fill - 1)) 105 m_buffer[i] = m_buffer[i+1]; 106 } else { 107 foreach(i; r.m_start .. mod(m_start + m_fill - 1)) 108 m_buffer[i] = m_buffer[i+1]; 109 } 110 } else { 111 assert(r.m_start >= m_start && r.m_start < m_start+m_fill); 112 foreach(i; r.m_start .. m_start+m_fill-1) 113 m_buffer[i] = m_buffer[i+1]; 114 } 115 m_fill--; 116 static if (hasElaborateDestructor!T) { // calls destructors 117 static if (is(T == struct) && isPointer!T) .destroy(*m_buffer[mod(m_start+m_fill)]); 118 else .destroy(m_buffer[mod(m_start+m_fill)]); 119 } 120 } 121 inout(T)[] peek() inout { return m_buffer[m_start .. min(m_start+m_fill, m_buffer.length)]; } 122 T[] peekDst() { 123 if( m_start + m_fill < m_buffer.length ) return m_buffer[m_start+m_fill .. $]; 124 else return m_buffer[mod(m_start+m_fill) .. m_start]; 125 } 126 void read(T[] dst) 127 { 128 import core.stdc..string : memset; 129 assert(dst.length <= length); 130 if( !dst.length ) return; 131 if( mod(m_start) >= mod(m_start+dst.length) ){ 132 size_t chunk1 = m_buffer.length - m_start; 133 size_t chunk2 = dst.length - chunk1; 134 dst[0 .. chunk1] = m_buffer[m_start .. $]; 135 dst[chunk1 .. $] = m_buffer[0 .. chunk2]; 136 //static if (is(ALLOC == SecureMem)) 137 //{ 138 // memset(m_buffer.ptr + m_start, 0, chunk1); 139 // memset(m_buffer.ptr, 0, chunk2); 140 //} 141 } else { 142 dst[] = m_buffer[m_start .. m_start+dst.length]; 143 //static if (is(ALLOC == SecureMem)) 144 // memset(m_buffer.ptr + m_start, 0, dst.length); 145 } 146 popFrontN(dst.length); 147 } 148 int opApply(scope int delegate(ref T itm) del) 149 { 150 if( m_start+m_fill > m_buffer.length ){ 151 foreach(i; m_start .. m_buffer.length) 152 if( auto ret = del(m_buffer[i]) ) 153 return ret; 154 foreach(i; 0 .. mod(m_start+m_fill)) 155 if( auto ret = del(m_buffer[i]) ) 156 return ret; 157 } else { 158 foreach(i; m_start .. m_start+m_fill) 159 if( auto ret = del(m_buffer[i]) ) 160 return ret; 161 } 162 return 0; 163 } 164 ref inout(T) opIndex(size_t idx) inout { assert(idx < length); return m_buffer[mod(m_start+idx)]; } 165 Range opSlice() { return Range(m_buffer, m_start, m_fill); } 166 Range opSlice(size_t from, size_t to) 167 { 168 assert(from <= to); 169 assert(to <= m_fill); 170 return Range(m_buffer, mod(m_start+from), to-from); 171 } 172 size_t opDollar(size_t dim)() const if(dim == 0) { return length; } 173 private size_t mod(size_t n) 174 const { 175 static if( N == 0 ){ 176 /*static if(PotOnly){ 177 return x & (m_buffer.length-1); 178 } else {*/ 179 return n % m_buffer.length; 180 //} 181 } else static if( ((N - 1) & N) == 0 ){ 182 return n & (N - 1); 183 } else return n % N; 184 } 185 static struct Range { 186 private { 187 T[] m_buffer; 188 size_t m_start; 189 size_t m_length; 190 } 191 private this(T[] buffer, size_t start, size_t length) 192 { 193 m_buffer = buffer; 194 m_start = start; 195 m_length = length; 196 } 197 @property bool empty() const { return m_length == 0; } 198 @property inout(T) front() inout { assert(!empty); return m_buffer[m_start]; } 199 void popFront() 200 { 201 assert(!empty); 202 m_start++; 203 m_length--; 204 if( m_start >= m_buffer.length ) 205 m_start = 0; 206 } 207 } 208 } 209 210 unittest { 211 import std.range : isInputRange, isOutputRange; 212 static assert(isInputRange!(CircularBuffer!int) && isOutputRange!(CircularBuffer!int, int)); 213 214 // test static buffer 215 CircularBuffer!(int, 5) buf; 216 assert(buf.length == 0 && buf.freeSpace == 5); buf.put(1); // |1 . . . . 217 assert(buf.length == 1 && buf.freeSpace == 4); buf.put(2); // |1 2 . . . 218 assert(buf.length == 2 && buf.freeSpace == 3); buf.put(3); // |1 2 3 . . 219 assert(buf.length == 3 && buf.freeSpace == 2); buf.put(4); // |1 2 3 4 . 220 assert(buf.length == 4 && buf.freeSpace == 1); buf.put(5); // |1 2 3 4 5 221 assert(buf.length == 5 && buf.freeSpace == 0); 222 assert(buf.front == 1); 223 buf.popFront(); // .|2 3 4 5 224 assert(buf.front == 2); 225 buf.popFrontN(2); // . . .|4 5 226 assert(buf.front == 4); 227 assert(buf.length == 2 && buf.freeSpace == 3); 228 buf.put([6, 7, 8]); // 6 7 8|4 5 229 assert(buf.length == 5 && buf.freeSpace == 0); 230 int[5] dst; 231 buf.read(dst); // . . .|. . 232 assert(dst == [4, 5, 6, 7, 8]); 233 assert(buf.length == 0 && buf.freeSpace == 5); 234 buf.put([1, 2]); // . . .|1 2 235 assert(buf.length == 2 && buf.freeSpace == 3); 236 buf.read(dst[0 .. 2]); //|. . . . . 237 assert(dst[0 .. 2] == [1, 2]); 238 }