January 13, 2010 Re: Variable-length stack allocated arrays | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | Andrei Alexandrescu wrote: > Yikes, thanks. Now something happened with my news reader - the part of your message containing your improved solution got cut off :o). template SuperStack(T) { private T[][] buffers; T[] getBuffer(size_t n) { if (buffers.length == 0) { buffers.length = 1; buffers[0].length = n; return buffers[0]; } else if (gc.capacity(buffers[$ - 1]) < buffers.length + n) { buffers.length = buffers.length + 1; // Prealloacte space, then truncate. buffers[$ - 1].length = max(buffers[$ - 2].length, n); buffers[$ - 1].length = n; return buffers[$ - 1]; } else { buffers[$ - 1].length = buffers[$ - 1].length + n; return buffers[$ - 1][$ - n .. $]; } } void releaseBuffer(T[] b) { enforce(b is buffers[$ - 1][$ - b.length .. $]); buffers[$ - 1].length = buffer[$ - 1].length - b.length; if (buffers[$ - 1].length == 0) { buffers.length = buffers.length - 1; } } } -- Rainer Deyke - rainerd@eldwood.com | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply