forked from mpraski/clusters
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
107 lines (85 loc) · 1.7 KB
/
common.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package clusters
import (
"container/heap"
"math/rand"
"sync"
)
// struct denoting start and end indices of database portion to be scanned for nearest neighbours by workers in DBSCAN and OPTICS
type rangeJob struct {
a, b int
}
// priority queue
type pItem struct {
v int
p float64
i int
}
type priorityQueue []*pItem
func newPriorityQueue(size int) priorityQueue {
q := make(priorityQueue, 0, size)
heap.Init(&q)
return q
}
func (pq priorityQueue) Len() int { return len(pq) }
func (pq priorityQueue) Less(i, j int) bool {
return pq[i].p > pq[j].p
}
func (pq priorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].i = i
pq[j].i = j
}
func (pq *priorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*pItem)
item.i = n
*pq = append(*pq, item)
heap.Fix(pq, item.i)
}
func (pq *priorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
item.i = -1
*pq = old[0 : n-1]
return item
}
func (pq *priorityQueue) NotEmpty() bool {
return len(*pq) > 0
}
func (pq *priorityQueue) Update(item *pItem, value int, priority float64) {
item.v = value
item.p = priority
heap.Fix(pq, item.i)
}
func bounds(data [][]float64) []*[2]float64 {
var (
wg sync.WaitGroup
l = len(data[0])
r = make([]*[2]float64, l)
)
for i := 0; i < l; i++ {
r[i] = &[2]float64{
data[0][i],
data[0][i],
}
}
wg.Add(l)
for i := 0; i < l; i++ {
go func(n int) {
defer wg.Done()
for j := 0; j < len(data); j++ {
if data[j][n] < r[n][0] {
r[n][0] = data[j][n]
} else if data[j][n] > r[n][1] {
r[n][1] = data[j][n]
}
}
}(i)
}
wg.Wait()
return r
}
func uniform(data *[2]float64) float64 {
return rand.Float64()*(data[1]-data[0]) + data[0]
}