On Friday, 14 November 2014 at 22:38:53 UTC, Steven Schveighoffer
wrote:
On 11/14/14 5:25 PM, Dmitry Olshansky wrote:
15-Nov-2014 01:16, IgorStepanov пишет:
Recently I encountered the following problem.
I need a simple stack of uint.
I want to push uints back and pop it. I don't want to copy
this stack
and I want it to work fast.
In a first approximation, the problem seems easy.
uint[] my_stack;
my_stack.reserve(256);
my_stack ~= 1; //push
uint head = my_stack[$ - 1]; //top
my_stack.length--; //pop
Just make push into:
my_stack.assumeSafeAppend();
my_stack ~= value;
To avoid relocations.
In actuality, you do not need this before every push. You only
need it between a pop and a push.
I highly recommend not doing arrays-as-stacks, because you end
up writing your own type.
I'm working on AssociativeArray implementation and I need to
array which contains indices of free entries (entries is an array
which contain Entry objects. When we need to find empty Entry to
add a new value to the AA, we are getting the first element of
the stack. (If the stack is empty, we need to add new element to
entries). If entry has been removed from AA, it index is added to
the stack).
Thus I don't want to add extra fields, and this stack is an
encapsulated entity.