Skip to content

Added Persistent disjoint set implementation. #69

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/FunctionalCollections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ include("PersistentQueue.jl")
export PersistentQueue, queue,
enq

include("PersistentDisjointSet.jl")

export PersistentDisjointSet

export @Persistent

fromexpr(ex::Expr, ::Type{pvec}) = :(pvec($(esc(ex))))
Expand Down
32 changes: 32 additions & 0 deletions src/PersistentDisjointSet.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
struct PersistentDisjointSet{T}
table::PersistentHashMap{T,T}
heights::PersistentHashMap{T,UInt8}
end
PersistentDisjointSet{T}() where {T} = PersistentDisjointSet{T}(PersistentHashMap{T,T}(),PersistentHashMap{T,UInt8}())


height(t::PersistentDisjointSet{T},i::T) where {T} = get(t.heights,i,zero(UInt8))

function Base.getindex(t::PersistentDisjointSet{T},i::T) where {T}
while haskey(t.table,i) # In a persistent setting, writes cost memory, so path compression may not be worth the allocations.
i = t.table[i] # Height balancing already guarentees finding the root in floor(lg(N)) lookups.
end
i
end

function Base.union(t::PersistentDisjointSet{T},i::T,j::T) where {T}
i = t[i];j=t[j]
if i == j return t end
if height(t,i) > height(t,j)
PersistentDisjointSet(assoc(t.table,j,i),t.heights)
elseif height(t,i) < height(t,j)
PersistentDisjointSet(assoc(t.table,i,j),t.heights)
else
PersistentDisjointSet(assoc(t.table,j,i),assoc(t.heights,i,height(t,i) + 1))
end
end

function Base.show(io::IO,t::PersistentDisjointSet{T}) where {T}
Base.print(io,"PersistentDisjointSet{$T}")
Base.print(io,sort!(map(x->first(x) => t[last(x)],collect(pairs(t.table)));by=x->(last(x),first(x))))
end
19 changes: 19 additions & 0 deletions test/PersistentDisjointSetTest.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using FunctionalCollections
using Test

@testset "Persistent Lists" begin

@testset "UnionFind" begin
uf = PersistentDisjointSet{Int}()
uf = union(uf,2,3)
uf = union(uf,5,7)
uf = union(uf,2,4)
uf = union(uf,4,8)
@assert uf[1] == 1
@assert uf[1] != uf[7]
@assert uf[3] == uf[8]
@assert uf[5] == uf[7]
@assert uf[3] != uf[7]
end

end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ include("./PersistentMapTest.jl")
include("./PersistentSetTest.jl")
include("./PersistentListTest.jl")
include("./PersistentQueueTest.jl")
include("./PersistentMacroTest.jl")
include("./PersistentDisjointSetTest.jl")
include("./PersistentMacroTest.jl")