tweaks
This commit is contained in:
parent
814470733e
commit
6a4f3936c6
|
@ -8,10 +8,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
valueBufferMinimumGrow = 4
|
bufferMinimumGrow = 4
|
||||||
valueBufferShrinkThreshold = 32
|
bufferShrinkThreshold = 32
|
||||||
valueBufferGrowFactor = 200
|
bufferGrowFactor = 200
|
||||||
valueBufferDefaultCapacity = 4
|
bufferDefaultCapacity = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -21,7 +21,7 @@ var (
|
||||||
// NewBuffer creates a new value buffer with an optional set of values.
|
// NewBuffer creates a new value buffer with an optional set of values.
|
||||||
func NewBuffer(values ...float64) *Buffer {
|
func NewBuffer(values ...float64) *Buffer {
|
||||||
var tail int
|
var tail int
|
||||||
array := make([]float64, util.Math.MaxInt(len(values), valueBufferDefaultCapacity))
|
array := make([]float64, util.Math.MaxInt(len(values), bufferDefaultCapacity))
|
||||||
if len(values) > 0 {
|
if len(values) > 0 {
|
||||||
copy(array, values)
|
copy(array, values)
|
||||||
tail = len(values)
|
tail = len(values)
|
||||||
|
@ -55,7 +55,7 @@ type Buffer struct {
|
||||||
size int
|
size int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Len returns the length of the ValueBuffer (as it is currently populated).
|
// Len returns the length of the Buffer (as it is currently populated).
|
||||||
// Actual memory footprint may be different.
|
// Actual memory footprint may be different.
|
||||||
func (b *Buffer) Len() int {
|
func (b *Buffer) Len() int {
|
||||||
return b.size
|
return b.size
|
||||||
|
@ -67,12 +67,12 @@ func (b *Buffer) GetValue(index int) float64 {
|
||||||
return b.array[effectiveIndex]
|
return b.array[effectiveIndex]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capacity returns the total size of the ValueBuffer, including empty elements.
|
// Capacity returns the total size of the Buffer, including empty elements.
|
||||||
func (b *Buffer) Capacity() int {
|
func (b *Buffer) Capacity() int {
|
||||||
return len(b.array)
|
return len(b.array)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCapacity sets the capacity of the ValueBuffer.
|
// SetCapacity sets the capacity of the Buffer.
|
||||||
func (b *Buffer) SetCapacity(capacity int) {
|
func (b *Buffer) SetCapacity(capacity int) {
|
||||||
newArray := make([]float64, capacity)
|
newArray := make([]float64, capacity)
|
||||||
if b.size > 0 {
|
if b.size > 0 {
|
||||||
|
@ -92,26 +92,20 @@ func (b *Buffer) SetCapacity(capacity int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear removes all objects from the ValueBuffer.
|
// Clear removes all objects from the Buffer.
|
||||||
func (b *Buffer) Clear() {
|
func (b *Buffer) Clear() {
|
||||||
if b.head < b.tail {
|
b.array = make([]float64, bufferDefaultCapacity)
|
||||||
arrayClear(b.array, b.head, b.size)
|
|
||||||
} else {
|
|
||||||
arrayClear(b.array, b.head, len(b.array)-b.head)
|
|
||||||
arrayClear(b.array, 0, b.tail)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.head = 0
|
b.head = 0
|
||||||
b.tail = 0
|
b.tail = 0
|
||||||
b.size = 0
|
b.size = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enqueue adds an element to the "back" of the ValueBuffer.
|
// Enqueue adds an element to the "back" of the Buffer.
|
||||||
func (b *Buffer) Enqueue(value float64) {
|
func (b *Buffer) Enqueue(value float64) {
|
||||||
if b.size == len(b.array) {
|
if b.size == len(b.array) {
|
||||||
newCapacity := int(len(b.array) * int(valueBufferGrowFactor/100))
|
newCapacity := int(len(b.array) * int(bufferGrowFactor/100))
|
||||||
if newCapacity < (len(b.array) + valueBufferMinimumGrow) {
|
if newCapacity < (len(b.array) + bufferMinimumGrow) {
|
||||||
newCapacity = len(b.array) + valueBufferMinimumGrow
|
newCapacity = len(b.array) + bufferMinimumGrow
|
||||||
}
|
}
|
||||||
b.SetCapacity(newCapacity)
|
b.SetCapacity(newCapacity)
|
||||||
}
|
}
|
||||||
|
@ -152,7 +146,7 @@ func (b *Buffer) PeekBack() float64 {
|
||||||
return b.array[b.tail-1]
|
return b.array[b.tail-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
// TrimExcess resizes the buffer to better fit the contents.
|
// TrimExcess resizes the capacity of the buffer to better fit the contents.
|
||||||
func (b *Buffer) TrimExcess() {
|
func (b *Buffer) TrimExcess() {
|
||||||
threshold := float64(len(b.array)) * 0.9
|
threshold := float64(len(b.array)) * 0.9
|
||||||
if b.size < int(threshold) {
|
if b.size < int(threshold) {
|
||||||
|
|
|
@ -139,3 +139,33 @@ func (ts timeSequence) HoursFilled(xdata []time.Time, ydata []float64) ([]time.T
|
||||||
|
|
||||||
return finalTimes, finalValues
|
return finalTimes, finalValues
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start returns the earliest (min) time in a list of times.
|
||||||
|
func (ts timeSequence) Start(times []time.Time) time.Time {
|
||||||
|
if len(times) == 0 {
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
|
start := times[0]
|
||||||
|
for _, t := range times[1:] {
|
||||||
|
if t.Before(start) {
|
||||||
|
start = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return start
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start returns the earliest (min) time in a list of times.
|
||||||
|
func (ts timeSequence) End(times []time.Time) time.Time {
|
||||||
|
if len(times) == 0 {
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
|
end := times[0]
|
||||||
|
for _, t := range times[1:] {
|
||||||
|
if t.After(end) {
|
||||||
|
end = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return end
|
||||||
|
}
|
||||||
|
|
30
util/date.go
30
util/date.go
|
@ -394,33 +394,3 @@ func (d date) NextDayOfWeek(after time.Time, dayOfWeek time.Weekday) time.Time {
|
||||||
dayDelta := 7 - int(afterWeekday-dayOfWeek)
|
dayDelta := 7 - int(afterWeekday-dayOfWeek)
|
||||||
return after.AddDate(0, 0, dayDelta)
|
return after.AddDate(0, 0, dayDelta)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start returns the earliest (min) time in a list of times.
|
|
||||||
func (d date) Start(times []time.Time) time.Time {
|
|
||||||
if len(times) == 0 {
|
|
||||||
return time.Time{}
|
|
||||||
}
|
|
||||||
|
|
||||||
start := times[0]
|
|
||||||
for _, t := range times[1:] {
|
|
||||||
if t.Before(start) {
|
|
||||||
start = t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return start
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start returns the earliest (min) time in a list of times.
|
|
||||||
func (d date) End(times []time.Time) time.Time {
|
|
||||||
if len(times) == 0 {
|
|
||||||
return time.Time{}
|
|
||||||
}
|
|
||||||
|
|
||||||
end := times[0]
|
|
||||||
for _, t := range times[1:] {
|
|
||||||
if t.After(end) {
|
|
||||||
end = t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return end
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user