Skip to content

Commit fee7e83

Browse files
authored
Remove color function from flood fill because it is redundant (#766)
1 parent 627aed9 commit fee7e83

File tree

4 files changed

+23
-57
lines changed

4 files changed

+23
-57
lines changed

contents/flood_fill/code/c/flood_fill.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ int inbounds(struct point p, struct canvas c) {
2525
return (p.x < 0 || p.y < 0 || p.y >= c.max_y || p.x >= c.max_x) ? 0 : 1;
2626
}
2727

28-
void color(struct canvas c, struct point p, int old_val, int new_val) {
29-
if (inbounds(p, c) && c.data[p.x + c.max_x * p.y] == old_val) {
30-
c.data[p.x + c.max_x * p.y] = new_val;
31-
}
32-
}
33-
3428
int find_neighbors(struct canvas c, struct point p, int old_val, int new_val,
3529
struct point *neighbors) {
3630
int cnt = 0;
@@ -93,7 +87,7 @@ void stack_fill(struct canvas c, struct point p, int old_val, int new_val) {
9387
while (!stack_empty(stk)) {
9488
struct point cur_loc = stack_pop(&stk);
9589
if (c.data[cur_loc.x + c.max_x * cur_loc.y] == old_val) {
96-
color(c, cur_loc, old_val, new_val);
90+
c.data[cur_loc.x + c.max_x * cur_loc.y] = new_val;
9791

9892
struct point neighbors[4];
9993
int cnt = find_neighbors(c, cur_loc, old_val, new_val, neighbors);
@@ -163,7 +157,7 @@ void queue_fill(struct canvas c, struct point p, int old_val, int new_val) {
163157
while (!queue_empty(q)) {
164158
struct point cur_loc = dequeue(&q);
165159
if (c.data[cur_loc.x + c.max_x * cur_loc.y] == old_val) {
166-
color(c, cur_loc, old_val, new_val);
160+
c.data[cur_loc.x + c.max_x * cur_loc.y] = new_val;
167161

168162
struct point neighbors[4];
169163
int cnt = find_neighbors(c, cur_loc, old_val, new_val, neighbors);
@@ -184,7 +178,7 @@ void recursive_fill(struct canvas c, struct point p, int old_val,
184178
return;
185179
}
186180

187-
color(c, p, old_val, new_val);
181+
c.data[p.x + c.max_x * p.y] = new_val;
188182

189183
struct point neighbors[4];
190184
int cnt = find_neighbors(c, p, old_val, new_val, neighbors);

contents/flood_fill/code/julia/flood_fill.jl

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@ function inbounds(canvas_size, loc)
2020
end
2121
end
2222

23-
function color!(canvas, loc::CartesianIndex, old_val, new_val)
24-
# bounds check, do not color if no in bounds!
25-
if !inbounds(size(canvas), loc)
26-
return
27-
end
28-
29-
# Only change color if the element value is the old value
30-
if (canvas[loc] != old_val)
31-
return
32-
else
33-
canvas[loc] = new_val
34-
end
35-
end
36-
3723
function find_neighbors(canvas, loc::CartesianIndex, old_val, new_val)
3824

3925
# Finding north, south, east, west neighbors
@@ -65,7 +51,7 @@ function stack_fill!(canvas, loc::CartesianIndex, old_val, new_val)
6551
while length(s) > 0
6652
current_loc = pop!(s)
6753
if canvas[current_loc] == old_val
68-
color!(canvas, current_loc, old_val, new_val)
54+
canvas[current_loc] = new_val
6955
possible_neighbors = find_neighbors(canvas, current_loc,
7056
old_val, new_val)
7157
for neighbor in possible_neighbors
@@ -86,7 +72,7 @@ function queue_fill!(canvas, loc::CartesianIndex, old_val, new_val)
8672
enqueue!(q, loc)
8773

8874
# Coloring the initial location
89-
color!(canvas, loc, old_val, new_val)
75+
canvas[loc] = new_val
9076

9177
while length(q) > 0
9278
current_loc = dequeue!(q)
@@ -96,7 +82,7 @@ function queue_fill!(canvas, loc::CartesianIndex, old_val, new_val)
9682

9783
# Coloring as we are enqueuing neighbors
9884
for neighbor in possible_neighbors
99-
color!(canvas, neighbor, old_val, new_val)
85+
canvas[neighbor] = new_val
10086
enqueue!(q,neighbor)
10187
end
10288

@@ -109,7 +95,7 @@ function recursive_fill!(canvas, loc::CartesianIndex, old_val, new_val)
10995
return
11096
end
11197

112-
color!(canvas, loc, old_val, new_val)
98+
canvas[loc] = new_val
11399

114100
possible_neighbors = find_neighbors(canvas, loc, old_val, new_val)
115101
for possible_neighbor in possible_neighbors

contents/flood_fill/code/python/flood_fill.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
def inbounds(canvas_shape, p):
88
return min(p) >= 0 and p.x < canvas_shape[0] and p.y < canvas_shape[1]
99

10-
def color(canvas, p, new_val):
11-
canvas[p] = new_val
12-
1310
def find_neighbors(canvas, p, old_val, new_val):
1411
# north, south, east, west neighbors
1512
possible_neighbors = [
@@ -35,7 +32,7 @@ def stack_fill(canvas, p, old_val, new_val):
3532

3633
while stack:
3734
cur_loc = stack.pop()
38-
color(canvas, cur_loc, new_val)
35+
canvas[cur_loc] = new_val
3936
stack += find_neighbors(canvas, cur_loc, old_val, new_val)
4037

4138
def queue_fill(canvas, p, old_val, new_val):
@@ -45,21 +42,21 @@ def queue_fill(canvas, p, old_val, new_val):
4542
q = Queue()
4643
q.put(p)
4744

48-
color(canvas, p, new_val)
45+
canvas[p] = new_val
4946

5047
while not q.empty():
5148
cur_loc = q.get()
5249
neighbors = find_neighbors(canvas, cur_loc, old_val, new_val)
5350

5451
for neighbor in neighbors:
55-
color(canvas, neighbor, new_val)
52+
canvas[neighbor] = new_val
5653
q.put(neighbor)
5754

5855
def recursive_fill(canvas, p, old_val, new_val):
5956
if old_val == new_val:
6057
return
6158

62-
color(canvas, p, new_val)
59+
canvas[p] = new_val
6360

6461
neighbors = find_neighbors(canvas, p, old_val, new_val)
6562
for neighbor in neighbors:

contents/flood_fill/flood_fill.md

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ In code, this might look like this:
8787

8888
{% method %}
8989
{% sample lang="jl" %}
90-
[import:37-55, lang:"julia"](code/julia/flood_fill.jl)
90+
[import:23-41, lang:"julia"](code/julia/flood_fill.jl)
9191
{% sample lang="c" %}
92-
[import:34-52, lang:"c"](code/c/flood_fill.c)
92+
[import:28-46, lang:"c"](code/c/flood_fill.c)
9393
{% sample lang="py" %}
94-
[import:13-28, lang="python"](code/python/flood_fill.py)
94+
[import:10-25, lang="python"](code/python/flood_fill.py)
9595
{% endmethod %}
9696

9797

@@ -105,22 +105,11 @@ In code, it might look like this:
105105

106106
{% method %}
107107
{% sample lang="jl" %}
108-
[import:106-118, lang:"julia"](code/julia/flood_fill.jl)
108+
[import:92-104, lang:"julia"](code/julia/flood_fill.jl)
109109
{% sample lang="c" %}
110-
[import:180-195, lang:"c"](code/c/flood_fill.c)
110+
[import:174-189, lang:"c"](code/c/flood_fill.c)
111111
{% sample lang="py" %}
112-
[import:58-66, lang="python"](code/python/flood_fill.py)
113-
{% endmethod %}
114-
115-
All code snippets for this chapter rely on an exterior `color` function, defined as
116-
117-
{% method %}
118-
{% sample lang="jl" %}
119-
[import:23-35, lang:"julia"](code/julia/flood_fill.jl)
120-
{% sample lang="c" %}
121-
[import:28-32, lang:"c"](code/c/flood_fill.c)
122-
{% sample lang="py" %}
123-
[import:10-11, lang="python"](code/python/flood_fill.py)
112+
[import:55-63, lang="python"](code/python/flood_fill.py)
124113
{% endmethod %}
125114

126115
The above code continues recursing through available neighbors as long as neighbors exist, and this should work so long as we are adding the correct set of neighbors.
@@ -129,11 +118,11 @@ Additionally, it is possible to do the same type of traversal by managing a stac
129118

130119
{% method %}
131120
{% sample lang="jl" %}
132-
[import:57-77, lang:"julia"](code/julia/flood_fill.jl)
121+
[import:43-63, lang:"julia"](code/julia/flood_fill.jl)
133122
{% sample lang="c" %}
134-
[import:85-108, lang:"c"](code/c/flood_fill.c)
123+
[import:79-102, lang:"c"](code/c/flood_fill.c)
135124
{% sample lang="py" %}
136-
[import:30-39, lang="python"](code/python/flood_fill.py)
125+
[import:27-36, lang="python"](code/python/flood_fill.py)
137126
{% endmethod %}
138127

139128
This is ultimately the same method of traversal as before; however, because we are managing our own data structure, there are a few distinct differences:
@@ -170,11 +159,11 @@ The code would look something like this:
170159

171160
{% method %}
172161
{% sample lang="jl" %}
173-
[import:80-104, lang:"julia"](code/julia/flood_fill.jl)
162+
[import:66-90, lang:"julia"](code/julia/flood_fill.jl)
174163
{% sample lang="c" %}
175-
[import:155-178, lang:"c"](code/c/flood_fill.c)
164+
[import:149-172, lang:"c"](code/c/flood_fill.c)
176165
{% sample lang="py" %}
177-
[import:41-56, lang="python"](code/python/flood_fill.py)
166+
[import:38-53, lang="python"](code/python/flood_fill.py)
178167
{% endmethod %}
179168

180169
Now, there is a small trick in this code that must be considered to make sure it runs optimally.

0 commit comments

Comments
 (0)