forked from sjbog/go-DBSCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterablePoint.go
63 lines (52 loc) · 1.35 KB
/
clusterablePoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package dbscan
import (
"fmt"
"sort"
)
type ClusterablePoint interface {
GetPoint() []float64
String() string
}
type NamedPoint struct {
Name string
Point []float64
}
func NewNamedPoint(name string, point []float64) *NamedPoint {
return &NamedPoint{
Name: name,
Point: point,
}
}
func (self *NamedPoint) String() string {
return fmt.Sprintf("\"%s\": %v", self.Name, self.Point)
}
func (self *NamedPoint) GetPoint() []float64 {
return self.Point
}
func (self *NamedPoint) Copy() *NamedPoint {
var p = new(NamedPoint)
p.Name = self.Name
copy(p.Point, self.Point)
return p
}
// Slice attaches the methods of Interface to []float64, sorting in increasing order.
type ClusterablePointSlice struct {
Data []ClusterablePoint
SortDimension int
}
func (self ClusterablePointSlice) Len() int { return len(self.Data) }
func (self ClusterablePointSlice) Less(i, j int) bool {
return self.Data[i].GetPoint()[self.SortDimension] < self.Data[j].GetPoint()[self.SortDimension]
}
func (self ClusterablePointSlice) Swap(i, j int) {
self.Data[i], self.Data[j] = self.Data[j], self.Data[i]
}
// Sort is a convenience method.
func (self ClusterablePointSlice) Sort() { sort.Sort(self) }
func NamedPointToClusterablePoint(in []*NamedPoint) (out []ClusterablePoint) {
out = make([]ClusterablePoint, len(in))
for i, v := range in {
out[i] = v
}
return
}