get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/server/stree/node16.go (about) 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 16 children 17 type node16 struct { 18 meta 19 child [16]node 20 key [16]byte 21 } 22 23 func newNode16(prefix []byte) *node16 { 24 nn := &node16{} 25 nn.setPrefix(prefix) 26 return nn 27 } 28 29 func (n *node16) isLeaf() bool { return false } 30 func (n *node16) base() *meta { return &n.meta } 31 32 func (n *node16) setPrefix(pre []byte) { 33 n.prefixLen = uint16(min(len(pre), maxPrefixLen)) 34 for i := uint16(0); i < n.prefixLen; i++ { 35 n.prefix[i] = pre[i] 36 } 37 } 38 39 // Currently we do not keep node16 sorted or use bitfields for traversal so just add to the end. 40 // TODO(dlc) - We should revisit here with more detailed benchmarks. 41 func (n *node16) addChild(c byte, nn node) { 42 if n.size >= 16 { 43 panic("node16 full!") 44 } 45 n.key[n.size] = c 46 n.child[n.size] = nn 47 n.size++ 48 } 49 50 func (n *node16) numChildren() uint16 { return n.size } 51 func (n *node16) path() []byte { return n.prefix[:n.prefixLen] } 52 53 func (n *node16) findChild(c byte) *node { 54 for i := uint16(0); i < n.size; i++ { 55 if n.key[i] == c { 56 return &n.child[i] 57 } 58 } 59 return nil 60 } 61 62 func (n *node16) isFull() bool { return n.size >= 16 } 63 64 func (n *node16) grow() node { 65 nn := newNode256(n.prefix[:n.prefixLen]) 66 for i := 0; i < 16; i++ { 67 nn.addChild(n.key[i], n.child[i]) 68 } 69 return nn 70 } 71 72 // Deletes a child from the node. 73 func (n *node16) deleteChild(c byte) { 74 for i, last := uint16(0), n.size-1; i < n.size; i++ { 75 if n.key[i] == c { 76 // Unsorted so just swap in last one here, else nil if last. 77 if i < last { 78 n.key[i] = n.key[last] 79 n.child[i] = n.child[last] 80 n.key[last] = 0 81 n.child[last] = nil 82 } else { 83 n.key[i] = 0 84 n.child[i] = nil 85 } 86 n.size-- 87 return 88 } 89 } 90 } 91 92 // Shrink if needed and return new node, otherwise return nil. 93 func (n *node16) shrink() node { 94 if n.size > 4 { 95 return nil 96 } 97 nn := newNode4(nil) 98 for i := uint16(0); i < n.size; i++ { 99 nn.addChild(n.key[i], n.child[i]) 100 } 101 return nn 102 } 103 104 // Will match parts against our prefix.no 105 func (n *node16) matchParts(parts [][]byte) ([][]byte, bool) { 106 return matchParts(parts, n.prefix[:n.prefixLen]) 107 } 108 109 // Iterate over all children calling func f. 110 func (n *node16) iter(f func(node) bool) { 111 for i := uint16(0); i < n.size; i++ { 112 if !f(n.child[i]) { 113 return 114 } 115 } 116 } 117 118 // Return our children as a slice. 119 func (n *node16) children() []node { 120 return n.child[:n.size] 121 }