Chromium Code Reviews| 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()); | |
|
jar (doing other things)
2012/10/05 01:27:36
nit: remove space after unary operator |-|
kaiwang
2012/10/05 03:16:17
Done.
| |
| 49 IncreaseRedundantCount(count); | |
| 50 } | |
|
Ilya Sherman
2012/10/05 03:29:24
This method feels a little odd as part of the API
kaiwang
2012/10/05 04:02:38
Yes, it may feel a bit odd. But it's caused by dif
jar (doing other things)
2012/10/05 16:55:19
It does seem like the right answer is to have this
| |
| 51 | |
| 52 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, | |
| 53 HistogramSamples::Instruction instruction) { | |
| 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] += | |
| 62 (instruction == HistogramSamples::ADD) ? count : -count; | |
| 63 } | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 SampleMapIterator::SampleMapIterator(const map<Sample, Count>& sample_counts) | |
| 68 : iter_(sample_counts.begin()), | |
| 69 end_(sample_counts.end()) {} | |
| 70 | |
| 71 SampleMapIterator::~SampleMapIterator() {} | |
| 72 | |
| 73 bool SampleMapIterator::Done() const { | |
| 74 return iter_ == end_; | |
| 75 } | |
| 76 | |
| 77 void SampleMapIterator::Next() { | |
| 78 DCHECK(!Done()); | |
| 79 iter_++; | |
| 80 } | |
| 81 | |
| 82 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const { | |
| 83 DCHECK(!Done()); | |
| 84 if (min != NULL) | |
| 85 *min = iter_->first; | |
| 86 if (max != NULL) | |
| 87 *max = iter_->first + 1; | |
| 88 if (count != NULL) | |
| 89 *count = iter_->second; | |
| 90 } | |
| 91 | |
| 92 } // namespace base | |
| OLD | NEW |