andyferris / Dictionaries.jl

An alternative interface for dictionaries in Julia, for improved productivity and performance
Other
278 stars 28 forks source link

Get values as array efficiently #110

Open aplavin opened 1 year ago

aplavin commented 1 year ago

The most intuitive way to get the array of values from an AbstractDictionary is collect(dict). However, it always allocates, even if values are readily available, as in Dictionary.values.

I would like to propose creating a function that returns dict.values if it's present (eg Dictionary), and does collect(dict) otherwise (eg PairDictionary). Not sure what it's name should be, though. It's like collect, but possibly without a copy.

mtfishman commented 1 year ago

Maybe the semantics could be convert(Vector, dict)? I've also come across situations where I have wanted this functionality (though in many way Dictionary acts like a Vector but with more general indexing so in general it seems like this could be avoided with generic programming).

andyferris commented 1 year ago

convert is a bag of worms - dictionaries assume the operation doesn’t affect the lookup of a key.

Hmm… what about hijacking vec?