Skip to content

Commit b67dcaa

Browse files
author
Bytekeeper
committed
Boundary case fixes.
1 parent 00ecdf6 commit b67dcaa

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ private Pivot partitionX(int start, int end) {
358358
if (pos(mid).x < pos(end).x) swap(mid, end);
359359
int pivot = pos(end).x;
360360
while (true) {
361-
while (pos(start).x <= pivot) start++;
361+
while (start < end && pos(start).x <= pivot) start++;
362362
while (pos(end).x > pivot) end--;
363363
if (start >= end) return new Pivot(pivot, end);
364364
swap(start, end);
@@ -374,7 +374,7 @@ private Pivot partitionY(int start, int end) {
374374
if (pos(mid).y < pos(end).y) swap(mid, end);
375375
int pivot = pos(end).y;
376376
while (true) {
377-
while (pos(start).y < pivot) start++;
377+
while (start < end && pos(start).y <= pivot) start++;
378378
while (pos(end).y > pivot) end--;
379379
if (start >= end) return new Pivot(pivot, end);
380380
swap(start, end);

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
import java.util.*;
88
import java.util.function.Function;
99
import java.util.stream.Collectors;
10+
import java.util.stream.IntStream;
1011

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

1516
class PositionQueriesTest {
16-
private static Collection<Position> positions;
17+
private static List<Position> positions;
1718

1819
@BeforeAll
1920
static void setup() {
@@ -26,18 +27,35 @@ static void setup() {
2627

2728
@Test
2829
void shouldFindNearest() {
30+
for (int i = 3; i < positions.size(); i = i * 14 / 10) {
31+
// GIVEN
32+
List<Position> testList = PositionQueriesTest.positions.subList(0, i);
33+
PositionQueries<Position> tree = new PositionQueries<>(testList, Function.identity());
34+
35+
// WHEN
36+
Position nearest = tree.nearest(500, 500);
37+
38+
// THEN
39+
Position actualNearest =
40+
testList.stream()
41+
.min(Comparator.comparingInt(a -> EUCLIDEAN_DISTANCE.distance(a.x, a.y, 500, 500)))
42+
.orElseThrow(RuntimeException::new);
43+
assertThat(nearest).isEqualTo(actualNearest);
44+
}
45+
}
46+
47+
@Test
48+
void shouldPivotMatchingToOneSide() {
2949
// GIVEN
50+
List<Position> positions =
51+
IntStream.range(0, 30).mapToObj(it -> new Position(0, it)).collect(Collectors.toList());
3052
PositionQueries<Position> tree = new PositionQueries<>(positions, Function.identity());
3153

3254
// WHEN
33-
Position nearest = tree.nearest(500, 500);
55+
Collection<Position> result = tree.inArea(0, 0, 0, 150);
3456

3557
// THEN
36-
Position actualNearest =
37-
positions.stream()
38-
.min(Comparator.comparingInt(a -> EUCLIDEAN_DISTANCE.distance(a.x, a.y, 500, 500)))
39-
.orElseThrow(RuntimeException::new);
40-
assertThat(nearest).isEqualTo(actualNearest);
58+
assertThat(result).containsExactlyInAnyOrderElementsOf(positions);
4159
}
4260

4361
@Test

0 commit comments

Comments
 (0)