Skip to content

Commit

Permalink
Minor rtree package cleanup
Browse files Browse the repository at this point in the history
These are just some minor fixes that were identified while working on
unrelated parts of the `rtree` package.
  • Loading branch information
peterstace committed Jun 5, 2023
1 parent d4a7f0f commit 40ab3ab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
15 changes: 3 additions & 12 deletions rtree/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,11 @@ func quickPartition(items []BulkItem, k int, horizontal bool) {
}

func itemsAreHorizontal(items []BulkItem) bool {
// fastMin and fastMax are used rather than Box's combine method to avoid
// math.Min and math.Max calls (which are more expensive).
minX := items[0].Box.MinX
maxX := items[0].Box.MaxX
minY := items[0].Box.MinY
maxY := items[0].Box.MaxY
box := items[0].Box
for _, item := range items[1:] {
box := item.Box
minX = fastMin(minX, box.MinX)
maxX = fastMax(maxX, box.MaxX)
minY = fastMin(minY, box.MinY)
maxY = fastMax(maxY, box.MaxY)
box = combine(box, item.Box)
}
return maxX-minX > maxY-minY
return box.MaxX-box.MinX > box.MaxY-box.MinY
}

// fastMin is a faster but not functionally identical version of math.Min.
Expand Down
10 changes: 5 additions & 5 deletions rtree/nearest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func (t *RTree) Nearest(box Box) (recordID int, found bool) {
}

// PrioritySearch iterates over the records in the RTree in priority order of
// distance from the input box (shortest distanace first using the Euclidean
// distance from the input box (shortest distance first using the Euclidean
// metric). The callback is called for every element iterated over. If an
// error is returned from the callback, then iteration stops immediately. Any
// error returned from the callback is returned by PrioritySearch, except for
// the case where the special Stop sentinal error is returned (in which case
// the case where the special Stop sentinel error is returned (in which case
// nil will be returned from PrioritySearch).
func (t *RTree) PrioritySearch(box Box, callback func(recordID int) error) error {
if t.root == nil {
Expand Down Expand Up @@ -61,9 +61,9 @@ func (q *entriesQueue) Len() int {
}

func (q *entriesQueue) Less(i int, j int) bool {
e1 := q.entries[i]
e2 := q.entries[j]
return squaredEuclideanDistance(e1.box, q.origin) < squaredEuclideanDistance(e2.box, q.origin)
d1 := squaredEuclideanDistance(q.entries[i].box, q.origin)
d2 := squaredEuclideanDistance(q.entries[j].box, q.origin)
return d1 < d2
}

func (q *entriesQueue) Swap(i int, j int) {
Expand Down
3 changes: 2 additions & 1 deletion rtree/rtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func checkInvariants(t *testing.T, rt *RTree, boxes []Box) {
t.Fatal("entry past numEntries is not the zero value")
}
}
if current.numEntries > maxChildren || (current != rt.root && current.numEntries < minChildren) {
if current.numEntries > maxChildren ||
(current != rt.root && current.numEntries < minChildren) {
t.Fatalf("%p: unexpected number of entries", current)
}
}
Expand Down

0 comments on commit 40ab3ab

Please sign in to comment.