Bug 9491 – [AA] allow for insert/return pointer to existing value if present on assoc array
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-09T11:18:08Z
Last change time
2018-11-23T11:35:22Z
Assigned to
No Owner
Creator
Daniel Davidson
Comments
Comment #0 by dbdavidson — 2013-02-09T11:18:08Z
By providing something like:
auto ptrToValue = hash.insertDefaultOrFind(key, init)
*ptrToValue += additional;
client code can perform an <insert if not present/lookup> combined so that on the
case of a nonexistent key, 1 toHash/lookup can be done instead of 3.
The best recap of what is needed is in this thread:
http://forum.dlang.org/post/[email protected]
Comment #1 by devw0rp — 2014-04-09T07:54:06Z
I know exactly the kind of thing you are talking about. What you want is a setDefault function with an overload. These functions should have these signatures.
ref V1 setDefault(K, V1, V2)(ref V1[K] map, K key, lazy V2 value) if (is(V2 : V1));
ref V setDefault(K, V)(ref V[K] map, K key);
Where the second overload is equivalent to setDefault(map, key, V1.init) above. This is basically 'setdefault' from Python, only better because the value is lazy.
I have written a higher level implementation of this myself before, but I believe a druntime implementation could do better by computing a hash for K once, like you say.
Comment #2 by stanislav.blinov — 2018-11-23T11:35:22Z