CircularArray.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. class CircularArray {
  2. constructor(e, t, r) {
  3. this.sum = 0,
  4. this.incomingSum = 0,
  5. this.count = 0,
  6. this.incomingCount = 0,
  7. this.max = 0,
  8. this.incomingMax = 0,
  9. this.goodLess = 0,
  10. this.wellLess = 0,
  11. this.fairLess = 0,
  12. this.badLess = 0,
  13. this.countLess = !1,
  14. this.lessThreshes = [],
  15. this.incomingData = [],
  16. this.circularData = Array(e).fill(-1),
  17. this.circularPtr = 0,
  18. this.circularLength = e,
  19. t && (this.countLess = !0,
  20. this.lessThreshes = r)
  21. }
  22. add(e) {
  23. this.circularData[this.circularPtr] != -1 ? (this.sum -= this.circularData[this.circularPtr],
  24. Math.abs(this.circularData[this.circularPtr] - this.max) < .01 && (this.circularData[this.circularPtr] = -1,
  25. this.max = this.getMax(!1))) : this.count += 1,
  26. this.sum += e,
  27. this.incomingSum += e,
  28. this.incomingCount += 1,
  29. this.max < e && (this.max = e),
  30. this.incomingMax < e && (this.incomingMax = e),
  31. this.circularData[this.circularPtr] = e,
  32. this.circularPtr = (this.circularPtr + 1) % this.circularLength,
  33. this.incomingData.push(e),
  34. this.incomingData.length > this.circularLength && (this.clearIncoming(),
  35. this.incomingCount = 0,
  36. this.incomingSum = 0)
  37. }
  38. computeAvg(e) {
  39. return e.reduce(add, 0) / e.reduce(count_valid, 0) || 0
  40. }
  41. computeMax(e) {
  42. return e.reduce(max, 0) || 0
  43. }
  44. computeThreshPercent(e) {
  45. if (this.countLess) {
  46. const t = count_less(e, this.lessThreshes[0]) || 0
  47. , r = count_less(e, this.lessThreshes[1]) || 0
  48. , n = count_less(e, this.lessThreshes[2]) || 0
  49. , o = count_less(e, this.lessThreshes[3]) || 0
  50. , a = e.reduce(count_valid, 0);
  51. return [t, r, n, o, a]
  52. } else
  53. return [0, 0, 0, 0, 0]
  54. }
  55. getAvg() {
  56. const e = this.sum / this.count || 0
  57. , t = this.computeAvg(this.circularData) || 0;
  58. return Math.abs(e - t) > .01 && console.error("avg value mismatch: ", e, t),
  59. this.computeAvg(this.circularData) || 0
  60. }
  61. getMax(e=!0) {
  62. const t = this.computeMax(this.circularData) || 0;
  63. return e && Math.abs(t - this.max) > .01 && console.error("max value mismatch: ", this.max, t),
  64. this.computeMax(this.circularData) || 0
  65. }
  66. getStandardDeviation() {
  67. return count_sd(this.circularData, this.getAvg())
  68. }
  69. getThreshPercent() {
  70. return this.computeThreshPercent(this.circularData)
  71. }
  72. getIncomingMax() {
  73. return this.computeMax(this.incomingData) || 0
  74. }
  75. getIncomingAvg() {
  76. return this.computeAvg(this.incomingData) || 0
  77. }
  78. getIncomingStandardDeviation() {
  79. return count_sd(this.incomingData, this.getIncomingAvg())
  80. }
  81. getIncomingThreshPercent() {
  82. return this.computeThreshPercent(this.incomingData)
  83. }
  84. clearFastComputeItem() {
  85. this.sum = 0,
  86. this.incomingSum = 0,
  87. this.count = 0,
  88. this.incomingCount = 0,
  89. this.max = 0,
  90. this.incomingMax = 0,
  91. this.goodLess = 0,
  92. this.wellLess = 0,
  93. this.fairLess = 0,
  94. this.badLess = 0
  95. }
  96. clearIncoming() {
  97. for (; this.incomingData.length > 0; )
  98. this.incomingData.pop()
  99. }
  100. clear() {
  101. this.circularData.fill(-1),
  102. this.circularPtr = 0,
  103. this.clearFastComputeItem(),
  104. this.clearIncoming()
  105. }
  106. }