removed unnecessary variable 'numberOfElementsInWindow' and just used window.Len() instead

This commit is contained in:
Pradyumna Kaushik 2016-12-20 15:03:43 -05:00 committed by Renan DelValle
parent bf6c5eded9
commit 9b5ac0bfa8

View file

@ -22,19 +22,17 @@ type runningAverageCalculator struct {
window list.List window list.List
windowSize int windowSize int
currentSum float64 currentSum float64
numberOfElementsInWindow int
} }
// singleton instance // singleton instance
var racSingleton *runningAverageCalculator var racSingleton *runningAverageCalculator
// return single instance // return single instance
func getInstance(curSum float64, n int, wSize int) *runningAverageCalculator { func getInstance(curSum float64, wSize int) *runningAverageCalculator {
if racSingleton == nil { if racSingleton == nil {
racSingleton = &runningAverageCalculator { racSingleton = &runningAverageCalculator {
windowSize: wSize, windowSize: wSize,
currentSum: curSum, currentSum: curSum,
numberOfElementsInWindow: n,
} }
return racSingleton return racSingleton
} else { } else {
@ -49,9 +47,8 @@ func getInstance(curSum float64, n int, wSize int) *runningAverageCalculator {
// Compute the running average by adding 'data' to the window. // Compute the running average by adding 'data' to the window.
// Updating currentSum to get constant time complexity for every running average computation. // Updating currentSum to get constant time complexity for every running average computation.
func (rac *runningAverageCalculator) calculate(data Interface) float64 { func (rac *runningAverageCalculator) calculate(data Interface) float64 {
if rac.numberOfElementsInWindow < rac.windowSize { if rac.window.Len() < rac.windowSize {
rac.window.PushBack(data) rac.window.PushBack(data)
rac.numberOfElementsInWindow++
rac.currentSum += data.Val() rac.currentSum += data.Val()
} else { } else {
// removing the element at the front of the window. // removing the element at the front of the window.
@ -75,7 +72,6 @@ func (rac *runningAverageCalculator) removeFromWindow(id string) (interface{}, e
if elementToRemove := element.Value.(Interface); elementToRemove.ID() == id { if elementToRemove := element.Value.(Interface); elementToRemove.ID() == id {
rac.window.Remove(element) rac.window.Remove(element)
rac.currentSum -= elementToRemove.Val() rac.currentSum -= elementToRemove.Val()
rac.numberOfElementsInWindow--
return elementToRemove, nil return elementToRemove, nil
} }
} }
@ -84,7 +80,7 @@ func (rac *runningAverageCalculator) removeFromWindow(id string) (interface{}, e
// Taking windowSize as a parameter to allow for sliding window implementation. // Taking windowSize as a parameter to allow for sliding window implementation.
func Calc(data Interface, windowSize int) float64 { func Calc(data Interface, windowSize int) float64 {
rac := getInstance(0.0, 0, windowSize) rac := getInstance(0.0, windowSize)
return rac.calculate(data) return rac.calculate(data)
} }
@ -102,12 +98,11 @@ func Remove(id string) (interface{}, error) {
func Init() { func Init() {
// checking to see if racSingleton needs top be instantiated // checking to see if racSingleton needs top be instantiated
if racSingleton == nil { if racSingleton == nil {
racSingleton = getInstance(0.0, 0, 0) racSingleton = getInstance(0.0, 0)
} }
// Setting parameters to default values. Could also set racSingleton to nil but this leads to unnecessary overhead of creating // 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. // another instance when Calc is called.
racSingleton.window.Init() racSingleton.window.Init()
racSingleton.windowSize = 0 racSingleton.windowSize = 0
racSingleton.currentSum = 0.0 racSingleton.currentSum = 0.0
racSingleton.numberOfElementsInWindow = 0
} }