Skip to content

Commit 18c7585

Browse files
committed
removed PointMinHeap and PointMaxHeap
1 parent 7c1b9d5 commit 18c7585

File tree

2 files changed

+12
-140
lines changed

2 files changed

+12
-140
lines changed

point.go

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"sort"
66

7+
datastructures "github.com/deepfabric/go-datastructures"
78
"github.com/keegancsmith/nth"
89
)
910

@@ -12,12 +13,6 @@ type Point struct {
1213
UserData uint64
1314
}
1415

15-
// PointMinHeap is a min-heap of points.
16-
type PointMinHeap []Point
17-
18-
// PointMaxHeap is a max-heap of points.
19-
type PointMaxHeap []Point
20-
2116
type PointArray interface {
2217
sort.Interface
2318
GetPoint(idx int) Point
@@ -41,6 +36,17 @@ type PointArrayExt struct {
4136
pointSize int
4237
}
4338

39+
// Compare is part of datastructures.Comparable interface
40+
func (p Point) Compare(other datastructures.Comparable) int {
41+
rhs := other.(Point)
42+
for dim := 0; dim < len(p.Vals); dim++ {
43+
if p.Vals[dim] != rhs.Vals[dim] {
44+
return int(p.Vals[dim] - rhs.Vals[dim])
45+
}
46+
}
47+
return int(p.UserData - rhs.UserData)
48+
}
49+
4450
func (p *Point) Inside(lowPoint, highPoint Point) (isInside bool) {
4551
for dim := 0; dim < len(p.Vals); dim++ {
4652
if p.Vals[dim] < lowPoint.Vals[dim] || p.Vals[dim] > highPoint.Vals[dim] {
@@ -111,68 +117,6 @@ func (p *Point) Decode(b []byte, numDims int, bytesPerDim int) {
111117
return
112118
}
113119

114-
// Len is part of sort.Interface.
115-
func (s PointMinHeap) Len() int {
116-
return len(s)
117-
}
118-
119-
// Swap is part of sort.Interface.
120-
func (s PointMinHeap) Swap(i, j int) {
121-
s[i], s[j] = s[j], s[i]
122-
}
123-
124-
// Less is part of sort.Interface.
125-
func (s PointMinHeap) Less(i, j int) bool {
126-
return s[i].LessThan(s[j])
127-
}
128-
129-
// Push is part of heap.Interface.
130-
func (s *PointMinHeap) Push(x interface{}) {
131-
// Push and Pop use pointer receivers because they modify the slice's length,
132-
// not just its contents.
133-
*s = append(*s, x.(Point))
134-
}
135-
136-
// Pop is part of heap.Interface.
137-
func (s *PointMinHeap) Pop() interface{} {
138-
old := *s
139-
n := len(old)
140-
x := old[n-1]
141-
*s = old[0 : n-1]
142-
return x
143-
}
144-
145-
// Len is part of sort.Interface.
146-
func (s PointMaxHeap) Len() int {
147-
return len(s)
148-
}
149-
150-
// Swap is part of sort.Interface.
151-
func (s PointMaxHeap) Swap(i, j int) {
152-
s[i], s[j] = s[j], s[i]
153-
}
154-
155-
// Less is part of sort.Interface.
156-
func (s PointMaxHeap) Less(i, j int) bool {
157-
return s[j].LessThan(s[i])
158-
}
159-
160-
// Push is part of heap.Interface.
161-
func (s *PointMaxHeap) Push(x interface{}) {
162-
// Push and Pop use pointer receivers because they modify the slice's length,
163-
// not just its contents.
164-
*s = append(*s, x.(Point))
165-
}
166-
167-
// Pop is part of heap.Interface.
168-
func (s *PointMaxHeap) Pop() interface{} {
169-
old := *s
170-
n := len(old)
171-
x := old[n-1]
172-
*s = old[0 : n-1]
173-
return x
174-
}
175-
176120
// Len is part of sort.Interface.
177121
func (s *PointArrayMem) Len() int {
178122
return len(s.points)

point_test.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ package bkdtree
22

33
import (
44
"bytes"
5-
"container/heap"
6-
"fmt"
75
"math/rand"
86
"os"
97
"testing"
10-
11-
"sort"
12-
13-
"github.com/juju/testing/checkers"
148
)
159

1610
type CaseInside struct {
@@ -101,72 +95,6 @@ func NewRandPoints(numDims int, maxVal uint64, size int) (points []Point) {
10195
return
10296
}
10397

104-
func TestPointHeap(t *testing.T) {
105-
numDims := 3
106-
maxVal := uint64(100)
107-
size := 10000
108-
points := NewRandPoints(numDims, maxVal, size)
109-
/*There are several ways to build a heap from array:
110-
111-
s := PointMinHeap(points)
112-
h := &s
113-
heap.Init(h)
114-
115-
h := &PointMinHeap{}
116-
*h = points
117-
heap.Init(h)
118-
119-
s := &points
120-
h := (*PointMinHeap)(s)
121-
heap.Init(h)
122-
123-
h := &PointMinHeap{}
124-
for i := 0; i < len(points); i++ {
125-
heap.Push(h, points[i])
126-
}
127-
*/
128-
h := &PointMinHeap{}
129-
*h = points
130-
heap.Init(h)
131-
var prevPoint *Point
132-
var smallestN1 []Point
133-
var smallestN2 []Point
134-
N := 1000
135-
for h.Len() > 0 {
136-
p := heap.Pop(h).(Point)
137-
if prevPoint != nil && p.LessThan(*prevPoint) {
138-
t.Fatalf("incorrect order of %v %v", *prevPoint, p)
139-
}
140-
prevPoint = &p
141-
if len(smallestN1) < N {
142-
smallestN1 = append(smallestN1, p)
143-
}
144-
}
145-
146-
/*Inspired by https://stackoverflow.com/questions/5845810/constant-size-priority-queue-insert-first-or-delete-first
147-
h2 is a max-heap which stores the N smallest points found so far. The top (largest) is at index zero.
148-
*/
149-
h2 := &PointMaxHeap{}
150-
heap.Init(h2)
151-
for i := 0; i < len(points); i++ {
152-
if len(*h2) < N {
153-
h2.Push(points[i])
154-
} else if points[i].LessThan((*h2)[0]) {
155-
(*h2)[0] = points[i]
156-
heap.Fix(h2, 0)
157-
}
158-
}
159-
s := PointMinHeap(*h2)
160-
sort.Sort(&s)
161-
smallestN2 = s
162-
isEqual, err := checkers.DeepEqual(smallestN1, smallestN2)
163-
if !isEqual {
164-
fmt.Printf("smallestN1: %+v\n", smallestN1)
165-
fmt.Printf("smallestN2: %+v\n", smallestN2)
166-
t.Fatalf("smallestN1 and smallestN2 %+v", err)
167-
}
168-
}
169-
17098
func TestPointArrayExt_ToMem(t *testing.T) {
17199
numDims := 3
172100
maxVal := uint64(100)

0 commit comments

Comments
 (0)