-
Notifications
You must be signed in to change notification settings - Fork 3
/
fixed-array.js
75 lines (71 loc) · 2.18 KB
/
fixed-array.js
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
module.exports = FixedValueHistory
// A fixed-length storage mechanism for a value (ideally a number)
// with some statistics methods. Expires oldest values to
// Input is not type checked, but non-numeric input will be considered NULL
// WRT the statistical methods.
function FixedValueHistory(maxLength, initial) {
if (!(this instanceof FixedValueHistory))
return new FixedValueHistory(maxLength, initial)
if (!isNumber(maxLength) || maxLength == 0) {
throw new Error("maxLength must be a positive number.")
}
this.maxLength = Math.floor(+maxLength)
if (initial != null) {
this.push(initial)
}
}
FixedValueHistory.prototype.sum = 0
FixedValueHistory.prototype.maxLength = undefined
FixedValueHistory.prototype.array = []
FixedValueHistory.prototype.push = function (vals) {
this.array = this.array.concat(vals)
var expired = this.array.splice(0, this.array.length - this.maxLength)
var expired_sum = FixedValueHistory.sum(expired)
this.sum -= expired_sum
var new_sum = FixedValueHistory.sum(vals)
this.sum += new_sum
}
FixedValueHistory.prototype.mean = function () {
if (this.array.length === 0) return NaN
return this.sum / this.array.length
}
FixedValueHistory.prototype.variance = function () {
if (this.array.length === 0) return NaN
var mean = this.mean()
return this.array.reduce(function(prevVal,currVal) {
var diff
if(isNumber(currVal)) {
diff = currVal - mean
} else {
diff = mean
}
return prevVal + diff*diff
}, 0) / this.array.length
}
FixedValueHistory.prototype.max = function () {
if (this.array.length === 0) return NaN
return Math.max.apply(null, this.array)
}
FixedValueHistory.prototype.min = function () {
if (this.array.length === 0) return NaN
return Math.min.apply(null, this.array)
}
FixedValueHistory.prototype.length = function () {
return this.array.length
}
FixedValueHistory.prototype.values = function () {
return this.array.slice(0)
}
// Static methods.
FixedValueHistory.sum = function (vals) {
var sum = 0
;[].concat(vals).forEach(function (n) {
if (isNumber(n)) {
sum += n
}
})
return sum
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n)
}