Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/disjoint_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function Base.sizehint!(s::IntDisjointSet, n::Integer)
sizehint!(s.ranks, n)
return s
end
Base.in(i::Integer, s::IntDisjointSet) = checkbounds(Bool, s.parents, i)

"""
num_groups(s::IntDisjointSet)
Expand Down Expand Up @@ -175,6 +176,7 @@ Base.iterate(s::DisjointSet) = iterate(s.revmap)
Base.iterate(s::DisjointSet, i) = iterate(s.revmap, i)

Base.length(s::DisjointSet) = length(s.internal)
Base.in(el, s::DisjointSet) = haskey(s.intmap, el)

"""
num_groups(s::DisjointSet)
Expand Down
25 changes: 25 additions & 0 deletions test/test_disjoint_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
@test find_root!(s, T(3)) == T(2)
end

@testset "`Base.in`" begin
@test 1 in s
@test 1 in s2
@test 10 in s
@test 10 in s2
@test !(11 in s)
@test !(11 in s2)
@test !(0 in s)
@test !(0 in s2)
end

@testset "more tests" begin
# We cannot support arbitrary indexing and still use @inbounds with IntDisjointSet
# (and it's not useful anyway)
Expand Down Expand Up @@ -138,6 +149,20 @@
@test find_root!(s, 7) == find_root!(s, 3)
end

@testset "`Base.in`" begin
s = DisjointSet{Int}([1, 3, 5, 7, 9])
for i in [1, 3, 5, 7, 9]
@test i in s
end
@test !(2 in s)

s = DisjointSet{String}(["a", "b", "c", "e"])
for i in ["a", "b", "c", "e"]
@test i in s
end
@test !("d" in s)
end

@testset "Some tests using non-integer disjoint sets" begin
elems = ["a", "b", "c", "d"]
a = DisjointSet{AbstractString}(elems)
Expand Down
Loading