| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/metrics/sample_map.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 namespace base { | |
| 10 | |
| 11 typedef HistogramBase::Count Count; | |
| 12 typedef HistogramBase::Sample Sample; | |
| 13 | |
| 14 SampleMap::SampleMap() {} | |
| 15 | |
| 16 SampleMap::~SampleMap() {} | |
| 17 | |
| 18 void SampleMap::Accumulate(Sample value, Count count) { | |
| 19 sample_counts_[value] += count; | |
| 20 IncreaseSum(count * value); | |
| 21 IncreaseRedundantCount(count); | |
| 22 } | |
| 23 | |
| 24 Count SampleMap::GetCount(Sample value) const { | |
| 25 std::map<Sample, Count>::const_iterator it = sample_counts_.find(value); | |
| 26 if (it == sample_counts_.end()) | |
| 27 return 0; | |
| 28 return it->second; | |
| 29 } | |
| 30 | |
| 31 Count SampleMap::TotalCount() const { | |
| 32 Count count = 0; | |
| 33 for (const auto& entry : sample_counts_) { | |
| 34 count += entry.second; | |
| 35 } | |
| 36 return count; | |
| 37 } | |
| 38 | |
| 39 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const { | |
| 40 return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_)); | |
| 41 } | |
| 42 | |
| 43 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, | |
| 44 HistogramSamples::Operator op) { | |
| 45 Sample min; | |
| 46 Sample max; | |
| 47 Count count; | |
| 48 for (; !iter->Done(); iter->Next()) { | |
| 49 iter->Get(&min, &max, &count); | |
| 50 if (min + 1 != max) | |
| 51 return false; // SparseHistogram only supports bucket with size 1. | |
| 52 | |
| 53 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; | |
| 54 } | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts) | |
| 59 : iter_(sample_counts.begin()), | |
| 60 end_(sample_counts.end()) { | |
| 61 SkipEmptyBuckets(); | |
| 62 } | |
| 63 | |
| 64 SampleMapIterator::~SampleMapIterator() {} | |
| 65 | |
| 66 bool SampleMapIterator::Done() const { | |
| 67 return iter_ == end_; | |
| 68 } | |
| 69 | |
| 70 void SampleMapIterator::Next() { | |
| 71 DCHECK(!Done()); | |
| 72 ++iter_; | |
| 73 SkipEmptyBuckets(); | |
| 74 } | |
| 75 | |
| 76 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const { | |
| 77 DCHECK(!Done()); | |
| 78 if (min != NULL) | |
| 79 *min = iter_->first; | |
| 80 if (max != NULL) | |
| 81 *max = iter_->first + 1; | |
| 82 if (count != NULL) | |
| 83 *count = iter_->second; | |
| 84 } | |
| 85 | |
| 86 void SampleMapIterator::SkipEmptyBuckets() { | |
| 87 while (!Done() && iter_->second == 0) { | |
| 88 ++iter_; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 } // namespace base | |
| OLD | NEW |