OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/sample_vector.h" | 5 #include "base/metrics/sample_vector.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/bucket_ranges.h" | 8 #include "base/metrics/bucket_ranges.h" |
9 | 9 |
10 using std::vector; | 10 using std::vector; |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 typedef HistogramBase::Count Count; | 14 typedef HistogramBase::Count Count; |
15 typedef HistogramBase::Sample Sample; | 15 typedef HistogramBase::Sample Sample; |
16 | 16 |
17 SampleVector::SampleVector(const BucketRanges* bucket_ranges) | 17 SampleVector::SampleVector(const BucketRanges* bucket_ranges) |
18 : counts_(bucket_ranges->size() - 1), | 18 : counts_(bucket_ranges->bucket_count()), |
19 bucket_ranges_(bucket_ranges) { | 19 bucket_ranges_(bucket_ranges) { |
20 CHECK_GE(bucket_ranges_->size(), 2u); | 20 CHECK_GE(bucket_ranges_->bucket_count(), 1u); |
21 } | 21 } |
22 | 22 |
23 SampleVector::~SampleVector() {} | 23 SampleVector::~SampleVector() {} |
24 | 24 |
25 void SampleVector::Accumulate(Sample value, Count count) { | 25 void SampleVector::Accumulate(Sample value, Count count) { |
26 size_t bucket_index = GetBucketIndex(value); | 26 size_t bucket_index = GetBucketIndex(value); |
27 counts_[bucket_index] += count; | 27 counts_[bucket_index] += count; |
28 IncreaseSum(count * value); | 28 IncreaseSum(count * value); |
29 IncreaseRedundantCount(count); | 29 IncreaseRedundantCount(count); |
30 } | 30 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 return false; | 76 return false; |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 return iter->Done(); | 80 return iter->Done(); |
81 } | 81 } |
82 | 82 |
83 // Use simple binary search. This is very general, but there are better | 83 // Use simple binary search. This is very general, but there are better |
84 // approaches if we knew that the buckets were linearly distributed. | 84 // approaches if we knew that the buckets were linearly distributed. |
85 size_t SampleVector::GetBucketIndex(Sample value) const { | 85 size_t SampleVector::GetBucketIndex(Sample value) const { |
86 size_t bucket_count = bucket_ranges_->size() - 1; | 86 size_t bucket_count = bucket_ranges_->bucket_count(); |
87 CHECK_GE(bucket_count, 1u); | 87 CHECK_GE(bucket_count, 1u); |
88 CHECK_GE(value, bucket_ranges_->range(0)); | 88 CHECK_GE(value, bucket_ranges_->range(0)); |
89 CHECK_LT(value, bucket_ranges_->range(bucket_count)); | 89 CHECK_LT(value, bucket_ranges_->range(bucket_count)); |
90 | 90 |
91 size_t under = 0; | 91 size_t under = 0; |
92 size_t over = bucket_count; | 92 size_t over = bucket_count; |
93 size_t mid; | 93 size_t mid; |
94 do { | 94 do { |
95 DCHECK_GE(over, under); | 95 DCHECK_GE(over, under); |
96 mid = under + (over - under)/2; | 96 mid = under + (over - under)/2; |
97 if (mid == under) | 97 if (mid == under) |
98 break; | 98 break; |
99 if (bucket_ranges_->range(mid) <= value) | 99 if (bucket_ranges_->range(mid) <= value) |
100 under = mid; | 100 under = mid; |
101 else | 101 else |
102 over = mid; | 102 over = mid; |
103 } while (true); | 103 } while (true); |
104 | 104 |
105 DCHECK_LE(bucket_ranges_->range(mid), value); | 105 DCHECK_LE(bucket_ranges_->range(mid), value); |
106 CHECK_GT(bucket_ranges_->range(mid + 1), value); | 106 CHECK_GT(bucket_ranges_->range(mid + 1), value); |
107 return mid; | 107 return mid; |
108 } | 108 } |
109 | 109 |
110 SampleVectorIterator::SampleVectorIterator(const vector<Count>* counts, | 110 SampleVectorIterator::SampleVectorIterator(const vector<Count>* counts, |
111 const BucketRanges* bucket_ranges) | 111 const BucketRanges* bucket_ranges) |
112 : counts_(counts), | 112 : counts_(counts), |
113 bucket_ranges_(bucket_ranges), | 113 bucket_ranges_(bucket_ranges), |
114 index_(0) { | 114 index_(0) { |
115 CHECK_GT(bucket_ranges_->size(), counts_->size()); | 115 CHECK_GE(bucket_ranges_->bucket_count(), counts_->size()); |
116 SkipEmptyBuckets(); | 116 SkipEmptyBuckets(); |
117 } | 117 } |
118 | 118 |
119 SampleVectorIterator::~SampleVectorIterator() {} | 119 SampleVectorIterator::~SampleVectorIterator() {} |
120 | 120 |
121 bool SampleVectorIterator::Done() const { | 121 bool SampleVectorIterator::Done() const { |
122 return index_ >= counts_->size(); | 122 return index_ >= counts_->size(); |
123 } | 123 } |
124 | 124 |
125 void SampleVectorIterator::Next() { | 125 void SampleVectorIterator::Next() { |
(...skipping 26 matching lines...) Expand all Loading... |
152 return; | 152 return; |
153 | 153 |
154 while (index_ < counts_->size()) { | 154 while (index_ < counts_->size()) { |
155 if ((*counts_)[index_] != 0) | 155 if ((*counts_)[index_] != 0) |
156 return; | 156 return; |
157 index_++; | 157 index_++; |
158 } | 158 } |
159 } | 159 } |
160 | 160 |
161 } // namespace base | 161 } // namespace base |
OLD | NEW |