histogram.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <util/generic/vector.h>
  4. namespace NKiwiAggr {
  5. class THistogram;
  6. class THistoRec;
  7. class IHistogram;
  8. typedef TAtomicSharedPtr<IHistogram> IHistogramPtr;
  9. class IHistogram {
  10. public:
  11. // Supposed constructors:
  12. //
  13. // TSomeHistogram(size_t intervals, ui64 id = 0); // where intervals is some constant that defines histogram accuracy
  14. // TSomeHistogram(const THistogram& histo); // histo must be acceptable for TSomeHistogram, for example, only with HT_FIXED_BIN_HISTOGRAM for TFixedBinHistogram
  15. // TSomeHistogram(IHistogram* histo); // any kind of IHistogram
  16. virtual ~IHistogram() {
  17. }
  18. virtual void Clear() = 0;
  19. // zero- or negative-weighted values are skipped
  20. virtual void Add(double value, double weight) = 0;
  21. virtual void Add(const THistoRec& histoRec) = 0;
  22. // Merge some other histos into current
  23. virtual void Merge(const THistogram& histo, double multiplier) = 0;
  24. virtual void Merge(const TVector<THistogram>& histogramsToMerge) = 0;
  25. virtual void Merge(TVector<IHistogramPtr> histogramsToMerge) = 0;
  26. // factor should be greater then zero
  27. virtual void Multiply(double factor) = 0;
  28. virtual void FromProto(const THistogram& histo) = 0; // throws exception in case of wrong histogram type of histo
  29. virtual void ToProto(THistogram& histo) = 0;
  30. virtual void SetId(ui64 id) = 0;
  31. virtual ui64 GetId() = 0;
  32. virtual bool Empty() = 0;
  33. virtual double GetMinValue() = 0;
  34. virtual double GetMaxValue() = 0;
  35. virtual double GetSum() = 0;
  36. virtual double GetSumInRange(double leftBound, double rightBound) = 0;
  37. virtual double GetSumAboveBound(double bound) = 0;
  38. virtual double GetSumBelowBound(double bound) = 0;
  39. virtual double CalcUpperBound(double sum) = 0;
  40. virtual double CalcLowerBound(double sum) = 0;
  41. virtual double CalcUpperBoundSafe(double sum) = 0;
  42. virtual double CalcLowerBoundSafe(double sum) = 0;
  43. double GetValueAtPercentile(double percentile) {
  44. return CalcUpperBound(percentile * GetSum());
  45. }
  46. double GetValueAtPercentileSafe(double percentile) {
  47. return CalcUpperBoundSafe(percentile * GetSum());
  48. }
  49. // Histogram implementation is supposed to clear all precomputed values() if Add() is called after PrecomputePartialSums()
  50. virtual void PrecomputePartialSums() {
  51. }
  52. };
  53. }