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"
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())
31 Count
SampleMap::TotalCount() const {
33 for (const auto& entry
: sample_counts_
) {
34 count
+= entry
.second
;
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
) {
48 for (; !iter
->Done(); iter
->Next()) {
49 iter
->Get(&min
, &max
, &count
);
51 return false; // SparseHistogram only supports bucket with size 1.
53 sample_counts_
[min
] += (op
== HistogramSamples::ADD
) ? count
: -count
;
58 SampleMapIterator::SampleMapIterator(const SampleToCountMap
& sample_counts
)
59 : iter_(sample_counts
.begin()),
60 end_(sample_counts
.end()) {
64 SampleMapIterator::~SampleMapIterator() {}
66 bool SampleMapIterator::Done() const {
70 void SampleMapIterator::Next() {
76 void SampleMapIterator::Get(Sample
* min
, Sample
* max
, Count
* count
) const {
81 *max
= iter_
->first
+ 1;
83 *count
= iter_
->second
;
86 void SampleMapIterator::SkipEmptyBuckets() {
87 while (!Done() && iter_
->second
== 0) {