-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathringbuffer.go
More file actions
334 lines (285 loc) · 8.69 KB
/
Copy pathringbuffer.go
File metadata and controls
334 lines (285 loc) · 8.69 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2014 @ericaro. All rights reserved.
// Use of this source code is governed by a Apache License, Version 2.0.
// Package ringbuffer provides "Ring" struct: a ring buffer.
//
//
// A ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.
// in http://en.wikipedia.org/wiki/Circular_buffer .
//
//
// Basic operations on a Ring are:
// Add: add value(s) to the head.
// Remove: remove value(s) from the tail.
// Get : read value from the Ring
// Push: Add and Remove at once. It does not consume any extra memory
//
// More advanced operations are:
// SetCapacity: increase this buffer capacity (preserving its size)
//
//
package ringbuffer
import (
"errors"
"sync"
)
var (
//ErrEmpty is the error returned when the ring is empty, preventing the function completion.
ErrEmpty = errors.New("empty ring buffer")
//ErrFull is the error returned when the ring is full, preventing the function completion.
ErrFull = errors.New("full ring buffer")
)
//Ring is a basic implementation of a circular buffer http://en.wikipedia.org/wiki/Circular_buffer
// or Ring Buffer
type Ring struct {
lock sync.RWMutex
head, size int
buf []interface{}
}
//New creates a new, empty ring buffer.
func New(capacity int) (b *Ring) {
return &Ring{
buf: make([]interface{}, capacity),
head: -1,
}
}
// Add values to the Ring's head, increasing its size.
//
// If you try to add more values than it can, an ErrFull error is returned and no value is actually added.
func (b *Ring) Add(values ...interface{}) error {
if len(values) == 0 {
return nil
}
if len(values) == 1 {
return b.add(values[0])
}
b.lock.Lock()
defer b.lock.Unlock()
//check that we will be able to fill it.
if b.size+len(values) > len(b.buf) {
return ErrFull
}
//alg: add as much as possible in a single copy, and repeat until exhaustion
for len(values) > 0 {
// is all about slicing right
//
// the extra space to add value too can be either
// from next position, to the end of the buffer
// or from the beginning to the tail of the ring
next := Next(1, b.head, len(b.buf))
//tail := Index(-1, b.head, b.size, len(b.buf)
// tail is the absolute index of the buffer tail
// next is the absolute index of the buffer head+1
//I want to write as much as possible after next.
// so it's either till the end, or till the tail
// if the ring's tail is behind we can use the slice from next to the end
// if the ring's tail is ahead we can't use the whole slice.
// BUT, we know that there is enough room left, so we don't care if the slice is "too" big
// Therefore, instead of dealing with all the cases
// we always use:
tgt := b.buf[next:]
// a slice of the buffer, that we'll used to write into
//we copy as much as possible.
n := copy(tgt, values) //n is the number of copied values
if n == 0 { // could not write ! the buf is exhausted
panic(ErrFull) // because we have tested this case before,I'd rather panic than infinite loop
}
// we adjust local variables (latest has moved, and so has size)
b.head = Next(n, b.head, len(b.buf))
b.size += n // increase the inner size
// we remove from the source, the value copied.
values = values[n:]
}
return nil
}
// Remove 'count' items from the ring's tail.
//
// If count is greater than the actual ring's size, the ring size is reset to zero.
func (b *Ring) Remove(count int) {
b.lock.Lock()
defer b.lock.Unlock()
if count <= 0 {
return
}
b.size -= count
if b.size <= 0 {
b.size = 0
b.head = -1 //small trick to mark as empty
}
return
}
//Push is equivalent to Remove then Add 'values' from the ring.
//
// It uses bulk operations (at most two).
func (b *Ring) Push(values ...interface{}) {
if len(values) == 0 || b.size == 0 {
return
}
if len(values) == 1 {
b.push(values[0])
return
}
b.lock.Lock()
defer b.lock.Unlock()
//alg: just write as much as you need after next
// if len(values) is greater than b.size it is useless to fully write it down.
// We know that the first items will be overwritten.
// so we slice down values in that case
if len(values) > b.size {
//only write down the last b.size ones
values = values[len(values)-b.size:] // cut the one before, there are useless.
}
// now we need to write down values (that is never greater than b.size)
// next is the absolute index of the buffer head+1
next := Next(1, b.head, len(b.buf))
// we are going to write down from 'next' toward the end of the buffer.
tgt := b.buf[next:]
//we copy as much as possible.
n := copy(tgt, values) //n is the number of copied values
// if we have exhausted the buffer (we have reached the end of the buffer) we need to start again from zero this time.
if n < len(values) { // there are still values to copy
copy(b.buf, values[n:]) //copy remaining from the begining this time.
}
//move the head
b.head = Next(len(values), b.head, len(b.buf))
}
//Get returns the value in the ring.
//
// Get(0) //retrieve the head
// Get(size-1) //is the oldest
// Get(-1) //is the oldest too
//
func (b *Ring) Get(i int) (interface{}, error) {
b.lock.RLock()
defer b.lock.RUnlock()
if b.size == 0 {
return 0, ErrEmpty
}
position := Index(i, b.head, b.size, len(b.buf))
return b.buf[position], nil
}
//SetCapacity tries to set the ring's capacity.
//
// The ring's content is not altered as a consequence of this operation,
// therefore the final capacity is kept at least equal to the ring's size.
//
// SetCapacity(0) is then equivalent to remove any extra capacity.
func (b *Ring) SetCapacity(capacity int) {
b.lock.Lock()
defer b.lock.Unlock()
if capacity < b.size {
capacity = b.size
}
if capacity == len(b.buf) { //nothing to be done
return
}
nbuf := make([]interface{}, capacity)
// now that the new capacity is enough we just copy down the buffer
//there are only two cases:
// either the values are contiguous, then they goes from
// tail to head
// or there are splitted in two:
// tail to buffer's end
// 0 to head.
head := b.head
tail := Index(-1, head, b.size, len(b.buf))
// we are not going to copy the buffer in the same state (absolute position of head and tail)
// instead, we are going to select the simplest solution.
if tail < head { //data is in one piece
copy(nbuf, b.buf[tail:head+1])
} else { //two pieces
//copy as much as possible to the end of the buf
n := copy(nbuf, b.buf[tail:])
//and then from the beginning
copy(nbuf[n:], b.buf[:head+1])
}
b.buf = nbuf
b.head = b.size - 1
return
}
//Capacity is the max size permitted
func (b *Ring) Capacity() int {
b.lock.RLock()
defer b.lock.RUnlock()
return len(b.buf)
}
//Size returns the ring's size.
func (b *Ring) Size() int {
b.lock.RLock()
defer b.lock.RUnlock()
return b.size
}
//private methods
//push 'value' into the ring and discard the oldest one.
func (b *Ring) push(value interface{}) {
b.lock.Lock()
defer b.lock.Unlock()
if len(b.buf) == 0 || b.size == 0 { // nothing to do
return
}
next := Next(1, b.head, len(b.buf))
b.buf[next] = value
b.head = next
// note that the oldest is auto pruned, when size== capacity, but with the size attribute we know it has been discarded
}
//add 'val' at the Ring's head, it also increases its size.
//If the capacity is exhausted (size == capacity) an error is returned.
func (b *Ring) add(val interface{}) error {
if b.size >= len(b.buf) {
return ErrFull
}
b.lock.Lock()
defer b.lock.Unlock()
next := Next(1, b.head, len(b.buf))
b.buf[next] = val
b.head = next
b.size++ // increase the inner size
return nil
}
//util functions.
// Next computes the next index for a ring buffer
func Next(i, latest, capacity int) int {
n := (latest + i) % capacity
if n < 0 {
n += capacity
}
return n
}
//Index computes absolute position of a ring buffer index.
//
// i, is the ring's index.
//
// head, is the absolute index of the ring's head
//
// size, is the ring' size
//
// capacity is the buffer's capacity.
//
func Index(i, head, size, capacity int) int {
// size=0 is a failure.
if size == 0 {
return -1
}
// first fold i values into ]-size , size[
i = i % size
// then translate negative parts
if i < 0 {
i += size
}
// this way -1 is interpreted as size-1 etc.
// now I've got the real i>=0
// actual theoretical index is simply
// last write minus the required offset.
// last write is lastest
// offset is i, because i==0 means exactly the last written.
//
pos := head - i
//pos might be negative. this is the actual index in the ring buffer.
// if head = 0, previous read is at len(buf)-1
// if head == 0 (and i was zero), pos=-1 (as the above calculation)
//so this is the same as before, negative indexes are added the actual size
for pos < 0 {
pos += capacity
}
// yehaa, pos is the head position.
return pos
}