-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathringbuffer.go
232 lines (203 loc) · 4.75 KB
/
ringbuffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// https://github.com/emirpasic/gods/blob/master/queues/circularbuffer/circularbuffer.go
// ringbuffer 是一种队列的实现方式,它是一个环形的缓冲区,可以用来实现队列、栈等数据结构
// 经过工业场景验证过的 ringbuffer 库:disruptor
// go的 buffered chan 是一个 ringbuffer
//
// RingCycle/RingBuffer
package main
import (
"fmt"
"strings"
)
func main() {
q := NewRingBuffer[int](3)
q.Append(1)
q.Append(2)
q.Append(3)
q.Append(4)
fmt.Println(q) // RingBuffer\n1, 2, 3
fmt.Println(q.Size())
fmt.Println(q.Popleft()) // 2
fmt.Println(q) // RingBuffer\n3, 4
fmt.Println(q.Head()) // 3
fmt.Println(q.At(0)) // 3
fmt.Println(q.At(1)) // 4
fmt.Println(q.At(-1)) // 4
}
// 固定大小的环形缓冲区, 可用于双端队列、固定大小滑动窗口等场景.
type RingBuffer[T comparable] struct {
values []T
start int
end int
maxSize int
size int
}
func NewRingBuffer[T comparable](maxSize int) *RingBuffer[T] {
if maxSize < 1 {
panic("Invalid maxSize, should be at least 1")
}
queue := &RingBuffer[T]{values: make([]T, maxSize, maxSize), maxSize: maxSize}
return queue
}
func (queue *RingBuffer[T]) Append(value T) {
if queue.Full() {
queue.Popleft()
}
queue.values[queue.end] = value
queue.end++
if queue.end >= queue.maxSize {
queue.end = 0
}
queue.size++
}
func (queue *RingBuffer[T]) AppendLeft(value T) {
if queue.Full() {
queue.Pop()
}
queue.start--
if queue.start < 0 {
queue.start = queue.maxSize - 1
}
queue.values[queue.start] = value
queue.size++
}
func (queue *RingBuffer[T]) Pop() T {
if queue.Empty() {
panic("RingBuffer is empty")
}
queue.size--
queue.end--
if queue.end < 0 {
queue.end = queue.maxSize - 1
}
value := queue.values[queue.end]
return value
}
func (queue *RingBuffer[T]) Popleft() T {
if queue.Empty() {
panic("RingBuffer is empty")
}
queue.size--
value := queue.values[queue.start]
queue.start++
if queue.start >= queue.maxSize {
queue.start = 0
}
return value
}
func (queue *RingBuffer[T]) Head() T {
if queue.Empty() {
panic("RingBuffer is empty")
}
return queue.values[queue.start]
}
func (queue *RingBuffer[T]) Tail() T {
if queue.Empty() {
panic("RingBuffer is empty")
}
index := queue.end - 1
if index < 0 {
index = queue.maxSize - 1
}
return queue.values[index]
}
func (queue *RingBuffer[T]) At(index int) T {
size := queue.Size()
if index < 0 {
index += size
}
if index < 0 || index >= size {
panic("Index out of range")
}
index += queue.start
if index >= queue.maxSize {
index -= queue.maxSize
}
return queue.values[index]
}
func (queue *RingBuffer[T]) Empty() bool {
return queue.Size() == 0
}
func (queue *RingBuffer[T]) Full() bool {
return queue.Size() == queue.maxSize
}
func (queue *RingBuffer[T]) Size() int {
return queue.size
}
func (queue *RingBuffer[T]) Clear() {
queue.start = 0
queue.end = 0
queue.size = 0
}
func (queue *RingBuffer[T]) ForEach(f func(value T)) {
ptr := queue.start
for i := 0; i < queue.Size(); i++ {
f(queue.values[ptr])
ptr++
if ptr >= queue.maxSize {
ptr = 0
}
}
}
func (queue *RingBuffer[T]) String() string {
str := "RingBuffer\n"
var values []string
queue.ForEach(func(value T) { values = append(values, fmt.Sprintf("%v", value)) })
str += strings.Join(values, ", ")
return str
}
// 641. 设计循环双端队列
// https://leetcode.cn/problems/design-circular-deque/description/
type MyCircularDeque struct {
k int
queue *RingBuffer[int]
}
func Constructor(k int) MyCircularDeque {
return MyCircularDeque{k: k, queue: NewRingBuffer[int](k)}
}
func (this *MyCircularDeque) InsertFront(value int) bool {
if this.queue.Size() == this.k {
return false
}
this.queue.AppendLeft(value)
return true
}
func (this *MyCircularDeque) InsertLast(value int) bool {
if this.queue.Size() == this.k {
return false
}
this.queue.Append(value)
return true
}
func (this *MyCircularDeque) DeleteFront() bool {
if this.queue.Size() == 0 {
return false
}
this.queue.Popleft()
return true
}
func (this *MyCircularDeque) DeleteLast() bool {
if this.queue.Size() == 0 {
return false
}
this.queue.Pop()
return true
}
func (this *MyCircularDeque) GetFront() int {
if this.queue.Size() == 0 {
return -1
}
return this.queue.Head()
}
func (this *MyCircularDeque) GetRear() int {
if this.queue.Size() == 0 {
return -1
}
return this.queue.Tail()
}
func (this *MyCircularDeque) IsEmpty() bool {
return this.queue.Size() == 0
}
func (this *MyCircularDeque) IsFull() bool {
return this.queue.Size() == this.k
}