Associative array key order

Sean Kelly sean at invisibleduck.org
Fri Aug 23 11:22:47 PDT 2013


On Jul 31, 2013, at 7:55 AM, Dicebot <public at dicebot.lv> wrote:

> On Wednesday, 31 July 2013 at 14:43:21 UTC, Daniel Kozak wrote:
>> is there a way for AA to behave same as PHP?
> 
> I doubt it. This snippet suggests that AA's in PHP are not simply AA's and do additionally track insertion order (or use some similar trick). Data in associative arrays is organized in a way that allows fast key lookup and iterating in an original order requires tracking extra state. Contrary to PHP, D does care about performance so I see no way this can happen automatically.

This seems more likely to be a library type.  I have something like this that I use very frequently at work where the behavior is pluggable, so it can be used as a cache that automatically does LIFO/FIFO eviction according to an age window, number of entries, etc.  Or none of those if all you want is a plain old hashtable.  It's not a tremendous amount of work to adapt an existing implementation to work this way.  The only annoying bit I ran into (at least with C++) is that the symbol lookup rules pretty much prevent overrides of existing functions to be mixed in via the policies.  Does alias this work around this problem in D?  I've never tried.


More information about the Digitalmars-d-learn mailing list