public
Last active

  • Download Gist
buffered_range.d
D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
module buffered_range;
 
import std.algorithm, std.conv, std.exception, std.range, std.traits, std.typecons;
import core.memory, core.stdc.stdlib;
 
/*
* Adds the save function to an input range thus promoting
* it to a forward range. This is done by buffering the input range
* in blocks of size blockSize.
*/
struct BufferedRange(R) if(isInputRange!R)
{
alias ElementType!R T;
immutable defaultBlockSize = max(4096 / T.sizeof, 1);
 
this(R input, size_t blockSize = defaultBlockSize)
{
_storage = RefCounted!Storage(input, blockSize);
fetchBlock();
}
 
@property bool empty() const
{
return _curbuf.empty && input.empty;
}
 
@property T front()
{
return _curbuf.front;
}
 
@property void popFront()
{
_curbuf.popFront;
if (_curbuf.empty)
fetchBlock();
}
 
~this()
{
decref();
}
 
this(this)
{
incref();
}
 
void opAssign(typeof(this) rhs)
{
rhs.incref();
decref();
 
_storage = rhs._storage;
_curbuf = rhs._curbuf;
_block = rhs._block;
}
 
@property BufferedRange save()
{
return this;
}
 
private:
void fetchBlock()
{
// no block at all
if (_block is null)
{
_block = _storage.allocBlock();
++_block._refcount;
_storage.fillBlock(_block);
_curbuf = _block._data;
}
// already has successor, possibly free current block
else if (_block._next !is null)
{
auto next = _block._next;
if (--_block._refcount == 0)
_storage.freeBlock(_block);
_block = next;
_curbuf = _block._data;
}
// sole owner reuse block
else if (_block._refcount == 1)
{
_storage.fillBlock(_block);
_curbuf = _block._data;
}
// still referenced => need to fetch a new one
else
{
_block._next = _storage.allocBlock();
_block._next._refcount = _block._refcount;
--_block._refcount;
_block = _block._next;
_storage.fillBlock(_block);
_curbuf = _block._data;
}
}
 
void incref()
{
auto p = _block;
while (p !is null)
{
assert(p._refcount);
++p._refcount;
p = p._next;
}
}
 
void decref()
{
if (_block is null)
return;
 
auto p = _block;
 
if (_block._refcount == 1)
_block = null;
 
while (p !is null)
{
auto next = p._next;
assert(p._refcount);
if (--p._refcount == 0)
_storage.freeBlock(p);
p = next;
}
}
 
@property ref R input()
{
return _storage._input;
}
 
@property ref const(R) input() const
{
return _storage._input;
}
 
static struct Block
{
T[] _data;
size_t _refcount;
Block* _next;
}
 
static struct Storage
{
~this()
{
foreach(block; _freeList)
{
static if (hasIndirections!T)
GC.removeRange(block._data.ptr);
free(block);
}
}
 
Block* allocBlock()
{
typeof(return) res;
if (_freeList.empty)
{
static assert(Block.alignof <= 8 && T.alignof <= 8);
static assert(Block.alignof % T.alignof == 0);
 
auto p = malloc(Block.sizeof + T.sizeof * _blockSize);
res = emplace(cast(Block*)p);
 
auto pt = p + Block.sizeof;
static if (hasElaborateAssign!T)
memset(pt, 0, T.sizeof * _blockSize);
static if (hasIndirections!T)
GC.addRange(pt, T.sizeof * _blockSize);
 
res._data = (cast(T*)pt)[0 .. _blockSize];
}
else
{
res = _freeList.back;
_freeList.popBack;
}
assert(res._refcount == 0);
return res;
}
 
void freeBlock(Block* p)
{
assert(p._refcount == 0);
_freeList ~= p;
}
 
void fillBlock(Block* p)
{
assert(p._refcount);
auto rdbuf = p._data.ptr[0 .. _blockSize];
for (; !_input.empty && !rdbuf.empty; _input.popFront, rdbuf.popFront)
{
rdbuf.front = _input.front;
}
p._data = p._data.ptr[0 .. _blockSize - rdbuf.length];
}
 
R _input;
/* immutable */ size_t _blockSize;
Block*[] _freeList;
}
 
RefCounted!Storage _storage;
T[] _curbuf;
Block* _block;
alias input this;
}
 
template BufferedRange(R) if(isForwardRange!R)
{
alias R BufferedRange;
}
 
unittest
{
static struct InputRange
{
this(size_t *pn)
{
_pn = pn;
}
 
bool empty() const { return *_pn == 1_000_000; }
size_t front() { return *_pn; }
void popFront() { ++*_pn; }
void reset() { *_pn = 0; }
size_t* _pn;
}
 
auto inp = InputRange(new size_t);
auto inp2 = inp;
assert(array(take(inp, 100)) != array(take(inp2, 100)));
inp.reset;
 
alias BufferedRange!InputRange FwdRange;
 
{
auto fwd1 = FwdRange(inp);
auto fwd2 = fwd1.save;
assert(array(take(fwd1, 100)) == array(take(fwd2, 100)));
 
// test alias this
assert(fwd1._pn !is null);
 
inp.reset;
}
 
{
// this is inefficient due to the implicit save-through-copy in foreach
auto fwd = FwdRange(inp);
foreach(e; fwd)
{}
 
inp.reset;
 
// this is efficient because no second copy is made
foreach(e; BufferedRange!InputRange(inp))
{}
 
inp.reset;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.