This repository has been archived on 2024-04-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
elektron/utilities/runAvg/runAvg.go

108 lines
3 KiB
Go
Raw Normal View History

2016-12-19 16:30:03 -05:00
/*
A utility to calculate the running average.
One should implement Val() to be able to use this utility.
*/
package runAvg
import (
"errors"
"container/list"
)
2016-12-19 16:30:03 -05:00
type Interface interface {
// Value to use for running average calculation.
Val() float64
// Unique ID
ID() string
2016-12-19 16:30:03 -05:00
}
type runningAverageCalculator struct {
window list.List
windowSize int
currentSum float64
}
// singleton instance
var racSingleton *runningAverageCalculator
// return single instance
func getInstance(curSum float64, wSize int) *runningAverageCalculator {
2016-12-19 16:30:03 -05:00
if racSingleton == nil {
racSingleton = &runningAverageCalculator {
windowSize: wSize,
currentSum: curSum,
}
return racSingleton
} else {
// Updating window size if a new window size is given.
if wSize != racSingleton.windowSize {
racSingleton.windowSize = wSize
}
return racSingleton
}
}
// Compute the running average by adding 'data' to the window.
// Updating currentSum to get constant time complexity for every running average computation.
func (rac *runningAverageCalculator) calculate(data Interface) float64 {
if rac.window.Len() < rac.windowSize {
2016-12-19 16:30:03 -05:00
rac.window.PushBack(data)
rac.currentSum += data.Val()
} else {
// removing the element at the front of the window.
elementToRemove := rac.window.Front()
rac.currentSum -= elementToRemove.Value.(Interface).Val()
rac.window.Remove(elementToRemove)
// adding new element to the window
rac.window.PushBack(data)
rac.currentSum += data.Val()
}
return rac.currentSum / float64(rac.window.Len())
}
/*
If element with given ID present in the window, then remove it and return (removeElement, nil).
Else, return (nil, error)
*/
func (rac *runningAverageCalculator) removeFromWindow(id string) (interface{}, error) {
for element := rac.window.Front(); element != nil; element = element.Next() {
if elementToRemove := element.Value.(Interface); elementToRemove.ID() == id {
rac.window.Remove(element)
rac.currentSum -= elementToRemove.Val()
return elementToRemove, nil
}
}
return nil, errors.New("Error: Element not found in the window.")
}
2016-12-19 16:30:03 -05:00
// Taking windowSize as a parameter to allow for sliding window implementation.
func Calc(data Interface, windowSize int) float64 {
rac := getInstance(0.0, windowSize)
2016-12-19 16:30:03 -05:00
return rac.calculate(data)
}
// Remove element from the window if it is present.
func Remove(id string) (interface{}, error) {
// checking if racSingleton has been instantiated
if racSingleton == nil {
return nil, errors.New("Error: Not instantiated. Please call Init() to instantiate.")
} else {
return racSingleton.removeFromWindow(id)
}
}
2016-12-19 16:30:03 -05:00
// initialize the parameters of the running average calculator
func Init() {
// checking to see if racSingleton needs top be instantiated
if racSingleton == nil {
racSingleton = getInstance(0.0, 0)
}
2016-12-19 16:30:03 -05:00
// Setting parameters to default values. Could also set racSingleton to nil but this leads to unnecessary overhead of creating
// another instance when Calc is called.
racSingleton.window.Init()
racSingleton.windowSize = 0
racSingleton.currentSum = 0.0
}