nathan-russell/hashmap

Creating an empty hashmap

talegari opened this issue · 3 comments

Dear Nathan Russell,

Is there a way to create an empty hashmap? If not, would it be handy to alllow hashmap::hashmap leaving keys and values empty? Or some syntactic sugar ?

I end up doing this (which isn't too bad):

H  = hashmap::hashmap("temp" = 1)
H$clear()

@talegari Constructing the object with zero-length vectors should accomplish this:

h <- hashmap::hashmap(character(), numeric())
h
## [empty Hashmap] 

h$size()
# [1] 0

h$empty()
# [1] TRUE

Do you have a particular use case for an empty hashmap, or were you asking just out of curiosity?

@nathan-russell I have a use-case of adding and deleting elements to the hashmap beginning with an empty hashmap. I am trying to build a sort of a sparse elastic matrix whose size and elements are not known beforehand. The values get added/deleted on the fly. I am planning to store it as a hashmap to keep it memory efficient and then create a sparse matrix at the end. Would hashmap be a good data structure for this purpose?

I almost use empty hashes all the time. For example as arguments to recursive functions for graph traversal.