But this doesn’t feel like the “front-end” way of doing this — I always find myself wanting to write values(od) and being surprised when the ValueIterator object it returns doesn’t support indexing.
Should I submit a PR to OrderedCollections.jl and implement getindex(::ValueIterator{<:OrderedDict}, …) so that values(od)[2] works?
I only hesitate because values does’t seem to return getindex-able objects normally. I’m not sure if that’s by design.
You’re quite right. I was careful to only add methods for OrderedDicts, not normal Dicts:
Base.getindex(h::Base.KeySet{K,<:OrderedDict{K}}, index) where K = h.dict.keys[index]
Base.getindex(h::Base.ValueIterator{<:OrderedDict}, index) = h.dict.vals[index]
Assuming the internal layout of OrderedDicts always works this way…
Perhaps there’s another dictionary-like data structure which is intended to be getindexed by key and/or linear index, without temporary allocation?