Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: base/metrics/histogram_samples.cc

Issue 10829466: SampleSet -> HistogramSamples (will be reused by SparseHistogram) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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 #include "base/metrics/histogram_samples.h"
6
7 #include "base/compiler_specific.h"
8 #include "base/pickle.h"
9
10 namespace base {
11
12 namespace {
13
14 class SampleCountPickleIterator : public SampleCountIterator {
15 public:
16 SampleCountPickleIterator(PickleIterator* iter);
17
18 virtual bool Done() OVERRIDE;
19 virtual void Next() OVERRIDE;
20 virtual void Get(HistogramBase::Sample* min,
21 HistogramBase::Sample* max,
22 HistogramBase::Count* count) OVERRIDE;
23
24 private:
25 bool ReadNext();
26
27 PickleIterator* iter_;
Ilya Sherman 2012/08/29 08:48:16 nit: PickleIterator* const iter_;?
kaiwang 2012/08/29 22:42:16 Done.
28
29 HistogramBase::Sample min_;
30 HistogramBase::Sample max_;
31 HistogramBase::Count count_;
32 bool is_done_;
33 };
34
35 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator* iter)
36 : iter_(iter),
37 is_done_(false) {
38 if (!ReadNext())
39 is_done_ = true;
40 }
41
42 bool SampleCountPickleIterator::Done() {
43 return is_done_;
44 }
45
46 void SampleCountPickleIterator::Next() {
47 CHECK(!Done());
48 if (!ReadNext())
49 is_done_ = true;
50 }
51
52 void SampleCountPickleIterator::Get(HistogramBase::Sample* min,
53 HistogramBase::Sample* max,
54 HistogramBase::Count* count) {
55 CHECK(!Done());
56 *min = min_;
57 *max = max_;
58 *count = count_;
59 }
60
61 bool SampleCountPickleIterator::ReadNext() {
62 return iter_->ReadInt(&min_) &&
63 iter_->ReadInt(&max_) &&
64 iter_->ReadInt(&count_);
65 }
66
67 } // namespace
68
69 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {}
70
71 HistogramSamples::~HistogramSamples() {}
72
73 void HistogramSamples::Add(const HistogramSamples& other) {
74 sum_ += other.sum();
75 redundant_count_ += other.redundant_count();
76 CHECK(AddSubtractImpl(other.Iterator().get(), true));
77 }
78
79 bool HistogramSamples::AddFromPickle(PickleIterator* iter) {
80 int64 sum;
81 HistogramBase::Count redundant_count;
82
83 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count))
84 return false;
85 sum_ += sum;
86 redundant_count_ += redundant_count;
87
88 SampleCountPickleIterator pickle_iter(iter);
89 return AddSubtractImpl(&pickle_iter, true);
90 }
91
92 void HistogramSamples::Subtract(const HistogramSamples& other) {
93 sum_ -= other.sum();
94 redundant_count_ -= other.redundant_count();
95 CHECK(AddSubtractImpl(other.Iterator().get(), false));
96 }
97
98 bool HistogramSamples::Serialize(Pickle* pickle) const {
99 if (!pickle->WriteInt64(sum_) || !pickle->WriteInt(redundant_count_))
100 return false;
101
102 HistogramBase::Sample min;
103 HistogramBase::Sample max;
104 HistogramBase::Count count;
105 for (scoped_ptr<SampleCountIterator> it = Iterator();
106 !it->Done();
107 it->Next()) {
108 it->Get(&min, &max, &count);
109 if (!pickle->WriteInt(min) ||
110 !pickle->WriteInt(max) ||
111 !pickle->WriteInt(count))
112 return false;
113 }
114 return true;
115 }
116
117 SampleCountIterator::~SampleCountIterator() {}
118
119 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698