src/emitters/szlquantile.cc

filter: Statementstmt 32.4% 116 / 358 Branchbran 16.2% 42 / 258 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 25.6% 158 / 616 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary src/emitters/szlquantile.h »
1 // Copyright 2010 Google Inc.
2 // Modified by Yuji Kaneda
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 // ------------------------------------------------------------------------
16  
17 // Implements szl table structure for storing approximate quantiles
18 // in a table. The implementation is based on the following paper:
19 //
20 // [MP80] Munro & Paterson, "Selection and Sorting with Limited Storage",
21 // Theoretical Computer Science, Vol 12, p 315-323, 1980.
22 // More info available at
23 // http://scholar.google.com/scholar?q=munro+paterson
24 //
25 // The above paper is not available online. You could read a detailed
26 // description of the same algorithm here:
27 //
28 // [MRL98] Manku, Rajagopalan & Lindsay, "Approximate Medians and other
29 // Quantiles in One Pass and with Limited Memory", Proc. 1998 ACM
30 // SIGMOD, Vol 27, No 2, p 426-435, June 1998.
31 //
32 // Also see the following paper by Greenwald and Khanna, which contains
33 // another implementation that is thought to be slower:
34 // M. Greenwald and S. Khanna. Space-efficient online computation of
35 // quantile summerles. SIGMOD'01, pp. 58-66, Santa Barbara, CA, May
36 // 2001.
37 //
38 // Brief description of Munro-Paterson algorithm
39 // =============================================
40 // Imagine a binary tree of buffers. Every buffer has size "k". Now imagine
41 // populating the leaves of the tree (from left to right) with the input
42 // stream. Munro-Paterson is very simple: As soon as both children of a
43 // buffer are full, we invoke a Collapse() operation. What is a Collapse()?
44 // Basically, we take two buffers of size "k" each, sort them together and pick
45 // every other element in the sorted sequence. That's it!
46 //
47 // When the input stream runs dry, we would have populated some "b" buffers at
48 // various levels by following the Munro-Paterson algorithm. How do we compute
49 // 100 quantiles from these "b" buffers? Assign a "weight" of 2^i to every
50 // element of a buffer at level i (leaves are at level 0). Now sort all the
51 // elements (in various buffers) together. Then compute the "weighted 100
52 // splitters" of this sequence. Please see the code below or the paper above for
53 // furthe details.
54  
55 #include <math.h>
56 #include <algorithm>
57  
58 #include "public/porting.h"
59 // #include "public/logging.h"
60  
61 namespace SZaru {
62 // We compute the "smallest possible k" satisfying two inequalities:
63 // 1) (b - 2) * (2 ^ (b - 2)) + 0.5 <= epsilon * MAX_TOT_ELEMS
64 // 2) k * (2 ^ (b - 1)) \geq MAX_TOT_ELEMS
65 //
66 // For an explanation of these inequalities, please read the Munro-Paterson or
67 // the Manku-Rajagopalan-Linday papers.
68 template<typename Key>
69 0 int64 QuantileEstimatorImpl<Key>::ComputeK() {
70 0 const double epsilon = 1.0 / (num_quantiles_ - 1);
71 0 int b = 2;
72 0 ▶ while ((b - 2) * (0x1LL << (b - 2)) + 0.5 <= epsilon * MAX_TOT_ELEMS) {
Branch: 3 branches
BranchTrueFalseTotal
while ((b - 2) * (0x1LL << (b - 2)) + 0.5 <= epsilon * MAX_TOT_ELEMS) { 29 1 30
while ((b - 2) * (0x1LL << (b - 2)) + 0.5 <= epsilon * MAX_TOT_ELEMS) { 0 0 0
while ((b - 2) * (0x1LL << (b - 2)) + 0.5 <= epsilon * MAX_TOT_ELEMS) { 0 0 0
73 0 ++b;
74 }
75 0 const int64 k = MAX_TOT_ELEMS / (0x1LL << (b - 1));
76 // VLOG(2) << StringPrintf(
77 // "ComputeK(): returning k = %lld for num_quantiles_ = %d (epsilon = %f)",
78 // k, num_quantiles_, epsilon);
79 0 return k;
80 }
81  
82  
83 // If buffer_[level] already exists, do nothing.
84 // Else create a new buffer_[level] that is empty.
85 template<typename Key>
86 0 void QuantileEstimatorImpl<Key>::EnsureBuffer(const int level) {
87 // int extra_memory = 0;
88 0 ▶ if (buffer_.size() < static_cast<size_t>(level) + 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (buffer_.size() < static_cast<size_t>(level) + 1) { 0 0 0
if (buffer_.size() < static_cast<size_t>(level) + 1) { 0 0 0
if (buffer_.size() < static_cast<size_t>(level) + 1) { 2 198 200
89 // size_t old_capacity = buffer_.capacity();
90 0 ▶ buffer_.resize(level + 1, NULL);
Branch: 3 branches
BranchTrueFalseTotal
buffer_.resize(level + 1, NULL); 0 0 0
buffer_.resize(level + 1, NULL); 0 0 0
buffer_.resize(level + 1, NULL); 2 0 2
91 // extra_memory +=
92 // (buffer_.capacity() - old_capacity) * sizeof(vector<string>*);
93 }
94 0 ▶ if (buffer_[level] == NULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (buffer_[level] == NULL) { 0 0 0
if (buffer_[level] == NULL) { 0 0 0
if (buffer_[level] == NULL) { 2 198 200
95 // VLOG(2) << StringPrintf("Creating buffer_[%d] ...", level);
96 0 buffer_[level] = new vector<Key>();
97 // extra_memory += sizeof(*(buffer_[level]));
98 }
99 // return extra_memory;
100 0 }
101  
102 // For Collapse(), both "a" and "b" should be vectors of length "k_".
103 // Conceptually, Collapse() combines "a" and "b" into a single vector, sorts
104 // this vector and then chooses every other member of this vector.
105 // The result is stored in "output".
106 //
107 // The return value is the change is memory requirements. What causes
108 // increase/decrease in memory? (i) "output" is populated and (ii) just before
109 // returning, both "a" and "b" are cleared.
110 template<typename Key>
111 0 void QuantileEstimatorImpl<Key>::Collapse(vector<Key> *const a,
112 vector<Key> *const b,
113 vector<Key> *const output) {
114 // CHECK_EQ(a->size(), k_);
115 // CHECK_EQ(b->size(), k_);
116 // CHECK_EQ(output->size(), 0);
117  
118 // int memory_delta = 0;
119 0 int index_a = 0;
120 0 int index_b = 0;
121 0 int count = 0;
122 const Key* smaller;
123  
124 0 ▶ while (index_a < k_ || index_b < k_) {
Branch: 6 branches
BranchTrueFalseTotal
while (index_a < k_ || index_b < k_) { 0 0 0
while (index_a < k_ || index_b < k_) { 0 0 0
while (index_a < k_ || index_b < k_) { 0 0 0
while (index_a < k_ || index_b < k_) { 0 0 0
while (index_a < k_ || index_b < k_) { 0 0 0
while (index_a < k_ || index_b < k_) { 0 0 0
125 0 ▶ if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) {
Branch: 12 branches
BranchTrueFalseTotal
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
if (index_a >= k_ || (index_b < k_ && a->at(index_a) >= b->at(index_b))) { 0 0 0
126 0 smaller = &(b->at(index_b++));
127 } else {
128 0 smaller = &(a->at(index_a++));
129 }
130  
131 0 ▶ if ((count++ % 2) == 0) { // remember "smallest"
Branch: 3 branches
BranchTrueFalseTotal
if ((count++ % 2) == 0) { // remember "smallest" 0 0 0
if ((count++ % 2) == 0) { // remember "smallest" 0 0 0
if ((count++ % 2) == 0) { // remember "smallest" 0 0 0
132 0 output->push_back(*smaller);
133 } else { // forget "smallest"
134 // memory_delta -= smaller->size();
135 }
136 }
137  
138 // Account for the memory taken by output and a & b.
139 // memory_delta += (output->capacity() - a->capacity() - b->capacity())
140 // * sizeof(string);
141  
142 // Make sure we completely deallocate the memory taken by a & b.
143 {
144 0 vector<Key> tmp;
145 0 a->swap(tmp);
146 0 }
147 {
148 0 vector<Key> tmp;
149 0 b->swap(tmp);
150 0 }
151  
152 // return memory_delta;
153 0 }
154  
155 // Algorithm for RecursiveCollapse():
156 //
157 // 1. Let "merged" denote the output of Collapse("buf", "buffer_[level]")
158 //
159 // 2. If "buffer_[level + 1]" is full (i.e., already exists)
160 // Collapse("merged", "buffer_[level + 1]")
161 // else
162 // "buffer_[level + 1]" <-- "merged"
163 //
164 // The return value is the difference in memory usage.
165 template<typename Key>
166 0 void QuantileEstimatorImpl<Key>::RecursiveCollapse(vector<Key> *buf,
167 const int level) {
168 // VLOG(2) << StringPrintf("RecursiveCollapse() invoked with level = %d", level);
169  
170 // CHECK_EQ(buf->size(), k_);
171 // CHECK_GE(level, 1);
172 // CHECK_GE(buffer_.size(), level + 1);
173 // CHECK(buffer_[level] != NULL);
174 // CHECK_EQ(buffer_[level]->size(), k_);
175  
176 // int memory_delta = EnsureBuffer(level + 1);
177 0 EnsureBuffer(level + 1);
178  
179 vector<Key> *merged;
180 0 ▶ if (buffer_[level + 1]->size() == 0) { // buffer_[level + 1] is empty
Branch: 3 branches
BranchTrueFalseTotal
if (buffer_[level + 1]->size() == 0) { // buffer_[level + 1] is empty 0 0 0
if (buffer_[level + 1]->size() == 0) { // buffer_[level + 1] is empty 0 0 0
if (buffer_[level + 1]->size() == 0) { // buffer_[level + 1] is empty 0 0 0
181 0 merged = buffer_[level + 1];
182 } else { // buffer_[level + 1] is full
183 0 merged = new vector<Key>;
184 // merged is going to be filled with k_ elements we might as well
185 // reserve the space now.
186 0 merged->reserve(k_);
187 }
188 // We account for the memory taken by merged even if it's a
189 // temporary vector, since in this case it will be passed to
190 // RecursiveCollapse, which will substract the memory it takes.
191 // memory_delta += Collapse(buffer_[level], buf, merged);
192 0 Collapse(buffer_[level], buf, merged);
193 0 ▶ if (buffer_[level + 1] == merged) {
Branch: 3 branches
BranchTrueFalseTotal
if (buffer_[level + 1] == merged) { 0 0 0
if (buffer_[level + 1] == merged) { 0 0 0
if (buffer_[level + 1] == merged) { 0 0 0
194 // return memory_delta;
195 0 return;
196 }
197 //memory_delta += RecursiveCollapse(merged, level + 1);
198 0 RecursiveCollapse(merged, level + 1);
199 0 ▶ delete merged;
Branch: 3 branches
BranchTrueFalseTotal
delete merged; 0 0 0
delete merged; 0 0 0
delete merged; 0 0 0
200 // return memory_delta;
201 }
202  
203 // Goal: Add a new element ("elem" is a SzlEncoded value).
204 // Return value: "diff in memory usage".
205 //
206 // Algorithm:
207 // if (buffer_[0] is not full (i.e., has less than k_ elements)) {
208 // insert "elem" into buffer_[0]
209 // } else if buffer_[1] is not full (i.e., has less than k_ elements) {
210 // insert "elem" into "buffer_[1]"
211 // } else {
212 // Sort buffer_[0] and buffer_[1]
213 // RecursiveCollapse(buffer_[0], buffer_[1])
214 // Insert into buffer_[0]
215 // }
216 template<typename Key>
217 0 void QuantileEstimatorImpl<Key>::AddElem(const Key& elem) {
218 // int memory_delta = 0;
219  
220 // Update min_ and max_.
221 0 ▶ if ((tot_elems_ == 0) || (elem < min_)) {
Branch: 6 branches
BranchTrueFalseTotal
if ((tot_elems_ == 0) || (elem < min_)) { 0 0 0
if ((tot_elems_ == 0) || (elem < min_)) { 0 0 0
if ((tot_elems_ == 0) || (elem < min_)) { 0 0 0
if ((tot_elems_ == 0) || (elem < min_)) { 0 0 0
if ((tot_elems_ == 0) || (elem < min_)) { 99 1 100
if ((tot_elems_ == 0) || (elem < min_)) { 0 99 99
222 // memory_delta += elem.size() - min_.size();
223 0 min_ = elem;
224 // VLOG(3) << "AddElem(" << elem << "): min_ updated to " << min_;
225 }
226 0 ▶ if ((tot_elems_ == 0) || (max_ < elem)) {
Branch: 6 branches
BranchTrueFalseTotal
if ((tot_elems_ == 0) || (max_ < elem)) { 0 0 0
if ((tot_elems_ == 0) || (max_ < elem)) { 0 0 0
if ((tot_elems_ == 0) || (max_ < elem)) { 0 0 0
if ((tot_elems_ == 0) || (max_ < elem)) { 0 0 0
if ((tot_elems_ == 0) || (max_ < elem)) { 99 1 100
if ((tot_elems_ == 0) || (max_ < elem)) { 99 0 99
227 // memory_delta += elem.size() - max_.size();
228 0 max_ = elem;
229 // VLOG(3) << "AddElem(" << elem << "): max_ updated to " << max_;
230 }
231  
232 // First, test if both buffer_[0] and buffer_[1] are full.
233 // This is equivalent to testing
234 // (tot_elems_ > 0) && (tot_elems_ % (2 * k_) == 0).
235 // If so, sort buffer_[0] and buffer_[1] and invoke RecursiveCollapse().
236 // When RecursiveCollapse() returns, both buffer_[0] and buffer_[1] would be
237 // "empty" (i.e., with zero elements in them).
238 0 ▶ if ((tot_elems_ > 0) && (tot_elems_ % (2 * k_) == 0)) {
Branch: 6 branches
BranchTrueFalseTotal
if ((tot_elems_ > 0) && (tot_elems_ % (2 * k_) == 0)) { 0 0 0
if ((tot_elems_ > 0) && (tot_elems_ % (2 * k_) == 0)) { 0 0 0
if ((tot_elems_ > 0) && (tot_elems_ % (2 * k_) == 0)) { 0 0 0
if ((tot_elems_ > 0) && (tot_elems_ % (2 * k_) == 0)) { 0 0 0
if ((tot_elems_ > 0) && (tot_elems_ % (2 * k_) == 0)) { 99 1 100
if ((tot_elems_ > 0) && (tot_elems_ % (2 * k_) == 0)) { 0 99 99
239 // CHECK(buffer_[0] != NULL);
240 // CHECK(buffer_[1] != NULL);
241 // CHECK_EQ(buffer_[0]->size(), k_);
242 // CHECK_EQ(buffer_[1]->size(), k_);
243 // VLOG(2) << "AddElem(" << elem << "): Sorting buffer_[0] ...";
244 0 sort(buffer_[0]->begin(), buffer_[0]->end());
245 // VLOG(2) << "AddElem(" << elem << "): Sorting buffer_[1] ...";
246 0 sort(buffer_[1]->begin(), buffer_[1]->end());
247 0 const int level = 1;
248 // RecursiveCollapse will start with Collapse(buffer_[0], buffer_[level]).
249 // memory_delta += RecursiveCollapse(buffer_[0], level);
250 0 RecursiveCollapse(buffer_[0], level);
251 }
252  
253 // At this point, we are sure that either buffer_[0] or buffer_[1] can
254 // accommodate "elem".
255 //memory_delta += EnsureBuffer(0);
256 //memory_delta += EnsureBuffer(1);
257 0 EnsureBuffer(0);
258 0 EnsureBuffer(1);
259 // CHECK((buffer_[0]->size(), k_) || (buffer_[1]->size(), k_));
260 0 int index = (buffer_[0]->size() < k_) ? 0 : 1;
261 // VLOG(3) << "AddElem(" << elem << "): Inserting into buffer_[" << index << "]";
262 // int old_capacity = buffer_[index]->capacity();
263 0 buffer_[index]->push_back(elem);
264 // memory_delta += elem.size()
265 // + (buffer_[index]->capacity() - old_capacity) * sizeof(string);
266 0 ++tot_elems_;
267 // VLOG(3) << StringPrintf("AddElem(%s): returning with tot_elems_ = %lld",
268 // elem.c_str(), tot_elems_);
269 // return memory_delta;
270 0 }
271  
272 // Imported from src/emitters/szlcomputequantiles.cc by Yuji Kaneda.
273 //
274 // Please read the short description of the Munro-Paterson algorithm at the
275 // beginning of sawquantile.cc
276 //
277 // Basically, our goal is to compute quantiles from a bunch of buffers.
278 // We assign a "weight" of 2^i to every element of a buffer at
279 // level i in the binary tree (leaves are at level 0). Now sort all the
280 // elements (in various buffers) together. Then compute the "weighted 100
281 // splitters" of this sequence.
282 template<typename Key>
283 0 void ComputeQuantiles(const vector<vector<Key>* >& buffer,
284 const Key& min_Key, const Key& max_Key,
285 size_t num_quantiles, int64 tot_elems,
286 vector<Key>* quantiles) {
287 // CHECK(max_Key >= min_Key);
288 // CHECK_GE(buffer.size(), 1);
289  
290 0 quantiles->clear();
291  
292 // VLOG(2) << "ComputeQuantiles(): min=" << min_Key;
293 0 ▶ quantiles->push_back(min_Key);
Branch: 3 branches
BranchTrueFalseTotal
quantiles->push_back(min_Key); 0 0 0
quantiles->push_back(min_Key); 0 0 0
quantiles->push_back(min_Key); 1 0 1
294  
295 // buffer[0] and buffer[1] may be unsorted; all others are already sorted.
296 0 ▶ if (buffer[0] == NULL) {
Branch: 3 branches
BranchTrueFalseTotal
if (buffer[0] == NULL) { 0 0 0
if (buffer[0] == NULL) { 0 0 0
if (buffer[0] == NULL) { 1 0 1
297 // VLOG(2) << "ComputeQuantiles(): Not sorting buffer[0] (it is NULL).";
298 } else {
299 // VLOG(2) << "ComputeQuantiles(): Sorting buffer[0] ...";
300 0 ▶ sort(buffer[0]->begin(), buffer[0]->end());
Branch: 3 branches
BranchTrueFalseTotal
sort(buffer[0]->begin(), buffer[0]->end()); 0 0 0
sort(buffer[0]->begin(), buffer[0]->end()); 0 0 0
sort(buffer[0]->begin(), buffer[0]->end()); 1 0 1
301 }
302 0 ▶ if ((buffer.size() < 2) || (buffer[1] == NULL)) {
Branch: 9 branches
BranchTrueFalseTotal
if ((buffer.size() < 2) || (buffer[1] == NULL)) { 0 0 0
if ((buffer.size() < 2) || (buffer[1] == NULL)) { 0 0 0
if ((buffer.size() < 2) || (buffer[1] == NULL)) { 0 0 0
if ((buffer.size() < 2) || (buffer[1] == NULL)) { 0 0 0
if ((buffer.size() < 2) || (buffer[1] == NULL)) { 0 0 0
if ((buffer.size() < 2) || (buffer[1] == NULL)) { 0 0 0
if ((buffer.size() < 2) || (buffer[1] == NULL)) { 1 0 1
if ((buffer.size() < 2) || (buffer[1] == NULL)) { 0 1 1
if ((buffer.size() < 2) || (buffer[1] == NULL)) { 1 0 1
303 // VLOG(2) << "ComputeQuantiles(): Not sorting buffer[1] (it doesn't exist).";
304 } else {
305 // VLOG(2) << "ComputeQuantiles(): Sorting buffer[1] ...";
306 0 ▶ sort(buffer[1]->begin(), buffer[1]->end());
Branch: 3 branches
BranchTrueFalseTotal
sort(buffer[1]->begin(), buffer[1]->end()); 0 0 0
sort(buffer[1]->begin(), buffer[1]->end()); 0 0 0
sort(buffer[1]->begin(), buffer[1]->end()); 1 0 1
307 }
308  
309 // Simple sanity check: the weighted sum of all buffers should equal
310 // "tot_elems". The weight of buffer[i] is 2^(i-1) for i >= 2. Otherwise,
311 // the weight is 1.
312 // int64 t = 0;
313 // for (int j = 0; j < buffer.size(); ++j) {
314 // const int64 weight = (j <= 1) ? 1LL : (0x1LL << (j - 1));
315 // t += (buffer[j] == NULL) ? 0 : (buffer[j]->size() * weight);
316 // }
317 // CHECK_EQ(t, tot_elems);
318  
319 0 ▶ vector<size_t> index(buffer.size(), 0);
Branch: 3 branches
BranchTrueFalseTotal
vector<size_t> index(buffer.size(), 0); 0 0 0
vector<size_t> index(buffer.size(), 0); 0 0 0
vector<size_t> index(buffer.size(), 0); 1 0 1
320  
321 // Our goal is to identify the weighted "num_quantiles - 2" splitters in the
322 // sorted sequence of all buffers taken together.
323 // "S" will store the cumulative weighted sum so far.
324 0 int64 S = 0;
325 0 ▶ for (size_t i = 1; i <= num_quantiles - 2; ++i) {
Branch: 3 branches
BranchTrueFalseTotal
for (size_t i = 1; i <= num_quantiles - 2; ++i) { 0 0 0
for (size_t i = 1; i <= num_quantiles - 2; ++i) { 0 0 0
for (size_t i = 1; i <= num_quantiles - 2; ++i) { 98 1 99
326 // Target "S" for the next splitter (next quantile).
327 0 const int64 target_S
328 0 = static_cast<int64>(ceil(i * (tot_elems / (num_quantiles - 1.0))));
329 // CHECK_LE(target_S, tot_elems);
330  
331 0 while (true) {
332 // Identify the smallest element among buffer_[0][index[0]],
333 // buffer_[1][index[1]], buffer_[2][index[2]], ...
334 0 Key smallest = max_Key;
335 0 int min_buffer_id = -1;
336 0 ▶ for (size_t j = 0; j < buffer.size(); ++j) {
Branch: 3 branches
BranchTrueFalseTotal
for (size_t j = 0; j < buffer.size(); ++j) { 0 0 0
for (size_t j = 0; j < buffer.size(); ++j) { 0 0 0
for (size_t j = 0; j < buffer.size(); ++j) { 392 196 588
337 0 ▶ if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) {
Branch: 9 branches
BranchTrueFalseTotal
if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) { 0 0 0
if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) { 0 0 0
if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) { 0 0 0
if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) { 0 0 0
if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) { 0 0 0
if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) { 0 0 0
if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) { 392 0 392
if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) { 196 196 392
if ((buffer[j] != NULL) && (index[j] < buffer[j]->size())) { 196 196 392
338 0 ▶ if (!(smallest < buffer[j]->at(index[j]))) {
Branch: 6 branches
BranchTrueFalseTotal
if (!(smallest < buffer[j]->at(index[j]))) { 0 0 0
if (!(smallest < buffer[j]->at(index[j]))) { 0 0 0
if (!(smallest < buffer[j]->at(index[j]))) { 0 0 0
if (!(smallest < buffer[j]->at(index[j]))) { 0 0 0
if (!(smallest < buffer[j]->at(index[j]))) { 196 0 196
if (!(smallest < buffer[j]->at(index[j]))) { 196 0 196
339 0 ▶ smallest = buffer[j]->at(index[j]);
Branch: 3 branches
BranchTrueFalseTotal
smallest = buffer[j]->at(index[j]); 0 0 0
smallest = buffer[j]->at(index[j]); 0 0 0
smallest = buffer[j]->at(index[j]); 196 0 196
340 0 min_buffer_id = j;
341 }
342 }
343 }
344 0 ▶ if (min_buffer_id < 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (min_buffer_id < 0) { 0 0 0
if (min_buffer_id < 0) { 0 0 0
if (min_buffer_id < 0) { 0 196 196
345 0 ▶ quantiles->push_back(max_Key);
Branch: 3 branches
BranchTrueFalseTotal
quantiles->push_back(max_Key); 0 0 0
quantiles->push_back(max_Key); 0 0 0
quantiles->push_back(max_Key); 0 0 0
346 0 break;
347 }
348  
349 // Now increment "S" by the weight associated with "min_buffer_id".
350 //
351 // Note: The "weight" of elements in buffer[0] and buffer[1] is 1 (these
352 // are leaf nodes in the Munro-Paterson "tree of buffers".
353 // The weight of elements in buffer[i] is 2^(i-1) for i >= 2.
354 0 ▶ int64 S_incr = (min_buffer_id <= 1) ? 1 : (min_buffer_id - 1 < 62 ? (static_cast<int64>(1) << (min_buffer_id - 1)) : (static_cast<int64>(1) << 62));
Branch: 6 branches
BranchTrueFalseTotal
int64 S_incr = (min_buffer_id <= 1) ? 1 : (min_buffer_id - 1 < 62 ? (static_cast<int64>(1) << (min_buffer_id - 1)) : (static_cast<int64>(1) << 62)); 0 0 0
int64 S_incr = (min_buffer_id <= 1) ? 1 : (min_buffer_id - 1 < 62 ? (static_cast<int64>(1) << (min_buffer_id - 1)) : (static_cast<int64>(1) << 62)); 0 0 0
int64 S_incr = (min_buffer_id <= 1) ? 1 : (min_buffer_id - 1 < 62 ? (static_cast<int64>(1) << (min_buffer_id - 1)) : (static_cast<int64>(1) << 62)); 0 0 0
int64 S_incr = (min_buffer_id <= 1) ? 1 : (min_buffer_id - 1 < 62 ? (static_cast<int64>(1) << (min_buffer_id - 1)) : (static_cast<int64>(1) << 62)); 0 0 0
int64 S_incr = (min_buffer_id <= 1) ? 1 : (min_buffer_id - 1 < 62 ? (static_cast<int64>(1) << (min_buffer_id - 1)) : (static_cast<int64>(1) << 62)); 0 196 196
int64 S_incr = (min_buffer_id <= 1) ? 1 : (min_buffer_id - 1 < 62 ? (static_cast<int64>(1) << (min_buffer_id - 1)) : (static_cast<int64>(1) << 62)); 0 0 0
355  
356 // If we have met/exceeded "target_S", we have found the next quantile.
357 // Then break the loop. Otherwise, just update index[min_buffer_id] and S
358 // appropriately.
359 0 ▶ if (S + S_incr >= target_S) {
Branch: 3 branches
BranchTrueFalseTotal
if (S + S_incr >= target_S) { 0 0 0
if (S + S_incr >= target_S) { 0 0 0
if (S + S_incr >= target_S) { 98 98 196
360 // CHECK(buffer[min_buffer_id]->at(index[min_buffer_id]) == smallest);
361 0 ▶ quantiles->push_back(smallest);
Branch: 3 branches
BranchTrueFalseTotal
quantiles->push_back(smallest); 0 0 0
quantiles->push_back(smallest); 0 0 0
quantiles->push_back(smallest); 98 0 98
362 0 break;
363 } else {
364 0 ++index[min_buffer_id];
365 0 S += S_incr;
366 }
367 }
368 }
369  
370 // VLOG(2) << KeyPrintf("ComputeQuantiles(): max=%s", max_Key.c_str());
371 0 ▶ quantiles->push_back(max_Key);
Branch: 3 branches
BranchTrueFalseTotal
quantiles->push_back(max_Key); 0 0 0
quantiles->push_back(max_Key); 0 0 0
quantiles->push_back(max_Key); 1 0 1
372 0 }
373  
374 template<typename Key>
375 2 void QuantileEstimatorImpl<Key>::Estimate(vector<Key>& output) {
376 2 output.clear();
377 2 if (tot_elems_ == 0) {
378 1 output.push_back(Key());
379 1 return;
380 }
381 // We display the quantiles, not the raw output.
382 1 ComputeQuantiles<Key>(buffer_, min_, max_, num_quantiles_, tot_elems_, &output);
383 }
384  
385 }
↑ Summary src/emitters/szlquantile.h »