| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
const ( |
| 9 |
bufferMinimumGrow = 4 |
| 10 |
bufferShrinkThreshold = 32 |
| 11 |
bufferGrowFactor = 200 |
| 12 |
bufferDefaultCapacity = 4 |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
func NewValueBuffer(values ...float64) *ValueBuffer { |
| 17 |
var tail int |
| 18 |
array := make([]float64, MaxInt(len(values), bufferDefaultCapacity)) |
| 19 |
if len(values) > 0 { |
| 20 |
copy(array, values) |
| 21 |
tail = len(values) |
| 22 |
} |
| 23 |
return &ValueBuffer{ |
| 24 |
array: array, |
| 25 |
head: 0, |
| 26 |
tail: tail, |
| 27 |
size: len(values), |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func NewValueBufferWithCapacity(capacity int) *ValueBuffer { |
| 33 |
return &ValueBuffer{ |
| 34 |
array: make([]float64, capacity), |
| 35 |
head: 0, |
| 36 |
tail: 0, |
| 37 |
size: 0, |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
type ValueBuffer struct { |
| 46 |
array []float64 |
| 47 |
head int |
| 48 |
tail int |
| 49 |
size int |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func (b *ValueBuffer) Len() int { |
| 55 |
return b.size |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
func (b *ValueBuffer) GetValue(index int) float64 { |
| 60 |
effectiveIndex := (b.head + index) % len(b.array) |
| 61 |
return b.array[effectiveIndex] |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
func (b *ValueBuffer) Capacity() int { |
| 66 |
return len(b.array) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (b *ValueBuffer) SetCapacity(capacity int) { |
| 71 |
newArray := make([]float64, capacity) |
| 72 |
if b.size > 0 { |
| 73 |
if b.head < b.tail { |
| 74 |
arrayCopy(b.array, b.head, newArray, 0, b.size) |
| 75 |
} else { |
| 76 |
arrayCopy(b.array, b.head, newArray, 0, len(b.array)-b.head) |
| 77 |
arrayCopy(b.array, 0, newArray, len(b.array)-b.head, b.tail) |
| 78 |
} |
| 79 |
} |
| 80 |
b.array = newArray |
| 81 |
b.head = 0 |
| 82 |
if b.size == capacity { |
| 83 |
b.tail = 0 |
| 84 |
} else { |
| 85 |
b.tail = b.size |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
func (b *ValueBuffer) Clear() { |
| 91 |
b.array = make([]float64, bufferDefaultCapacity) |
| 92 |
b.head = 0 |
| 93 |
b.tail = 0 |
| 94 |
b.size = 0 |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func (b *ValueBuffer) Enqueue(value float64) { |
| 99 |
if b.size == len(b.array) { |
| 100 |
newCapacity := int(len(b.array) * int(bufferGrowFactor/100)) |
| 101 |
if newCapacity < (len(b.array) + bufferMinimumGrow) { |
| 102 |
newCapacity = len(b.array) + bufferMinimumGrow |
| 103 |
} |
| 104 |
b.SetCapacity(newCapacity) |
| 105 |
} |
| 106 |
|
| 107 |
b.array[b.tail] = value |
| 108 |
b.tail = (b.tail + 1) % len(b.array) |
| 109 |
b.size++ |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
func (b *ValueBuffer) Dequeue() float64 { |
| 114 |
if b.size == 0 { |
| 115 |
return 0 |
| 116 |
} |
| 117 |
|
| 118 |
removed := b.array[b.head] |
| 119 |
b.head = (b.head + 1) % len(b.array) |
| 120 |
b.size-- |
| 121 |
return removed |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func (b *ValueBuffer) Peek() float64 { |
| 126 |
if b.size == 0 { |
| 127 |
return 0 |
| 128 |
} |
| 129 |
return b.array[b.head] |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
func (b *ValueBuffer) PeekBack() float64 { |
| 134 |
if b.size == 0 { |
| 135 |
return 0 |
| 136 |
} |
| 137 |
if b.tail == 0 { |
| 138 |
return b.array[len(b.array)-1] |
| 139 |
} |
| 140 |
return b.array[b.tail-1] |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func (b *ValueBuffer) TrimExcess() { |
| 145 |
threshold := float64(len(b.array)) * 0.9 |
| 146 |
if b.size < int(threshold) { |
| 147 |
b.SetCapacity(b.size) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
func (b *ValueBuffer) Array() Array { |
| 153 |
newArray := make([]float64, b.size) |
| 154 |
|
| 155 |
if b.size == 0 { |
| 156 |
return newArray |
| 157 |
} |
| 158 |
|
| 159 |
if b.head < b.tail { |
| 160 |
arrayCopy(b.array, b.head, newArray, 0, b.size) |
| 161 |
} else { |
| 162 |
arrayCopy(b.array, b.head, newArray, 0, len(b.array)-b.head) |
| 163 |
arrayCopy(b.array, 0, newArray, len(b.array)-b.head, b.tail) |
| 164 |
} |
| 165 |
|
| 166 |
return Array(newArray) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
func (b *ValueBuffer) Each(mapfn func(int, float64)) { |
| 171 |
if b.size == 0 { |
| 172 |
return |
| 173 |
} |
| 174 |
|
| 175 |
var index int |
| 176 |
if b.head < b.tail { |
| 177 |
for cursor := b.head; cursor < b.tail; cursor++ { |
| 178 |
mapfn(index, b.array[cursor]) |
| 179 |
index++ |
| 180 |
} |
| 181 |
} else { |
| 182 |
for cursor := b.head; cursor < len(b.array); cursor++ { |
| 183 |
mapfn(index, b.array[cursor]) |
| 184 |
index++ |
| 185 |
} |
| 186 |
for cursor := 0; cursor < b.tail; cursor++ { |
| 187 |
mapfn(index, b.array[cursor]) |
| 188 |
index++ |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
func (b *ValueBuffer) String() string { |
| 195 |
var values []string |
| 196 |
for _, elem := range b.Array() { |
| 197 |
values = append(values, fmt.Sprintf("%v", elem)) |
| 198 |
} |
| 199 |
return strings.Join(values, " <= ") |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
func arrayClear(source []float64, index, length int) { |
| 207 |
for x := 0; x < length; x++ { |
| 208 |
absoluteIndex := x + index |
| 209 |
source[absoluteIndex] = 0 |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func arrayCopy(source []float64, sourceIndex int, destination []float64, destinationIndex, length int) { |
| 214 |
for x := 0; x < length; x++ { |
| 215 |
from := sourceIndex + x |
| 216 |
to := destinationIndex + x |
| 217 |
|
| 218 |
destination[to] = source[from] |
| 219 |
} |
| 220 |
} |