-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularbuffer.go
174 lines (153 loc) · 4.05 KB
/
circularbuffer.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
package gocontainers
import "errors"
// CircularBuffer is the basic class in gocontainers.
// There are no public members in this struct.
type CircularBuffer struct {
buffer []interface{}
capacity int
shift int
size int
}
// NewCircularBuffer is the constructor function for CircularBuffer.
func NewCircularBuffer(capacity int) CircularBuffer {
var cb CircularBuffer
cb.buffer = make([]interface{}, capacity)
cb.capacity = capacity
cb.shift = 0
cb.size = 0
return cb
}
// At returns element from CircularBuffer by index.
func (cb *CircularBuffer) At(index int) (interface{}, error) {
if 0 <= index && index < cb.size {
return cb.buffer[(cb.shift+index)%cb.capacity], nil
}
return nil, errors.New("index out of bounds")
}
// Back returns the back element in CircularBuffer.
// In case of empty CircularBuffer nil returns.
func (cb *CircularBuffer) Back() (interface{}, error) {
if cb.Empty() {
return nil, errors.New("empty buffer")
}
v, e := cb.At(cb.Size() - 1)
if e != nil {
return nil, e
}
return v, nil
}
// Capacity returns the maximum possible number elements in CircularBuffer.
func (cb *CircularBuffer) Capacity() int {
return cb.capacity
}
// Clear removes all the data from CircularBuffer.
func (cb *CircularBuffer) Clear() {
for i := 0; i < cb.size; i++ {
cb.buffer[(cb.shift+i)%cb.capacity] = nil
}
cb.size = 0
}
// Do calls function f on each element of the CircularBuffer.
func (cb *CircularBuffer) Do(f func(interface{}) error) error {
for i := 0; i < cb.size; i++ {
v, e := cb.At(i)
if e != nil {
return e
}
e = f(v)
if e != nil {
return e
}
}
return nil
}
// Empty checks if CircularBuffer has no elements.
func (cb *CircularBuffer) Empty() bool {
return cb.size == 0
}
// Front returns the front element in CircularBuffer.
// In case of empty CircularBuffer nil returns.
func (cb *CircularBuffer) Front() (interface{}, error) {
return cb.At(0)
}
// Full checks if CircularBuffer is full.
func (cb *CircularBuffer) Full() bool {
return cb.size == cb.capacity
}
// PopBack removes back element from CircularBuffer.
func (cb *CircularBuffer) PopBack() {
if !cb.Empty() {
cb.buffer[(cb.shift+cb.size-1)%cb.capacity] = nil
cb.size = cb.size - 1
}
}
// PopFront removes front element from CircularBuffer.
func (cb *CircularBuffer) PopFront() {
if !cb.Empty() {
cb.buffer[cb.shift%cb.capacity] = nil
cb.size = cb.size - 1
cb.shift = (cb.shift + 1) % cb.capacity
}
}
// PushBack appends new element into CircularBuffer.
// If CircularBuffer is full, PopFront() will be called.
func (cb *CircularBuffer) PushBack(value interface{}) {
if cb.Full() {
cb.PopFront()
}
cb.buffer[(cb.size+cb.shift)%cb.capacity] = value
cb.size = cb.size + 1
}
// PushFront appends new element into CircularBuffer.
// If CircularBuffer is full, PopBack() will be called.
func (cb *CircularBuffer) PushFront(value interface{}) {
if cb.Full() {
cb.PopBack()
}
index := (cb.shift + cb.capacity - 1) % cb.capacity
cb.buffer[index] = value
cb.shift = index
cb.size = cb.size + 1
}
// Resize affects capacity of CircularBuffer. TODO: Better algorithm.
func (cb *CircularBuffer) Resize(size int) {
cb.shiftToZero()
if size > cb.size {
if len(cb.buffer) < size {
abuffer := make([]interface{}, size-len(cb.buffer))
cb.buffer = append(cb.buffer, abuffer...)
}
} else {
cb.size = size
}
cb.capacity = size
}
// shiftToZero makes shift zero. TODO: Make private.
func (cb *CircularBuffer) shiftToZero() {
var swap = func(i, j int) {
temp := cb.buffer[i]
cb.buffer[i] = cb.buffer[j]
cb.buffer[j] = temp
}
var revert = func(i, j int) {
for k := i; k < (i+j)/2; k++ {
swap(k, j+i-k-1)
}
}
revert(0, cb.shift)
revert(cb.shift, cb.capacity)
revert(0, cb.capacity)
cb.shift = 0
}
// Size returns number of elements in CircularBuffer.
func (cb *CircularBuffer) Size() int {
return cb.size
}
// ToArray converts CircularBuffer to Array. TODO: Better algorithm?
func (cb *CircularBuffer) ToArray() []interface{} {
array := make([]interface{}, cb.size)
for i := 0; i < cb.size; i++ {
array[i], _ = cb.At(i)
}
return array
}