-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsparse.go
64 lines (54 loc) · 1.33 KB
/
sparse.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
package measure
import (
"encoding/json"
"sync/atomic"
)
// Sparse measure returns pre-computed costs between two locations without
// requiring a full data set. If two locations do not have an associated cost,
// then a backup measure is used.
func Sparse(m ByIndex, arcs map[int]map[int]float64) ByIndex {
return &sparse{m: m, arcs: arcs}
}
// DebugSparse returns a Sparse that when marshalled will include debugging
// information describing the number of queries for elements included in (and
// not included in) the matrix.
func DebugSparse(m ByIndex, arcs map[int]map[int]float64) ByIndex {
return &sparse{m: m, arcs: arcs, debugMode: true}
}
type sparse struct {
m ByIndex
debugMode bool
arcs map[int]map[int]float64
counts struct {
hit uint64
miss uint64
}
}
func (s *sparse) Cost(from, to int) float64 {
if m, ok := s.arcs[from]; ok {
if c, ok := m[to]; ok {
if s.debugMode {
atomic.AddUint64(&s.counts.hit, 1)
}
return c
}
}
if s.debugMode {
atomic.AddUint64(&s.counts.miss, 1)
}
return s.m.Cost(from, to)
}
func (s sparse) MarshalJSON() ([]byte, error) {
output := map[string]any{
"measure": s.m,
"arcs": s.arcs,
"type": "sparse",
}
if s.debugMode {
output["counts"] = map[string]uint64{
"hit": s.counts.hit,
"miss": s.counts.miss,
}
}
return json.Marshal(output)
}