-
Notifications
You must be signed in to change notification settings - Fork 3
/
validate.go
220 lines (195 loc) · 5.46 KB
/
validate.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mctree
import (
"fmt"
"sort"
)
type maxPair [8]Pair
func newMaxPair(p []Pair) (m maxPair) {
if copy(m[:], p) < len(p) {
panic("must increase max_words_per_key")
}
return
}
func (p *maxPair) is_equal(q []Pair) bool {
for i := range q {
if !p[i].equal(&q[i]) {
return false
}
}
return true
}
type validate_tree struct {
validate_pair_counts map[maxPair]int
}
type validate_main struct {
validate_active_nodes map[node_index]bool
validate_referenced_pairs map[uint]uint
validate_all_pairs map[maxPair]bool
}
func (c *Config) wantValidate() bool { return c.Validate_iter > 0 }
func (m *validate_main) add_del_pair(p []Pair, is_del bool) {
if m.validate_all_pairs == nil {
return
}
mp := newMaxPair(p)
if is_del {
delete(m.validate_all_pairs, mp)
} else {
m.validate_all_pairs[mp] = true
}
}
func (t *tree) validate_node(i node_index, key *maxPair, cost *tree_cost, n_pairs_per_key uint) {
n := t.get_node(i)
nkey := t.get_node_key(i)
if !key.is_equal(nkey) {
panic(fmt.Errorf("key %x != %x", nkey, key[:len(nkey)]))
}
t.validate_active_nodes[i] = true
if n.is_split() {
if n.n_pairs() != 0 {
panic(fmt.Errorf("split node must have no pairs"))
}
var sk [2]maxPair
b0, b1 := index(n.split_bit)
sk[0] = *key
sk[0][b0].Mask |= b1
sk[1] = sk[0]
sk[1][b0].Value |= b1
for j := range n.sub_nodes {
t.validate_node(n.sub_nodes[j], &sk[j], cost, n_pairs_per_key)
}
} else {
po := &n.pair_offsets
if po.pool_index != invalid {
t.validate_referenced_pairs[po.pool_index] += 1
}
for i, o := range po.vec {
if n.index_is_free(uint(i)) {
continue
}
p := t.pair_hash.get_pairs_for_offset(o)
var px maxPair
for j := range p {
if p[j].Value&p[j].Mask&nkey[j].Mask != nkey[j].Value&p[j].Mask {
panic(fmt.Errorf("wrong sub node"))
}
px[j] = p[j]
}
t.validate_pair_counts[px] += 1
}
x := float64(n.n_pairs())
cost.occupancy += x
cost.occupancy2 += x * x
}
}
func (m *Main) validate_tree(t *tree) {
if t.validate_pair_counts == nil {
t.validate_pair_counts = make(map[maxPair]int)
}
t.Main = m
var c tree_cost
t.validate_node(t.root_node_index, &maxPair{}, &c, m.n_pairs_per_key)
// Validate cost
if t.occupancy != c.occupancy {
panic(fmt.Errorf("occupancy mismatch got %.0f != want %.0f", t.occupancy, c.occupancy))
}
if t.occupancy2 != c.occupancy2 {
panic(fmt.Errorf("occupancy2 mismatch got %.0f != want %.0f", t.occupancy2, c.occupancy2))
}
c.compute_cost(m)
if t.cost != c.cost {
panic(fmt.Errorf("cost mismatch got %.0f != want %.0f", t.cost, c.cost))
}
if got, want := len(t.validate_pair_counts), len(m.validate_all_pairs); got != want {
panic(fmt.Errorf("pair leak got %d != want %d", got, want))
}
for k := range m.validate_all_pairs {
if _, ok := t.validate_pair_counts[k]; !ok {
panic(fmt.Errorf("pair %v vanished", &k))
}
delete(t.validate_pair_counts, k)
}
}
func (m *Main) Validate() {
if m.validate_active_nodes == nil {
m.validate_active_nodes = make(map[node_index]bool)
m.validate_referenced_pairs = make(map[uint]uint)
}
t := m.get_tree_seq(m.tree_sequence)
if t.n_steps > 0 {
m.validate_tree(t)
}
m.validate_tree(m.get_tree_seq(m.tree_sequence - 1))
if want, got := m.node_pool.Elts(), uint(len(m.validate_active_nodes)); want != got {
panic(fmt.Errorf("wrong number of active nodes want %d != got %d", want, got))
}
for ni := range m.validate_active_nodes {
if m.node_pool.IsFree(uint(ni)) {
panic("node pool pointer to free node")
}
delete(m.validate_active_nodes, ni)
}
if got, want := uint(len(m.validate_referenced_pairs)), m.shared_pair_offsets_pool.Elts(); got != want {
panic(fmt.Errorf("node referenced pairs %d != pair vec pool elts %d", got, want))
}
for i := range m.nodes {
if m.node_pool.IsFree(uint(i)) {
continue
}
n := m.get_node(node_index(i))
po := &n.pair_offsets
if po.pool_index == invalid {
continue
}
if got, want := m.shared_pair_offsets_pool.elts[po.pool_index].reference_count, m.validate_referenced_pairs[po.pool_index]; got != want {
panic(fmt.Errorf("ref count mismatch %d != %d", got, want))
}
}
for i := range m.validate_referenced_pairs {
delete(m.validate_referenced_pairs, i)
}
}
type dump_node struct {
key maxPair
n_pairs uint32
level uint32
}
//go:generate gentemplate -d Package=mctree -id dump_node -d VecType=dump_node_vec -d Type=dump_node github.com/platinasystems/elib/vec.tmpl
func (d *dump_node_vec) dump_node(m *Main, i node_index, level uint32) {
n := m.get_node(i)
if n.is_split() {
d.dump_node(m, n.sub_nodes[0], level+1)
d.dump_node(m, n.sub_nodes[1], level+1)
} else {
*d = append(*d, dump_node{
level: level,
key: newMaxPair(m.get_node_key(i)),
n_pairs: uint32(n.n_pairs()),
})
}
}
func (d *dump_node_vec) dump(m *Main, t *tree) {
d.dump_node(m, t.root_node_index, 0)
}
type dump_node_sort dump_node_vec
func (d dump_node_sort) Len() int { return len(d) }
func (d dump_node_sort) Less(i, j int) bool {
for k := range d[i].key {
if d[i].key[k].Value < d[j].key[k].Value {
return true
}
}
return false
}
func (d dump_node_sort) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (t *tree) dump(m *Main) {
d := dump_node_vec{}
d.dump(m, t)
sort.Sort(dump_node_sort(d))
for _, x := range d {
fmt.Printf("[%d] %v %d leafs\n", x.level, &x.key, x.n_pairs)
}
}