FIFO stack

Timon Gehr timon.gehr at gmx.ch
Wed Oct 26 12:23:04 PDT 2011


On 10/26/2011 07:38 PM, Ary Manzana wrote:
> On 10/26/11 1:28 PM, Jonathan M Davis wrote:
>> On Wednesday, October 26, 2011 09:00 Dominic Jones wrote:
>>>> Also an plain array is a good stack. :)
>>>
>>> I'd rather not use a plain array because (I assume) that when I push
>>> or pop using arrays, a swap array is created to resize the original.
>>> If this is not the case, then an array will certainly do.
>>> -Dominic
>>
>> Not exactly. If you want to know more about how arrays work, you
>> should read
>> this: http://www.dsource.org/projects/dcollections/wiki/ArrayArticle
>> It's a
>> great read. As for using an array as a stack, you can do it with a
>> wrapper
>> struct, but using it by itself would result in a lot more
>> reallocations than
>> you'd want, as discussed here:
>> https://www.semitwist.com/articles/article/view/don-t-use-arrays-as-stacks
>>
>>
>> - Jonathan M Davis
>
> I think that if you have to read an article that long, with all the
> explanations of the different caveats a programmer can bump to when
> using them, to understand how arrays and slices work.... something must
> be wrong.
>
> Things should be simpler.

You exaggerate. The word 'caveat' appears exactly once in that article. 
The rest are straightforward explanations, mainly about how the runtime 
implements D array concatenation. After reading Steve's (actually quite 
short) article, you know about everything described in Nick's.

D arrays and slices are so powerful that they are well worth a tiny 
little bit of complexity. The behaviour of dynamic arrays is a good 
trade-off between simplicity and performance imho.








More information about the Digitalmars-d-learn mailing list