-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer.go
147 lines (127 loc) · 3.09 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
package array
import "fmt"
type RingBuffer[T any] struct {
head int
tail int
Length int
data []T
}
func (ringBuffer *RingBuffer[T]) Shift(item T) {
if ringBuffer.shouldGrow() {
ringBuffer.Grow()
}
if ringBuffer.head == 0 {
ringBuffer.head = len(ringBuffer.data) - 1
} else {
ringBuffer.head = ringBuffer.head - 1
}
ringBuffer.data[ringBuffer.head] = item
ringBuffer.Length += 1
}
func (ringBuf *RingBuffer[T]) shouldGrow() bool {
if len(ringBuf.data) == 0 {
return true
}
return ringBuf.Length+1 > len(ringBuf.data)
}
func (ringBuf *RingBuffer[T]) UnShift() (value T) {
if ringBuf.IsEmpty() {
return
}
var zeroValue T
value = ringBuf.data[ringBuf.head]
ringBuf.data[ringBuf.head] = zeroValue
ringBuf.Length -= 1
ringBuf.head = (ringBuf.head + 1) % len(ringBuf.data)
return
}
func (ringBuf *RingBuffer[T]) IsEmpty() bool {
return ringBuf.Length == 0
}
func (ringBuf *RingBuffer[T]) Push(value T) {
if ringBuf.shouldGrow() {
ringBuf.Grow()
}
if ringBuf.IsEmpty() {
ringBuf.head = ringBuf.tail
ringBuf.data[ringBuf.tail] = value
} else {
ringBuf.tail = (ringBuf.tail + 1) % len(ringBuf.data)
ringBuf.data[ringBuf.tail] = value
}
ringBuf.Length += 1
}
func (ringBuf *RingBuffer[T]) Pop() (value T) {
if ringBuf.IsEmpty() {
return value
}
var zeroValue T
value = ringBuf.data[ringBuf.tail]
ringBuf.data[ringBuf.tail] = zeroValue
if ringBuf.tail == 0 {
ringBuf.tail = len(ringBuf.data) - 1
} else {
ringBuf.tail -= 1
}
ringBuf.Length -= 1
return
}
func (ringBuf *RingBuffer[T]) Grow() {
oldDataLength := len(ringBuf.data)
newArray := make([]T, len(ringBuf.data)+(len(ringBuf.data)/2)+1)
for i := 0; i < oldDataLength; i++ {
newArray[i] = ringBuf.Get(i)
}
ringBuf.data = newArray
ringBuf.head = 0
ringBuf.tail = oldDataLength - 1
}
func (ringBuf *RingBuffer[T]) Get(index int) (value T) {
accessIndex := (ringBuf.head + index) % len(ringBuf.data)
return ringBuf.data[accessIndex]
}
func (ringBuf *RingBuffer[T]) String() string {
str := ""
for i := 0; i < ringBuf.Length; i++ {
str += fmt.Sprintf("%v ", ringBuf.Get(i))
}
return str
}
func NewRingBuffer[T any](items ...T) *RingBuffer[T] {
tail := len(items) - 1
if tail < 0 {
tail = 0
}
return &RingBuffer[T]{
head: 0,
tail: tail,
data: items,
Length: len(items),
}
}
func RunRingBufferExample() {
ringBuf := NewRingBuffer[int]()
examples := []int{5, 6, 7, 8}
fmt.Println("Shifting ringBuf ==>")
for _, value := range examples {
ringBuf.Shift(value)
fmt.Printf("After shifting %v: %v\n", value, ringBuf)
}
fmt.Println("After shifting length:", ringBuf.Length)
fmt.Println("Unshifting ringBuf ==>")
for ringBuf.Length > 0 {
value := ringBuf.UnShift()
fmt.Printf("After unshifting %v: %v\n", value, ringBuf)
}
fmt.Println("After unshifting length:", ringBuf.Length)
fmt.Println("Pushing ring buffer ==>")
for _, value := range examples {
ringBuf.Push(value)
fmt.Printf("After pushing %v: %v\n", value, ringBuf)
}
fmt.Println("Popping ring buffer ==>")
for ringBuf.Length > 0 {
popped := ringBuf.Pop()
fmt.Printf("After popping %v: %v\n", popped, ringBuf)
}
}