Debian Codegraph @

common/collections/stack.go

raw
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 collections
15
16 import (
17 "iter"
18 "slices"
19 "sync"
20
21 "github.com/gohugoio/hugo/common/hiter"
22 )
23
24 // StackThreadSafe is a simple LIFO stack that is safe for concurrent use.
25 type StackThreadSafe[T any] struct {
26 items []T
27 zero T
28 mu sync.RWMutex
29 }
30
31 func NewStackThreadSafe[T any]() *StackThreadSafe[T] {
32 return &StackThreadSafe[T]{}
33 }
34
35 func (s *StackThreadSafe[T]) Push(item T) {
36 s.mu.Lock()
37 defer s.mu.Unlock()
38 s.items = append(s.items, item)
39 }
40
41 func (s *StackThreadSafe[T]) Pop() (T, bool) {
42 s.mu.Lock()
43 defer s.mu.Unlock()
44 if len(s.items) == 0 {
45 return s.zero, false
46 }
47 item := s.items[len(s.items)-1]
48 s.items = s.items[:len(s.items)-1]
49 return item, true
50 }
51
52 func (s *StackThreadSafe[T]) Peek() (T, bool) {
53 s.mu.RLock()
54 defer s.mu.RUnlock()
55 if len(s.items) == 0 {
56 return s.zero, false
57 }
58 return s.items[len(s.items)-1], true
59 }
60
61 func (s *StackThreadSafe[T]) Len() int {
62 s.mu.RLock()
63 defer s.mu.RUnlock()
64 return len(s.items)
65 }
66
67 // All returns all items in the stack, from bottom to top.
68 func (s *StackThreadSafe[T]) All() iter.Seq2[int, T] {
69 return hiter.Lock2(slices.All(s.items), s.mu.RLock, s.mu.RUnlock)
70 }
71
72 func (s *StackThreadSafe[T]) Drain() []T {
73 s.mu.Lock()
74 defer s.mu.Unlock()
75 items := s.items
76 s.items = nil
77 return items
78 }
79
80 func (s *StackThreadSafe[T]) DrainMatching(predicate func(T) bool) []T {
81 s.mu.Lock()
82 defer s.mu.Unlock()
83 var items []T
84 for i := len(s.items) - 1; i >= 0; i-- {
85 if predicate(s.items[i]) {
86 items = append(items, s.items[i])
87 s.items = slices.Delete(s.items, i, i+1)
88 }
89 }
90 return items
91 }
92
93 // Stack is a simple LIFO stack that is not safe for concurrent use.
94 type Stack[T any] struct {
95 items []T
96 zero T
97 }
98
99 func NewStack[T any]() *Stack[T] {
100 return &Stack[T]{}
101 }
102
103 func (s *Stack[T]) Push(item T) {
104 s.items = append(s.items, item)
105 }
106
107 func (s *Stack[T]) Pop() (T, bool) {
108 if len(s.items) == 0 {
109 return s.zero, false
110 }
111 item := s.items[len(s.items)-1]
112 s.items = s.items[:len(s.items)-1]
113 return item, true
114 }
115
116 func (s *Stack[T]) Peek() (T, bool) {
117 if len(s.items) == 0 {
118 return s.zero, false
119 }
120 return s.items[len(s.items)-1], true
121 }
122
123 func (s *Stack[T]) Len() int {
124 return len(s.items)
125 }
126
127 func (s *Stack[T]) All() iter.Seq2[int, T] {
128 return slices.All(s.items)
129 }
130
131 func (s *Stack[T]) Drain() []T {
132 items := s.items
133 s.items = nil
134 return items
135 }