Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::bloom_filter Class Reference

#include <bloom_filter.hpp>

Collaboration diagram for fc::bloom_filter:

Public Member Functions

 bloom_filter ()
 
 bloom_filter (const bloom_parameters &p)
 
 bloom_filter (const bloom_filter &filter)
 
bool operator== (const bloom_filter &f) const
 
bool operator!= (const bloom_filter &f) const
 
bloom_filteroperator= (const bloom_filter &f)
 
virtual ~bloom_filter ()
 
bool operator! () const
 
void clear ()
 
void insert (const unsigned char *key_begin, const std::size_t &length)
 
template<typename T >
void insert (const T &t)
 
void insert (const std::string &key)
 
void insert (const char *data, const std::size_t &length)
 
template<typename InputIterator >
void insert (const InputIterator begin, const InputIterator end)
 
virtual bool contains (const unsigned char *key_begin, const std::size_t length) const
 
template<typename T >
bool contains (const T &t) const
 
bool contains (const std::string &key) const
 
bool contains (const char *data, const std::size_t &length) const
 
template<typename InputIterator >
InputIterator contains_all (const InputIterator begin, const InputIterator end) const
 
template<typename InputIterator >
InputIterator contains_none (const InputIterator begin, const InputIterator end) const
 
virtual unsigned long long int size () const
 
std::size_t element_count () const
 
double effective_fpp () const
 
bloom_filteroperator&= (const bloom_filter &f)
 
bloom_filteroperator|= (const bloom_filter &f)
 
bloom_filteroperator^= (const bloom_filter &f)
 
const cell_typetable () const
 
std::size_t hash_count ()
 

Public Attributes

std::vector< bloom_typesalt_
 
std::vector< unsigned char > bit_table_
 
unsigned int salt_count_
 
unsigned long long int table_size_
 
unsigned long long int raw_table_size_
 
unsigned long long int projected_element_count_
 
unsigned int inserted_element_count_
 
unsigned long long int random_seed_
 
double desired_false_positive_probability_
 

Protected Types

typedef unsigned int bloom_type
 
typedef unsigned char cell_type
 

Protected Member Functions

virtual void compute_indices (const bloom_type &hash, std::size_t &bit_index, std::size_t &bit) const
 
void generate_unique_salt ()
 
bloom_type hash_ap (const unsigned char *begin, std::size_t remaining_length, bloom_type hash) const
 

Detailed Description

Definition at line 161 of file bloom_filter.hpp.

Member Typedef Documentation

◆ bloom_type

unsigned int fc::bloom_filter::bloom_type
protected

Definition at line 165 of file bloom_filter.hpp.

◆ cell_type

unsigned char fc::bloom_filter::cell_type
protected

Definition at line 166 of file bloom_filter.hpp.

Constructor & Destructor Documentation

◆ bloom_filter() [1/3]

fc::bloom_filter::bloom_filter ( )
inline

Definition at line 170 of file bloom_filter.hpp.

171 : salt_count_(0),
172 table_size_(0),
176 random_seed_(0),
178 {}
unsigned int salt_count_
unsigned long long int table_size_
unsigned long long int projected_element_count_
unsigned long long int raw_table_size_
double desired_false_positive_probability_
unsigned int inserted_element_count_
unsigned long long int random_seed_

◆ bloom_filter() [2/3]

fc::bloom_filter::bloom_filter ( const bloom_parameters & p)
inline

Definition at line 180 of file bloom_filter.hpp.

181 : projected_element_count_(p.projected_element_count),
183 random_seed_((p.random_seed * 0xA5A5A5A5) + 1),
184 desired_false_positive_probability_(p.false_positive_probability)
185 {
186 salt_count_ = p.optimal_parameters.number_of_hashes;
187 table_size_ = p.optimal_parameters.table_size;
189 raw_table_size_ = table_size_ / bits_per_char;
190
191 bit_table_.resize( static_cast<std::size_t>(raw_table_size_) );
192 //bit_table_ = new cell_type[static_cast<std::size_t>(raw_table_size_)];
193 std::fill_n(bit_table_.data(),raw_table_size_,0x00);
194 }
const mie::Vuint & p
Definition bn.cpp:27
std::vector< unsigned char > bit_table_
Here is the call graph for this function:

◆ bloom_filter() [3/3]

fc::bloom_filter::bloom_filter ( const bloom_filter & filter)
inline

Definition at line 196 of file bloom_filter.hpp.

197 {
198 this->operator=(filter);
199 }
bloom_filter & operator=(const bloom_filter &f)
Here is the call graph for this function:

◆ ~bloom_filter()

virtual fc::bloom_filter::~bloom_filter ( )
inlinevirtual

Definition at line 243 of file bloom_filter.hpp.

244 {
245 }

Member Function Documentation

◆ clear()

void fc::bloom_filter::clear ( )
inline

Definition at line 252 of file bloom_filter.hpp.

253 {
254 std::fill_n(bit_table_.data(),raw_table_size_,0x00);
256 }

◆ compute_indices()

virtual void fc::bloom_filter::compute_indices ( const bloom_type & hash,
std::size_t & bit_index,
std::size_t & bit ) const
inlineprotectedvirtual

Definition at line 443 of file bloom_filter.hpp.

444 {
445 bit_index = hash % table_size_;
446 bit = bit_index % bits_per_char;
447 }
int bit
Definition yubihsm.h:566
Here is the caller graph for this function:

◆ contains() [1/4]

bool fc::bloom_filter::contains ( const char * data,
const std::size_t & length ) const
inline

Definition at line 323 of file bloom_filter.hpp.

324 {
325 return contains(reinterpret_cast<const unsigned char*>(data),length);
326 }
virtual bool contains(const unsigned char *key_begin, const std::size_t length) const
Here is the call graph for this function:

◆ contains() [2/4]

bool fc::bloom_filter::contains ( const std::string & key) const
inline

Definition at line 318 of file bloom_filter.hpp.

319 {
320 return contains(reinterpret_cast<const unsigned char*>(key.c_str()),key.size());
321 }
Here is the call graph for this function:

◆ contains() [3/4]

template<typename T >
bool fc::bloom_filter::contains ( const T & t) const
inline

Definition at line 313 of file bloom_filter.hpp.

314 {
315 return contains(reinterpret_cast<const unsigned char*>(&t),static_cast<std::size_t>(sizeof(T)));
316 }
#define T(meth, val, expected)
Here is the call graph for this function:

◆ contains() [4/4]

virtual bool fc::bloom_filter::contains ( const unsigned char * key_begin,
const std::size_t length ) const
inlinevirtual

Definition at line 297 of file bloom_filter.hpp.

298 {
299 std::size_t bit_index = 0;
300 std::size_t bit = 0;
301 for (std::size_t i = 0; i < salt_.size(); ++i)
302 {
303 compute_indices(hash_ap(key_begin,length,salt_[i]),bit_index,bit);
304 if ((bit_table_[bit_index / bits_per_char] & bit_mask[bit]) != bit_mask[bit])
305 {
306 return false;
307 }
308 }
309 return true;
310 }
std::vector< bloom_type > salt_
virtual void compute_indices(const bloom_type &hash, std::size_t &bit_index, std::size_t &bit) const
bloom_type hash_ap(const unsigned char *begin, std::size_t remaining_length, bloom_type hash) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ contains_all()

template<typename InputIterator >
InputIterator fc::bloom_filter::contains_all ( const InputIterator begin,
const InputIterator end ) const
inline

Definition at line 329 of file bloom_filter.hpp.

330 {
331 InputIterator itr = begin;
332 while (end != itr)
333 {
334 if (!contains(*itr))
335 {
336 return itr;
337 }
338 ++itr;
339 }
340 return end;
341 }
Here is the call graph for this function:

◆ contains_none()

template<typename InputIterator >
InputIterator fc::bloom_filter::contains_none ( const InputIterator begin,
const InputIterator end ) const
inline

Definition at line 344 of file bloom_filter.hpp.

345 {
346 InputIterator itr = begin;
347 while (end != itr)
348 {
349 if (contains(*itr))
350 {
351 return itr;
352 }
353 ++itr;
354 }
355 return end;
356 }
Here is the call graph for this function:

◆ effective_fpp()

double fc::bloom_filter::effective_fpp ( ) const
inline

Definition at line 368 of file bloom_filter.hpp.

369 {
370 /*
371 Note:
372 The effective false positive probability is calculated using the
373 designated table size and hash function count in conjunction with
374 the current number of inserted elements - not the user defined
375 predicated/expected number of inserted elements.
376 */
377 return std::pow(1.0 - std::exp(-1.0 * salt_.size() * inserted_element_count_ / size()), 1.0 * salt_.size());
378 }
virtual unsigned long long int size() const
Here is the call graph for this function:

◆ element_count()

std::size_t fc::bloom_filter::element_count ( ) const
inline

Definition at line 363 of file bloom_filter.hpp.

364 {
366 }

◆ generate_unique_salt()

void fc::bloom_filter::generate_unique_salt ( )
inlineprotected

Definition at line 449 of file bloom_filter.hpp.

450 {
451 /*
452 Note:
453 A distinct hash function need not be implementation-wise
454 distinct. In the current implementation "seeding" a common
455 hash function with different values seems to be adequate.
456 */
457 const unsigned int predef_salt_count = 128;
458 static const bloom_type predef_salt[predef_salt_count] =
459 {
460 0xAAAAAAAA, 0x55555555, 0x33333333, 0xCCCCCCCC,
461 0x66666666, 0x99999999, 0xB5B5B5B5, 0x4B4B4B4B,
462 0xAA55AA55, 0x55335533, 0x33CC33CC, 0xCC66CC66,
463 0x66996699, 0x99B599B5, 0xB54BB54B, 0x4BAA4BAA,
464 0xAA33AA33, 0x55CC55CC, 0x33663366, 0xCC99CC99,
465 0x66B566B5, 0x994B994B, 0xB5AAB5AA, 0xAAAAAA33,
466 0x555555CC, 0x33333366, 0xCCCCCC99, 0x666666B5,
467 0x9999994B, 0xB5B5B5AA, 0xFFFFFFFF, 0xFFFF0000,
468 0xB823D5EB, 0xC1191CDF, 0xF623AEB3, 0xDB58499F,
469 0xC8D42E70, 0xB173F616, 0xA91A5967, 0xDA427D63,
470 0xB1E8A2EA, 0xF6C0D155, 0x4909FEA3, 0xA68CC6A7,
471 0xC395E782, 0xA26057EB, 0x0CD5DA28, 0x467C5492,
472 0xF15E6982, 0x61C6FAD3, 0x9615E352, 0x6E9E355A,
473 0x689B563E, 0x0C9831A8, 0x6753C18B, 0xA622689B,
474 0x8CA63C47, 0x42CC2884, 0x8E89919B, 0x6EDBD7D3,
475 0x15B6796C, 0x1D6FDFE4, 0x63FF9092, 0xE7401432,
476 0xEFFE9412, 0xAEAEDF79, 0x9F245A31, 0x83C136FC,
477 0xC3DA4A8C, 0xA5112C8C, 0x5271F491, 0x9A948DAB,
478 0xCEE59A8D, 0xB5F525AB, 0x59D13217, 0x24E7C331,
479 0x697C2103, 0x84B0A460, 0x86156DA9, 0xAEF2AC68,
480 0x23243DA5, 0x3F649643, 0x5FA495A8, 0x67710DF8,
481 0x9A6C499E, 0xDCFB0227, 0x46A43433, 0x1832B07A,
482 0xC46AFF3C, 0xB9C8FFF0, 0xC9500467, 0x34431BDF,
483 0xB652432B, 0xE367F12B, 0x427F4C1B, 0x224C006E,
484 0x2E7E5A89, 0x96F99AA5, 0x0BEB452A, 0x2FD87C39,
485 0x74B2E1FB, 0x222EFD24, 0xF357F60C, 0x440FCB1E,
486 0x8BBE030F, 0x6704DC29, 0x1144D12F, 0x948B1355,
487 0x6D8FD7E9, 0x1C11A014, 0xADD1592F, 0xFB3C712E,
488 0xFC77642F, 0xF9C4CE8C, 0x31312FB9, 0x08B0DD79,
489 0x318FA6E7, 0xC040D23D, 0xC0589AA7, 0x0CA5C075,
490 0xF874B172, 0x0CF914D5, 0x784D3280, 0x4E8CFEBC,
491 0xC569F575, 0xCDB2A091, 0x2CC016B4, 0x5C5F4421
492 };
493
494 if (salt_count_ <= predef_salt_count)
495 {
496 std::copy(predef_salt,
497 predef_salt + salt_count_,
498 std::back_inserter(salt_));
499 for (unsigned int i = 0; i < salt_.size(); ++i)
500 {
501 /*
502 Note:
503 This is done to integrate the user defined random seed,
504 so as to allow for the generation of unique bloom filter
505 instances.
506 */
507 salt_[i] = salt_[i] * salt_[(i + 3) % salt_.size()] + static_cast<bloom_type>(random_seed_);
508 }
509 }
510 else
511 {
512 std::copy(predef_salt,predef_salt + predef_salt_count,std::back_inserter(salt_));
513 srand(static_cast<unsigned int>(random_seed_));
514 while (salt_.size() < salt_count_)
515 {
516 bloom_type current_salt = static_cast<bloom_type>(rand()) * static_cast<bloom_type>(rand());
517 if (0 == current_salt) continue;
518 if (salt_.end() == std::find(salt_.begin(), salt_.end(), current_salt))
519 {
520 salt_.push_back(current_salt);
521 }
522 }
523 }
524 }
unsigned int bloom_type
Here is the caller graph for this function:

◆ hash_ap()

bloom_type fc::bloom_filter::hash_ap ( const unsigned char * begin,
std::size_t remaining_length,
bloom_type hash ) const
inlineprotected

Definition at line 526 of file bloom_filter.hpp.

527 {
528 const unsigned char* itr = begin;
529 unsigned int loop = 0;
530 while (remaining_length >= 8)
531 {
532 const unsigned int& i1 = *(reinterpret_cast<const unsigned int*>(itr)); itr += sizeof(unsigned int);
533 const unsigned int& i2 = *(reinterpret_cast<const unsigned int*>(itr)); itr += sizeof(unsigned int);
534 hash ^= (hash << 7) ^ i1 * (hash >> 3) ^
535 (~((hash << 11) + (i2 ^ (hash >> 5))));
536 remaining_length -= 8;
537 }
538 if (remaining_length)
539 {
540 if (remaining_length >= 4)
541 {
542 const unsigned int& i = *(reinterpret_cast<const unsigned int*>(itr));
543 if (loop & 0x01)
544 hash ^= (hash << 7) ^ i * (hash >> 3);
545 else
546 hash ^= (~((hash << 11) + (i ^ (hash >> 5))));
547 ++loop;
548 remaining_length -= 4;
549 itr += sizeof(unsigned int);
550 }
551 if (remaining_length >= 2)
552 {
553 const unsigned short& i = *(reinterpret_cast<const unsigned short*>(itr));
554 if (loop & 0x01)
555 hash ^= (hash << 7) ^ i * (hash >> 3);
556 else
557 hash ^= (~((hash << 11) + (i ^ (hash >> 5))));
558 ++loop;
559 remaining_length -= 2;
560 itr += sizeof(unsigned short);
561 }
562 if (remaining_length)
563 {
564 hash += ((*itr) ^ (hash * 0xA5A5A5A5)) + loop;
565 }
566 }
567 return hash;
568 }
Here is the caller graph for this function:

◆ hash_count()

std::size_t fc::bloom_filter::hash_count ( )
inline

Definition at line 436 of file bloom_filter.hpp.

437 {
438 return salt_.size();
439 }

◆ insert() [1/5]

void fc::bloom_filter::insert ( const char * data,
const std::size_t & length )
inline

Definition at line 282 of file bloom_filter.hpp.

283 {
284 insert(reinterpret_cast<const unsigned char*>(data),length);
285 }
void insert(const unsigned char *key_begin, const std::size_t &length)
Here is the call graph for this function:

◆ insert() [2/5]

template<typename InputIterator >
void fc::bloom_filter::insert ( const InputIterator begin,
const InputIterator end )
inline

Definition at line 288 of file bloom_filter.hpp.

289 {
290 InputIterator itr = begin;
291 while (end != itr)
292 {
293 insert(*(itr++));
294 }
295 }
Here is the call graph for this function:

◆ insert() [3/5]

void fc::bloom_filter::insert ( const std::string & key)
inline

Definition at line 277 of file bloom_filter.hpp.

278 {
279 insert(reinterpret_cast<const unsigned char*>(key.c_str()),key.size());
280 }
Here is the call graph for this function:

◆ insert() [4/5]

template<typename T >
void fc::bloom_filter::insert ( const T & t)
inline

Definition at line 271 of file bloom_filter.hpp.

272 {
273 // Note: T must be a C++ POD type.
274 insert(reinterpret_cast<const unsigned char*>(&t),sizeof(T));
275 }
Here is the call graph for this function:

◆ insert() [5/5]

void fc::bloom_filter::insert ( const unsigned char * key_begin,
const std::size_t & length )
inline

Definition at line 258 of file bloom_filter.hpp.

259 {
260 std::size_t bit_index = 0;
261 std::size_t bit = 0;
262 for (std::size_t i = 0; i < salt_.size(); ++i)
263 {
264 compute_indices(hash_ap(key_begin,length,salt_[i]),bit_index,bit);
265 bit_table_[bit_index / bits_per_char] |= bit_mask[bit];
266 }
268 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator!()

bool fc::bloom_filter::operator! ( ) const
inline

Definition at line 247 of file bloom_filter.hpp.

248 {
249 return (0 == table_size_);
250 }

◆ operator!=()

bool fc::bloom_filter::operator!= ( const bloom_filter & f) const
inline

Definition at line 220 of file bloom_filter.hpp.

221 {
222 return !operator==(f);
223 }
bool operator==(const bloom_filter &f) const
Here is the call graph for this function:

◆ operator&=()

bloom_filter & fc::bloom_filter::operator&= ( const bloom_filter & f)
inline

Definition at line 380 of file bloom_filter.hpp.

381 {
382 /* intersection */
383 if (
384 (salt_count_ == f.salt_count_) &&
385 (table_size_ == f.table_size_) &&
386 (random_seed_ == f.random_seed_)
387 )
388 {
389 for (std::size_t i = 0; i < raw_table_size_; ++i)
390 {
391 bit_table_[i] &= f.bit_table_[i];
392 }
393 }
394 return *this;
395 }

◆ operator=()

bloom_filter & fc::bloom_filter::operator= ( const bloom_filter & f)
inline

Definition at line 225 of file bloom_filter.hpp.

226 {
227 if (this != &f)
228 {
229 salt_count_ = f.salt_count_;
230 table_size_ = f.table_size_;
231 raw_table_size_ = f.raw_table_size_;
232 projected_element_count_ = f.projected_element_count_;
233 inserted_element_count_ = f.inserted_element_count_;
234 random_seed_ = f.random_seed_;
235 desired_false_positive_probability_ = f.desired_false_positive_probability_;
236 bit_table_.resize( raw_table_size_ );
237 std::copy(f.bit_table_.data(),f.bit_table_.data() + raw_table_size_,bit_table_.data());
238 salt_ = f.salt_;
239 }
240 return *this;
241 }
Here is the caller graph for this function:

◆ operator==()

bool fc::bloom_filter::operator== ( const bloom_filter & f) const
inline

Definition at line 201 of file bloom_filter.hpp.

202 {
203 if (this != &f)
204 {
205 return
206 (salt_count_ == f.salt_count_) &&
207 (table_size_ == f.table_size_) &&
208 (raw_table_size_ == f.raw_table_size_) &&
209 (projected_element_count_ == f.projected_element_count_) &&
210 (inserted_element_count_ == f.inserted_element_count_) &&
211 (random_seed_ == f.random_seed_) &&
212 (desired_false_positive_probability_ == f.desired_false_positive_probability_) &&
213 (salt_ == f.salt_) &&
214 std::equal(f.bit_table_.data(),f.bit_table_.data() + raw_table_size_,bit_table_.data());
215 }
216 else
217 return true;
218 }
Here is the caller graph for this function:

◆ operator^=()

bloom_filter & fc::bloom_filter::operator^= ( const bloom_filter & f)
inline

Definition at line 414 of file bloom_filter.hpp.

415 {
416 /* difference */
417 if (
418 (salt_count_ == f.salt_count_) &&
419 (table_size_ == f.table_size_) &&
420 (random_seed_ == f.random_seed_)
421 )
422 {
423 for (std::size_t i = 0; i < raw_table_size_; ++i)
424 {
425 bit_table_[i] ^= f.bit_table_[i];
426 }
427 }
428 return *this;
429 }

◆ operator|=()

bloom_filter & fc::bloom_filter::operator|= ( const bloom_filter & f)
inline

Definition at line 397 of file bloom_filter.hpp.

398 {
399 /* union */
400 if (
401 (salt_count_ == f.salt_count_) &&
402 (table_size_ == f.table_size_) &&
403 (random_seed_ == f.random_seed_)
404 )
405 {
406 for (std::size_t i = 0; i < raw_table_size_; ++i)
407 {
408 bit_table_[i] |= f.bit_table_[i];
409 }
410 }
411 return *this;
412 }

◆ size()

virtual unsigned long long int fc::bloom_filter::size ( ) const
inlinevirtual

Definition at line 358 of file bloom_filter.hpp.

359 {
360 return table_size_;
361 }
Here is the caller graph for this function:

◆ table()

const cell_type * fc::bloom_filter::table ( ) const
inline

Definition at line 431 of file bloom_filter.hpp.

432 {
433 return bit_table_.data();
434 }

Member Data Documentation

◆ bit_table_

std::vector<unsigned char> fc::bloom_filter::bit_table_

Definition at line 572 of file bloom_filter.hpp.

◆ desired_false_positive_probability_

double fc::bloom_filter::desired_false_positive_probability_

Definition at line 579 of file bloom_filter.hpp.

◆ inserted_element_count_

unsigned int fc::bloom_filter::inserted_element_count_

Definition at line 577 of file bloom_filter.hpp.

◆ projected_element_count_

unsigned long long int fc::bloom_filter::projected_element_count_

Definition at line 576 of file bloom_filter.hpp.

◆ random_seed_

unsigned long long int fc::bloom_filter::random_seed_

Definition at line 578 of file bloom_filter.hpp.

◆ raw_table_size_

unsigned long long int fc::bloom_filter::raw_table_size_

Definition at line 575 of file bloom_filter.hpp.

◆ salt_

std::vector<bloom_type> fc::bloom_filter::salt_

Definition at line 571 of file bloom_filter.hpp.

◆ salt_count_

unsigned int fc::bloom_filter::salt_count_

Definition at line 573 of file bloom_filter.hpp.

◆ table_size_

unsigned long long int fc::bloom_filter::table_size_

Definition at line 574 of file bloom_filter.hpp.


The documentation for this class was generated from the following file: