-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedges4.go
82 lines (63 loc) · 1.1 KB
/
edges4.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
package art
import "bytes"
type edges4 struct {
edges [4]*node
keys [4]byte
children uint8
}
func newEdges4() *edges4 {
return &edges4{}
}
func newEdges4p() *edges {
var e edges
e = &edges4{}
return &e
}
func (e *edges4) ntype() uint8 {
return Node4
}
func (e *edges4) next(b byte) *node {
i := bytes.IndexByte(e.keys[:], b)
if i < 0 {
return nil
}
return e.edges[i]
}
func (e *edges4) setNext(b byte, next *node) {
i := e.search(b)
if e.keys[i] == b {
e.edges[i] = next
return
}
copy(e.keys[i+1:], e.keys[i:])
copy(e.edges[i+1:], e.edges[i:])
e.keys[i] = b
e.edges[i] = next
e.children++
}
func (e *edges4) search(b byte) uint8 {
for i := uint8(0); i < uint8(len(e.keys)); i++ {
if e.keys[i] >= b {
return i
}
}
return e.children
}
func (e *edges4) copy() edges {
ne := &edges4{
children: e.children,
}
ne.keys = e.keys
ne.edges = e.edges
return ne
}
func (e *edges4) upgrade() edges {
newEdges := newEdges16()
for i := 0; i < 4; i++ {
newEdges.setNext(e.keys[i], e.edges[i])
}
return newEdges
}
func (e *edges4) full() bool {
return e.children == 4
}