Skip to content

Commit

Permalink
Deprecate indexing for OrderedSet
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jun 29, 2018
1 parent d951aee commit 35ac721
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
32 changes: 27 additions & 5 deletions src/ordered_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ pop!(s::OrderedSet, x) = (pop!(s.dict, x); x)
pop!(s::OrderedSet, x, deflt) = pop!(s.dict, x, deflt) == deflt ? deflt : x
delete!(s::OrderedSet, x) = (delete!(s.dict, x); s)

getindex(x::OrderedSet,i::Int) = x.dict.keys[i]
lastindex(x::OrderedSet) = lastindex(x.dict.keys)
nextind(::OrderedSet, i::Int) = i + 1 # Needed on 0.7 to mimic array indexing.
keys(s::OrderedSet) = 1:length(s)

union!(s::OrderedSet, xs) = (for x in xs; push!(s,x); end; s)
setdiff!(s::OrderedSet, xs) = (for x in xs; delete!(s,x); end; s)
setdiff!(s::Set, xs::OrderedSet) = (for x in xs; delete!(s,x); end; s)
Expand Down Expand Up @@ -111,3 +106,30 @@ function hash(s::OrderedSet, h::UInt)
s.dict.ndel > 0 && rehash!(s.dict)
hash(s.dict.keys, h)
end


# 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

function lastindex(s::OrderedSet)
Base.depwarn("indexing is deprecated for OrderedSet, please rewrite your code to use iteration", :lastindex)
s.dict.ndel > 0 && rehash!(s.dict)
return lastindex(s.dict.keys)
end

function nextind(::OrderedSet, i::Int)
Base.depwarn("indexing is deprecated for OrderedSet, please rewrite your code to use iteration", :lastindex)
return i + 1 # Needed on 0.7 to mimic array indexing.
end

function keys(s::OrderedSet)
Base.depwarn("indexing is deprecated for OrderedSet, please rewrite your code to use iteration", :lastindex)
s.dict.ndel > 0 && rehash!(s.dict)
return 1:length(s)
end
12 changes: 0 additions & 12 deletions test/test_ordered_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,6 @@
@test isequal(s, OrderedSet([7,8]))
@test isequal(intersect(OrderedSet([2,3,1]), OrderedSet([4,2,3]), OrderedSet([5,4,3,2])), OrderedSet([2,3]))

# indexing
s = OrderedSet([1,3,5,7])
@test s[1] == 1
@test s[2] == 3
@test s[end] == 7

# find
s = OrderedSet([1,3,5,7])
@test findfirst(isequal(1), s) == 1
@test findfirst(isequal(7), s) == 4
@test findfirst(isequal(2), s) == nothing

# setdiff
@test isequal(setdiff(OrderedSet([1,2,3]), OrderedSet()), OrderedSet([1,2,3]))
@test isequal(setdiff(OrderedSet([1,2,3]), OrderedSet([1])), OrderedSet([2,3]))
Expand Down

0 comments on commit 35ac721

Please sign in to comment.