FIFO stack
Jonathan M Davis
jmdavisProg at gmx.com
Wed Oct 26 11:41:06 PDT 2011
On Wednesday, October 26, 2011 10:38 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-stack
> > s
> >
> > - 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.
Perhaps. But doing so and still having them be appropriately powerful is not
straightforward if it's even possible. What we have works very well overall.
It's just that if you start doing stuff that can cause an array to reallocate,
and you don't understand enough about how arrays and slices work, you're going
to end up reallocating your arrays way too often and harm performance. So, for
the most part, you can use arrays just fine without understanding everything
in that article, but your code risks being less efficient.
Given how much you gain from D arrays, I think whatever complexity they have
is _well_ worth it. It would be nice if the complexity could be reduced
without reducing their usefuless or efficiency, but I don't know how possible
that is.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list