00001 // Copyright 2010 Yuji Kaneda 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // http://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 // ------------------------------------------------------------------------ 00015 00073 #ifndef _SZARU_ 00074 #define _SZARU_ 00075 00076 #include <stdint.h> 00077 #include <string> 00078 #include <vector> 00079 00080 namespace SZaru { 00081 00082 const char* const VERSION = "0.1.0"; 00083 00085 00100 class UniqueEstimator { 00101 public: 00103 00106 static UniqueEstimator* Create(int nElemes); 00107 00108 virtual ~UniqueEstimator() {}; 00109 00111 00114 virtual void AddElem(const std::string& elm) = 0; 00115 00117 00121 virtual void AddElemInCIF(const char *data, size_t size) = 0; 00122 00124 00127 virtual int64_t Estimate() const = 0; 00128 00130 00133 virtual uint64_t TotElems() const = 0; 00134 00135 protected: 00136 UniqueEstimator() {} 00137 }; 00138 00140 00147 template<typename Value> 00148 class TopEstimator { 00149 public: 00151 00154 static TopEstimator* Create(uint32_t numTops); 00155 00156 virtual ~TopEstimator() {}; 00157 00159 struct Elem { 00160 std::string value; 00161 Value weight; 00162 }; 00163 00165 00168 virtual void AddElem(const std::string& elm) = 0; 00169 00171 00175 virtual void AddWeightedElem(const std::string& elem, Value weight) = 0; 00176 00178 00181 virtual void Estimate(std::vector<Elem>& topElems) = 0; 00182 00184 00187 virtual uint64_t TotElems() const = 0; 00188 00189 protected: 00190 TopEstimator() {} 00191 }; 00192 00194 00202 template <typename Key> 00203 class QuantileEstimator { 00204 public: 00206 00209 static QuantileEstimator* Create(uint32_t numQuantiles); 00210 00211 virtual ~QuantileEstimator() {}; 00212 00214 00217 virtual void AddElem(const Key& elm) = 0; 00218 00220 00223 virtual void Estimate(std::vector<Key>& output) = 0; 00224 00226 00229 virtual uint64_t TotElems() const = 0; 00230 00231 protected: 00232 QuantileEstimator() {} 00233 }; 00234 00235 00236 00237 00238 // template specialization 00239 template <> 00240 TopEstimator<int32_t>* 00241 TopEstimator<int32_t>::Create(uint32_t numTops); 00242 00243 template <> 00244 TopEstimator<int64_t>* 00245 TopEstimator<int64_t>::Create(uint32_t numTops); 00246 00247 template <> 00248 TopEstimator<double>* 00249 TopEstimator<double>::Create(uint32_t numTops); 00250 00251 template <> 00252 QuantileEstimator<int32_t>* 00253 QuantileEstimator<int32_t>::Create(uint32_t numQuantiles); 00254 00255 template <> 00256 QuantileEstimator<int64_t>* 00257 QuantileEstimator<int64_t>::Create(uint32_t numQuantiles); 00258 00259 template <> 00260 QuantileEstimator<double>* 00261 QuantileEstimator<double>::Create(uint32_t numQuantiles); 00262 00263 } 00264 00265 #endif // _SZARU_