JsHistogramIterator.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const HistogramIterationValue_1 = require("./HistogramIterationValue");
  4. /**
  5. * Used for iterating through histogram values.
  6. */
  7. class JsHistogramIterator /* implements Iterator<HistogramIterationValue> */ {
  8. constructor() {
  9. this.currentIterationValue = new HistogramIterationValue_1.default();
  10. }
  11. resetIterator(histogram) {
  12. this.histogram = histogram;
  13. this.savedHistogramTotalRawCount = histogram.totalCount;
  14. this.arrayTotalCount = histogram.totalCount;
  15. this.currentIndex = 0;
  16. this.currentValueAtIndex = 0;
  17. this.nextValueAtIndex = Math.pow(2, histogram.unitMagnitude);
  18. this.prevValueIteratedTo = 0;
  19. this.totalCountToPrevIndex = 0;
  20. this.totalCountToCurrentIndex = 0;
  21. this.totalValueToCurrentIndex = 0;
  22. this.countAtThisValue = 0;
  23. this.freshSubBucket = true;
  24. this.currentIterationValue.reset();
  25. }
  26. /**
  27. * Returns true if the iteration has more elements. (In other words, returns true if next would return an
  28. * element rather than throwing an exception.)
  29. *
  30. * @return true if the iterator has more elements.
  31. */
  32. hasNext() {
  33. if (this.histogram.totalCount !== this.savedHistogramTotalRawCount) {
  34. throw "Concurrent Modification Exception";
  35. }
  36. return this.totalCountToCurrentIndex < this.arrayTotalCount;
  37. }
  38. /**
  39. * Returns the next element in the iteration.
  40. *
  41. * @return the {@link HistogramIterationValue} associated with the next element in the iteration.
  42. */
  43. next() {
  44. // Move through the sub buckets and buckets until we hit the next reporting level:
  45. while (!this.exhaustedSubBuckets()) {
  46. this.countAtThisValue = this.histogram.getCountAtIndex(this.currentIndex);
  47. if (this.freshSubBucket) {
  48. // Don't add unless we've incremented since last bucket...
  49. this.totalCountToCurrentIndex += this.countAtThisValue;
  50. this.totalValueToCurrentIndex +=
  51. this.countAtThisValue *
  52. this.histogram.highestEquivalentValue(this.currentValueAtIndex);
  53. this.freshSubBucket = false;
  54. }
  55. if (this.reachedIterationLevel()) {
  56. const valueIteratedTo = this.getValueIteratedTo();
  57. Object.assign(this.currentIterationValue, {
  58. valueIteratedTo,
  59. valueIteratedFrom: this.prevValueIteratedTo,
  60. countAtValueIteratedTo: this.countAtThisValue,
  61. countAddedInThisIterationStep: this.totalCountToCurrentIndex - this.totalCountToPrevIndex,
  62. totalCountToThisValue: this.totalCountToCurrentIndex,
  63. totalValueToThisValue: this.totalValueToCurrentIndex,
  64. percentile: (100 * this.totalCountToCurrentIndex) / this.arrayTotalCount,
  65. percentileLevelIteratedTo: this.getPercentileIteratedTo(),
  66. });
  67. this.prevValueIteratedTo = valueIteratedTo;
  68. this.totalCountToPrevIndex = this.totalCountToCurrentIndex;
  69. this.incrementIterationLevel();
  70. if (this.histogram.totalCount !== this.savedHistogramTotalRawCount) {
  71. throw new Error("Concurrent Modification Exception");
  72. }
  73. return this.currentIterationValue;
  74. }
  75. this.incrementSubBucket();
  76. }
  77. throw new Error("Index Out Of Bounds Exception");
  78. }
  79. getPercentileIteratedTo() {
  80. return (100 * this.totalCountToCurrentIndex) / this.arrayTotalCount;
  81. }
  82. getPercentileIteratedFrom() {
  83. return (100 * this.totalCountToPrevIndex) / this.arrayTotalCount;
  84. }
  85. getValueIteratedTo() {
  86. return this.histogram.highestEquivalentValue(this.currentValueAtIndex);
  87. }
  88. exhaustedSubBuckets() {
  89. return this.currentIndex >= this.histogram.countsArrayLength;
  90. }
  91. incrementSubBucket() {
  92. this.freshSubBucket = true;
  93. this.currentIndex++;
  94. this.currentValueAtIndex = this.histogram.valueFromIndex(this.currentIndex);
  95. this.nextValueAtIndex = this.histogram.valueFromIndex(this.currentIndex + 1);
  96. }
  97. }
  98. exports.default = JsHistogramIterator;
  99. //# sourceMappingURL=JsHistogramIterator.js.map