auto_histogram.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma once
  2. #include "adaptive_histogram.h"
  3. #include "fixed_bin_histogram.h"
  4. #include "histogram.h"
  5. #include <library/cpp/histogram/adaptive/protos/histo.pb.h>
  6. #include <util/generic/ptr.h>
  7. #include <util/generic/yexception.h>
  8. namespace NKiwiAggr {
  9. class TAutoHistogram: private TNonCopyable, public IHistogram {
  10. private:
  11. static const size_t DEFAULT_INTERVALS = 100;
  12. ui64 Id;
  13. size_t Intervals;
  14. THolder<IHistogram> HistogramImpl;
  15. public:
  16. TAutoHistogram(size_t intervals, ui64 id = 0) {
  17. Y_UNUSED(intervals);
  18. Y_UNUSED(id);
  19. ythrow yexception() << "Empty constructor is not defined for TAutoHistogram";
  20. }
  21. TAutoHistogram(const THistogram& histo, size_t defaultIntervals = DEFAULT_INTERVALS, ui64 defaultId = 0)
  22. : Id(defaultId)
  23. , Intervals(defaultIntervals)
  24. {
  25. FromProto(histo);
  26. }
  27. TAutoHistogram(IHistogram* histo, size_t defaultIntervals = DEFAULT_INTERVALS, ui64 defaultId = 0) {
  28. Y_UNUSED(histo);
  29. Y_UNUSED(defaultIntervals);
  30. Y_UNUSED(defaultId);
  31. ythrow yexception() << "IHistogram constructor is not defined for TAutoHistogram";
  32. }
  33. virtual ~TAutoHistogram() {
  34. }
  35. virtual void Clear() {
  36. HistogramImpl->Clear();
  37. }
  38. virtual void Add(double value, double weight) {
  39. HistogramImpl->Add(value, weight);
  40. }
  41. virtual void Add(const THistoRec& histoRec) {
  42. HistogramImpl->Add(histoRec);
  43. }
  44. virtual void Merge(const THistogram& histo, double multiplier) {
  45. HistogramImpl->Merge(histo, multiplier);
  46. }
  47. virtual void Merge(const TVector<THistogram>& histogramsToMerge) {
  48. HistogramImpl->Merge(histogramsToMerge);
  49. }
  50. virtual void Merge(TVector<IHistogramPtr> histogramsToMerge) {
  51. HistogramImpl->Merge(histogramsToMerge);
  52. }
  53. virtual void Multiply(double factor) {
  54. HistogramImpl->Multiply(factor);
  55. }
  56. virtual void FromProto(const THistogram& histo) {
  57. if (!histo.HasType() || histo.GetType() == HT_FIXED_BIN_HISTOGRAM) {
  58. HistogramImpl.Reset(new TFixedBinHistogram(histo, Intervals, Id));
  59. } else if (histo.GetType() == HT_ADAPTIVE_DISTANCE_HISTOGRAM) {
  60. HistogramImpl.Reset(new TAdaptiveDistanceHistogram(histo, Intervals, Id));
  61. } else if (histo.GetType() == HT_ADAPTIVE_WEIGHT_HISTOGRAM) {
  62. HistogramImpl.Reset(new TAdaptiveWeightHistogram(histo, Intervals, Id));
  63. } else if (histo.GetType() == HT_ADAPTIVE_WARD_HISTOGRAM) {
  64. HistogramImpl.Reset(new TAdaptiveWardHistogram(histo, Intervals, Id));
  65. } else {
  66. ythrow yexception() << "Can't parse TAutoHistogram from a THistogram of type " << (ui32)histo.GetType();
  67. }
  68. }
  69. virtual void ToProto(THistogram& histo) {
  70. HistogramImpl->ToProto(histo);
  71. }
  72. virtual void SetId(ui64 id) {
  73. HistogramImpl->SetId(id);
  74. }
  75. virtual ui64 GetId() {
  76. return HistogramImpl->GetId();
  77. }
  78. virtual bool Empty() {
  79. return HistogramImpl->Empty();
  80. }
  81. virtual double GetMinValue() {
  82. return HistogramImpl->GetMinValue();
  83. }
  84. virtual double GetMaxValue() {
  85. return HistogramImpl->GetMaxValue();
  86. }
  87. virtual double GetSum() {
  88. return HistogramImpl->GetSum();
  89. }
  90. virtual double GetSumInRange(double leftBound, double rightBound) {
  91. return HistogramImpl->GetSumInRange(leftBound, rightBound);
  92. }
  93. virtual double GetSumAboveBound(double bound) {
  94. return HistogramImpl->GetSumAboveBound(bound);
  95. }
  96. virtual double GetSumBelowBound(double bound) {
  97. return HistogramImpl->GetSumBelowBound(bound);
  98. }
  99. virtual double CalcUpperBound(double sum) {
  100. return HistogramImpl->CalcUpperBound(sum);
  101. }
  102. virtual double CalcLowerBound(double sum) {
  103. return HistogramImpl->CalcLowerBound(sum);
  104. }
  105. virtual double CalcUpperBoundSafe(double sum) {
  106. return HistogramImpl->CalcUpperBoundSafe(sum);
  107. }
  108. virtual double CalcLowerBoundSafe(double sum) {
  109. return HistogramImpl->CalcLowerBoundSafe(sum);
  110. }
  111. virtual void PrecomputePartialSums() {
  112. return HistogramImpl->PrecomputePartialSums();
  113. }
  114. };
  115. }