28 #ifndef INCLUDE_BLOOM_FILTER_HPP
29 #define INCLUDE_BLOOM_FILTER_HPP
128 double min_m = std::numeric_limits<double>::infinity();
137 curr_m = numerator / denominator;
149 optp.
table_size =
static_cast<unsigned long long int>(min_m);
266 inline void insert(
const unsigned char* key_begin,
const std::size_t& length)
268 std::size_t bit_index = 0;
270 for (std::size_t i = 0; i <
salt_.size(); ++i)
282 insert(
reinterpret_cast<const unsigned char*
>(&t),
sizeof(T));
285 inline void insert(
const std::string& key)
287 insert(
reinterpret_cast<const unsigned char*
>(key.c_str()),key.size());
290 inline void insert(
const char* data,
const std::size_t& length)
292 insert(
reinterpret_cast<const unsigned char*
>(data),length);
295 template<
typename InputIterator>
296 inline void insert(
const InputIterator begin,
const InputIterator end)
298 InputIterator itr = begin;
305 inline virtual bool contains(
const unsigned char* key_begin,
const std::size_t length)
const
307 std::size_t bit_index = 0;
309 for (std::size_t i = 0; i <
salt_.size(); ++i)
323 return contains(
reinterpret_cast<const unsigned char*
>(&t),
static_cast<std::size_t
>(
sizeof(T)));
328 return contains(
reinterpret_cast<const unsigned char*
>(key.c_str()),key.size());
331 inline bool contains(
const char* data,
const std::size_t& length)
const
333 return contains(
reinterpret_cast<const unsigned char*
>(data),length);
336 template<
typename InputIterator>
337 inline InputIterator
contains_all(
const InputIterator begin,
const InputIterator end)
const
339 InputIterator itr = begin;
351 template<
typename InputIterator>
352 inline InputIterator
contains_none(
const InputIterator begin,
const InputIterator end)
const
354 InputIterator itr = begin;
366 inline virtual unsigned long long int size()
const
465 const unsigned int predef_salt_count = 128;
466 static const bloom_type predef_salt[predef_salt_count] =
468 0xAAAAAAAA, 0x55555555, 0x33333333, 0xCCCCCCCC,
469 0x66666666, 0x99999999, 0xB5B5B5B5, 0x4B4B4B4B,
470 0xAA55AA55, 0x55335533, 0x33CC33CC, 0xCC66CC66,
471 0x66996699, 0x99B599B5, 0xB54BB54B, 0x4BAA4BAA,
472 0xAA33AA33, 0x55CC55CC, 0x33663366, 0xCC99CC99,
473 0x66B566B5, 0x994B994B, 0xB5AAB5AA, 0xAAAAAA33,
474 0x555555CC, 0x33333366, 0xCCCCCC99, 0x666666B5,
475 0x9999994B, 0xB5B5B5AA, 0xFFFFFFFF, 0xFFFF0000,
476 0xB823D5EB, 0xC1191CDF, 0xF623AEB3, 0xDB58499F,
477 0xC8D42E70, 0xB173F616, 0xA91A5967, 0xDA427D63,
478 0xB1E8A2EA, 0xF6C0D155, 0x4909FEA3, 0xA68CC6A7,
479 0xC395E782, 0xA26057EB, 0x0CD5DA28, 0x467C5492,
480 0xF15E6982, 0x61C6FAD3, 0x9615E352, 0x6E9E355A,
481 0x689B563E, 0x0C9831A8, 0x6753C18B, 0xA622689B,
482 0x8CA63C47, 0x42CC2884, 0x8E89919B, 0x6EDBD7D3,
483 0x15B6796C, 0x1D6FDFE4, 0x63FF9092, 0xE7401432,
484 0xEFFE9412, 0xAEAEDF79, 0x9F245A31, 0x83C136FC,
485 0xC3DA4A8C, 0xA5112C8C, 0x5271F491, 0x9A948DAB,
486 0xCEE59A8D, 0xB5F525AB, 0x59D13217, 0x24E7C331,
487 0x697C2103, 0x84B0A460, 0x86156DA9, 0xAEF2AC68,
488 0x23243DA5, 0x3F649643, 0x5FA495A8, 0x67710DF8,
489 0x9A6C499E, 0xDCFB0227, 0x46A43433, 0x1832B07A,
490 0xC46AFF3C, 0xB9C8FFF0, 0xC9500467, 0x34431BDF,
491 0xB652432B, 0xE367F12B, 0x427F4C1B, 0x224C006E,
492 0x2E7E5A89, 0x96F99AA5, 0x0BEB452A, 0x2FD87C39,
493 0x74B2E1FB, 0x222EFD24, 0xF357F60C, 0x440FCB1E,
494 0x8BBE030F, 0x6704DC29, 0x1144D12F, 0x948B1355,
495 0x6D8FD7E9, 0x1C11A014, 0xADD1592F, 0xFB3C712E,
496 0xFC77642F, 0xF9C4CE8C, 0x31312FB9, 0x08B0DD79,
497 0x318FA6E7, 0xC040D23D, 0xC0589AA7, 0x0CA5C075,
498 0xF874B172, 0x0CF914D5, 0x784D3280, 0x4E8CFEBC,
499 0xC569F575, 0xCDB2A091, 0x2CC016B4, 0x5C5F4421
504 std::copy(predef_salt,
506 std::back_inserter(
salt_));
507 for (
unsigned int i = 0; i <
salt_.size(); ++i)
520 std::copy(predef_salt,predef_salt + predef_salt_count,std::back_inserter(
salt_));
525 if (0 == current_salt)
continue;
526 if (
salt_.end() == std::find(
salt_.begin(),
salt_.end(), current_salt))
528 salt_.push_back(current_salt);
536 const unsigned char* itr = begin;
537 unsigned int loop = 0;
538 while (remaining_length >= 8)
540 const unsigned int& i1 = *(
reinterpret_cast<const unsigned int*
>(itr)); itr +=
sizeof(
unsigned int);
541 const unsigned int& i2 = *(
reinterpret_cast<const unsigned int*
>(itr)); itr +=
sizeof(
unsigned int);
542 hash ^= (hash << 7) ^ i1 * (hash >> 3) ^
543 (~((hash << 11) + (i2 ^ (hash >> 5))));
544 remaining_length -= 8;
546 if (remaining_length)
548 if (remaining_length >= 4)
550 const unsigned int& i = *(
reinterpret_cast<const unsigned int*
>(itr));
552 hash ^= (hash << 7) ^ i * (hash >> 3);
554 hash ^= (~((hash << 11) + (i ^ (hash >> 5))));
556 remaining_length -= 4;
557 itr +=
sizeof(
unsigned int);
559 if (remaining_length >= 2)
561 const unsigned short& i = *(
reinterpret_cast<const unsigned short*
>(itr));
563 hash ^= (hash << 7) ^ i * (hash >> 3);
565 hash ^= (~((hash << 11) + (i ^ (hash >> 5))));
567 remaining_length -= 2;
568 itr +=
sizeof(
unsigned short);
570 if (remaining_length)
572 hash += ((*itr) ^ (hash * 0xA5A5A5A5)) + loop;
620 inline unsigned long long int size()
const
627 if ((0.0 >= percentage) || (percentage >= 100.0))
632 unsigned long long int original_table_size =
size_list.back();
633 unsigned long long int new_table_size =
static_cast<unsigned long long int>((
size_list.back() * (1.0 - (percentage / 100.0))));
636 if ((
bits_per_char > new_table_size) || (new_table_size >= original_table_size))
650 *(itr_tmp++) |= (*itr++);
665 for (std::size_t i = 0; i <
size_list.size(); ++i)