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 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, 48 HistogramSamples::Operator op) { 49 Sample min; 50 Sample max; 51 Count count; 52 for (; !iter->Done(); iter->Next()) { 53 iter->Get(&min, &max, &count); 54 if (min + 1 != max) 55 return false; // SparseHistogram only supports bucket with size 1. 56 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; 57 } 58 return true; 59 } 60 61 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts) 62 : iter_(sample_counts.begin()), 63 end_(sample_counts.end()) {} 64 65 SampleMapIterator::~SampleMapIterator() {} 66 67 bool SampleMapIterator::Done() const { 68 return iter_ == end_; 69 } 70 71 void SampleMapIterator::Next() { 72 DCHECK(!Done()); 73 iter_++; 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 } // namespace base 87