-
-
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
Define a token API for Associative types #24454
Comments
Why not just
the outcome of the above should be the same as
so it's in one line and faster? |
The "token" feature of SortedDict is meant to address two problems: multiple key lookups for an update operation (the topic of this issue) and also iterating over a sorted container in the sort-order of the keys. The token serves as the state-variable for this iteration. The updateindex! proposal in the above comment would solve the first problem for both Dict and SortedDict, but tokens or something similar would still be needed to solve the second problem for the sorted containers. |
I favor API 2. I've been hoping that we can copy the I'd also like to point out that linear indexing is a kind-of token system for dense arrays. |
Wouldn't that be API 1, then? That'd more transparently give you a "fast" key to use with indexing syntaxes. The downside with API 1 is that then dictionaries can no longer support all types in the language as keys — the |
I guess I was imagining methods of Wrapping in a In any case, the tricky bit is when you do operations like |
I favor adding |
API 1 seems a lot clearer to me. I don't think it's correct that API 1 wouldn't be able to support all types in the language, because you would parameterize the token type on the dictionary type. That is, for If you then wanted a new dictionary |
Note that you'd want the function to be the first argument of if haskey(dict, k)
.... do something...
else
... do something else with dict[k] ....
end ? |
Also, without tokens, how would you swap two elements, i.e. d[k1], d[k2] = d[k2], d[k1] without hashing the keys twice? The proposed |
API 1 is ambiguous for Dicts of It's true, tokens are more powerful, but therefore more difficult to support correctly. |
I'm still not convinced that One obvious but tricky thing is that |
|
Regarding update!, my favorite
The |
(From a recent discourse discussion.)
Consider
In this code, the hash of the key is calculated 3 times. Ideally, this would happen only once.
Tokens provide one way to handle this. Two possible interfaces:
(
gettoken1
andgettoken2
are named as such to distinguish them here. The actual name for whatever API is chosen should begettoken
)I have a slight preference for API 1. API 2 was proposed by @StefanKarpinski here. (@StefanKarpinski, please fix if incorrect!)
Related solutions/issues:
SortedDict
s in DataStructures.jl, to allowO(1)
access to a value that was looked upget!(d, key, default)
to store and return a default value, if the key is not in the dictionaryDefaultDict
s in DataStructures.jl, for a similar purpose?=
as a get-or-set operation (?= "get or set" operator #2108)Ref
s to access elements (julep/rfc: proposal for unified Dict get/insert/has interface #12157)Cc: @StephenVavasis @andyferris
The text was updated successfully, but these errors were encountered: