Skip to content

Commit

Permalink
fix removal not splicing the right array
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Mar 29, 2017
1 parent d4f9c13 commit 4a80cae
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions build/js/quadtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Quadtree = (function() {
relatedChild = tree.children[direction];
if (tree.width === 1 || tree.height === 1 || isOversized(element, relatedChild.create())) {
tree.oversized.push(element);
} else if (tree.size <= tree.maxElements) {
} else if ((tree.size - tree.oversized.length) <= tree.maxElements) {
tree.contents.push(element);
} else {
if (fifoCandidates[direction] == null) {
Expand Down Expand Up @@ -234,7 +234,7 @@ Quadtree = (function() {
}
index = this.contents.indexOf(item);
if (index > -1) {
this.oversized.splice(index, 1);
this.contents.splice(index, 1);
this.size--;
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions build/js/quadtree.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/demo/quadtree.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/quadtree.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Quadtree
if tree.width is 1 or tree.height is 1 or isOversized element, relatedChild.create()
tree.oversized.push element

else if tree.size <= tree.maxElements
else if (tree.size - tree.oversized.length) <= tree.maxElements
tree.contents.push element

else
Expand Down Expand Up @@ -195,7 +195,7 @@ class Quadtree
return true
index = @contents.indexOf item
if index > - 1
@oversized.splice index, 1
@contents.splice index, 1
@size--
return true

Expand Down
12 changes: 6 additions & 6 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ describe 'quadtree', () ->
quadtree = new Quadtree width: 100, height: 100

elementArray = [
element0 = x: 55, y: 60, width: 10, height: 10,
element1 = x: 60, y: 65, width: 15, height: 10,
element2 = x: 10, y: 10, width: 5, height: 5,
element3 = x: 12, y: 20, width: 5, height: 5,
element4 = x: 49, y: 49 ]
element0 = x: 55, y: 60, width: 10, height: 10, toString: () -> 0,
element1 = x: 60, y: 65, width: 15, height: 10, toString: () -> 1,
element2 = x: 10, y: 10, width: 5, height: 5, toString: () -> 2,
element3 = x: 12, y: 20, width: 5, height: 5, toString: () -> 3,
element4 = x: 49, y: 49, toString: () -> 4 ]

quadtree.pushAll elementArray

Expand All @@ -168,7 +168,7 @@ describe 'quadtree', () ->
element.width > 5

for element in elementArray
quadtree.remove element
assert.equal quadtree.remove(element), true

assert.equal quadtree.size, 0

Expand Down

0 comments on commit 4a80cae

Please sign in to comment.