Add a Notification Settings Button to all web notifications behind the web platform...
[chromium-blink-merge.git] / base / metrics / sample_map.cc
blobf2540a4d00bf08ee5b6548692e80ce9bbb155462
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.
5 #include "base/metrics/sample_map.h"
7 #include "base/logging.h"
9 namespace base {
11 typedef HistogramBase::Count Count;
12 typedef HistogramBase::Sample Sample;
14 SampleMap::SampleMap() {}
16 SampleMap::~SampleMap() {}
18 void SampleMap::Accumulate(Sample value, Count count) {
19 sample_counts_[value] += count;
20 IncreaseSum(count * value);
21 IncreaseRedundantCount(count);
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;
31 Count SampleMap::TotalCount() const {
32 Count count = 0;
33 for (const auto& entry : sample_counts_) {
34 count += entry.second;
36 return count;
39 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const {
40 return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_));
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.
53 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count;
55 return true;
58 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts)
59 : iter_(sample_counts.begin()),
60 end_(sample_counts.end()) {
61 SkipEmptyBuckets();
64 SampleMapIterator::~SampleMapIterator() {}
66 bool SampleMapIterator::Done() const {
67 return iter_ == end_;
70 void SampleMapIterator::Next() {
71 DCHECK(!Done());
72 ++iter_;
73 SkipEmptyBuckets();
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;
86 void SampleMapIterator::SkipEmptyBuckets() {
87 while (!Done() && iter_->second == 0) {
88 ++iter_;
92 } // namespace base