OrderedCollections.jl icon indicating copy to clipboard operation
OrderedCollections.jl copied to clipboard

keys and getindex of OrderedSet

Open duanhuaiyu opened this issue 2 years ago • 3 comments

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 avatar Oct 24 '22 23:10 duanhuaiyu

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?

duanhuaiyu avatar Oct 24 '22 23:10 duanhuaiyu

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 avatar Oct 25 '22 15:10 StephenVavasis

ordered_set[i] is currently deprecated: https://github.com/JuliaCollections/OrderedCollections.jl/blob/6497eb58bdc7898db7e2fd1f9685f4218e55c62d/src/ordered_set.jl#L84-L91

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

timholy avatar Mar 14 '23 12:03 timholy