histogram_iter.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #pragma once
  2. #include "histogram.h"
  3. struct hdr_iter;
  4. namespace NHdr {
  5. /**
  6. * Used for iterating through histogram values.
  7. */
  8. class TBaseHistogramIterator {
  9. public:
  10. /**
  11. * Iterate to the next value for the iterator. If there are no more values
  12. * available return false.
  13. *
  14. * @return 'false' if there are no values remaining for this iterator.
  15. */
  16. bool Next();
  17. /**
  18. * @return Raw index into the counts array.
  19. */
  20. i32 GetCountsIndex() const;
  21. /**
  22. * @return Snapshot of the length at the time the iterator is created.
  23. */
  24. i32 GetTotalCount() const;
  25. /**
  26. * @return Value directly from array for the current countsIndex.
  27. */
  28. i64 GetCount() const;
  29. /**
  30. * @return Sum of all of the counts up to and including the count at
  31. * this index.
  32. */
  33. i64 GetCumulativeCount() const;
  34. /**
  35. * @return The current value based on countsIndex.
  36. */
  37. i64 GetValue() const;
  38. /**
  39. * @return The highest value that is equivalent to the current value
  40. * within the histogram's resolution.
  41. */
  42. i64 GetHighestEquivalentValue() const;
  43. /**
  44. * @return The lowest value that is equivalent to the current value
  45. * within the histogram's resolution.
  46. */
  47. i64 GetLowestEquivalentValue() const;
  48. /**
  49. * @return The value lies in the middle (rounded up) of the range of
  50. * values equivalent the current value.
  51. */
  52. i64 GetMedianEquivalentValue() const;
  53. /**
  54. * @return The actual value level that was iterated from by the iterator.
  55. */
  56. i64 GetValueIteratedFrom() const;
  57. /**
  58. * @return The actual value level that was iterated to by the iterator.
  59. */
  60. i64 GetValueIteratedTo() const;
  61. protected:
  62. // must not be instantiated directly
  63. TBaseHistogramIterator();
  64. ~TBaseHistogramIterator();
  65. protected:
  66. THolder<hdr_iter> Iter_;
  67. };
  68. /**
  69. * Used for iterating through histogram values using the finest granularity
  70. * steps supported by the underlying representation. The iteration steps
  71. * through all possible unit value levels, regardless of whether or not there
  72. * were recorded values for that value level, and terminates when all recorded
  73. * histogram values are exhausted.
  74. */
  75. class TAllValuesIterator: public TBaseHistogramIterator {
  76. public:
  77. /**
  78. * @param histogram The histogram this iterator will operate on
  79. */
  80. explicit TAllValuesIterator(const THistogram& histogram);
  81. };
  82. /**
  83. * Used for iterating through all recorded histogram values using the finest
  84. * granularity steps supported by the underlying representation. The iteration
  85. * steps through all non-zero recorded value counts, and terminates when all
  86. * recorded histogram values are exhausted.
  87. */
  88. class TRecordedValuesIterator: public TBaseHistogramIterator {
  89. public:
  90. /**
  91. * @param histogram The histogram this iterator will operate on
  92. */
  93. explicit TRecordedValuesIterator(const THistogram& histogram);
  94. /**
  95. * @return The count of recorded values in the histogram that were added
  96. * to the totalCount as a result on this iteration step. Since
  97. * multiple iteration steps may occur with overlapping equivalent
  98. * value ranges, the count may be lower than the count found at
  99. * the value (e.g. multiple linear steps or percentile levels can
  100. * occur within a single equivalent value range).
  101. */
  102. i64 GetCountAddedInThisIterationStep() const;
  103. };
  104. /**
  105. * Used for iterating through histogram values according to percentile levels.
  106. * The iteration is performed in steps that start at 0% and reduce their
  107. * distance to 100% according to the <i>percentileTicksPerHalfDistance</i>
  108. * parameter, ultimately reaching 100% when all recorded histogram
  109. * values are exhausted.
  110. */
  111. class TPercentileIterator: public TBaseHistogramIterator {
  112. public:
  113. /**
  114. * @param histogram The histogram this iterator will operate on
  115. * @param ticksPerHalfDistance The number of equal-sized iteration steps
  116. * per half-distance to 100%.
  117. */
  118. TPercentileIterator(const THistogram& histogram, ui32 ticksPerHalfDistance);
  119. /**
  120. * @return The number of equal-sized iteration steps per half-distance
  121. * to 100%.
  122. */
  123. i32 GetTicketsPerHalfDistance() const;
  124. double GetPercentileToIterateTo() const;
  125. /**
  126. * @return The percentile of recorded values in the histogram at values
  127. * equal or smaller than valueIteratedTo.
  128. *
  129. */
  130. double GetPercentile() const;
  131. };
  132. /**
  133. * Used for iterating through histogram values in linear steps. The iteration
  134. * is performed in steps of <i>valueUnitsPerBucket</i> in size, terminating
  135. * when all recorded histogram values are exhausted. Note that each iteration
  136. * "bucket" includes values up to and including the next bucket boundary value.
  137. */
  138. class TLinearIterator: public TBaseHistogramIterator {
  139. public:
  140. /**
  141. * @param histogram The histogram this iterator will operate on
  142. * @param valueUnitsPerBucket The size (in value units) of each bucket
  143. * iteration.
  144. */
  145. TLinearIterator(const THistogram& histogram, i64 valueUnitsPerBucket);
  146. /**
  147. * @return The size (in value units) of each bucket iteration.
  148. */
  149. i64 GetValueUnitsPerBucket() const;
  150. /**
  151. * @return The count of recorded values in the histogram that were added
  152. * to the totalCount as a result on this iteration step. Since
  153. * multiple iteration steps may occur with overlapping equivalent
  154. * value ranges, the count may be lower than the count found at
  155. * the value (e.g. multiple linear steps or percentile levels can
  156. * occur within a single equivalent value range).
  157. */
  158. i64 GetCountAddedInThisIterationStep() const;
  159. i64 GetNextValueReportingLevel() const;
  160. i64 GetNextValueReportingLevelLowestEquivalent() const;
  161. };
  162. /**
  163. * Used for iterating through histogram values in logarithmically increasing
  164. * levels. The iteration is performed in steps that start at
  165. * <i>valueUnitsInFirstBucket</i> and increase exponentially according to
  166. * <i>logBase</i>, terminating when all recorded histogram values are
  167. * exhausted. Note that each iteration "bucket" includes values up to and
  168. * including the next bucket boundary value.
  169. */
  170. class TLogarithmicIterator: public TBaseHistogramIterator {
  171. public:
  172. /**
  173. * @param histogram The histogram this iterator will operate on
  174. * @param valueUnitsInFirstBucket the size (in value units) of the first
  175. * value bucket step
  176. * @param logBase the multiplier by which the bucket size is expanded in
  177. * each iteration step.
  178. */
  179. TLogarithmicIterator(
  180. const THistogram& histogram, i64 valueUnitsInFirstBucket,
  181. double logBase);
  182. /**
  183. * @return The multiplier by which the bucket size is expanded in each
  184. * iteration step.
  185. */
  186. double GetLogBase() const;
  187. /**
  188. * @return The count of recorded values in the histogram that were added
  189. * to the totalCount as a result on this iteration step. Since
  190. * multiple iteration steps may occur with overlapping equivalent
  191. * value ranges, the count may be lower than the count found at
  192. * the value (e.g. multiple linear steps or percentile levels can
  193. * occur within a single equivalent value range).
  194. */
  195. i64 GetCountAddedInThisIterationStep() const;
  196. i64 GetNextValueReportingLevel() const;
  197. i64 GetNextValueReportingLevelLowestEquivalent() const;
  198. };
  199. }