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

Make sets indexable #24508

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ Library improvements
This supersedes the old behavior of reinterpret on Arrays. As a result, reinterpreting
arrays with different alignment requirements (removed in 0.6) is once again allowed ([#23750]).

* `AbstractSet`s are now indexable, such that `set[x] == x` and `keys(set) == set`
([#24508]).

Compiler/Runtime improvements
-----------------------------

Expand Down
2 changes: 0 additions & 2 deletions base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ very large integers), use [`Set`](@ref) instead.
"""
BitSet(itr) = union!(BitSet(), itr)

eltype(::Type{BitSet}) = Int
similar(s::BitSet) = BitSet()
copy(s1::BitSet) = copy!(BitSet(), s1)
function copy!(dest::BitSet, src::BitSet)
resize!(dest.bits, length(src.bits))
copy!(dest.bits, src.bits)
dest
end
eltype(s::BitSet) = Int
sizehint!(s::BitSet, n::Integer) = (n > length(s.bits) && _resize0!(s.bits, n); s)

# An internal function for setting the inclusion bit for a given integer n >= 0
Expand Down
21 changes: 17 additions & 4 deletions base/set.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# AbstractSet

# AbstractSets are idempotent under indexing:
keys(set::AbstractSet) = set
@inline function getindex(set::AbstractSet, x)
@boundscheck if x ∉ set
throw(KeyError(x))
end
return x
end
eltype(set::Type{<:AbstractSet{T}}) where {T} = T

# Set

mutable struct Set{T} <: AbstractSet{T}
dict::Dict{T,Void}

Expand All @@ -23,9 +37,8 @@ function Set(g::Generator)
return Set{T}(g)
end

eltype(::Type{Set{T}}) where {T} = T
similar(s::Set{T}) where {T} = Set{T}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's now just a clean-up

similar(s::Set, T::Type) = Set{T}()
similar(s::AbstractSet) where {T} = similar(s, eltype(s))
similar(s::AbstractSet, T::Type) = Set{T}() # default empty set type

function show(io::IO, s::Set)
print(io, "Set")
Expand Down Expand Up @@ -64,7 +77,7 @@ rehash!(s::Set) = (rehash!(s.dict); s)
start(s::Set) = start(s.dict)
done(s::Set, state) = done(s.dict, state)
# NOTE: manually optimized to take advantage of Dict representation
next(s::Set, i) = (s.dict.keys[i], skip_deleted(s.dict, i+1))
@propagate_inbounds next(s::Set, i) = (s.dict.keys[i], skip_deleted(s.dict, i+1))

"""
union(s1,s2...)
Expand Down
6 changes: 6 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ end
end
end

@testset "indexing" begin
s = Set(["a", "b", "c"])
@test s["b"] == "b"
@test_throws KeyError s["d"]
end

@testset "union" begin
@test isequal(union(Set([1])),Set([1]))
s = ∪(Set([1,2]), Set([3,4]))
Expand Down