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