James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 <map> |
| 12 | |
| 13 | #include "base/compiler_specific.h" |
| 14 | #include "base/memory/scoped_ptr.h" |
| 15 | #include "base/metrics/histogram_base.h" |
| 16 | #include "base/metrics/histogram_samples.h" |
| 17 | |
| 18 | namespace base { |
| 19 | |
| 20 | class BASE_EXPORT_PRIVATE SampleMap : public HistogramSamples { |
| 21 | public: |
| 22 | SampleMap(); |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 23 | ~SampleMap() override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 24 | |
| 25 | // HistogramSamples implementation: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 26 | void Accumulate(HistogramBase::Sample value, |
| 27 | HistogramBase::Count count) override; |
| 28 | HistogramBase::Count GetCount(HistogramBase::Sample value) const override; |
| 29 | HistogramBase::Count TotalCount() const override; |
| 30 | scoped_ptr<SampleCountIterator> Iterator() const override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 31 | |
| 32 | protected: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 33 | bool AddSubtractImpl( |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 34 | SampleCountIterator* iter, |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 35 | HistogramSamples::Operator op) override; // |op| is ADD or SUBTRACT. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 36 | |
| 37 | private: |
| 38 | std::map<HistogramBase::Sample, HistogramBase::Count> sample_counts_; |
| 39 | |
| 40 | DISALLOW_COPY_AND_ASSIGN(SampleMap); |
| 41 | }; |
| 42 | |
| 43 | class BASE_EXPORT_PRIVATE SampleMapIterator : public SampleCountIterator { |
| 44 | public: |
| 45 | typedef std::map<HistogramBase::Sample, HistogramBase::Count> |
| 46 | SampleToCountMap; |
| 47 | |
| 48 | explicit SampleMapIterator(const SampleToCountMap& sample_counts); |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 49 | ~SampleMapIterator() override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 50 | |
| 51 | // SampleCountIterator implementation: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 52 | bool Done() const override; |
| 53 | void Next() override; |
| 54 | void Get(HistogramBase::Sample* min, |
| 55 | HistogramBase::Sample* max, |
| 56 | HistogramBase::Count* count) const override; |
| 57 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 58 | private: |
| 59 | SampleToCountMap::const_iterator iter_; |
| 60 | const SampleToCountMap::const_iterator end_; |
| 61 | }; |
| 62 | |
| 63 | } // namespace base |
| 64 | |
| 65 | #endif // BASE_METRICS_SAMPLE_MAP_H_ |