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



Reply via email to