Debian Codegraph @

common/types/evictingqueue.go

raw
1 // Copyright 2017-present 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 types contains types shared between packages in Hugo.
15 package types
16
17 import (
18 "slices"
19 "sync"
20 )
21
22 // EvictingQueue is a queue which automatically evicts elements from the head of
23 // the queue when attempting to add new elements onto the queue and it is full.
24 // This queue orders elements LIFO (last-in-first-out). It throws away duplicates.
25 type EvictingQueue[T comparable] struct {
26 size int
27 vals []T
28 set map[T]bool
29 mu sync.Mutex
30 zero T
31 }
32
33 // NewEvictingQueue creates a new queue with the given size.
34 func NewEvictingQueue[T comparable](size int) *EvictingQueue[T] {
35 return &EvictingQueue[T]{size: size, set: make(map[T]bool)}
36 }
37
38 // Add adds a new string to the tail of the queue if it's not already there.
39 func (q *EvictingQueue[T]) Add(v T) *EvictingQueue[T] {
40 q.mu.Lock()
41 if q.set[v] {
42 q.mu.Unlock()
43 return q
44 }
45
46 if len(q.set) == q.size {
47 // Full
48 delete(q.set, q.vals[0])
49 q.vals = slices.Delete(q.vals, 0, 1)
50 }
51 q.set[v] = true
52 q.vals = append(q.vals, v)
53 q.mu.Unlock()
54
55 return q
56 }
57
58 func (q *EvictingQueue[T]) Len() int {
59 if q == nil {
60 return 0
61 }
62 q.mu.Lock()
63 defer q.mu.Unlock()
64 return len(q.vals)
65 }
66
67 // Contains returns whether the queue contains v.
68 func (q *EvictingQueue[T]) Contains(v T) bool {
69 if q == nil {
70 return false
71 }
72 q.mu.Lock()
73 defer q.mu.Unlock()
74 return q.set[v]
75 }
76
77 // Peek looks at the last element added to the queue.
78 func (q *EvictingQueue[T]) Peek() T {
79 q.mu.Lock()
80 l := len(q.vals)
81 if l == 0 {
82 q.mu.Unlock()
83 return q.zero
84 }
85 elem := q.vals[l-1]
86 q.mu.Unlock()
87 return elem
88 }
89
90 // PeekAll looks at all the elements in the queue, with the newest first.
91 func (q *EvictingQueue[T]) PeekAll() []T {
92 if q == nil {
93 return nil
94 }
95 q.mu.Lock()
96 vals := make([]T, len(q.vals))
97 copy(vals, q.vals)
98 q.mu.Unlock()
99 for i, j := 0, len(vals)-1; i < j; i, j = i+1, j-1 {
100 vals[i], vals[j] = vals[j], vals[i]
101 }
102 return vals
103 }
104
105 // PeekAllSet returns PeekAll as a set.
106 func (q *EvictingQueue[T]) PeekAllSet() map[T]bool {
107 all := q.PeekAll()
108 set := make(map[T]bool)
109 for _, v := range all {
110 set[v] = true
111 }
112
113 return set
114 }