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 using std::map; |
| 10 |
| 11 namespace base { |
| 12 |
| 13 typedef HistogramBase::Count Count; |
| 14 typedef HistogramBase::Sample Sample; |
| 15 |
| 16 SampleMap::SampleMap() {} |
| 17 |
| 18 SampleMap::~SampleMap() {} |
| 19 |
| 20 void SampleMap::Accumulate(Sample value, Count count) { |
| 21 sample_counts_[value] += count; |
| 22 IncreaseSum(count * value); |
| 23 IncreaseRedundantCount(count); |
| 24 } |
| 25 |
| 26 Count SampleMap::GetCount(Sample value) const { |
| 27 map<Sample, Count>::const_iterator it = sample_counts_.find(value); |
| 28 if (it == sample_counts_.end()) |
| 29 return 0; |
| 30 return it->second; |
| 31 } |
| 32 |
| 33 Count SampleMap::TotalCount() const { |
| 34 Count count = 0; |
| 35 for (map<Sample, Count>::const_iterator it = sample_counts_.begin(); |
| 36 it != sample_counts_.end(); |
| 37 ++it) { |
| 38 count += it->second; |
| 39 } |
| 40 return count; |
| 41 } |
| 42 |
| 43 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const { |
| 44 return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_)); |
| 45 } |
| 46 |
| 47 void SampleMap::ResetRedundantCount(Count count) { |
| 48 IncreaseRedundantCount(-redundant_count()); |
| 49 IncreaseRedundantCount(count); |
| 50 } |
| 51 |
| 52 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, |
| 53 HistogramSamples::Operator op) { |
| 54 Sample min; |
| 55 Sample max; |
| 56 Count count; |
| 57 for (; !iter->Done(); iter->Next()) { |
| 58 iter->Get(&min, &max, &count); |
| 59 if (min + 1 != max) |
| 60 return false; // SparseHistogram only supports bucket with size 1. |
| 61 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; |
| 62 } |
| 63 return true; |
| 64 } |
| 65 |
| 66 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts) |
| 67 : iter_(sample_counts.begin()), |
| 68 end_(sample_counts.end()) {} |
| 69 |
| 70 SampleMapIterator::~SampleMapIterator() {} |
| 71 |
| 72 bool SampleMapIterator::Done() const { |
| 73 return iter_ == end_; |
| 74 } |
| 75 |
| 76 void SampleMapIterator::Next() { |
| 77 DCHECK(!Done()); |
| 78 iter_++; |
| 79 } |
| 80 |
| 81 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const { |
| 82 DCHECK(!Done()); |
| 83 if (min != NULL) |
| 84 *min = iter_->first; |
| 85 if (max != NULL) |
| 86 *max = iter_->first + 1; |
| 87 if (count != NULL) |
| 88 *count = iter_->second; |
| 89 } |
| 90 |
| 91 } // namespace base |
OLD | NEW |