Skip to content

Commit 3260b6e

Browse files
committed
refactor!: ♻️ rename Query API to the RangeSearch API
I have renamed the `Query` API to the `RangeSearch` API. I made this change to make the APIs of my K-D Tree library consistent with those of existing ones written in Go. BREAKING CHANGE: This commit renames the `Query` API to the `RangeSearch` API. Any code that uses the `Query` API will no longer work with future releases of this library created after this commit. Affected codebases must migrate to the `RangeSearch` API to be compatible with future releases of this library.
1 parent d8caff8 commit 3260b6e

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

kdtree.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ func (t *KDTree[T]) NearestNeighbor(value T) (T, bool) {
9999
return *res, true
100100
}
101101

102-
func (t *KDTree[T]) Query(getRelativePosition RangeFunc[T]) []T {
102+
func (t *KDTree[T]) RangeSearch(getRelativePosition RangeFunc[T]) []T {
103103
var res []T
104-
query(getRelativePosition, t.dimensions, &res, t.root, 0)
104+
rangeSearch(getRelativePosition, t.dimensions, &res, t.root, 0)
105105
return res
106106
}
107107

@@ -222,7 +222,7 @@ func (t *KDTree[T]) Balance() {
222222
t.root = NewKDTreeWithValues(t.dimensions, t.Values()).root
223223
}
224224

225-
func query[T Comparable[T]](getRelativePosition RangeFunc[T], d int, res *[]T, r *kdNode[T], cd int) {
225+
func rangeSearch[T Comparable[T]](getRelativePosition RangeFunc[T], d int, res *[]T, r *kdNode[T], cd int) {
226226
if r == nil {
227227
return
228228
}
@@ -235,12 +235,12 @@ func query[T Comparable[T]](getRelativePosition RangeFunc[T], d int, res *[]T, r
235235
ncd := (cd + 1) % d
236236
switch relInCD := getRelativePosition(r.value, cd); relInCD {
237237
case BeforeRange:
238-
query(getRelativePosition, d, res, r.right, ncd)
238+
rangeSearch(getRelativePosition, d, res, r.right, ncd)
239239
case AfterRange:
240-
query(getRelativePosition, d, res, r.left, ncd)
240+
rangeSearch(getRelativePosition, d, res, r.left, ncd)
241241
case InRange:
242-
query(getRelativePosition, d, res, r.left, ncd)
243-
query(getRelativePosition, d, res, r.right, ncd)
242+
rangeSearch(getRelativePosition, d, res, r.left, ncd)
243+
rangeSearch(getRelativePosition, d, res, r.right, ncd)
244244
default:
245245
panic(fmt.Sprintf("Invalid value returned: %v", relInCD))
246246
}

tests/kdtree_2d_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ func Test2DFindMax2(t *testing.T) {
438438
}
439439
}
440440

441-
func Test2DTree_Query(t *testing.T) {
441+
func Test2DTree_RangeSearch(t *testing.T) {
442442
const dimensions = 2
443443
inputTensor2D := []types.Tensor2D{{1, 0}, {1, 8}, {2, 2}, {2, 10}, {3, 4}, {4, 1}, {5, 4}, {6, 8}, {7, 4}, {7, 7}, {8, 2}, {8, 5}, {9, 9}, {3, 6}, {4, 2}, {9, 2}, {6, 5}, {3, 8}, {6, 2}, {1, 3}, {3, 3}, {6, 4}, {9, 8}, {2, 1}, {2, 8}, {3, 1}, {7, 3}, {3, 9}, {4, 4}, {5, 3}, {9, 6}}
444444
tests := []struct {
@@ -605,7 +605,7 @@ func Test2DTree_Query(t *testing.T) {
605605
}
606606
for _, test := range tests {
607607
t.Run(test.name, func(t *testing.T) {
608-
assert.Equal(t, test.expected, test.tree.Query(test.input))
608+
assert.Equal(t, test.expected, test.tree.RangeSearch(test.input))
609609
})
610610
}
611611
}

0 commit comments

Comments
 (0)