Skip to content
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

keys and getindex of OrderedSet #97

Open
duanhuaiyu opened this issue Oct 24, 2022 · 3 comments
Open

keys and getindex of OrderedSet #97

duanhuaiyu opened this issue Oct 24, 2022 · 3 comments

Comments

@duanhuaiyu
Copy link

My understanding of a set is a collection of keys without values. Therefore, I would expect OrderedSet to have keys as elements and their positions in the set as values or indices. This interpretation agrees with the description of Base.getindex: "Retrieve the value(s) stored at the given key or index within a collection."

The current implementation of getindex(ordered_set, i) returns the ith element in ordered_set which is opposite to this interpretation. I agree that it's useful to get the element at a given position, but it's more intuitive to use ordered_set[i] instead. Similarly, the current implementation of keys(ordered_set) returns 1:length(ordered_set) instead of collect(ordered_set).

@duanhuaiyu
Copy link
Author

duanhuaiyu commented Oct 24, 2022

Just realized that Julia actually converts ordered_set[i] to getindex(ordered_set, i). Nevertheless, is there an efficient way to get the index of an element in an OrderedSet?

@StephenVavasis
Copy link
Contributor

Note that OrderSet is part of OrderedCollections, which is not this repository (DataStructures) but is adjacent. So I am closing this issue and requesting that you reopen it in OrderedCollections if you wish.

@StephenVavasis StephenVavasis closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2022
@oxinabox oxinabox transferred this issue from JuliaCollections/DataStructures.jl Mar 14, 2023
@oxinabox oxinabox reopened this Mar 14, 2023
@timholy
Copy link
Member

timholy commented Mar 14, 2023

ordered_set[i] is currently deprecated:

# Deprecated functionality, see
# https://github.com/JuliaCollections/DataStructures.jl/pull/180#issuecomment-400269803
function getindex(s::OrderedSet, i::Int)
Base.depwarn("indexing is deprecated for OrderedSet, please rewrite your code to use iteration", :getindex)
s.dict.ndel > 0 && rehash!(s.dict)
return s.dict.keys[i]
end

Personally I think getting rid of it is the right answer, but I know there have been voices to the contrary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants