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

Support pushfirst! and popfirst! for ordered collections #403

Open
omus opened this issue Jul 21, 2018 · 3 comments
Open

Support pushfirst! and popfirst! for ordered collections #403

omus opened this issue Jul 21, 2018 · 3 comments

Comments

@omus
Copy link
Contributor

omus commented Jul 21, 2018

It would be nice to have these for OrderedDict and OrderedSet. At the moment in Julia 0.7:

julia> using DataStructures

julia> o = OrderedSet([1,2,3])
OrderedSet{Int64}([1, 2, 3])

julia> push!(o, 4)
OrderedSet{Int64}([1, 2, 3, 4])

julia> pop!(o, 4)
4

julia> pushfirst!(o, 0)
ERROR: MethodError: no method matching pushfirst!(::OrderedSet{Int64}, ::Int64)
Closest candidates are:
  pushfirst!(::Any, ::Any, ::Any) at abstractarray.jl:2040
  pushfirst!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:2041
  pushfirst!(::Array{T,1}, ::Any) where T at array.jl:1063
  ...
Stacktrace:
 [1] top-level scope at none:0

julia> popfirst!(o)
ERROR: MethodError: no method matching popfirst!(::OrderedSet{Int64})
Closest candidates are:
  popfirst!(::DataStructures.IntSet) at /Users/omus/.julia/packages/DataStructures/eUHI/src/int_set.jl:90
  popfirst!(::BitSet) at bitset.jl:255
  popfirst!(::BitArray{1}) at bitarray.jl:817
  ...
Stacktrace:
 [1] top-level scope at none:0
@SimonCoste
Copy link

SimonCoste commented Feb 24, 2021

Has this proposition been adressed?

It seems that by now (v0.8), the pop!(ss) method allows to pop the first element in a SortedSet, but apparently there is no equivalent method for SortedDict, where the item with the smallest key would be popped.

What is the most efficitent way to do this at the moment ?

@StephenVavasis
Copy link
Contributor

StephenVavasis commented Feb 24, 2021

I suggest startof followed by deref followed by delete! If you don't actually need the first item and you are using pop merely to discard it, then delete!((sd, startof(sd))) should suffice.

@SimonCoste
Copy link

Stephen, thank you!
A simple pop!(SortedDict) would be natural and welcome in the future.

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

No branches or pull requests

3 participants