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 // SampleMap implements HistogramSamples interface. It is used by the
6 // SparseHistogram class to store samples.
7 
8 #ifndef BASE_METRICS_SAMPLE_MAP_H_
9 #define BASE_METRICS_SAMPLE_MAP_H_
10 
11 #include <stdint.h>
12 
13 #include <map>
14 
15 #include "base/compiler_specific.h"
16 #include "base/macros.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/metrics/histogram_base.h"
19 #include "base/metrics/histogram_samples.h"
20 
21 namespace base {
22 
23 class BASE_EXPORT SampleMap : public HistogramSamples {
24  public:
25   SampleMap();
26   explicit SampleMap(uint64_t id);
27   ~SampleMap() override;
28 
29   // HistogramSamples implementation:
30   void Accumulate(HistogramBase::Sample value,
31                   HistogramBase::Count count) override;
32   HistogramBase::Count GetCount(HistogramBase::Sample value) const override;
33   HistogramBase::Count TotalCount() const override;
34   scoped_ptr<SampleCountIterator> Iterator() const override;
35 
36  protected:
37   bool AddSubtractImpl(
38       SampleCountIterator* iter,
39       HistogramSamples::Operator op) override;  // |op| is ADD or SUBTRACT.
40 
41  private:
42   std::map<HistogramBase::Sample, HistogramBase::Count> sample_counts_;
43 
44   DISALLOW_COPY_AND_ASSIGN(SampleMap);
45 };
46 
47 class BASE_EXPORT SampleMapIterator : public SampleCountIterator {
48  public:
49   typedef std::map<HistogramBase::Sample, HistogramBase::Count>
50       SampleToCountMap;
51 
52   explicit SampleMapIterator(const SampleToCountMap& sample_counts);
53   ~SampleMapIterator() override;
54 
55   // SampleCountIterator implementation:
56   bool Done() const override;
57   void Next() override;
58   void Get(HistogramBase::Sample* min,
59            HistogramBase::Sample* max,
60            HistogramBase::Count* count) const override;
61 
62  private:
63   void SkipEmptyBuckets();
64 
65   SampleToCountMap::const_iterator iter_;
66   const SampleToCountMap::const_iterator end_;
67 };
68 
69 }  // namespace base
70 
71 #endif  // BASE_METRICS_SAMPLE_MAP_H_
72