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
Add indexing to OrderedSets #180
Conversation
I was using an outdated version, so the code is tweaked slightly. Just by providing indexing we can rely on the |
LGTM. @DanielArndt @rawls238, any thoughts? If you have nothing against this, I think it can be merged. |
this lgtm as well |
As discovered by @StephenVavasis, indexing does not currently play well with deletion: julia> function testindexing()
s = OrderedSet([i for i = 1:12])
delete!(s, 7)
prerh = s[7]
OrderedCollections.rehash!(s.dict) # see #392 for the OrderedCollections name
postrh = s[7]
return (prerh, postrh)
end
julia> testindexing()
(7, 8) (I did this inside a function because simply Our options appear to be to check the So, worth asking, how useful is this really? If we do drop it, I can add a deprecation that also has the proper behavior. |
Sorry for butting in, but if we shouldn't index but use iteration instead, why not add a function like this: function getindex(s, i)
for (j, sj) in enumerate(s)
j == i && return sj
end
throw(BoundsError(s, i))
end |
I've found myself often wanting an ordered dict object that is ergonomic to index into either by dict key or by index in the key sequence. And, often I don't need the ability to delete from this data structure. For this case I've created >>> d = AppendOnlyOrderedDict(:a => 2, :b => 4, :c => 6);
>>> d[:b]
4
>>> d.seq[2]
:b => 4
>>> d.seqvals[2]
4
>>> d.seqvals[2:end]
2-element Array{Int64,1}:
4
6 |
This is currently very slow. If adata has N names and we want to extract K names, the complexity is O(NK). Ideally we would use an ordered set for row_names and var_names, but we need the ordered set to support both key and index lookup as well as being able to return an index for a given key. OrderedSet from OrderedCollections.jl deprecated index lookup and does not have an API for looking up an index for a given key (see JuliaCollections/OrderedCollections.jl#64 and JuliaCollections/DataStructures.jl#180 (comment)
Discussed here: #178