| 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 |
// Leaf node |
| 17 |
// Order of struct fields for best memory alignment (as per govet/fieldalignment) |
| 18 |
type leaf[T any] struct { |
| 19 |
value T |
| 20 |
// This could be the whole subject, but most likely just the suffix portion. |
| 21 |
// We will only store the suffix here and assume all prior prefix paths have |
| 22 |
// been checked once we arrive at this leafnode. |
| 23 |
suffix string |
| 24 |
} |
| 25 |
|
| 26 |
func newLeaf[T any](suffix []byte, value T) *leaf[T] { |
| 27 |
return &leaf[T]{value, string(suffix)} |
| 28 |
} |
| 29 |
|
| 30 |
func (n *leaf[T]) isLeaf() bool { return true } |
| 31 |
func (n *leaf[T]) base() *meta { return nil } |
| 32 |
func (n *leaf[T]) match(subject []byte) bool { return string(subject) == n.suffix } |
| 33 |
func (n *leaf[T]) setSuffix(suffix []byte) { n.suffix = string(suffix) } |
| 34 |
func (n *leaf[T]) isFull() bool { return true } |
| 35 |
func (n *leaf[T]) iter(f func(node) bool) {} |
| 36 |
func (n *leaf[T]) children() []node { return nil } |
| 37 |
func (n *leaf[T]) numChildren() uint16 { return 0 } |
| 38 |
func (n *leaf[T]) path() string { return n.suffix } |
| 39 |
|
| 40 |
// Not applicable to leafs and should not be called, so panic if we do. |
| 41 |
func (n *leaf[T]) setPrefix(pre []byte) { panic("setPrefix called on leaf") } |
| 42 |
func (n *leaf[T]) addChild(_ byte, _ node) { panic("addChild called on leaf") } |
| 43 |
func (n *leaf[T]) findChild(_ byte) *node { panic("findChild called on leaf") } |
| 44 |
func (n *leaf[T]) grow() node { panic("grow called on leaf") } |
| 45 |
func (n *leaf[T]) deleteChild(_ byte) { panic("deleteChild called on leaf") } |
| 46 |
func (n *leaf[T]) shrink() node { panic("shrink called on leaf") } |