Successively prepending to slices.

JR zorael at gmail.com
Thu Apr 10 13:03:44 PDT 2014


It depends on your use-case, I think. How often will you be 
prepending? Will you be appending as well?

On Thursday, 10 April 2014 at 16:10:04 UTC, Steven Schveighoffer 
wrote:
> Note, I create a deque class in dcollections which maintains 2 
> arrays, one for prepending (and is in reverse order), and one 
> for appending. The class handles the indexing so that it looks 
> like a standard array. But prepending is also amortized O(1)

I like this one. If you're only ever prepending you would only 
need the reverse one, appending values in .retro to it as you 
wish, and then .retro.dup when you want a copy.

In my NewlineBufferThingy struct, I sometimes need to move a 
slice at an arbitrary point of an array to the very beginning of 
it, like a poor man's circular buffer. I ended up using 
std.algorithm.copy for that, but in your case -- once more, 
depending on your use-case -- that could end up in a *lot* of 
copying.


More information about the Digitalmars-d-learn mailing list