Skip to content

Commit 3ca7420

Browse files
authored
Merge pull request #772 from aviatesk/dsdoc
update disjoint_set docs
2 parents f57330a + 26cd641 commit 3ca7420

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/disjoint_set.jl

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,37 +63,37 @@ end
6363
"""
6464
find_root!(s::IntDisjointSet{T}, x::T)
6565
66-
Find the root element of the subset that contains an member x.
67-
Path compression is implemented here.
66+
Find the root element of the subset that contains an member `x`.
67+
Path compression happens here.
6868
"""
6969
find_root!(s::IntDisjointSet{T}, x::T) where {T<:Integer} = find_root_impl!(s.parents, x)
7070

7171
"""
7272
in_same_set(s::IntDisjointSet{T}, x::T, y::T)
7373
74-
Returns `true` if `x` and `y` belong to the same subset in `s` and `false` otherwise.
74+
Returns `true` if `x` and `y` belong to the same subset in `s`, and `false` otherwise.
7575
"""
7676
in_same_set(s::IntDisjointSet{T}, x::T, y::T) where {T<:Integer} = find_root!(s, x) == find_root!(s, y)
7777

7878
"""
7979
union!(s::IntDisjointSet{T}, x::T, y::T)
8080
81-
Merge the subset containing x and that containing y into one
81+
Merge the subset containing `x` and that containing `y` into one
8282
and return the root of the new set.
8383
"""
8484
function Base.union!(s::IntDisjointSet{T}, x::T, y::T) where {T<:Integer}
8585
parents = s.parents
8686
xroot = find_root_impl!(parents, x)
8787
yroot = find_root_impl!(parents, y)
88-
xroot != yroot ? root_union!(s, xroot, yroot) : xroot
88+
return xroot != yroot ? root_union!(s, xroot, yroot) : xroot
8989
end
9090

9191
"""
9292
root_union!(s::IntDisjointSet{T}, x::T, y::T)
9393
9494
Form a new set that is the union of the two sets whose root elements are
95-
x and y and return the root of the new set.
96-
Assume x ≠ y (unsafe).
95+
`x` and `y` and return the root of the new set.
96+
Assume `x ≠ y` (unsafe).
9797
"""
9898
function root_union!(s::IntDisjointSet{T}, x::T, y::T) where {T<:Integer}
9999
parents = s.parents
@@ -114,7 +114,7 @@ end
114114
"""
115115
push!(s::IntDisjointSet{T})
116116
117-
Make a new subset with an automatically chosen new element x.
117+
Make a new subset with an automatically chosen new element `x`.
118118
Returns the new element. Throw an `ArgumentError` if the
119119
capacity of the set would be exceeded.
120120
"""
@@ -131,9 +131,9 @@ end
131131
"""
132132
DisjointSet{T}(xs)
133133
134-
A forest of disjoint sets of arbitrary value type T.
134+
A forest of disjoint sets of arbitrary value type `T`.
135135
136-
It is a wrapper of IntDisjointSet{Int}, which uses a
136+
It is a wrapper of `IntDisjointSet{Int}`, which uses a
137137
dictionary to map the input value to an internal index.
138138
"""
139139
mutable struct DisjointSet{T} <: AbstractSet{T}
@@ -145,15 +145,15 @@ mutable struct DisjointSet{T} <: AbstractSet{T}
145145
function DisjointSet{T}(xs) where T # xs must be iterable
146146
imap = Dict{T,Int}()
147147
rmap = Vector{T}()
148-
n = length(xs)
148+
n = length(xs)::Int
149149
sizehint!(imap, n)
150150
sizehint!(rmap, n)
151151
id = 0
152152
for x in xs
153153
imap[x] = (id += 1)
154154
push!(rmap,x)
155155
end
156-
new{T}(imap, rmap, IntDisjointSet(n))
156+
return new{T}(imap, rmap, IntDisjointSet(n))
157157
end
158158
end
159159

@@ -188,21 +188,21 @@ end
188188
"""
189189
find_root!{T}(s::DisjointSet{T}, x::T)
190190
191-
Finds the root element of the subset in `s` which has the element `x` as a member.
191+
Find the root element of the subset in `s` which has the element `x` as a member.
192192
"""
193193
find_root!(s::DisjointSet{T}, x::T) where {T} = s.revmap[find_root!(s.internal, s.intmap[x])]
194194

195195
"""
196196
in_same_set(s::DisjointSet{T}, x::T, y::T)
197197
198-
Returns `true` if `x` and `y` belong to the same subset in `s` and `false` otherwise.
198+
Return `true` if `x` and `y` belong to the same subset in `s`, and `false` otherwise.
199199
"""
200200
in_same_set(s::DisjointSet{T}, x::T, y::T) where {T} = in_same_set(s.internal, s.intmap[x], s.intmap[y])
201201

202202
"""
203203
union!(s::DisjointSet{T}, x::T, y::T)
204204
205-
Merge the subset containing x and that containing y into one
205+
Merge the subset containing `x` and that containing `y` into one
206206
and return the root of the new set.
207207
"""
208208
Base.union!(s::DisjointSet{T}, x::T, y::T) where {T} = s.revmap[union!(s.internal, s.intmap[x], s.intmap[y])]
@@ -211,16 +211,15 @@ Base.union!(s::DisjointSet{T}, x::T, y::T) where {T} = s.revmap[union!(s.interna
211211
root_union!(s::DisjointSet{T}, x::T, y::T)
212212
213213
Form a new set that is the union of the two sets whose root elements are
214-
x and y and return the root of the new set.
215-
Assume x ≠ y (unsafe).
214+
`x` and `y` and return the root of the new set.
215+
Assume `x ≠ y` (unsafe).
216216
"""
217217
root_union!(s::DisjointSet{T}, x::T, y::T) where {T} = s.revmap[root_union!(s.internal, s.intmap[x], s.intmap[y])]
218218

219219
"""
220220
push!(s::DisjointSet{T}, x::T)
221221
222-
Make a new subset with an automatically chosen new element x.
223-
Returns the new element.
222+
Make a new subset containing `x` if any existing subset of `s` does not contain `x`.
224223
"""
225224
function Base.push!(s::DisjointSet{T}, x::T) where T
226225
haskey(s.intmap, x) && return x

0 commit comments

Comments
 (0)