Lists, Stacks, Maps, Indexes, oh my!

Monkey Forums/Monkey Programming/Lists, Stacks, Maps, Indexes, oh my!

wiebow(Posted 2011) [#1]
With all these great container objects, can someone tell me which is the fastest or best to use in which situation? I know: "it depends" but anyway... =]

Why would I, for example, choose to use an indexed Stack (they sound great) over an IntMap (which gives me the same functionality, afaik), or a list containing objects holding an index value (this will be slower, cos I need to iterate through all the values until I get to the correct index) ?

Or do all these indeed offer the same but in different flavours?


muddy_shoes(Posted 2011) [#2]
Well it does depend on many factors and some of those factors may be no more than personal preference. In order to pick the optimal data structure you'd need to know exactly how it was going to be used. For the most part you don't know this when you're making the initial choice so trying to pick the perfect data structure from the start is a little futile.

I just consider how I'll be storing and accessing the data in a broad sense. Will I need indexed access or the ability to retrieve by key or both? Will I be adding and removing elements a lot? If so, will I be adding and removing from arbitrary positions or like a queue or stack? Do I need the data to be ordered? If so, do I need the ordering occasionally or to be guaranteed at all times?

Unless I know that the choice will affect a performance sensitive part of the code, I'll favour convenience of interface over absolute performance. If the choice turns out to be a problem then changing code to retrofit a different data structure is generally not a big deal.


Warpy(Posted 2011) [#3]
Stacks are built on top of maps, so they're probably a bit slower.


dmaz(Posted 2011) [#4]
Stacks are built on top of maps, so they're probably a bit slower.
where did you get that idea? Did you mean Sets are built on Maps?


Warpy(Posted 2011) [#5]
Whoops, that's what I meant. I wasn't near a copy of the source. Stacks are built on top of arrays.


wiebow(Posted 2011) [#6]
Also: documentation claims that Sets are just as quick in inserting and finding as maps. I haven't tested this.