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"
13 typedef HistogramBase::Count Count
;
14 typedef HistogramBase::Sample Sample
;
16 SampleMap::SampleMap() {}
18 SampleMap::~SampleMap() {}
20 void SampleMap::Accumulate(Sample value
, Count count
) {
21 sample_counts_
[value
] += count
;
22 IncreaseSum(count
* value
);
23 IncreaseRedundantCount(count
);
26 Count
SampleMap::GetCount(Sample value
) const {
27 map
<Sample
, Count
>::const_iterator it
= sample_counts_
.find(value
);
28 if (it
== sample_counts_
.end())
33 Count
SampleMap::TotalCount() const {
35 for (map
<Sample
, Count
>::const_iterator it
= sample_counts_
.begin();
36 it
!= sample_counts_
.end();
43 scoped_ptr
<SampleCountIterator
> SampleMap::Iterator() const {
44 return scoped_ptr
<SampleCountIterator
>(new SampleMapIterator(sample_counts_
));
47 bool SampleMap::AddSubtractImpl(SampleCountIterator
* iter
,
48 HistogramSamples::Operator op
) {
52 for (; !iter
->Done(); iter
->Next()) {
53 iter
->Get(&min
, &max
, &count
);
55 return false; // SparseHistogram only supports bucket with size 1.
56 sample_counts_
[min
] += (op
== HistogramSamples::ADD
) ? count
: -count
;
61 SampleMapIterator::SampleMapIterator(const SampleToCountMap
& sample_counts
)
62 : iter_(sample_counts
.begin()),
63 end_(sample_counts
.end()) {}
65 SampleMapIterator::~SampleMapIterator() {}
67 bool SampleMapIterator::Done() const {
71 void SampleMapIterator::Next() {
76 void SampleMapIterator::Get(Sample
* min
, Sample
* max
, Count
* count
) const {
81 *max
= iter_
->first
+ 1;
83 *count
= iter_
->second
;