Debian Codegraph @

queues/circularbuffer/circularbuffer.go

raw symbols SCIP
1 // Copyright (c) 2021, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package circularbuffer implements the circular buffer.
6 //
7 // In computer science, a circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.
8 //
9 // Structure is not thread safe.
10 //
11 // Reference: https://en.wikipedia.org/wiki/Circular_buffer
12 package circularbuffer
13
14 import (
15 "fmt"
16 "strings"
17
18 "github.com/emirpasic/gods/v2/queues"
19 )
20
21 // Assert Queue implementation
22 var _ queues.Queue[int] = (*Queue[int])(nil)
23
24 // Queue holds values in a slice.
25 type Queue[T comparable] struct {
26 values []T
27 start int
28 end int
29 full bool
30 maxSize int
31 size int
32 }
33
34 // New instantiates a new empty queue with the specified size of maximum number of elements that it can hold.
35 // This max size of the buffer cannot be changed.
36 func New[T comparable](maxSize int) *Queue[T] {
37 if maxSize < 1 {
38 panic("Invalid maxSize, should be at least 1")
39 }
40 queue := &Queue[T]{maxSize: maxSize}
41 queue.Clear()
42 return queue
43 }
44
45 // Enqueue adds a value to the end of the queue
46 func (queue *Queue[T]) Enqueue(value T) {
47 if queue.Full() {
48 queue.Dequeue()
49 }
50 queue.values[queue.end] = value
51 queue.end = queue.end + 1
52 if queue.end >= queue.maxSize {
53 queue.end = 0
54 }
55 if queue.end == queue.start {
56 queue.full = true
57 }
58
59 queue.size = queue.calculateSize()
60 }
61
62 // Dequeue removes first element of the queue and returns it, or the 0-value if queue is empty.
63 // Second return parameter is true, unless the queue was empty and there was nothing to dequeue.
64 func (queue *Queue[T]) Dequeue() (value T, ok bool) {
65 if queue.Empty() {
66 return value, false
67 }
68
69 value, ok = queue.values[queue.start], true
70 queue.start = queue.start + 1
71 if queue.start >= queue.maxSize {
72 queue.start = 0
73 }
74 queue.full = false
75 queue.size = queue.size - 1
76
77 return
78 }
79
80 // Peek returns first element of the queue without removing it, or nil if queue is empty.
81 // Second return parameter is true, unless the queue was empty and there was nothing to peek.
82 func (queue *Queue[T]) Peek() (value T, ok bool) {
83 if queue.Empty() {
84 return value, false
85 }
86 return queue.values[queue.start], true
87 }
88
89 // Empty returns true if queue does not contain any elements.
90 func (queue *Queue[T]) Empty() bool {
91 return queue.Size() == 0
92 }
93
94 // Full returns true if the queue is full, i.e. has reached the maximum number of elements that it can hold.
95 func (queue *Queue[T]) Full() bool {
96 return queue.Size() == queue.maxSize
97 }
98
99 // Size returns number of elements within the queue.
100 func (queue *Queue[T]) Size() int {
101 return queue.size
102 }
103
104 // Clear removes all elements from the queue.
105 func (queue *Queue[T]) Clear() {
106 queue.values = make([]T, queue.maxSize, queue.maxSize)
107 queue.start = 0
108 queue.end = 0
109 queue.full = false
110 queue.size = 0
111 }
112
113 // Values returns all elements in the queue (FIFO order).
114 func (queue *Queue[T]) Values() []T {
115 values := make([]T, queue.Size(), queue.Size())
116 for i := 0; i < queue.Size(); i++ {
117 values[i] = queue.values[(queue.start+i)%queue.maxSize]
118 }
119 return values
120 }
121
122 // String returns a string representation of container
123 func (queue *Queue[T]) String() string {
124 str := "CircularBuffer\n"
125 var values []string
126 for _, value := range queue.Values() {
127 values = append(values, fmt.Sprintf("%v", value))
128 }
129 str += strings.Join(values, ", ")
130 return str
131 }
132
133 // Check that the index is within bounds of the list
134 func (queue *Queue[T]) withinRange(index int) bool {
135 return index >= 0 && index < queue.size
136 }
137
138 func (queue *Queue[T]) calculateSize() int {
139 if queue.end < queue.start {
140 return queue.maxSize - queue.start + queue.end
141 } else if queue.end == queue.start {
142 if queue.full {
143 return queue.maxSize
144 }
145 return 0
146 }
147 return queue.end - queue.start
148 }