-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.go
More file actions
73 lines (55 loc) · 1.27 KB
/
level.go
File metadata and controls
73 lines (55 loc) · 1.27 KB
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
package bitset
import (
"fmt"
"math"
)
type (
level struct {
shift uint // shift to compute node index
leaf bool // is leaf node
total int // number of child inodes/leaf-bits
mask uint64 // mask to compute node index
next *level // lower level
numNodes int // #stats
height int // level
bits uint // number of bits in 'address'
}
)
func initLevels(levelBits []uint) (rootLevel *level, maxIdx uint64) {
// must at least include spec for leaf node
if len(levelBits) == 0 {
return nil, 0
}
// leaf node should have non-zero allocation
if levelBits[0] == 0 {
return nil, 0
}
levels := make([]*level, len(levelBits))
var shift uint
var next *level
// compute and initialize level definitions
for i, n := range levelBits {
levels[i] = &level{
shift: shift,
mask: (uint64(1) << shift) - 1,
total: 1 << n,
next: next,
leaf: i == 0,
height: i,
bits: n,
numNodes: 0, // #stats
}
next = levels[i]
shift += n
}
if shift > 64 {
return nil, math.MaxInt64
}
rootLevel = levels[len(levelBits)-1]
maxIdx = (uint64(1) << shift) - 1
return
}
func (t *level) String() string {
return fmt.Sprintf("level(%p): h=%d bits=%d leaf=%v mask=%d shift=%d next=%p",
t, t.height, t.bits, t.leaf, t.mask, t.shift, t.next)
}