best idiom for insert if not present; else use value in assoc array
Dan
dbdavidson at yahoo.com
Thu Feb 7 13:36:47 PST 2013
On Thursday, 7 February 2013 at 20:46:31 UTC, Andrej Mitrovic
wrote:
> auto ref set(Hash, Key, Val)(Hash hash, Key key, Val val)
> if (isAssociativeArray!Hash &&
> is(Key : KeyType!Hash) &&
> is(Val : ValueType!Hash))
> {
> if (auto res = key in hash)
> {
> return res;
> }
> else
> {
> hash[key] = val;
> return key in hash;
> }
> }
Definitely a convenient function, but I think what Jonathan and
bearophile suggested was something more like:
auto ptrToValue = hash.insertDefaultOrFind(key, init)
*ptrToValue += additional;
and then on the first insert for a given key the hash is computed
just once. I believe this would have to be done at the level of
AssociativeArray and can not be done as a helper function, since
this set helper is still doing 3 hashes/lookups.
Thanks
Dan
More information about the Digitalmars-d-learn
mailing list