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

Adding has_key() and/or default values to hasmaps #16

Open
vgherard opened this issue Apr 14, 2021 · 0 comments
Open

Adding has_key() and/or default values to hasmaps #16

vgherard opened this issue Apr 14, 2021 · 0 comments

Comments

@vgherard
Copy link

Hi! I just came along this very nice package and was trying to play around a bit with hash tables.

My particular use cases require many queries to keys which may be absent from the table. For a simplified example, suppose I want to build a table of occurrences of elements in a vector v of strings through a single pass over the vector; ideally, I would like to do something like this:

# pseudocode
h <- hashmap("integer")
for (x in v) {
    h[x] <- h[x] + 1
}

where, if x is not yet a key, h[x] on the right-hand side of the assignment should return zero (like e.g. a C++ STL map with numeric values).

As far as I can see, the only sensible way to correctly implement my pseudocode above is to wrap the h[x]<- assignment in a tryCatch() clause. It would be thus nice to have either:

  • an has_key() method to check the presence of x in keys(h) in constant time, or
  • the possibility to set a default (R object) value to missing keys.

If you think any of these two features are worth implementing, I could try to provide some starting code!

Hope this helps,
Thanks.

Valerio

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

1 participant