Skip to content
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

WeakObjectIdDict & more general dicts #42

Closed
mauro3 opened this issue Jun 17, 2014 · 5 comments
Closed

WeakObjectIdDict & more general dicts #42

mauro3 opened this issue Jun 17, 2014 · 5 comments

Comments

@mauro3
Copy link

mauro3 commented Jun 17, 2014

I'm looking for a weak object-id dict for JuliaLang/julia/pull/5572. There is one implementation here: JuliaLang/julia/issues/3002. One feature needed is that it also works with immutable keys.

A potentially general way to go about this is to add two features to HashDict:

  • possibility of key transformation: For this case it would be key -> WeakRef(key). (This is essentially what the add_weak_key function in Base does.)
  • possibility of adapting the hashindex function. For object-id it would look:
    hashindex(key, sz) = (int(hash(object_id(key))) & (sz-1)) + 1

I'm not sure how to go about this: The usual way would be to use dispatch on the functions: hashindex(::Associative, key, sz) = (int(hash(key)) & (sz-1)) + 1. However, because HashDict is wrapped this does not work (or at least I cannot see how). Any ideas on how to go about this?

@kmsquire
Copy link
Member

For everything that wraps HashDict, we delegate most functions to the wrapped dictionary through the @delegate macro (e.g., https://github.com/JuliaLang/DataStructures.jl/blob/master/src/ordereddict.jl#L38-L41). I think this should work here as well. Do you see a reason why that might not be true?

@mauro3
Copy link
Author

mauro3 commented Jun 18, 2014

Tnx for looking into this. The method concerned here is hashindex which is internal to HashDict. Thus I don't think delegating works. If I make a new type MyDict which wraps HashDict, the internal hashindex still gets called, even if redefined as something like this hashindex(::Associative, key, sz) and hashindex(::MyDict, key, sz) because the underlying datatype is still HashDict.

@kmsquire
Copy link
Member

Okay. Are you importing hashindex() before overriding for HashDict and
MyDict? Do you have any code you can share in a gist?

On Wednesday, June 18, 2014, Mauro notifications@github.com wrote:

Tnx for looking into this. The method concerned here is hashindex which
is internal to HashDict. Thus I don't think delegating works. If I make a
new type MyDict which wraps HashDict, the internal hashindex still gets
called, even if redefined as something like this hashindex(::Associative,
key, sz) and hashindex(::MyDict, key, sz) because the underlying datatype
is still HashDict.


Reply to this email directly or view it on GitHub
#42 (comment)
.

@mauro3
Copy link
Author

mauro3 commented Jun 19, 2014

At the moment I just copy-pasted Dict and modified it, which works but I think it could be done better:
https://github.com/mauro3/julia/blob/metadata/base/dict.jl#L775-L1186

There should be a way to unify all the hash-based dicts including this one. You have been pursuing this quest for a wee while, maybe you got some insights on how to solve this more generally?

@oxinabox
Copy link
Member

I think this is stalled and can be closed?

@oxinabox oxinabox closed this as completed Nov 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants