-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Add get!
for Dicts (addresses #1529)
#5519
Conversation
I'm for this. The case where the RHS is very expensive could be handled with a higher-order method: get!(dict,key) do
value
end I'd also like to add syntax for this: |
+1 for this and for syntax. I hope the latter would only evaluate the rhs |
Okay, good. After playing with this for Questions:
#1764 could also be trivially reworked to use the new |
I think that ?= is a pretty major thing that definitely deserves its own |
Agree. I don't know how to do it off-hand, but I think I could probably figure it out. But if you (or someone else) want to take a shot, feel free! It might be a couple of simple additions to |
I've added the |
* Refactored setindex! into ht_keyindex2, for use by get! and setindex!
Also added get, get! for WeakKeyDicts
In response to #1764 (comment), I added do-block syntax for Also added |
I like it. I'm down do merge this. @JeffBezanson? |
Ok by me. |
RFC: Add `get!` for Dicts (addresses #1529)
This PR refactors
setindex!(d::Dict, v, key)
intosetindex!
andht_keyindex2
, whereht_keyindex2
returns either the currentindex
of the key, or-index
for the insertion location of a new key (after rehashing, etc).(Compare with
ht_keyindex
, which returns-1
if the key is not in the hash table, and does not callrehash
.)This is used by
get!
to get the current or new location of a key.The PR does not have any measurable impact on performance.
Related:
DefaultDict
implementation based on the Python one.ht_keyindex
more than once --> kills performance.CC: @toivoh, @StefanKarpinski