Skip to content

Commit ee01243

Browse files
author
Bytekeeper
committed
Added PositionQueries.add()
1 parent 6cffcb1 commit ee01243

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

src/main/java/org/bk/ass/query/PositionQueries.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,23 @@ public int size() {
8181
}
8282

8383
/**
84-
* Removes the given element, the internal data structure will not be changed - queries will
85-
* therefore not be faster.
84+
* Adds the given element, the internal data structure will not be changed - query performance
85+
* will suffer.
8686
*/
8787
@Override
88-
public boolean remove(Object o) {
88+
public boolean add(U u) {
89+
Node node = findNodeFor(u);
90+
Object[] newValues = Arrays.copyOf(node.values, node.values.length + 1);
91+
newValues[node.values.length] = u;
92+
node.values = newValues;
93+
return true;
94+
}
95+
96+
private Node findNodeFor(Object o) {
8997
Node current = root;
9098
boolean xDim = true;
9199
while (current != null) {
92-
if (current.values != null && current.values.length > 0) {
93-
Object[] replacement = new Object[current.values.length - 1];
94-
int i;
95-
for (i = 0; i < replacement.length && !Objects.equals(current.values[i], o); i++) {
96-
replacement[i] = current.values[i];
97-
}
98-
if (i == replacement.length) return false;
99-
for (; i < replacement.length; i++) {
100-
replacement[i] = current.values[i + 1];
101-
}
102-
current.values = replacement;
103-
size--;
104-
return true;
105-
}
100+
if (current.values != null) return current;
106101
Position pos = positionExtractor.apply((U) o);
107102
if (xDim && pos.x <= current.p || !xDim && pos.y <= current.p) {
108103
current = current.left;
@@ -111,6 +106,30 @@ public boolean remove(Object o) {
111106
}
112107
xDim = !xDim;
113108
}
109+
throw new IllegalStateException("No node with values found!");
110+
}
111+
112+
/**
113+
* Removes the given element, the internal data structure will not be changed - queries will
114+
* therefore not be faster.
115+
*/
116+
@Override
117+
public boolean remove(Object o) {
118+
Node node = findNodeFor(o);
119+
if (node.values.length > 0) {
120+
Object[] replacement = new Object[node.values.length - 1];
121+
int i;
122+
for (i = 0; i < replacement.length && !Objects.equals(node.values[i], o); i++) {
123+
replacement[i] = node.values[i];
124+
}
125+
if (i == replacement.length) return false;
126+
for (; i < replacement.length; i++) {
127+
replacement[i] = node.values[i + 1];
128+
}
129+
node.values = replacement;
130+
size--;
131+
return true;
132+
}
114133
return false;
115134
}
116135

src/test/java/org/bk/ass/query/PositionQueriesTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.function.Function;
99
import java.util.stream.Collectors;
1010

11+
import static java.util.Collections.emptyList;
1112
import static org.assertj.core.api.Assertions.assertThat;
1213
import static org.bk.ass.query.Distances.EUCLIDEAN_DISTANCE;
1314

@@ -100,7 +101,7 @@ void shouldNotDieIfEmpty() {
100101
// GIVEN
101102
PositionQueries<Object> finder =
102103
new PositionQueries<>(
103-
Collections.emptyList(),
104+
emptyList(),
104105
o -> {
105106
throw new IllegalStateException("On what object are you calling me?");
106107
});
@@ -207,6 +208,18 @@ void shouldFindAllInRadiusOfUnit() {
207208
assertThat(result).hasSize(5);
208209
}
209210

211+
@Test
212+
void shouldAddItem() {
213+
// GIVEN
214+
PositionQueries<Position> queries = new PositionQueries<>(emptyList(), Function.identity());
215+
216+
// WHEN
217+
queries.add(new Position(0, 0));
218+
219+
// THEN
220+
assertThat(queries).containsExactly(new Position(0, 0));
221+
}
222+
210223
@Test
211224
void shouldRemoveItem() {
212225
// GIVEN

0 commit comments

Comments
 (0)