| 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 |
|
| #include <stdint.h> |
| 18 |
|
| #include <string> |
| 19 |
|
| #include <vector> |
| 20 |
|
| |
| 21 |
|
| #include "szaru.h" |
| 22 |
|
| |
| 23 |
|
| using namespace std; |
| 24 |
|
| // Structure for storing approx quantiles for each key in the table. |
| 25 |
|
| // Declared as: table quantile(N)[...] of value: <ordered sawtype> |
| 26 |
|
| // The parameter N (>=2) sets the relative error (eps_) that we are ready to |
| 27 |
|
| // tolerate. eps_ is set equal to 1/(N-1). This means that if |
| 28 |
|
| // there are "tot_elems_ " total emits to a particular key, then an |
| 29 |
|
| // element that we claim has rank X could have true rank in the range |
| 30 |
|
| // [X - eps_*tot_elems_, X + eps_*tot_elems_]. |
| 31 |
|
| |
| 32 |
|
| namespace SZaru { |
| 33 |
|
| // The entry for each key inserted in a szl "table". If the |
| 34 |
|
| // table is not indexed then there is only one entry |
| 35 |
|
| // for the entire table. |
| 36 |
|
| template<typename Key> |
| 37 |
|
| class QuantileEstimatorImpl : public QuantileEstimator<Key> { |
| 38 |
|
| public: |
| 39 |
0 |
| explicit QuantileEstimatorImpl(int param) |
| 40 |
0 |
| : tot_elems_(0), |
| 41 |
0 |
| num_quantiles_(std::max(param, 2)) { |
| 42 |
0 |
| k_ = ComputeK(); |
| 43 |
0 |
| Clear(); |
| 44 |
0 |
| } |
| 45 |
0 |
| virtual ~QuantileEstimatorImpl() { Clear(); } |
| 46 |
|
| |
| 47 |
|
| virtual void AddElem(const Key& elem); |
| 48 |
|
| // virtual void Flush(string* output); |
| 49 |
|
| // virtual void FlushForDisplay(vector<string>* output); |
| 50 |
|
| // virtual SzlTabEntry::MergeStatus Merge(const string& val); |
| 51 |
|
| |
| 52 |
0 |
| void Clear() { |
| 53 |
0 |
▶ | for (size_t i = 0; i < buffer_.size(); i++) |
Branch: 3 branches
| Branch | True | False | Total |
| for (size_t i = 0; i < buffer_.size(); i++) |
2 |
2 |
4 |
| for (size_t i = 0; i < buffer_.size(); i++) |
0 |
0 |
0 |
| for (size_t i = 0; i < buffer_.size(); i++) |
0 |
0 |
0 |
|
| 54 |
0 |
▶ | delete buffer_[i]; |
Branch: 3 branches
| Branch | True | False | Total |
| delete buffer_[i]; |
2 |
0 |
2 |
| delete buffer_[i]; |
0 |
0 |
0 |
| delete buffer_[i]; |
0 |
0 |
0 |
|
| 55 |
0 |
| buffer_.clear(); |
| 56 |
0 |
| tot_elems_ = 0; |
| 57 |
0 |
| } |
| 58 |
|
| |
| 59 |
|
| // virtual int Memory(); |
| 60 |
0 |
| virtual uint64_t TotElems() const { return tot_elems_; } |
| 61 |
0 |
| virtual int TupleCount() { return buffer_.size(); } |
| 62 |
|
| |
| 63 |
|
| virtual void Estimate(vector<Key>& output); |
| 64 |
|
| |
| 65 |
|
| // const SzlOps& element_ops() const { return element_ops_; } |
| 66 |
|
| |
| 67 |
|
| private: |
| 68 |
|
| // const SzlOps& element_ops_; |
| 69 |
|
| |
| 70 |
|
| // We support quantiles over a sequence of upto MAX_TOT_ELEMS = 1 Trillion |
| 71 |
|
| // elements. The value of k_, the buffer-size in the Munro-Paterson algorithm |
| 72 |
|
| // grows roughly logarithmically as MAX_TOT_ELEMS. So we set |
| 73 |
|
| // MAX_TOT_ELEMS to a "large-enough" value, and not to kint64max. |
| 74 |
|
| static const int64_t MAX_TOT_ELEMS = 1024LL * 1024LL * 1024LL * 1024LL; |
| 75 |
|
| |
| 76 |
|
| int64_t ComputeK(); |
| 77 |
|
| void EnsureBuffer(const int level); |
| 78 |
|
| void Collapse(std::vector<Key> *const a, std::vector<Key> *const b, |
| 79 |
|
| std::vector<Key> *const output); |
| 80 |
|
| void RecursiveCollapse(std::vector<Key> *buf, const int level); |
| 81 |
|
| // bool EncodingToString(SzlDecoder *const dec, string *const output); |
| 82 |
|
| |
| 83 |
|
| uint64_t tot_elems_; |
| 84 |
|
| const int num_quantiles_; // #quantiles |
| 85 |
|
| std::vector<std::vector<Key>* > buffer_; |
| 86 |
|
| int64_t k_; // max #elements in any buffer_[i] |
| 87 |
|
| Key min_; |
| 88 |
|
| Key max_; |
| 89 |
|
| }; |
| 90 |
|
| |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| #include "emitters/szlquantile.cc" |