Debian Codegraph @

server/stree/node256.go

raw
1 // Copyright 2023-2024 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package stree
15
16 // Node with 256 children
17 // Order of struct fields for best memory alignment (as per govet/fieldalignment)
18 type node256 struct {
19 child [256]node
20 meta
21 }
22
23 func newNode256(prefix []byte) *node256 {
24 nn := &node256{}
25 nn.setPrefix(prefix)
26 return nn
27 }
28
29 func (n *node256) addChild(c byte, nn node) {
30 n.child[c] = nn
31 n.size++
32 }
33
34 func (n *node256) findChild(c byte) *node {
35 if n.child[c] != nil {
36 return &n.child[c]
37 }
38 return nil
39 }
40
41 func (n *node256) isFull() bool { return false }
42 func (n *node256) grow() node { panic("grow can not be called on node256") }
43
44 // Deletes a child from the node.
45 func (n *node256) deleteChild(c byte) {
46 if n.child[c] != nil {
47 n.child[c] = nil
48 n.size--
49 }
50 }
51
52 // Shrink if needed and return new node, otherwise return nil.
53 func (n *node256) shrink() node {
54 if n.size > 48 {
55 return nil
56 }
57 nn := newNode48(nil)
58 for c, child := range n.child {
59 if child != nil {
60 nn.addChild(byte(c), n.child[c])
61 }
62 }
63 return nn
64 }
65
66 // Iterate over all children calling func f.
67 func (n *node256) iter(f func(node) bool) {
68 for i := 0; i < 256; i++ {
69 if n.child[i] != nil {
70 if !f(n.child[i]) {
71 return
72 }
73 }
74 }
75 }
76
77 // Return our children as a slice.
78 func (n *node256) children() []node {
79 return n.child[:256]
80 }