Skip to content

Commit 172fd12

Browse files
authored
Reduce unnecessary indexing in coloring.jl (#251)
1 parent d9ec840 commit 172fd12

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

src/coloring.jl

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ function partial_distance2_coloring!(
3838
for v in vertices_in_order
3939
for w in neighbors(bg, Val(side), v)
4040
for x in neighbors(bg, Val(other_side), w)
41-
if !iszero(color[x])
42-
forbidden_colors[color[x]] = v
41+
color_x = color[x]
42+
if !iszero(color_x)
43+
forbidden_colors[color_x] = v
4344
end
4445
end
4546
end
@@ -91,9 +92,10 @@ function star_coloring(
9192
for v in vertices_in_order
9293
for (w, index_vw) in neighbors_with_edge_indices(g, v)
9394
!has_diagonal(g) || (v == w && continue)
94-
iszero(color[w]) && continue
95-
forbidden_colors[color[w]] = v
96-
(p, q, _) = first_neighbor[color[w]]
95+
color_w = color[w]
96+
iszero(color_w) && continue
97+
forbidden_colors[color_w] = v
98+
(p, q, _) = first_neighbor[color_w]
9799
if p == v # Case 1
98100
if treated[q] != v
99101
# forbid colors of neighbors of q
@@ -105,9 +107,10 @@ function star_coloring(
105107
first_neighbor[color[w]] = (v, w, index_vw)
106108
for (x, index_wx) in neighbors_with_edge_indices(g, w)
107109
!has_diagonal(g) || (w == x && continue)
108-
(x == v || iszero(color[x])) && continue
110+
color_x = color[x]
111+
(x == v || iszero(color_x)) && continue
109112
if x == hub[star[index_wx]] # potential Case 2 (which is always false for trivial stars with two vertices, since the associated hub is negative)
110-
forbidden_colors[color[x]] = v
113+
forbidden_colors[color_x] = v
111114
end
112115
end
113116
end
@@ -141,8 +144,9 @@ function _treat!(
141144
)
142145
for x in neighbors(g, w)
143146
!has_diagonal(g) || (w == x && continue)
144-
iszero(color[x]) && continue
145-
forbidden_colors[color[x]] = v
147+
color_x = color[x]
148+
iszero(color_x) && continue
149+
forbidden_colors[color_x] = v
146150
end
147151
treated[w] = v
148152
return nothing
@@ -160,7 +164,8 @@ function _update_stars!(
160164
)
161165
for (w, index_vw) in neighbors_with_edge_indices(g, v)
162166
!has_diagonal(g) || (v == w && continue)
163-
iszero(color[w]) && continue
167+
color_w = color[w]
168+
iszero(color_w) && continue
164169
x_exists = false
165170
for (x, index_wx) in neighbors_with_edge_indices(g, w)
166171
!has_diagonal(g) || (w == x && continue)
@@ -173,7 +178,7 @@ function _update_stars!(
173178
end
174179
end
175180
if !x_exists
176-
(p, q, index_pq) = first_neighbor[color[w]]
181+
(p, q, index_pq) = first_neighbor[color_w]
177182
if p == v && q != w # vw, vq ∈ E and color[w] = color[q]
178183
star_vq = star[index_pq]
179184
hub[star_vq] = v # this may already be true
@@ -241,22 +246,24 @@ function acyclic_coloring(
241246
for v in vertices_in_order
242247
for w in neighbors(g, v)
243248
!has_diagonal(g) || (v == w && continue)
244-
iszero(color[w]) && continue
245-
forbidden_colors[color[w]] = v
249+
color_w = color[w]
250+
iszero(color_w) && continue
251+
forbidden_colors[color_w] = v
246252
end
247253
for w in neighbors(g, v)
248254
!has_diagonal(g) || (v == w && continue)
249255
iszero(color[w]) && continue
250256
for (x, index_wx) in neighbors_with_edge_indices(g, w)
251257
!has_diagonal(g) || (w == x && continue)
252-
iszero(color[x]) && continue
253-
if forbidden_colors[color[x]] != v
258+
color_x = color[x]
259+
iszero(color_x) && continue
260+
if forbidden_colors[color_x] != v
254261
_prevent_cycle!(
255262
v,
256263
w,
257264
x,
258265
index_wx,
259-
color,
266+
color_x,
260267
first_visit_to_tree,
261268
forbidden_colors,
262269
forest,
@@ -272,16 +279,18 @@ function acyclic_coloring(
272279
end
273280
for (w, index_vw) in neighbors_with_edge_indices(g, v) # grow two-colored stars around the vertex v
274281
!has_diagonal(g) || (v == w && continue)
275-
iszero(color[w]) && continue
276-
_grow_star!(v, w, index_vw, color, first_neighbor, forest)
282+
color_w = color[w]
283+
iszero(color_w) && continue
284+
_grow_star!(v, w, index_vw, color_w, first_neighbor, forest)
277285
end
278286
for (w, index_vw) in neighbors_with_edge_indices(g, v)
279287
!has_diagonal(g) || (v == w && continue)
280288
iszero(color[w]) && continue
281289
for (x, index_wx) in neighbors_with_edge_indices(g, w)
282290
!has_diagonal(g) || (w == x && continue)
283-
(x == v || iszero(color[x])) && continue
284-
if color[x] == color[v]
291+
color_x = color[x]
292+
(x == v || iszero(color_x)) && continue
293+
if color_x == color[v]
285294
_merge_trees!(v, w, x, index_vw, index_wx, forest) # merge trees T₁ ∋ vw and T₂ ∋ wx if T₁ != T₂
286295
end
287296
end
@@ -305,7 +314,7 @@ function _prevent_cycle!(
305314
w::Integer,
306315
x::Integer,
307316
index_wx::Integer,
308-
color::AbstractVector{<:Integer},
317+
color_x::Integer,
309318
# modified
310319
first_visit_to_tree::AbstractVector{<:Tuple},
311320
forbidden_colors::AbstractVector{<:Integer},
@@ -316,7 +325,7 @@ function _prevent_cycle!(
316325
if p != v # T is being visited from vertex v for the first time
317326
first_visit_to_tree[root_wx] = (v, w)
318327
elseif q != w # T is connected to vertex v via at least two edges
319-
forbidden_colors[color[x]] = v
328+
forbidden_colors[color_x] = v
320329
end
321330
return nothing
322331
end
@@ -326,15 +335,15 @@ function _grow_star!(
326335
v::Integer,
327336
w::Integer,
328337
index_vw::Integer,
329-
color::AbstractVector{<:Integer},
338+
color_w::Integer,
330339
# modified
331340
first_neighbor::AbstractVector{<:Tuple},
332341
forest::Forest{<:Integer},
333342
)
334343
# Create a new tree T_{vw} consisting only of edge vw
335-
(p, q, index_pq) = first_neighbor[color[w]]
344+
(p, q, index_pq) = first_neighbor[color_w]
336345
if p != v # a neighbor of v with color[w] encountered for the first time
337-
first_neighbor[color[w]] = (v, w, index_vw)
346+
first_neighbor[color_w] = (v, w, index_vw)
338347
else # merge T_{vw} with a two-colored star being grown around v
339348
root_vw = find_root!(forest, index_vw)
340349
root_pq = find_root!(forest, index_pq)

0 commit comments

Comments
 (0)