Feature to get or add value to an associative array.
Cym13
cpicard at openmailbox.org
Tue Apr 17 00:04:32 UTC 2018
On Monday, 16 April 2018 at 18:59:54 UTC, Giles Bathgate wrote:
> On Monday, 16 April 2018 at 12:41:07 UTC, JN wrote:
>> It's only one additional "in", but makes the code more
>> explicit and clear. I think in most cases, you will want to
>> check if you are dealing with a fetched object or a default
>> created one, so that will complicate the function even further.
>
> You can still use a combination of `in` and the update syntax
> if you want, this doesn't take that away. In the future, for a
> concurrent implementation of associative arrays, a way of
> getting or adding an element as an atomic operation becomes
> more important. Either way the beauty of hashed based lookups
> is that they average O(1), seems a shame to double that up for
> no reason ;)
"in" returns a pointer to the object, there'es not double lookup
necessary:
// if we don't know .get(key, default) exists
auto ptr = key in aa;
auto value = ptr ? *ptr : default;
// to set default value on the fly
auto value = ptr ? *ptr : *ptr = default;
is a new flag/method really that necessary? In my experience if
you have trouble naming it you haven't found its true purpose yet.
More information about the Digitalmars-d
mailing list