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

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_;
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), is_done_(false) {
Ilya Sherman 2012/08/23 07:50:54 nit: Initializations should be one per line: http:
kaiwang 2012/08/24 04:17:58 actually it only says subsequent lines should inde
37 if (!ReadNext()) {
38 is_done_ = true;
39 }
Ilya Sherman 2012/08/23 07:50:54 nit: No need for curly braces
Ilya Sherman 2012/08/23 07:50:54 nit: Why not just call Next()?
kaiwang 2012/08/24 04:17:58 Done.
Ilya Sherman 2012/08/29 08:48:16 (bump)
kaiwang 2012/08/30 03:13:21 Done.
40 }
41
42 bool SampleCountPickleIterator::Done() {
43 return is_done_;
44 }
45
46 void SampleCountPickleIterator::Next() {
47 CHECK(!Done());
Ilya Sherman 2012/08/23 07:50:54 Does this really need to be a CHECK rather than a
kaiwang 2012/08/24 04:17:58 Using DCHECK asks code owner to consider 2 situati
Ilya Sherman 2012/08/29 08:48:16 The Chromium default is to use DCHECK rather than
48 if (!ReadNext()) {
49 is_done_ = true;
50 }
51 }
52
53 void SampleCountPickleIterator::Get(HistogramBase::Sample* min,
54 HistogramBase::Sample* max,
55 HistogramBase::Count* count) {
56 CHECK(!Done());
Ilya Sherman 2012/08/23 07:50:54 Ditto
57 *min = min_;
58 *max = max_;
59 *count = count_;
60 }
61
62 bool SampleCountPickleIterator::ReadNext() {
63 if (!iter_->ReadInt(&min_) ||
64 !iter_->ReadInt(&max_) ||
65 !iter_->ReadInt(&count_)) {
66 return false;
67 }
68 return true;
Ilya Sherman 2012/08/23 07:50:54 nit: This can be written as return iter_->ReadInt
kaiwang 2012/08/24 04:17:58 Done.
69 }
70
71 } // namespace
72
73 HistogramSamples::HistogramSamples()
74 : sum_(0), redundant_count_(0) {}
Ilya Sherman 2012/08/23 07:50:54 nit: One per line
kaiwang 2012/08/24 04:17:58 moved to a single line
75
76 HistogramSamples::~HistogramSamples() {}
77
78 void HistogramSamples::Add(const HistogramSamples& other) {
79 sum_ += other.sum();
80 redundant_count_ += other.redundant_count();
81 CHECK(AddSubtractHelper(other.Iterator().get(), true));
Ilya Sherman 2012/08/23 07:50:54 This looks like it will leak memory.
Ilya Sherman 2012/08/23 07:50:54 nit: Does this need to be a CHECK, or would a DCHE
kaiwang 2012/08/24 04:17:58 You mean iterator? It's in a scoped_ptr
Ilya Sherman 2012/08/29 08:48:16 Sorry, you're right, this should be fine.
82 }
83
84 bool HistogramSamples::Add(PickleIterator* iter) {
85 int64 sum;
86 HistogramBase::Count redundant_count;
87
88 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count))
89 return false;
90 sum_ += sum;
91 redundant_count_ += redundant_count;
92
93 scoped_ptr<SampleCountIterator> pickle_iter(
94 new SampleCountPickleIterator(iter));
Ilya Sherman 2012/08/23 07:50:54 nit: Why not stack-allocate this?
kaiwang 2012/08/24 04:17:58 Done.
95 return AddSubtractHelper(pickle_iter.get(), true);
96 }
97
98 void HistogramSamples::Subtract(const HistogramSamples& other) {
99 sum_ -= other.sum();
100 redundant_count_ -= other.redundant_count();
101 CHECK(AddSubtractHelper(other.Iterator().get(), false));
102 }
103
104 bool HistogramSamples::Serialize(Pickle* pickle) const {
105 if (!pickle->WriteInt64(sum_) || !pickle->WriteInt(redundant_count_))
106 return false;
107
108 HistogramBase::Sample min;
109 HistogramBase::Sample max;
110 HistogramBase::Count count;
111 for (scoped_ptr<SampleCountIterator> it = Iterator();
112 !it->Done();
113 it->Next()) {
114 it->Get(&min, &max, &count);
115 if (!pickle->WriteInt(min) ||
116 !pickle->WriteInt(max) ||
117 !pickle->WriteInt(count))
118 return false;
119 }
120 return true;
121 }
122
123 SampleCountIterator::~SampleCountIterator() {}
124
125 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698