63
63
"""
64
64
find_root!(s::IntDisjointSet{T}, x::T)
65
65
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.
68
68
"""
69
69
find_root! (s:: IntDisjointSet{T} , x:: T ) where {T<: Integer } = find_root_impl! (s. parents, x)
70
70
71
71
"""
72
72
in_same_set(s::IntDisjointSet{T}, x::T, y::T)
73
73
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.
75
75
"""
76
76
in_same_set (s:: IntDisjointSet{T} , x:: T , y:: T ) where {T<: Integer } = find_root! (s, x) == find_root! (s, y)
77
77
78
78
"""
79
79
union!(s::IntDisjointSet{T}, x::T, y::T)
80
80
81
- Merge the subset containing x and that containing y into one
81
+ Merge the subset containing `x` and that containing `y` into one
82
82
and return the root of the new set.
83
83
"""
84
84
function Base. union! (s:: IntDisjointSet{T} , x:: T , y:: T ) where {T<: Integer }
85
85
parents = s. parents
86
86
xroot = find_root_impl! (parents, x)
87
87
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
89
89
end
90
90
91
91
"""
92
92
root_union!(s::IntDisjointSet{T}, x::T, y::T)
93
93
94
94
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).
97
97
"""
98
98
function root_union! (s:: IntDisjointSet{T} , x:: T , y:: T ) where {T<: Integer }
99
99
parents = s. parents
114
114
"""
115
115
push!(s::IntDisjointSet{T})
116
116
117
- Make a new subset with an automatically chosen new element x .
117
+ Make a new subset with an automatically chosen new element `x` .
118
118
Returns the new element. Throw an `ArgumentError` if the
119
119
capacity of the set would be exceeded.
120
120
"""
131
131
"""
132
132
DisjointSet{T}(xs)
133
133
134
- A forest of disjoint sets of arbitrary value type T .
134
+ A forest of disjoint sets of arbitrary value type `T` .
135
135
136
- It is a wrapper of IntDisjointSet{Int}, which uses a
136
+ It is a wrapper of ` IntDisjointSet{Int}` , which uses a
137
137
dictionary to map the input value to an internal index.
138
138
"""
139
139
mutable struct DisjointSet{T} <: AbstractSet{T}
@@ -145,15 +145,15 @@ mutable struct DisjointSet{T} <: AbstractSet{T}
145
145
function DisjointSet {T} (xs) where T # xs must be iterable
146
146
imap = Dict {T,Int} ()
147
147
rmap = Vector {T} ()
148
- n = length (xs)
148
+ n = length (xs):: Int
149
149
sizehint! (imap, n)
150
150
sizehint! (rmap, n)
151
151
id = 0
152
152
for x in xs
153
153
imap[x] = (id += 1 )
154
154
push! (rmap,x)
155
155
end
156
- new {T} (imap, rmap, IntDisjointSet (n))
156
+ return new {T} (imap, rmap, IntDisjointSet (n))
157
157
end
158
158
end
159
159
@@ -188,21 +188,21 @@ end
188
188
"""
189
189
find_root!{T}(s::DisjointSet{T}, x::T)
190
190
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.
192
192
"""
193
193
find_root! (s:: DisjointSet{T} , x:: T ) where {T} = s. revmap[find_root! (s. internal, s. intmap[x])]
194
194
195
195
"""
196
196
in_same_set(s::DisjointSet{T}, x::T, y::T)
197
197
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.
199
199
"""
200
200
in_same_set (s:: DisjointSet{T} , x:: T , y:: T ) where {T} = in_same_set (s. internal, s. intmap[x], s. intmap[y])
201
201
202
202
"""
203
203
union!(s::DisjointSet{T}, x::T, y::T)
204
204
205
- Merge the subset containing x and that containing y into one
205
+ Merge the subset containing `x` and that containing `y` into one
206
206
and return the root of the new set.
207
207
"""
208
208
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
211
211
root_union!(s::DisjointSet{T}, x::T, y::T)
212
212
213
213
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).
216
216
"""
217
217
root_union! (s:: DisjointSet{T} , x:: T , y:: T ) where {T} = s. revmap[root_union! (s. internal, s. intmap[x], s. intmap[y])]
218
218
219
219
"""
220
220
push!(s::DisjointSet{T}, x::T)
221
221
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`.
224
223
"""
225
224
function Base. push! (s:: DisjointSet{T} , x:: T ) where T
226
225
haskey (s. intmap, x) && return x
0 commit comments