Skip to content

Commit 76be92d

Browse files
committed
refactor: ♻️ create a new type for the Query API's callback function
1 parent 4c01823 commit 76be92d

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

defs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package kdtree
22

33
import "fmt"
44

5+
type RangeFunc[T Comparable[T]] func(T, int) RelativePosition
6+
57
type Relation int
68

79
const (

kdtree.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (t *KDTree[T]) NearestNeighbor(value T) (T, bool) {
101101
return *res, true
102102
}
103103

104-
func (t *KDTree[T]) Query(getRelativePosition func(T, int) RelativePosition) []T {
104+
func (t *KDTree[T]) Query(getRelativePosition RangeFunc[T]) []T {
105105
var res []T
106106
query(getRelativePosition, t.dimensions, &res, t.root, 0)
107107
return res
@@ -226,18 +226,17 @@ func (t *KDTree[T]) Balance() {
226226
t.root = NewKDTreeWithValues(t.dimensions, t.Values()).root
227227
}
228228

229-
func query[T Comparable[T]](getRelativePosition func(T, int) RelativePosition, d int, res *[]T, r *kdNode[T], cd int) {
229+
func query[T Comparable[T]](getRelativePosition RangeFunc[T], d int, res *[]T, r *kdNode[T], cd int) {
230230
if r == nil {
231231
return
232232
}
233233

234-
ncd := (cd + 1) % d
235-
236234
rel := getRelativePosition(r.value, -1)
237235
if rel == InRange {
238236
*res = append(*res, r.value)
239237
}
240238

239+
ncd := (cd + 1) % d
241240
switch relInCD := getRelativePosition(r.value, cd); relInCD {
242241
case BeforeRange:
243242
query(getRelativePosition, d, res, r.right, ncd)

0 commit comments

Comments
 (0)