Let’s say I want to call a function on the first n elements of sorted multi-dictionary. I can only come up with this clumsy way of doing it:
smdict = SortedMultiDict{Float64, Int64}(0.7=>7, 0.1=>1, 0.4=>4, 0.2=>2)
i = startof(smdict)
n = 3
for _ in 1:n
global i
println((deref((smdict, i))))
i = advance((smdict, i))
end
Is there a more compact way of iterating through an end interval of SortedMultiDict?
To iterate backwards through a SortedMultiDict requires invoking lower level primitives like deref and regress. If you need to do this operation often, then you could make your code cleaner by implementing an iterate protocol for stepping backwards through a SortedMultiDict.
In the C++ standard library, wherever an iterator is defined, the language also defines a reverse iterator if it makes sense. As far as I know, nobody has proposed a similar rule for Julia.
The current library does not support efficient integer indexing. With a nontrivial amount of additional code, this feature could be supported by adding a field to each internal search-tree node that stores how many descendants it has.
In my use case today the efficiency is not an issue. Convenience is more important. I just realized that I can fill an array with tuples, sort it and get n elements from whichever end I need: