File size: 11,415 Bytes
158b61b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
/**
* LossyCounter - implementation of Lossy Counting algorithm as described in:
* Approximate Frequency Counts over Data Streams, G.S.Manku & R.Motwani, (2002)
*
* (C) Ceslav Przywara, UFAL MFF UK, 2011
*
* Implementation note: define USE_UNORDERED_MAP to use std::tr1::unordered_map
* instead std::map for storage of lossy counted items.
*
* $Id$
*/
#ifndef LOSSYCOUNTER_H
#define LOSSYCOUNTER_H
#include <cstddef>
#include <cmath>
#ifdef USE_UNORDERED_MAP
#include <tr1/unordered_map>
#else
#include <map>
#endif
#include <iterator>
// Iterators:
template<class T>
class LossyCounterIterator;
template<class T>
class LossyCounterErasingIterator;
////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Lossy Counter Interface ////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* Implementation of Lossy Counting algorithm as described in:
* Approximate Frequency Counts over Data Streams, G.S.Manku & R.Motwani, (2002)
*/
template<class T>
class LossyCounter {
public:
// Error parameter type definition.
typedef double error_t;
// Support parameter type definition.
typedef double support_t;
// Counters type definition.
typedef size_t counter_t;
// Frequency counter type definition (f).
typedef counter_t frequency_t;
// Maximum error counter type definition (Δ).
typedef counter_t maximum_error_t;
// Pair: frequency (f) and possible maximum error (Δ).
typedef std::pair<frequency_t, maximum_error_t> item_counts_t;
#ifdef USE_UNORDERED_MAP
typedef std::tr1::unordered_map<T, item_counts_t, typename T::Hash> storage_t;
#else
// Mapping: counted item and its frequency and max. error counts.
typedef std::map<T, item_counts_t> storage_t;
#endif
// We provide own version of const iterator.
typedef LossyCounterIterator<T> const_iterator;
// Special type of iterator: leaves no item behind!
typedef LossyCounterErasingIterator<T> erasing_iterator;
/** @var Error parameter value (ε) */
const error_t error;
/** @var Supprort parameter value (s) */
const support_t support;
/** @var Width of single bucket (w) */
const counter_t bucketWidth; // ceil(1/error)
private:
/** @var Current epoch bucket ID (b-current) */
counter_t _bucketId;
/** @var Count of items read in so far (N) */
counter_t _count;
/** @var Items storage (Ɗ) */
storage_t _storage;
public:
/**
* Set error to 0 to disable lossy-pruning.
* @param _error Value from interval [0.0, 1.0).
* @param _support Value from interval [0.0, 1.0).
*/
LossyCounter<T>(error_t _error, support_t _support):
error(_error), support(_support), bucketWidth(_error > 0.0 ? ceil(1/_error) : 0), _bucketId(1), _count(0), _storage() {}
/**
* @param item Item to be added to storage.
*/
void add(const T& item);
/**
* @return Constant iterator pointing to the beginning of storage.
*/
const_iterator begin(void) const { return LossyCounterIterator<T>(threshold(), _storage.begin(), _storage.end()); }
/**
* @return Constant iterator pointing to the end of storage.
*/
const_iterator end(void) const { return LossyCounterIterator<T>(_storage.end()); }
/**
* @return Erasing iterator pointing to the beginning of storage.
*/
erasing_iterator beginErase(void) { return LossyCounterErasingIterator<T>(threshold(), _storage); }
/**
* @return Erasing iterator pointing to the end of storage.
*/
erasing_iterator endErase(void) { return LossyCounterErasingIterator<T>(_storage); }
/**
* @return Current bucket ID.
*/
counter_t bucketId(void) const { return _bucketId; }
/**
* @return Number of items added to storage so far (N).
*/
counter_t count(void) const { return _count; }
/**
* @return Number of items currently in storage.
*/
size_t size(void) const { return _storage.size(); }
/**
* @param positive Return sN value instead of (s-ε)N?
* @return Threshold (either positive or negative) value.
*/
double threshold(bool positive = false) const { return positive ? support * _count : (support - error) * _count; }
/**
* @return The maximum value of which estimated frequencies are less than the true frequencies.
*/
double maxError(void) const { return error * _count; }
/**
* @return True, if it's prunning time right now!
*/
bool aboutToPrune(void) const { return (bucketWidth != 0) && ((_count % bucketWidth) == 0); }
private:
/**
* Prunes counts table.
*/
void prune(void);
};
////////////////////////////////////////////////////////////////////////////////
/////////////////////// Lossy Counter Iterator Interface ///////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* Lossy counter iterator is designed to iterate over items passing the current
* threshold.
*/
template<class T>
class LossyCounterIterator: public std::iterator<std::forward_iterator_tag, typename LossyCounter<T>::storage_t::value_type> {
public:
typedef LossyCounterIterator<T> self_type;
typedef typename LossyCounter<T>::storage_t::const_iterator const_iterator;
protected:
/** @var Minimum frequency threshold */
const double _threshold;
/** @var Current position of iterator (based on underlying container) */
const_iterator _current;
/** @var End position in underlying container */
const const_iterator _end;
public:
// Constructors.
LossyCounterIterator<T>(const_iterator end):
_threshold(0), _current(_end), _end(end) {}
LossyCounterIterator<T>(double threshold, const_iterator begin, const_iterator end):
_threshold(threshold), _current(begin), _end(end) {
// Forward the iterator to the first valid item (with frequency above threshold)!
this->forward(true);
}
// Operators.
/**
* Only items passing the threshold are included in iteration.
*/
LossyCounterIterator<T> operator++(void); // ++this
/**
* Only items passing the threshold are included in iteration.
*/
LossyCounterIterator<T> operator++(int junk); // this++
bool operator==(const self_type& rhs) const { return _current == rhs._current; }
bool operator!=(const self_type& rhs) const { return _current != rhs._current; }
// Interface.
/**
* @return Current item.
*/
const T& item(void) const { return _current->first; }
/**
* @return Current item's frequency.
*/
typename LossyCounter<T>::frequency_t frequency(void) const { return _current->second.first; }
/**
* @return Current item's maximum error.
*/
typename LossyCounter<T>::error_t max_error(void) const { return _current->second.second; }
protected:
/**
* @param init Check also the item that iterator _current points to? Useful when initializing.
*/
void forward(bool init);
};
/**
* Lossy counter iterator erasing all items passed by.
*/
template<class T>
class LossyCounterErasingIterator: public LossyCounterIterator<T> {
public:
typedef typename LossyCounter<T>::storage_t storage_t;
private:
storage_t& _storage;
public:
// Constructors - have to be aware of the "mother" container.
LossyCounterErasingIterator<T>(storage_t& storage): LossyCounterIterator<T>(storage.end()), _storage(storage) {}
LossyCounterErasingIterator<T>(double threshold, storage_t& storage): LossyCounterIterator<T>(threshold, storage.begin(), storage.end()), _storage(storage) {}
protected:
/**
* @param init Check also the item that iterator _current points to? Useful when initializing.
*/
void forward(bool init);
};
////////////////////////////////////////////////////////////////////////////////
//////////////////////// Lossy Counter Implementation //////////////////////////
////////////////////////////////////////////////////////////////////////////////
template<class T>
void LossyCounter<T>::add(const T& item) {
typename storage_t::iterator iter = _storage.find(item);
if ( iter == _storage.end() ) {
// Insert new item with appropriate frequency and maximum-possible-error.
_storage.insert(std::make_pair(item, item_counts_t(1, _bucketId - 1)));
}
else {
// Update frequency of existing.
iter->second.first += 1;
}
// Finally increment the counter and check if the table shall be pruned.
++_count;
if ( this->aboutToPrune() ) {
this->prune();
++_bucketId;
}
}
template<class T>
void LossyCounter<T>::prune(void) {
for ( typename storage_t::iterator iter = _storage.begin(); iter != _storage.end(); /* Incrementation step missing intentionally! */ ) {
// Prune, if: maximum possible error + frequency <= ID of current bucket
if ( (iter->second.first + iter->second.second) <= _bucketId ) {
_storage.erase(iter++); // Post increment!
}
else {
++iter;
}
}
}
////////////////////////////////////////////////////////////////////////////////
//////////////////// Lossy Counter Iterator Implementation /////////////////////
////////////////////////////////////////////////////////////////////////////////
template<class T>
LossyCounterIterator<T> LossyCounterIterator<T>::operator++(void) {
this->forward();
return *this;
}
template<class T>
LossyCounterIterator<T> LossyCounterIterator<T>::operator++(int junk) {
self_type iter = *this;
this->forward();
return iter;
}
template<class T>
void LossyCounterIterator<T>::forward(bool init = false) {
if ( _current == _end ) {
return; // Nowhere to go, we're at the end already.
}
if ( init && (this->frequency() >= _threshold) ) {
// Ok, we're initing and we're already at the item passing the threshold.
return;
}
// Keep going until we reach the end or the next item with frequency NOT below the threshold.
while ( (++_current != _end) && (this->frequency() < _threshold) );
}
////////////////////////////////////////////////////////////////////////////////
//////////////// Lossy Counter Erasing Iterator Implementation /////////////////
////////////////////////////////////////////////////////////////////////////////
template<class T>
void LossyCounterErasingIterator<T>::forward(bool init = false) {
if (this->_current == this->_end ) {
return; // Nowhere to go, we're at the end already.
}
if ( init && (this->frequency() >= this->_threshold) ) {
// Ok, we're initing and we're already at the item passing the threshold.
return;
}
// This is where erasing forward() and std forward() differ:
typename LossyCounterIterator<T>::const_iterator previous = this->_current;
// Keep going...
while ( ++this->_current != this->_end ) {
if ( this->frequency() < this->_threshold ) {
// Get rid of previous, cause we'll go on.
_storage.erase(previous);
previous = this->_current;
} else {
break;
}
}
_storage.erase(previous); // !
}
#endif /* LOSSYCOUNTER_H */
|