CircularArray.js 4.3 KB

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