Skip to content

Commit 6ce3852

Browse files
jainankitkjpountz
authored andcommitted
Minor refactoring in ComponentTree (apache#14474)
1 parent f9bfd45 commit 6ce3852

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

lucene/CHANGES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Build
8787

8888
Other
8989
---------------------
90-
(No changes)
90+
* GITHUB#14474: Minor refactor of ComponentTree (Ankit Jain)
9191

9292
======================= Lucene 10.2.0 =======================
9393

lucene/core/src/java/org/apache/lucene/geo/ComponentTree.java

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
* <p>Construction takes {@code O(n log n)} time for sorting and tree construction.
2727
*/
2828
final class ComponentTree implements Component2D {
29+
30+
private static final Comparator<Component2D> XComparator =
31+
Comparator.comparingDouble(Component2D::getMinX).thenComparingDouble(Component2D::getMaxX);
32+
33+
private static final Comparator<Component2D> YComparator =
34+
Comparator.comparingDouble(Component2D::getMinY).thenComparingDouble(Component2D::getMaxY);
35+
2936
/** minimum Y of this geometry's bounding box area */
3037
private double minY;
3138

@@ -94,9 +101,7 @@ public boolean contains(double x, double y) {
94101
if (right != null
95102
&& ((splitX == false && y >= this.component.getMinY())
96103
|| (splitX && x >= this.component.getMinX()))) {
97-
if (right.contains(x, y)) {
98-
return true;
99-
}
104+
return right.contains(x, y);
100105
}
101106
}
102107
return false;
@@ -124,9 +129,7 @@ public boolean intersectsLine(
124129
if (right != null
125130
&& ((splitX == false && maxY >= this.component.getMinY())
126131
|| (splitX && maxX >= this.component.getMinX()))) {
127-
if (right.intersectsLine(minX, maxX, minY, maxY, aX, aY, bX, bY)) {
128-
return true;
129-
}
132+
return right.intersectsLine(minX, maxX, minY, maxY, aX, aY, bX, bY);
130133
}
131134
}
132135
return false;
@@ -156,9 +159,7 @@ public boolean intersectsTriangle(
156159
if (right != null
157160
&& ((splitX == false && maxY >= this.component.getMinY())
158161
|| (splitX && maxX >= this.component.getMinX()))) {
159-
if (right.intersectsTriangle(minX, maxX, minY, maxY, aX, aY, bX, bY, cX, cY)) {
160-
return true;
161-
}
162+
return right.intersectsTriangle(minX, maxX, minY, maxY, aX, aY, bX, bY, cX, cY);
162163
}
163164
}
164165
return false;
@@ -186,9 +187,7 @@ public boolean containsLine(
186187
if (right != null
187188
&& ((splitX == false && maxY >= this.component.getMinY())
188189
|| (splitX && maxX >= this.component.getMinX()))) {
189-
if (right.containsLine(minX, maxX, minY, maxY, aX, aY, bX, bY)) {
190-
return true;
191-
}
190+
return right.containsLine(minX, maxX, minY, maxY, aX, aY, bX, bY);
192191
}
193192
}
194193
return false;
@@ -218,9 +217,7 @@ public boolean containsTriangle(
218217
if (right != null
219218
&& ((splitX == false && maxY >= this.component.getMinY())
220219
|| (splitX && maxX >= this.component.getMinX()))) {
221-
if (right.containsTriangle(minX, maxX, minY, maxY, aX, aY, bX, bY, cX, cY)) {
222-
return true;
223-
}
220+
return right.containsTriangle(minX, maxX, minY, maxY, aX, aY, bX, bY, cX, cY);
224221
}
225222
}
226223
return false;
@@ -292,9 +289,7 @@ public Relation relate(double minX, double maxX, double minY, double maxY) {
292289
&& ((splitX == false && maxY >= this.component.getMinY())
293290
|| (splitX && maxX >= this.component.getMinX()))) {
294291
relation = right.relate(minX, maxX, minY, maxY);
295-
if (relation != Relation.CELL_OUTSIDE_QUERY) {
296-
return relation;
297-
}
292+
return relation;
298293
}
299294
}
300295
return Relation.CELL_OUTSIDE_QUERY;
@@ -322,27 +317,11 @@ private static ComponentTree createTree(
322317
}
323318
final int mid = (low + high) >>> 1;
324319
if (low < high) {
325-
Comparator<Component2D> comparator;
326320
if (splitX) {
327-
comparator =
328-
(left, right) -> {
329-
int ret = Double.compare(left.getMinX(), right.getMinX());
330-
if (ret == 0) {
331-
ret = Double.compare(left.getMaxX(), right.getMaxX());
332-
}
333-
return ret;
334-
};
321+
ArrayUtil.select(components, low, high + 1, mid, XComparator);
335322
} else {
336-
comparator =
337-
(left, right) -> {
338-
int ret = Double.compare(left.getMinY(), right.getMinY());
339-
if (ret == 0) {
340-
ret = Double.compare(left.getMaxY(), right.getMaxY());
341-
}
342-
return ret;
343-
};
323+
ArrayUtil.select(components, low, high + 1, mid, YComparator);
344324
}
345-
ArrayUtil.select(components, low, high + 1, mid, comparator);
346325
}
347326
ComponentTree newNode = new ComponentTree(components[mid], splitX);
348327
// find children

0 commit comments

Comments
 (0)