-
Notifications
You must be signed in to change notification settings - Fork 103
/
matrix_storage.go
79 lines (70 loc) · 1.86 KB
/
matrix_storage.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
package pointcloud
import (
"sync"
"github.com/golang/geo/r3"
)
type matrixStorage struct {
mu sync.RWMutex
points []PointAndData
indexMap map[r3.Vector]uint // TODO (aidanglickman): when r3.Vector has a hash method update this to save space
}
func (ms *matrixStorage) Size() int {
ms.mu.RLock()
defer ms.mu.RUnlock()
return len(ms.points)
}
func (ms *matrixStorage) Set(v r3.Vector, d Data) error {
ms.mu.Lock()
defer ms.mu.Unlock()
if v.X > maxPreciseFloat64 || v.X < minPreciseFloat64 {
return newOutOfRangeErr("x", v.X)
}
if v.Y > maxPreciseFloat64 || v.Y < minPreciseFloat64 {
return newOutOfRangeErr("y", v.Y)
}
if v.Z > maxPreciseFloat64 || v.Z < minPreciseFloat64 {
return newOutOfRangeErr("z", v.Z)
}
if i, found := ms.indexMap[v]; found {
ms.points[i].D = d
} else {
ms.points = append(ms.points, PointAndData{v, d})
ms.indexMap[v] = uint(len(ms.points) - 1) // TODO (aidanglickman): update this to save space
}
return nil
}
func (ms *matrixStorage) At(x, y, z float64) (Data, bool) {
ms.mu.RLock()
defer ms.mu.RUnlock()
// TODO (aidanglickman): Update this whole function with the new hashing
v := r3.Vector{x, y, z}
if i, found := ms.indexMap[v]; found {
return ms.points[i].D, true
}
return nil, false
}
func (ms *matrixStorage) Iterate(numBatches, myBatch int, fn func(p r3.Vector, d Data) bool) {
ms.mu.RLock()
defer ms.mu.RUnlock()
lowerBound := 0
upperBound := ms.Size()
if numBatches > 0 {
batchSize := (ms.Size() + numBatches - 1) / numBatches
lowerBound = myBatch * batchSize
upperBound = (myBatch + 1) * batchSize
}
if upperBound > ms.Size() {
upperBound = ms.Size()
}
for i := lowerBound; i < upperBound; i++ {
if cont := fn(ms.points[i].P, ms.points[i].D); !cont {
return
}
}
}
func (ms *matrixStorage) EditSupported() bool {
return true
}
func (ms *matrixStorage) IsOrdered() bool {
return true
}