Skip to content

Commit

Permalink
Removing redundant checks mentioned in algorithm-archivists#765
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJonas committed Oct 13, 2020
1 parent bc738e2 commit ae40c50
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
21 changes: 8 additions & 13 deletions contents/flood_fill/code/javascript/flood_fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ function isInBounds(canvas, x, y) {
return (x >= 0) && (x < canvas[0].length) && (y >= 0) && (y < canvas.length)
}

function color(canvas, x, y, oldColor, newColor) {
if (isInBounds(canvas, x, y) && canvas[y][x] == oldColor)
canvas[y][x] = newColor
}

function findNeighbors(canvas, x, y, oldColor) {
const allNeighbors = [
[x, y - 1], // North
Expand All @@ -27,10 +22,10 @@ function stackFill(canvas, x, y, oldColor, newColor) {
]

while (stack.length > 0) {
const currentLoc = stack.pop()
color(canvas, ...currentLoc, oldColor, newColor)
const [x, y] = stack.pop()
canvas[y][x] = newColor

for(const n of findNeighbors(canvas, ...currentLoc, oldColor))
for(const n of findNeighbors(canvas, x, y, oldColor))
stack.push(n)
}
}
Expand All @@ -41,20 +36,20 @@ function queueFill(canvas, x, y, oldColor, newColor) {
]

while (queue.length > 0) {
const currentLoc = queue.shift()
color(canvas, ...currentLoc, oldColor, newColor)
const [x, y] = queue.shift()
canvas[y][x] = newColor

for (const n of findNeighbors(canvas, ...currentLoc, oldColor)) {
for (const n of findNeighbors(canvas, x, y, oldColor)) {
// Color neighbor pixel before enqueuing to prevent
// it from being colored multiple times
color(canvas, ...n, oldColor, newColor)
canvas[n[1]][n[0]] = newColor
queue.push(n)
}
}
}

function recursiveFill(canvas, x, y, oldColor, newColor) {
color(canvas, x, y, oldColor, newColor)
canvas[y][x] = newColor

for(const n of findNeighbors(canvas, x, y, oldColor))
recursiveFill(canvas, ...n, oldColor, newColor)
Expand Down
19 changes: 4 additions & 15 deletions contents/flood_fill/flood_fill.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ In code, this might look like this:
{% sample lang="c" %}
[import:34-52, lang:"c"](code/c/flood_fill.c)
{% sample lang="js" %}
[import:11-22, lang:"javascript"](code/javascript/flood_fill.js)
[import:6-17, lang:"javascript"](code/javascript/flood_fill.js)
{% endmethod %}


Expand All @@ -109,18 +109,7 @@ In code, it might look like this:
{% sample lang="c" %}
[import:180-195, lang:"c"](code/c/flood_fill.c)
{% sample lang="js" %}
[import:56-61, lang:"javascript"](code/javascript/flood_fill.js)
{% endmethod %}

All code snippets for this chapter rely on an exterior `color` function, defined as

{% method %}
{% sample lang="jl" %}
[import:23-35, lang:"julia"](code/julia/flood_fill.jl)
{% sample lang="c" %}
[import:28-32, lang:"c"](code/c/flood_fill.c)
{% sample lang="js" %}
[import:6-9, lang:"javascript"](code/javascript/flood_fill.js)
[import:51-56, lang:"javascript"](code/javascript/flood_fill.js)
{% endmethod %}

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.
Expand All @@ -133,7 +122,7 @@ Additionally, it is possible to do the same type of traversal by managing a stac
{% sample lang="c" %}
[import:85-108, lang:"c"](code/c/flood_fill.c)
{% sample lang="js" %}
[import:24-36, lang:"javascript"](code/javascript/flood_fill.js)
[import:19-31, lang:"javascript"](code/javascript/flood_fill.js)
{% endmethod %}

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:
Expand Down Expand Up @@ -174,7 +163,7 @@ The code would look something like this:
{% sample lang="c" %}
[import:155-178, lang:"c"](code/c/flood_fill.c)
{% sample lang="js" %}
[import:38-54, lang:"javascript"](code/javascript/flood_fill.js)
[import:33-49, lang:"javascript"](code/javascript/flood_fill.js)
{% endmethod %}

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

0 comments on commit ae40c50

Please sign in to comment.