| 1 |
// Copyright 2025 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 |
"iter" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 20 |
) |
| 21 |
|
| 22 |
var _ TreeThreadSafe[string] = (*TreeShiftTreeSlice[string])(nil) |
| 23 |
|
| 24 |
type TreeShiftTreeSlice[T comparable] struct { |
| 25 |
// v points to a specific tree in the slice. |
| 26 |
v sitesmatrix.Vector |
| 27 |
|
| 28 |
// The zero value of T. |
| 29 |
zero T |
| 30 |
|
| 31 |
// trees is a 3D slice that holds all the trees. |
| 32 |
// Note that we have tested a version backed by a map, which is as fast to use, but is twice as epxensive/slow to create. |
| 33 |
trees [][][]*SimpleThreadSafeTree[T] |
| 34 |
} |
| 35 |
|
| 36 |
func NewTreeShiftTree[T comparable](v sitesmatrix.Vector) *TreeShiftTreeSlice[T] { |
| 37 |
trees := make([][][]*SimpleThreadSafeTree[T], v[0]) |
| 38 |
for i := 0; i < v[0]; i++ { |
| 39 |
trees[i] = make([][]*SimpleThreadSafeTree[T], v[1]) |
| 40 |
for j := 0; j < v[1]; j++ { |
| 41 |
trees[i][j] = make([]*SimpleThreadSafeTree[T], v[2]) |
| 42 |
for k := 0; k < v[2]; k++ { |
| 43 |
trees[i][j][k] = NewSimpleThreadSafeTree[T]() |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
return &TreeShiftTreeSlice[T]{trees: trees} |
| 48 |
} |
| 49 |
|
| 50 |
func (t TreeShiftTreeSlice[T]) Shape(v sitesmatrix.Vector) *TreeShiftTreeSlice[T] { |
| 51 |
t.v = v |
| 52 |
return &t |
| 53 |
} |
| 54 |
|
| 55 |
func (t *TreeShiftTreeSlice[T]) tree() *SimpleThreadSafeTree[T] { |
| 56 |
return t.trees[t.v[0]][t.v[1]][t.v[2]] |
| 57 |
} |
| 58 |
|
| 59 |
func (t *TreeShiftTreeSlice[T]) Get(s string) T { |
| 60 |
return t.tree().Get(s) |
| 61 |
} |
| 62 |
|
| 63 |
func (t *TreeShiftTreeSlice[T]) DeleteAllFunc(s string, f func(s string, v T) bool) { |
| 64 |
for tt := range t.Trees() { |
| 65 |
if v := tt.Get(s); v != t.zero { |
| 66 |
if f(s, v) { |
| 67 |
// Delete. |
| 68 |
tt.tree.Delete(s) |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func (t *TreeShiftTreeSlice[T]) Trees() iter.Seq[*SimpleThreadSafeTree[T]] { |
| 75 |
return func(yield func(v *SimpleThreadSafeTree[T]) bool) { |
| 76 |
for _, l1 := range t.trees { |
| 77 |
for _, l2 := range l1 { |
| 78 |
for _, l3 := range l2 { |
| 79 |
if !yield(l3) { |
| 80 |
return |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func (t *TreeShiftTreeSlice[T]) LongestPrefix(s string) (string, T) { |
| 89 |
return t.tree().LongestPrefix(s) |
| 90 |
} |
| 91 |
|
| 92 |
func (t *TreeShiftTreeSlice[T]) Insert(s string, v T) T { |
| 93 |
return t.tree().Insert(s, v) |
| 94 |
} |
| 95 |
|
| 96 |
func (t *TreeShiftTreeSlice[T]) Lock(lockType LockType) { |
| 97 |
t.tree().Lock(lockType) |
| 98 |
} |
| 99 |
|
| 100 |
func (t *TreeShiftTreeSlice[T]) Unlock(lockType LockType) { |
| 101 |
t.tree().Unlock(lockType) |
| 102 |
} |
| 103 |
|
| 104 |
func (t *TreeShiftTreeSlice[T]) WalkPrefix(lockType LockType, s string, f func(s string, v T) (bool, error)) error { |
| 105 |
return t.tree().WalkPrefix(lockType, s, f) |
| 106 |
} |
| 107 |
|
| 108 |
func (t *TreeShiftTreeSlice[T]) WalkPrefixRaw(lockType LockType, s string, f func(s string, v T) (bool, error)) error { |
| 109 |
for tt := range t.Trees() { |
| 110 |
if err := tt.WalkPrefix(lockType, s, f); err != nil { |
| 111 |
return err |
| 112 |
} |
| 113 |
} |
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
func (t *TreeShiftTreeSlice[T]) WalkPath(lockType LockType, s string, f func(s string, v T) (bool, error)) error { |
| 118 |
return t.tree().WalkPath(lockType, s, f) |
| 119 |
} |
| 120 |
|
| 121 |
func (t *TreeShiftTreeSlice[T]) All(lockType LockType) iter.Seq2[string, T] { |
| 122 |
return t.tree().All(lockType) |
| 123 |
} |
| 124 |
|
| 125 |
func (t *TreeShiftTreeSlice[T]) LenRaw() int { |
| 126 |
var count int |
| 127 |
for tt := range t.Trees() { |
| 128 |
count += tt.tree.Len() |
| 129 |
} |
| 130 |
return count |
| 131 |
} |
| 132 |
|
| 133 |
func (t *TreeShiftTreeSlice[T]) Delete(key string) { |
| 134 |
for tt := range t.Trees() { |
| 135 |
tt.tree.Delete(key) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func (t *TreeShiftTreeSlice[T]) DeletePrefix(prefix string) int { |
| 140 |
var count int |
| 141 |
for tt := range t.Trees() { |
| 142 |
count += tt.tree.DeletePrefix(prefix) |
| 143 |
} |
| 144 |
return count |
| 145 |
} |