Dynamic array as stack and GC.BlkAttr.APPENDABLE

IgorStepanov via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 14 14:36:14 PST 2014


On Friday, 14 November 2014 at 22:25:20 UTC, 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.
Thanks! Your magic is works!


More information about the Digitalmars-d mailing list