| 6 |
| 7 | #include "base/logging.h" |
| 8 | #include "base/numerics/safe_conversions.h" |
| 9 | #include "base/stl_util.h" |
| 10 |
| 11 | namespace base { |
| 12 |
| 13 | typedef HistogramBase::Count Count; |
| 14 | typedef HistogramBase::Sample Sample; |
| 15 |
| 16 | namespace { |
| 17 |
| 18 | // An iterator for going through a SampleMap. The logic here is identical |
| 19 | // to that of PersistentSampleMapIterator but with different data structures. |
| 20 | // Changes here likely need to be duplicated there. |
| 21 | class SampleMapIterator : public SampleCountIterator { |
| 22 | public: |
| 23 | typedef std::map<HistogramBase::Sample, HistogramBase::Count> |
| 24 | SampleToCountMap; |
| 25 |
| 26 | explicit SampleMapIterator(const SampleToCountMap& sample_counts); |
| 27 | ~SampleMapIterator() override; |
| 28 |
| 29 | // SampleCountIterator: |
| 30 | bool Done() const override; |
| 31 | void Next() override; |
| 32 | void Get(HistogramBase::Sample* min, |
| 33 | int64_t* max, |
| 34 | HistogramBase::Count* count) const override; |
| 35 |
| 36 | private: |
| 37 | void SkipEmptyBuckets(); |
| 38 |
| 39 | SampleToCountMap::const_iterator iter_; |
| 40 | const SampleToCountMap::const_iterator end_; |
| 41 | }; |
| 42 |
| 43 | SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts) |
| 44 | : iter_(sample_counts.begin()), |
| 45 | end_(sample_counts.end()) { |
| 46 | SkipEmptyBuckets(); |
| 47 | } |
| 48 |
| 49 | SampleMapIterator::~SampleMapIterator() = default; |
| 50 |
| 51 | bool SampleMapIterator::Done() const { |
| 52 | return iter_ == end_; |
| 53 | } |
| 54 |
| 55 | void SampleMapIterator::Next() { |
| 56 | DCHECK(!Done()); |
| 57 | ++iter_; |
| 58 | SkipEmptyBuckets(); |
| 59 | } |
| 60 |
| 61 | void SampleMapIterator::Get(Sample* min, int64_t* max, Count* count) const { |
| 62 | DCHECK(!Done()); |
| 63 | if (min) |
| 64 | *min = iter_->first; |
| 65 | if (max) |
| 66 | *max = strict_cast<int64_t>(iter_->first) + 1; |
| 67 | if (count) |
| 68 | *count = iter_->second; |
| 69 | } |
| 70 |
| 71 | void SampleMapIterator::SkipEmptyBuckets() { |
| 72 | while (!Done() && iter_->second == 0) { |
| 73 | ++iter_; |
| 74 | } |
| 75 | } |
| 76 |
| 77 | } // namespace |
| 78 |
| 79 | SampleMap::SampleMap() : SampleMap(0) {} |
| 80 |
| 81 | SampleMap::SampleMap(uint64_t id) : HistogramSamples(id, new LocalMetadata()) {} |
| 82 |
| 83 | SampleMap::~SampleMap() { |
| 84 | delete static_cast<LocalMetadata*>(meta()); |
| 85 | } |
| 86 |
| 87 | void SampleMap::Accumulate(Sample value, Count count) { |
| 88 | sample_counts_[value] += count; |
| 89 | IncreaseSumAndCount(strict_cast<int64_t>(count) * value, count); |
| 90 | } |
| 91 |
| 92 | Count SampleMap::GetCount(Sample value) const { |
| 93 | auto it = sample_counts_.find(value); |
| 94 | if (it == sample_counts_.end()) |
| 95 | return 0; |
| 96 | return it->second; |
| 97 | } |
| 98 |
| 99 | Count SampleMap::TotalCount() const { |
| 100 | Count count = 0; |
| 101 | for (const auto& entry : sample_counts_) { |
| 102 | count += entry.second; |
| 103 | } |
| 104 | return count; |
| 105 | } |
| 106 |
| 107 | std::unique_ptr<SampleCountIterator> SampleMap::Iterator() const { |
| 108 | return std::make_unique<SampleMapIterator>(sample_counts_); |
| 109 | } |
| 110 |
| 111 | bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, Operator op) { |
| 112 | Sample min; |
| 113 | int64_t max; |
| 114 | Count count; |
| 115 | for (; !iter->Done(); iter->Next()) { |
| 116 | iter->Get(&min, &max, &count); |
| 117 | if (strict_cast<int64_t>(min) + 1 != max) |
| 118 | return false; // SparseHistogram only supports bucket with size 1. |
| 119 |
| 120 | sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 |
| 125 | } // namespace base |
| 126 |