github.com/neohugo/neohugo@v0.123.8/hugolib/doctree/simpletree.go (about) 1 // Copyright 2024 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 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 doctree 15 16 import ( 17 "sync" 18 19 radix "github.com/armon/go-radix" 20 ) 21 22 // Tree is a radix tree that holds T. 23 type Tree[T any] interface { 24 Get(s string) T 25 LongestPrefix(s string) (string, T) 26 Insert(s string, v T) T 27 WalkPrefix(lockType LockType, s string, f func(s string, v T) (bool, error)) error 28 } 29 30 // NewSimpleTree creates a new SimpleTree. 31 func NewSimpleTree[T any]() *SimpleTree[T] { 32 return &SimpleTree[T]{tree: radix.New()} 33 } 34 35 // SimpleTree is a thread safe radix tree that holds T. 36 type SimpleTree[T any] struct { 37 mu sync.RWMutex 38 tree *radix.Tree 39 zero T 40 } 41 42 func (tree *SimpleTree[T]) Get(s string) T { 43 tree.mu.RLock() 44 defer tree.mu.RUnlock() 45 46 if v, ok := tree.tree.Get(s); ok { 47 return v.(T) 48 } 49 return tree.zero 50 } 51 52 func (tree *SimpleTree[T]) LongestPrefix(s string) (string, T) { 53 tree.mu.RLock() 54 defer tree.mu.RUnlock() 55 56 if s, v, ok := tree.tree.LongestPrefix(s); ok { 57 return s, v.(T) 58 } 59 return "", tree.zero 60 } 61 62 func (tree *SimpleTree[T]) Insert(s string, v T) T { 63 tree.mu.Lock() 64 defer tree.mu.Unlock() 65 66 tree.tree.Insert(s, v) 67 return v 68 } 69 70 func (tree *SimpleTree[T]) WalkPrefix(lockType LockType, s string, f func(s string, v T) (bool, error)) error { 71 switch lockType { 72 case LockTypeNone: 73 case LockTypeRead: 74 tree.mu.RLock() 75 defer tree.mu.RUnlock() 76 case LockTypeWrite: 77 tree.mu.Lock() 78 defer tree.mu.Unlock() 79 } 80 var err error 81 tree.tree.WalkPrefix(s, func(s string, v any) bool { 82 var b bool 83 b, err = f(s, v.(T)) 84 if err != nil { 85 return true 86 } 87 return b 88 }) 89 90 return err 91 }