forked from wcharczuk/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring_buffer.go
217 lines (188 loc) · 4.92 KB
/
ring_buffer.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
package chart
import (
"fmt"
"strings"
)
const (
ringBufferMinimumGrow = 4
ringBufferShrinkThreshold = 32
ringBufferGrowFactor = 200
ringBufferDefaultCapacity = 4
)
var (
emptyArray = make([]interface{}, 0)
)
// NewRingBuffer creates a new, empty, RingBuffer.
func NewRingBuffer() *RingBuffer {
return &RingBuffer{
array: make([]interface{}, ringBufferDefaultCapacity),
head: 0,
tail: 0,
size: 0,
}
}
// NewRingBufferWithCapacity creates a new RingBuffer pre-allocated with the given capacity.
func NewRingBufferWithCapacity(capacity int) *RingBuffer {
return &RingBuffer{
array: make([]interface{}, capacity),
head: 0,
tail: 0,
size: 0,
}
}
// NewRingBufferFromSlice createsa ring buffer out of a slice.
func NewRingBufferFromSlice(values []interface{}) *RingBuffer {
return &RingBuffer{
array: values,
head: 0,
tail: len(values) - 1,
size: len(values),
}
}
// RingBuffer is a fifo buffer that is backed by a pre-allocated array, instead of allocating
// a whole new node object for each element (which saves GC churn).
// Enqueue can be O(n), Dequeue can be O(1).
type RingBuffer struct {
array []interface{}
head int
tail int
size int
}
// Len returns the length of the ring buffer (as it is currently populated).
// Actual memory footprint may be different.
func (rb *RingBuffer) Len() int {
return rb.size
}
// TotalLen returns the total size of the ring bufffer, including empty elements.
func (rb *RingBuffer) TotalLen() int {
return len(rb.array)
}
// Clear removes all objects from the RingBuffer.
func (rb *RingBuffer) Clear() {
if rb.head < rb.tail {
arrayClear(rb.array, rb.head, rb.size)
} else {
arrayClear(rb.array, rb.head, len(rb.array)-rb.head)
arrayClear(rb.array, 0, rb.tail)
}
rb.head = 0
rb.tail = 0
rb.size = 0
}
// Enqueue adds an element to the "back" of the RingBuffer.
func (rb *RingBuffer) Enqueue(object interface{}) {
if rb.size == len(rb.array) {
newCapacity := int(len(rb.array) * int(ringBufferGrowFactor/100))
if newCapacity < (len(rb.array) + ringBufferMinimumGrow) {
newCapacity = len(rb.array) + ringBufferMinimumGrow
}
rb.setCapacity(newCapacity)
}
rb.array[rb.tail] = object
rb.tail = (rb.tail + 1) % len(rb.array)
rb.size++
}
// Dequeue removes the first element from the RingBuffer.
func (rb *RingBuffer) Dequeue() interface{} {
if rb.size == 0 {
return nil
}
removed := rb.array[rb.head]
rb.head = (rb.head + 1) % len(rb.array)
rb.size--
return removed
}
// Peek returns but does not remove the first element.
func (rb *RingBuffer) Peek() interface{} {
if rb.size == 0 {
return nil
}
return rb.array[rb.head]
}
// PeekBack returns but does not remove the last element.
func (rb *RingBuffer) PeekBack() interface{} {
if rb.size == 0 {
return nil
}
if rb.tail == 0 {
return rb.array[len(rb.array)-1]
}
return rb.array[rb.tail-1]
}
func (rb *RingBuffer) setCapacity(capacity int) {
newArray := make([]interface{}, capacity)
if rb.size > 0 {
if rb.head < rb.tail {
arrayCopy(rb.array, rb.head, newArray, 0, rb.size)
} else {
arrayCopy(rb.array, rb.head, newArray, 0, len(rb.array)-rb.head)
arrayCopy(rb.array, 0, newArray, len(rb.array)-rb.head, rb.tail)
}
}
rb.array = newArray
rb.head = 0
if rb.size == capacity {
rb.tail = 0
} else {
rb.tail = rb.size
}
}
// TrimExcess resizes the buffer to better fit the contents.
func (rb *RingBuffer) TrimExcess() {
threshold := float64(len(rb.array)) * 0.9
if rb.size < int(threshold) {
rb.setCapacity(rb.size)
}
}
// AsSlice returns the ring buffer, in order, as a slice.
func (rb *RingBuffer) AsSlice() []interface{} {
newArray := make([]interface{}, rb.size)
if rb.size == 0 {
return newArray
}
if rb.head < rb.tail {
arrayCopy(rb.array, rb.head, newArray, 0, rb.size)
} else {
arrayCopy(rb.array, rb.head, newArray, 0, len(rb.array)-rb.head)
arrayCopy(rb.array, 0, newArray, len(rb.array)-rb.head, rb.tail)
}
return newArray
}
// Each calls the consumer for each element in the buffer.
func (rb *RingBuffer) Each(consumer func(value interface{})) {
if rb.size == 0 {
return
}
if rb.head < rb.tail {
for cursor := rb.head; cursor < rb.tail; cursor++ {
consumer(rb.array[cursor])
}
} else {
for cursor := rb.head; cursor < len(rb.array); cursor++ {
consumer(rb.array[cursor])
}
for cursor := 0; cursor < rb.tail; cursor++ {
consumer(rb.array[cursor])
}
}
}
func (rb *RingBuffer) String() string {
var values []string
for _, elem := range rb.AsSlice() {
values = append(values, fmt.Sprintf("%v", elem))
}
return strings.Join(values, " <= ")
}
func arrayClear(source []interface{}, index, length int) {
for x := 0; x < length; x++ {
absoluteIndex := x + index
source[absoluteIndex] = nil
}
}
func arrayCopy(source []interface{}, sourceIndex int, destination []interface{}, destinationIndex, length int) {
for x := 0; x < length; x++ {
from := sourceIndex + x
to := destinationIndex + x
destination[to] = source[from]
}
}