github.com/nats-io/nats-server/v2@v2.11.0-preview.2/server/stree/node4.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 4 children
    17  type node4 struct {
    18  	meta
    19  	child [4]node
    20  	key   [4]byte
    21  }
    22  
    23  func newNode4(prefix []byte) *node4 {
    24  	nn := &node4{}
    25  	nn.setPrefix(prefix)
    26  	return nn
    27  }
    28  
    29  func (n *node4) isLeaf() bool { return false }
    30  func (n *node4) base() *meta  { return &n.meta }
    31  
    32  func (n *node4) 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 need to keep sorted for traversal so just add to the end.
    40  func (n *node4) addChild(c byte, nn node) {
    41  	if n.size >= 4 {
    42  		panic("node4 full!")
    43  	}
    44  	n.key[n.size] = c
    45  	n.child[n.size] = nn
    46  	n.size++
    47  }
    48  
    49  func (n *node4) numChildren() uint16 { return n.size }
    50  func (n *node4) path() []byte        { return n.prefix[:n.prefixLen] }
    51  
    52  func (n *node4) findChild(c byte) *node {
    53  	for i := uint16(0); i < n.size; i++ {
    54  		if n.key[i] == c {
    55  			return &n.child[i]
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  func (n *node4) isFull() bool { return n.size >= 4 }
    62  
    63  func (n *node4) grow() node {
    64  	nn := newNode16(n.prefix[:n.prefixLen])
    65  	for i := 0; i < 4; i++ {
    66  		nn.addChild(n.key[i], n.child[i])
    67  	}
    68  	return nn
    69  }
    70  
    71  // Deletes a child from the node.
    72  func (n *node4) deleteChild(c byte) {
    73  	for i, last := uint16(0), n.size-1; i < n.size; i++ {
    74  		if n.key[i] == c {
    75  			// Unsorted so just swap in last one here, else nil if last.
    76  			if i < last {
    77  				n.key[i] = n.key[last]
    78  				n.child[i] = n.child[last]
    79  				n.key[last] = 0
    80  				n.child[last] = nil
    81  			} else {
    82  				n.key[i] = 0
    83  				n.child[i] = nil
    84  			}
    85  			n.size--
    86  			return
    87  		}
    88  	}
    89  }
    90  
    91  // Shrink if needed and return new node, otherwise return nil.
    92  func (n *node4) shrink() node {
    93  	if n.size == 1 {
    94  		return n.child[0]
    95  	}
    96  	return nil
    97  }
    98  
    99  // Will match parts against our prefix.
   100  func (n *node4) matchParts(parts [][]byte) ([][]byte, bool) {
   101  	return matchParts(parts, n.prefix[:n.prefixLen])
   102  }
   103  
   104  // Iterate over all children calling func f.
   105  func (n *node4) iter(f func(node) bool) {
   106  	for i := uint16(0); i < n.size; i++ {
   107  		if !f(n.child[i]) {
   108  			return
   109  		}
   110  	}
   111  }
   112  
   113  // Return our children as a slice.
   114  func (n *node4) children() []node {
   115  	return n.child[:n.size]
   116  }