Dynamic array as stack and GC.BlkAttr.APPENDABLE

Dmitry Olshansky via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 14 14:25:15 PST 2014


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.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list