Skip to content

Commit 37d47f0

Browse files
committed
refactor!: ♻️ change return type of KNN API from []*T to []T
I have changed the return type of `KNN` API from `[]*T` to `[]T`. 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 change of the return type of the `KNN` API from `[]*T` to `[]T` in this commit, any code that uses the `KNN` API will no longer work with future releases of this library created after this commit. Affected codebases must migrate to the updated version of the `KNN` API to be compatible with future releases of this library.
1 parent 422b80d commit 37d47f0

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

kdtree.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,18 +451,18 @@ func nearestNeighbor[T Comparable[T]](d int, v, nn *T, cd int, r *kdNode[T]) *T
451451
return nn
452452
}
453453

454-
func (t *KDTree[T]) KNN(value T, k int) []*T {
454+
func (t *KDTree[T]) KNN(value T, k int) []T {
455455
if t == nil || t.root == nil || t.size < k {
456456
return nil
457457
}
458458

459459
pqRes := NewBoundedPriorityQueue[T](k)
460460
knn(k, t.dimensions, &value, &pqRes, 0, t.root)
461461

462-
res := make([]*T, 0, k)
462+
res := make([]T, 0, k)
463463
for range k {
464464
// heap with a preset capacity
465-
d := internal.Pop(&pqRes).Data
465+
d := *internal.Pop(&pqRes).Data
466466
res = append(res, d)
467467
}
468468

tests/kdtree_2d_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func Test2DNearestNeighbor5(t *testing.T) {
181181
}
182182
}
183183

184-
func tensor2DSortFunc(a, b *types.Tensor2D) int {
184+
func tensor2DSortFunc(a, b types.Tensor2D) int {
185185
if a[0] != b[0] {
186186
return a[0] - b[0]
187187
} else {
@@ -206,19 +206,19 @@ func Test2DKNN1(t *testing.T) {
206206
tree := kdtree.NewKDTreeWithValues(dimensions2DCount, ps)
207207
testTable := map[string]struct {
208208
input input
209-
expected []*types.Tensor2D
209+
expected []types.Tensor2D
210210
}{
211211
"Find the 2 closest neighbors to a point that is not in the KD tree.": {
212212
input: input{p: [2]int{25, 25}, k: 2},
213-
expected: []*types.Tensor2D{{40, 20}, {10, 25}},
213+
expected: []types.Tensor2D{{40, 20}, {10, 25}},
214214
},
215215
"The closest neighbor to a point that is in the KD tree.": {
216216
input: input{p: [2]int{60, 90}, k: 1},
217-
expected: []*types.Tensor2D{{60, 90}},
217+
expected: []types.Tensor2D{{60, 90}},
218218
},
219219
"The three closest neighbors to a point that is in the KD tree.": {
220220
input: input{p: [2]int{70, 70}, k: 3},
221-
expected: []*types.Tensor2D{{50, 50}, {60, 90}, {70, 70}},
221+
expected: []types.Tensor2D{{50, 50}, {60, 90}, {70, 70}},
222222
},
223223
}
224224
for name, st := range testTable {

0 commit comments

Comments
 (0)