summaryrefslogtreecommitdiffstats
path: root/base/metrics/sample_map.cc
blob: 42468cb57f31816fa6a5fbd0c2c5e734a86c6773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/metrics/sample_map.h"

#include "base/logging.h"

using std::map;

namespace base {

typedef HistogramBase::Count Count;
typedef HistogramBase::Sample Sample;

SampleMap::SampleMap() {}

SampleMap::~SampleMap() {}

void SampleMap::Accumulate(Sample value, Count count) {
  sample_counts_[value] += count;
  IncreaseSum(count * value);
  IncreaseRedundantCount(count);
}

Count SampleMap::GetCount(Sample value) const {
  map<Sample, Count>::const_iterator it = sample_counts_.find(value);
  if (it == sample_counts_.end())
    return 0;
  return it->second;
}

Count SampleMap::TotalCount() const {
  Count count = 0;
  for (map<Sample, Count>::const_iterator it = sample_counts_.begin();
       it != sample_counts_.end();
       ++it) {
    count += it->second;
  }
  return count;
}

scoped_ptr<SampleCountIterator> SampleMap::Iterator() const {
  return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_));
}

bool SampleMap::AddSubtractImpl(SampleCountIterator* iter,
                                HistogramSamples::Operator op) {
  Sample min;
  Sample max;
  Count count;
  for (; !iter->Done(); iter->Next()) {
    iter->Get(&min, &max, &count);
    if (min + 1 != max)
      return false;  // SparseHistogram only supports bucket with size 1.
    sample_counts_[min] += (op ==  HistogramSamples::ADD) ? count : -count;
  }
  return true;
}

SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts)
    : iter_(sample_counts.begin()),
      end_(sample_counts.end()) {}

SampleMapIterator::~SampleMapIterator() {}

bool SampleMapIterator::Done() const {
  return iter_ == end_;
}

void SampleMapIterator::Next() {
  DCHECK(!Done());
  iter_++;
}

void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const {
  DCHECK(!Done());
  if (min != NULL)
    *min = iter_->first;
  if (max != NULL)
    *max = iter_->first + 1;
  if (count != NULL)
    *count = iter_->second;
}

}  // namespace base