get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/server/stree/node256.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 256 children
    17  type node256 struct {
    18  	meta
    19  	child [256]node
    20  }
    21  
    22  func newNode256(prefix []byte) *node256 {
    23  	nn := &node256{}
    24  	nn.setPrefix(prefix)
    25  	return nn
    26  }
    27  
    28  func (n *node256) isLeaf() bool { return false }
    29  func (n *node256) base() *meta  { return &n.meta }
    30  
    31  func (n *node256) setPrefix(pre []byte) {
    32  	n.prefixLen = uint16(min(len(pre), maxPrefixLen))
    33  	for i := uint16(0); i < n.prefixLen; i++ {
    34  		n.prefix[i] = pre[i]
    35  	}
    36  }
    37  
    38  func (n *node256) addChild(c byte, nn node) {
    39  	n.child[c] = nn
    40  	n.size++
    41  }
    42  
    43  func (n *node256) numChildren() uint16 { return n.size }
    44  func (n *node256) path() []byte        { return n.prefix[:n.prefixLen] }
    45  
    46  func (n *node256) findChild(c byte) *node {
    47  	if n.child[c] != nil {
    48  		return &n.child[c]
    49  	}
    50  	return nil
    51  }
    52  
    53  func (n *node256) isFull() bool { return false }
    54  func (n *node256) grow() node   { panic("grow can not be called on node256") }
    55  
    56  // Deletes a child from the node.
    57  func (n *node256) deleteChild(c byte) {
    58  	if n.child[c] != nil {
    59  		n.child[c] = nil
    60  		n.size--
    61  	}
    62  }
    63  
    64  // Shrink if needed and return new node, otherwise return nil.
    65  func (n *node256) shrink() node {
    66  	if n.size > 16 {
    67  		return nil
    68  	}
    69  	nn := newNode16(nil)
    70  	for c, child := range n.child {
    71  		if child != nil {
    72  			nn.addChild(byte(c), n.child[c])
    73  		}
    74  	}
    75  	return nn
    76  }
    77  
    78  // Will match parts against our prefix.
    79  func (n *node256) matchParts(parts [][]byte) ([][]byte, bool) {
    80  	return matchParts(parts, n.prefix[:n.prefixLen])
    81  }
    82  
    83  // Iterate over all children calling func f.
    84  func (n *node256) iter(f func(node) bool) {
    85  	for i := 0; i < 256; i++ {
    86  		if n.child[i] != nil {
    87  			if !f(n.child[i]) {
    88  				return
    89  			}
    90  		}
    91  	}
    92  }
    93  
    94  // Return our children as a slice.
    95  func (n *node256) children() []node {
    96  	return n.child[:256]
    97  }