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