github.com/fighterlyt/hugo@v0.47.1/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) {
    39  	q.mu.Lock()
    40  	if q.set[v] {
    41  		q.mu.Unlock()
    42  		return
    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  
    55  // Peek looks at the last element added to the queue.
    56  func (q *EvictingStringQueue) Peek() string {
    57  	q.mu.Lock()
    58  	l := len(q.vals)
    59  	if l == 0 {
    60  		q.mu.Unlock()
    61  		return ""
    62  	}
    63  	elem := q.vals[l-1]
    64  	q.mu.Unlock()
    65  	return elem
    66  }
    67  
    68  // PeekAll looks at all the elements in the queue, with the newest first.
    69  func (q *EvictingStringQueue) PeekAll() []string {
    70  	q.mu.Lock()
    71  	vals := make([]string, len(q.vals))
    72  	copy(vals, q.vals)
    73  	q.mu.Unlock()
    74  	for i, j := 0, len(vals)-1; i < j; i, j = i+1, j-1 {
    75  		vals[i], vals[j] = vals[j], vals[i]
    76  	}
    77  	return vals
    78  }
    79  
    80  // PeekAllSet returns PeekAll as a set.
    81  func (q *EvictingStringQueue) PeekAllSet() map[string]bool {
    82  	all := q.PeekAll()
    83  	set := make(map[string]bool)
    84  	for _, v := range all {
    85  		set[v] = true
    86  	}
    87  
    88  	return set
    89  }