best idiom for insert if not present; else use value in assoc array

Dan dbdavidson at yahoo.com
Thu Feb 7 12:20:46 PST 2013


For an associative array, what is the best idiom that allows for 
checking if a key exists in the AA. If it does do something with 
the value, if not initialize the value and then do something with 
it.

In this code: http://dpaste.dzfl.pl/daab318f

How would this piece be improved? It looks like it would have to 
perform the hash and do a find 3 times:

     auto exists = (k in _dataMap);
     if(!exists) {
       _dataMap[k] = init;
       exists = (k in _dataMap);
     }
     ... use *exists

At a higher level and assuming the general goal of this basic 
struct is clear, any other suggestions welcome.

Also, an FYI to dpaste maintainer that the compiler service has 
been unavailable for a while.

Thanks
Dan



More information about the Digitalmars-d-learn mailing list