-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
89 lines (80 loc) · 1.76 KB
/
index.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
package vector
// Index represents nodes index matrix.
//
// Contain indexes of nodes in the vector divided by depth.
// Y-axis means depth, X-axis means position in index.
type Index struct {
// Index tree.
tree []branch
// Index depth.
depth int
}
type branch struct {
buf []int
len int
}
// Register new index for given depth.
func (idx *Index) Register(depth, i int) int {
if len(idx.tree) <= depth {
for len(idx.tree) <= depth {
idx.tree = append(idx.tree, branch{})
idx.depth = len(idx.tree)
}
}
b := &idx.tree[depth]
if b.len < len(b.buf) {
b.buf[b.len] = i
} else {
b.buf = append(b.buf, i)
}
b.len++
return b.len
}
// Len returns length of index row registered on depth.
func (idx *Index) Len(depth int) int {
if len(idx.tree) <= depth {
return 0
}
return idx.tree[depth].len
}
// GetRow returns indices row registered at given depth.
func (idx *Index) GetRow(depth int) []int {
if depth < 0 || depth >= len(idx.tree) {
return nil
}
b := &idx.tree[depth]
return b.buf[:b.len]
}
// Reset rest of the index starting of given depth and offset in the tree.
func (idx *Index) Reset(depth, offset int) {
if depth >= len(idx.tree) {
return
}
if idx.tree[depth].len > offset {
idx.tree[depth].buf = idx.tree[depth].buf[:offset]
}
if depth+1 < len(idx.tree) {
for i := depth + 1; i < len(idx.tree); i++ {
idx.tree[i].len = 0
}
}
}
// Get subset [s:e] of index row registered on depth.
func (idx *Index) get(depth, s, e int) []int {
l := idx.Len(depth)
if l > s {
return idx.tree[depth].buf[s:e]
}
return nil
}
// Get index value.
func (idx *Index) val(depth, i int) int {
return idx.tree[depth].buf[i]
}
// Reset index object.
func (idx *Index) reset() {
for i := 0; i < len(idx.tree); i++ {
idx.tree[i].len = 0
}
idx.depth = 0
}