-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.go
103 lines (91 loc) · 1.68 KB
/
matrix.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
package distmatrix
import (
"errors"
"time"
log "github.com/sirupsen/logrus"
)
type DistMatrix struct {
values [][]int
}
func NewDM(count int) *DistMatrix {
var dm DistMatrix
dm.values = make([][]int, count)
return &dm
}
func (d *DistMatrix) addA(arr []int, ix int) {
if len(arr) < 1 {
return
}
d.values[ix] = arr
}
func (d *DistMatrix) dist(a, b int) (int, error) {
if a == b {
return 0, nil
}
maxa := len(d.values)
if a > maxa || b > maxa {
return 0, errors.New("A or B is greater than max accounts")
}
lower := a
higher := b
if a > b {
lower = b
higher = a
}
return d.values[lower][higher-lower], nil
}
func (d *DistMatrix) belowDistance(pos []int, cutoff int) (int, error) {
less := 0
for ix := 0; ix < len(pos); ix++ {
subpos := pos[ix:]
for _, v := range subpos {
dist, err := d.dist(pos[ix], v)
if err != nil {
return 0, err
}
if dist == 0 {
continue
}
//log.Infof("Between %d and %d, dist is %d", pos[ix], v, dist)
if dist < cutoff {
less++
}
}
}
return less, nil
}
func (d *DistMatrix) multiMinMax(pos []int) (int, int, error) {
max := 0
min := -1
for ix := 0; ix < len(pos); ix++ {
subpos := pos[ix:]
for _, v := range subpos {
dist, err := d.dist(pos[ix], v)
if err != nil {
return 0, 0, err
}
if dist == 0 {
continue
}
if dist > max {
max = dist
}
if min == -1 {
min = dist
}
if dist < min {
min = dist
}
}
}
return min, max, nil
}
func (d *DistMatrix) print() {
for ix, arr := range d.values {
log.Debugf("%d: %+v", ix, arr)
}
}
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Infof("%s took %s", name, elapsed)
}