Skip to content

Commit d8caff8

Browse files
committed
refactor!: ♻️ rename KNearestNeighbor API to the KNN API
I have renamed the `KNearestNeighbor` API to the `KNN` 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: With the rename of the `KNearestNeighbor` API to the `KNN` API in this commit, any code that uses the `KNearestNeighbor` API will no longer work with future releases of this library created after this commit. Affected codebases must migrate to the `KNN` API to be compatible with future releases of this library.
1 parent 145a479 commit d8caff8

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

kdtree.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,13 @@ func nearestNeighbor[T Comparable[T]](d int, v, nn *T, cd int, r *kdNode[T]) *T
402402
return nn
403403
}
404404

405-
func (t *KDTree[T]) KNearestNeighbor(value T, k int) []*T {
405+
func (t *KDTree[T]) KNN(value T, k int) []*T {
406406
if t == nil || t.root == nil || t.size < k {
407407
return nil
408408
}
409409

410410
pqRes := NewBoundedPriorityQueue[T](k)
411-
kNearestNeighbor(k, t.dimensions, &value, &pqRes, 0, t.root)
411+
knn(k, t.dimensions, &value, &pqRes, 0, t.root)
412412

413413
res := make([]*T, 0, k)
414414
for range k {
@@ -432,7 +432,7 @@ type nodeInfo[T Comparable[T]] struct {
432432
dir direction
433433
}
434434

435-
func kNearestNeighbor[T Comparable[T]](k, d int, v *T, pq *BoundedPriorityQueue[T], cd int, r *kdNode[T]) {
435+
func knn[T Comparable[T]](k, d int, v *T, pq *BoundedPriorityQueue[T], cd int, r *kdNode[T]) {
436436
if r == nil {
437437
return
438438
}
@@ -471,7 +471,7 @@ func kNearestNeighbor[T Comparable[T]](k, d int, v *T, pq *BoundedPriorityQueue[
471471
} else {
472472
next = cn.left
473473
}
474-
kNearestNeighbor(k, d, v, pq, (ncd+1)%d, next)
474+
knn(k, d, v, pq, (ncd+1)%d, next)
475475
}
476476
ncd = (ncd - 1 + d) % d
477477
}

tests/kdtree_2d_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func tensor2DSortFunc(a, b *types.Tensor2D) int {
192192
}
193193
}
194194

195-
func Test2DKNearestNeighbor1(t *testing.T) {
195+
func Test2DKNN1(t *testing.T) {
196196
const dimensions = 2
197197
type input struct {
198198
p types.Tensor2D
@@ -227,7 +227,7 @@ func Test2DKNearestNeighbor1(t *testing.T) {
227227
}
228228
for name, st := range testTable {
229229
t.Run(name, func(t *testing.T) {
230-
nns := tree.KNearestNeighbor(st.input.p, st.input.k)
230+
nns := tree.KNN(st.input.p, st.input.k)
231231
slices.SortFunc(nns, tensor2DSortFunc)
232232
slices.SortFunc(st.expected, tensor2DSortFunc)
233233
for i := range len(st.expected) {

0 commit comments

Comments
 (0)