| 1 | #include "BlockedBloomFilter.hpp" |
| 2 | |
| 3 | BlockedBloomFilter::BlockedBloomFilter() : table_(nullptr) { |
| 4 | |
| 5 | clear(); |
| 6 | } |
| 7 | |
| 8 | BlockedBloomFilter::BlockedBloomFilter(size_t nb_elem, size_t bits_per_elem) : table_(nullptr) { |
| 9 | |
| 10 | clear(); |
| 11 | |
| 12 | if ((nb_elem != 0) && (bits_per_elem != 0)){ |
| 13 | |
| 14 | std::random_device rd; |
| 15 | std::mt19937_64 gen(rd()); |
| 16 | std::uniform_int_distribution<unsigned long long> distribution; |
| 17 | |
| 18 | blocks_ = (bits_per_elem * nb_elem + MASK_BITS_BLOCK) / NB_BITS_BLOCK; |
| 19 | k_ = (int) (bits_per_elem * log(2)); |
| 20 | |
| 21 | if (fpp(bits_per_elem, k_) >= fpp(bits_per_elem, k_+1)) ++k_; |
| 22 | |
| 23 | seed1 = distribution(gen); |
| 24 | seed2 = distribution(gen); |
| 25 | |
| 26 | init_arrays(); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | BlockedBloomFilter::BlockedBloomFilter(const BlockedBloomFilter& o) : table_(nullptr), blocks_(o.blocks_), |
| 31 | k_(o.k_), fast_div_(o.fast_div_), seed1(o.seed1), seed2(o.seed2), ush(o.ush) { |
| 32 | |
| 33 | if (blocks_ != 0) { |
| 34 | |
| 35 | init_arrays(); |
| 36 | |
| 37 | for (uint64_t i = 0; i != blocks_; ++i){ |
| 38 | |
| 39 | memcpy(&(table_[i].block), &(o.table_[i].block), NB_ELEM_BLOCK * sizeof(uint64_t)); |
| 40 | |
| 41 | table_[i].bits_occupancy = o.table_[i].bits_occupancy; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | BlockedBloomFilter::BlockedBloomFilter(BlockedBloomFilter&& o) : table_(o.table_), blocks_(o.blocks_), |
| 47 | k_(o.k_), fast_div_(o.fast_div_), seed1(o.seed1), seed2(o.seed2), ush(move(o.ush)) { |
| 48 | |
| 49 | o.table_ = nullptr; |
| 50 | |
| 51 | o.clear(); |
| 52 | } |
| 53 | |
| 54 | BlockedBloomFilter::~BlockedBloomFilter() { |
| 55 | |
| 56 | clear(); |
| 57 | } |
| 58 | |
| 59 | BlockedBloomFilter& BlockedBloomFilter::operator=(const BlockedBloomFilter& o) { |
| 60 | |
| 61 | clear(); |
| 62 | |
| 63 | blocks_ = o.blocks_; |
| 64 | k_ = o.k_; |
| 65 | fast_div_ = o.fast_div_; |
| 66 | seed1 = o.seed1; |
| 67 | seed2 = o.seed2; |
| 68 | ush = o.ush; |
| 69 | |
| 70 | if (blocks_ != 0) { |
| 71 | |
| 72 | init_arrays(); |
| 73 | |
| 74 | for (uint64_t i = 0; i != blocks_; ++i){ |
| 75 | |
| 76 | memcpy(&(table_[i].block), &(o.table_[i].block), NB_ELEM_BLOCK * sizeof(uint64_t)); |
| 77 | |
| 78 | table_[i].bits_occupancy = o.table_[i].bits_occupancy; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | BlockedBloomFilter& BlockedBloomFilter::operator=(BlockedBloomFilter&& o) { |
| 86 | |
| 87 | if (this != &o) { |
| 88 | |
| 89 | clear(); |
| 90 | |
| 91 | table_ = o.table_; |
| 92 | blocks_ = o.blocks_; |
| 93 | k_ = o.k_; |
| 94 | fast_div_ = o.fast_div_; |
| 95 | seed1 = o.seed1; |
| 96 | seed2 = o.seed2; |
| 97 | |
| 98 | ush = move(o.ush); |
| 99 | |
| 100 | o.table_ = nullptr; |
| 101 | |
| 102 | o.clear(); |
| 103 | } |
| 104 | |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | void BlockedBloomFilter::clear() { |
| 109 | |
| 110 | if (table_ != nullptr){ |
| 111 | |
| 112 | delete[] table_; |
| 113 | table_ = nullptr; |
| 114 | } |
| 115 | |
| 116 | blocks_ = 0; |
| 117 | k_ = 0; |
| 118 | seed1 = 0; |
| 119 | seed2 = 0; |
| 120 | |
| 121 | ush.clear(); |
| 122 | |
| 123 | lck_ush.clear(std::memory_order_release); |
| 124 | } |
| 125 | |
| 126 | void BlockedBloomFilter::init_arrays() { |
| 127 | |
| 128 | fast_div_ = libdivide::divider<uint64_t>(blocks_); |
| 129 | table_ = new BBF_Block[blocks_]; |
| 130 | } |
| 131 | |
| 132 | int BlockedBloomFilter::contains(const uint64_t (&kmh)[4], const uint64_t minh, bool (&pres)[4], const int limit) const { |
| 133 | |
| 134 | const std::array<int64_t, 4> bids = contains_bids(kmh, minh, limit); |
| 135 | |
| 136 | pres[0] = (bids[0] != -1); |
| 137 | pres[1] = (bids[1] != -1); |
| 138 | pres[2] = (bids[2] != -1); |
| 139 | pres[3] = (bids[3] != -1); |
| 140 | |
| 141 | return (static_cast<int>(pres[0]) + static_cast<int>(pres[1]) + static_cast<int>(pres[2]) + static_cast<int>(pres[3])); |
| 142 | } |
| 143 | |
| 144 | std::array<int64_t, 4> BlockedBloomFilter::contains_bids(const uint64_t (&kmh)[4], const uint64_t minh, const int limit) const { |
| 145 | |
| 146 | const uint64_t minh_s1 = wyhash(&minh, sizeof(uint64_t), seed1, _wyp); |
| 147 | const uint64_t minh_s2 = wyhash(&minh, sizeof(uint64_t), seed2, _wyp); |
| 148 | |
| 149 | const uint64_t kmh_s1[4] = {wyhash(&kmh[0], sizeof(uint64_t), seed1, _wyp), wyhash(&kmh[1], sizeof(uint64_t), seed1, _wyp), |
| 150 | wyhash(&kmh[2], sizeof(uint64_t), seed1, _wyp), wyhash(&kmh[3], sizeof(uint64_t), seed1, _wyp)}; |
| 151 | |
| 152 | const uint64_t kmh_s2[4] = {wyhash(&kmh[0], sizeof(uint64_t), seed2, _wyp), wyhash(&kmh[1], sizeof(uint64_t), seed2, _wyp), |
| 153 | wyhash(&kmh[2], sizeof(uint64_t), seed2, _wyp), wyhash(&kmh[3], sizeof(uint64_t), seed2, _wyp)}; |
| 154 | |
| 155 | const uint64_t min_overload_bits = NB_BITS_BLOCK * 0.65; |
| 156 | |
| 157 | uint64_t nb_overflow = 0; |
| 158 | uint64_t minh1 = minh_s1; |
| 159 | |
| 160 | std::array<int64_t, 4> bids = {-1, -1, -1, -1}; |
| 161 | |
| 162 | int cpt = 0; |
| 163 | |
| 164 | while (nb_overflow < 8) { |
| 165 | |
| 166 | uint64_t bid1 = minh1 - (minh1 / fast_div_) * blocks_; |
| 167 | |
| 168 | const uint64_t bits_occupancy_1 = table_[bid1].bits_occupancy; |
| 169 | |
| 170 | for (uint64_t i = 0, j = 0; (j != 4) && (cpt != limit); ++j){ |
| 171 | |
| 172 | if (bids[j] == -1){ |
| 173 | |
| 174 | uint64_t kmh1 = kmh_s1[j]; |
| 175 | |
| 176 | for (i = 0; i != k_; ++i, kmh1 += kmh_s2[j]) { |
| 177 | |
| 178 | if ((table_[bid1].block[(kmh1 & MASK_BITS_BLOCK) >> 6] & (1ULL << (kmh1 & 0x3fULL))) == 0) break; |
| 179 | } |
| 180 | |
| 181 | bids[j] = static_cast<int64_t>((bid1 + 2) & (static_cast<uint64_t>(i != k_) - 1)) - 1; |
| 182 | cpt += (i == k_); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (cpt != limit){ |
| 187 | |
| 188 | minh1 += minh_s2; |
| 189 | bid1 = minh1 - (minh1 / fast_div_) * blocks_; |
| 190 | |
| 191 | const uint64_t bits_occupancy_2 = table_[bid1].bits_occupancy; |
| 192 | |
| 193 | for (size_t i = 0, j = 0; (j != 4) && (cpt != limit); ++j){ |
| 194 | |
| 195 | if (bids[j] == -1){ |
| 196 | |
| 197 | uint64_t kmh1 = kmh_s1[j]; |
| 198 | |
| 199 | for (i = 0; i != k_; ++i, kmh1 += kmh_s2[j]) { |
| 200 | |
| 201 | if ((table_[bid1].block[(kmh1 & MASK_BITS_BLOCK) >> 6] & (1ULL << (kmh1 & 0x3fULL))) == 0) break; |
| 202 | } |
| 203 | |
| 204 | bids[j] = static_cast<int64_t>((bid1 + 2) & (static_cast<uint64_t>(i != k_) - 1)) - 1; |
| 205 | cpt += (i == k_); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if ((bits_occupancy_1 < min_overload_bits) || (bits_occupancy_2 < min_overload_bits) || (cpt == limit)) break; |
| 210 | |
| 211 | minh1 += minh_s2; |
| 212 | ++nb_overflow; |
| 213 | } |
| 214 | else break; |
| 215 | } |
| 216 | |
| 217 | if (nb_overflow >= 8) { |
| 218 | |
| 219 | for (size_t j = 0; (j != 4) && (cpt != limit); ++j) { |
| 220 | |
| 221 | if ((bids[j] == -1) && (ush.find(kmh[j]) != ush.end())) { |
| 222 | |
| 223 | bids[j] = 0; |
| 224 | ++cpt; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return bids; |
| 230 | } |
| 231 | |
| 232 | int64_t BlockedBloomFilter::contains_bids(const uint64_t kmh, const uint64_t minh) const { |
| 233 | |
| 234 | const uint64_t minh_s1 = wyhash(&minh, sizeof(uint64_t), seed1, _wyp); |
| 235 | const uint64_t minh_s2 = wyhash(&minh, sizeof(uint64_t), seed2, _wyp); |
| 236 | |
| 237 | const uint64_t kmh_s1 = wyhash(&kmh, sizeof(uint64_t), seed1, _wyp); |
| 238 | const uint64_t kmh_s2 = wyhash(&kmh, sizeof(uint64_t), seed2, _wyp); |
| 239 | |
| 240 | const uint64_t min_overload_bits = NB_BITS_BLOCK * 0.65; |
| 241 | |
| 242 | uint64_t nb_overflow = 0; |
| 243 | uint64_t minh1 = minh_s1; |
| 244 | uint64_t kmh1, bid1; |
| 245 | |
| 246 | int i = 0; |
| 247 | |
| 248 | while (nb_overflow < 8) { |
| 249 | |
| 250 | kmh1 = kmh_s1; |
| 251 | bid1 = minh1 - (minh1 / fast_div_) * blocks_; |
| 252 | |
| 253 | const uint64_t bits_occupancy_1 = table_[bid1].bits_occupancy; |
| 254 | |
| 255 | for (i = 0; i != k_; ++i, kmh1 += kmh_s2) { |
| 256 | |
| 257 | if ((table_[bid1].block[(kmh1 & MASK_BITS_BLOCK) >> 6] & (1ULL << (kmh1 & 0x3fULL))) == 0) break; |
| 258 | } |
| 259 | |
| 260 | if (i != k_){ |
| 261 | |
| 262 | minh1 += minh_s2; |
| 263 | bid1 = minh1 - (minh1 / fast_div_) * blocks_; |
| 264 | kmh1 = kmh_s1; |
| 265 | |
| 266 | const uint64_t bits_occupancy_2 = table_[bid1].bits_occupancy; |
| 267 | |
| 268 | const bool overloaded = (bits_occupancy_1 >= min_overload_bits) && (bits_occupancy_2 >= min_overload_bits); |
| 269 | |
| 270 | for (i = 0; i != k_; ++i, kmh1 += kmh_s2) { |
| 271 | |
| 272 | if ((table_[bid1].block[(kmh1 & MASK_BITS_BLOCK) >> 6] & (1ULL << (kmh1 & 0x3fULL))) == 0) { |
| 273 | |
| 274 | if (overloaded) break; |
| 275 | |
| 276 | return -1; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if (i == k_) return static_cast<int64_t>(bid1 + 1); |
| 281 | |
| 282 | minh1 += minh_s2; |
| 283 | ++nb_overflow; |
| 284 | } |
| 285 | else return static_cast<int64_t>(bid1 + 1); |
| 286 | } |
| 287 | |
| 288 | if ((nb_overflow >= 8) && (ush.find(kmh) != ush.end())) return 0; |
| 289 | |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | bool BlockedBloomFilter::WriteBloomFilter(FILE *fp) const { |
| 294 | |
| 295 | if (fwrite(&blocks_, sizeof(uint64_t), 1, fp) != 1) return false; |
| 296 | if (fwrite(&seed1, sizeof(uint64_t), 1, fp) != 1) return false; |
| 297 | if (fwrite(&seed2, sizeof(uint64_t), 1, fp) != 1) return false; |
| 298 | if (fwrite(&k_, sizeof(int), 1, fp) != 1) return false; |
| 299 | |
| 300 | const uint64_t ush_sz = ush.size(); |
| 301 | |
| 302 | if (fwrite(&ush_sz, sizeof(uint64_t), 1, fp) != 1) return false; |
| 303 | |
| 304 | for (const uint64_t h : ush) { |
| 305 | |
| 306 | if (fwrite(&h, sizeof(uint64_t), 1, fp) != 1) return false; |
| 307 | } |
| 308 | |
| 309 | for (uint64_t i = 0; i != blocks_; ++i){ |
| 310 | |
| 311 | if (fwrite(&(table_[i].block), sizeof(uint64_t), NB_ELEM_BLOCK, fp) != NB_ELEM_BLOCK) return false; |
| 312 | if (fwrite(&(table_[i].bits_occupancy), sizeof(uint64_t), 1, fp) != 1) return false; |
| 313 | } |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | bool BlockedBloomFilter::ReadBloomFilter(FILE *fp) { |
| 319 | |
| 320 | uint64_t ush_sz = 0; |
| 321 | |
| 322 | clear(); |
| 323 | |
| 324 | if (fread(&blocks_, sizeof(uint64_t), 1, fp) != 1) return false; |
| 325 | if (fread(&seed1, sizeof(uint64_t), 1, fp) != 1) return false; |
| 326 | if (fread(&seed2, sizeof(uint64_t), 1, fp) != 1) return false; |
| 327 | if (fread(&k_, sizeof(int), 1, fp) != 1) return false; |
| 328 | |
| 329 | if (fread(&ush_sz, sizeof(uint64_t), 1, fp) != 1) return false; |
| 330 | |
| 331 | for (uint64_t i = 0, h = 0; i != ush_sz; ++i){ |
| 332 | |
| 333 | if (fread(&h, sizeof(uint64_t), 1, fp) != 1) return false; |
| 334 | |
| 335 | ush.insert(h); |
| 336 | } |
| 337 | |
| 338 | init_arrays(); |
| 339 | |
| 340 | for (uint64_t i = 0; i != blocks_; ++i){ |
| 341 | |
| 342 | if (fread(&(table_[i].block), sizeof(uint64_t), NB_ELEM_BLOCK, fp) != NB_ELEM_BLOCK) return false; |
| 343 | if (fread(&(table_[i].bits_occupancy), sizeof(uint64_t), 1, fp) != 1) return false; |
| 344 | } |
| 345 | |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | uint64_t BlockedBloomFilter::insert_par(const uint64_t kmh, const uint64_t minh) { |
| 350 | |
| 351 | const uint64_t minh_s1 = wyhash(&minh, sizeof(uint64_t), seed1, _wyp); |
| 352 | const uint64_t minh_s2 = wyhash(&minh, sizeof(uint64_t), seed2, _wyp); |
| 353 | |
| 354 | const uint64_t kmh_s1 = wyhash(&kmh, sizeof(uint64_t), seed1, _wyp); |
| 355 | const uint64_t kmh_s2 = wyhash(&kmh, sizeof(uint64_t), seed2, _wyp); |
| 356 | |
| 357 | const uint64_t min_overload_bits = NB_BITS_BLOCK * 0.65; |
| 358 | |
| 359 | uint64_t nb_overflow = 0; |
| 360 | uint64_t minh1 = minh_s1; |
| 361 | |
| 362 | int i = 0, j = 0; |
| 363 | |
| 364 | while (i != k_) { |
| 365 | |
| 366 | uint64_t kmh1 = kmh_s1, kmh2 = kmh_s1; |
| 367 | uint64_t minh2 = minh1 + minh_s2; |
| 368 | |
| 369 | uint64_t bid1 = minh1 - (minh1 / fast_div_) * blocks_; |
| 370 | uint64_t bid2 = minh2 - (minh2 / fast_div_) * blocks_; |
| 371 | |
| 372 | if (bid2 < bid1) std::swap(bid1, bid2); |
| 373 | |
| 374 | table_[bid1].lock(); |
| 375 | |
| 376 | for (i = 0; i != k_; ++i, kmh1 += kmh_s2) { |
| 377 | |
| 378 | if ((table_[bid1].block[(kmh1 & MASK_BITS_BLOCK) >> 6] & (1ULL << (kmh1 & 0x3fULL))) == 0) break; |
| 379 | } |
| 380 | |
| 381 | if (i != k_){ |
| 382 | |
| 383 | if (bid2 == bid1){ |
| 384 | |
| 385 | j = i; |
| 386 | kmh2 = kmh1; |
| 387 | } |
| 388 | else { |
| 389 | |
| 390 | table_[bid2].lock(); |
| 391 | |
| 392 | for (j = 0; j != k_; ++j, kmh2 += kmh_s2) { |
| 393 | |
| 394 | if ((table_[bid2].block[(kmh2 & MASK_BITS_BLOCK) >> 6] & (1ULL << (kmh2 & 0x3fULL))) == 0) break; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (j != k_){ |
| 399 | |
| 400 | if ((table_[bid1].bits_occupancy < min_overload_bits) || (table_[bid2].bits_occupancy < min_overload_bits)) { |
| 401 | |
| 402 | uint64_t nb_inserted_bits = 0; |
| 403 | |
| 404 | if (table_[bid2].bits_occupancy < table_[bid1].bits_occupancy){ |
| 405 | |
| 406 | i = j; |
| 407 | kmh1 = kmh2; |
| 408 | |
| 409 | std::swap(bid1, bid2); |
| 410 | } |
| 411 | |
| 412 | for (; i != k_; ++i, kmh1 += kmh_s2) { |
| 413 | |
| 414 | const uint64_t div = (kmh1 & MASK_BITS_BLOCK) >> 6; |
| 415 | const uint64_t mod = 1ULL << (kmh1 & 0x3fULL); |
| 416 | |
| 417 | nb_inserted_bits += static_cast<uint64_t>((table_[bid1].block[div] & mod) == 0); |
| 418 | table_[bid1].block[div] |= mod; |
| 419 | } |
| 420 | |
| 421 | table_[bid1].bits_occupancy += nb_inserted_bits; |
| 422 | |
| 423 | if (bid2 != bid1) table_[bid2].unlock(); |
| 424 | |
| 425 | table_[bid1].unlock(); |
| 426 | |
| 427 | return (((bid1 + 1) << 1) | 0x1ULL); |
| 428 | } |
| 429 | else if (nb_overflow == 7) { |
| 430 | |
| 431 | bool inserted = false; |
| 432 | |
| 433 | if (bid2 != bid1) table_[bid2].unlock(); |
| 434 | |
| 435 | table_[bid1].unlock(); |
| 436 | |
| 437 | while (lck_ush.test_and_set(std::memory_order_acquire)); |
| 438 | |
| 439 | inserted = ush.insert(kmh).second; |
| 440 | |
| 441 | lck_ush.clear(std::memory_order_release); |
| 442 | |
| 443 | return static_cast<uint64_t>(inserted); |
| 444 | } |
| 445 | } |
| 446 | else i = j; |
| 447 | |
| 448 | if (bid2 != bid1) table_[bid2].unlock(); |
| 449 | } |
| 450 | |
| 451 | table_[bid1].unlock(); |
| 452 | |
| 453 | minh1 += minh_s2 + minh_s2; |
| 454 | ++nb_overflow; |
| 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | uint64_t BlockedBloomFilter::insert_unpar(const uint64_t kmh, const uint64_t minh) { |
| 461 | |
| 462 | const uint64_t minh_s1 = wyhash(&minh, sizeof(uint64_t), seed1, _wyp); |
| 463 | const uint64_t minh_s2 = wyhash(&minh, sizeof(uint64_t), seed2, _wyp); |
| 464 | |
| 465 | const uint64_t kmh_s1 = wyhash(&kmh, sizeof(uint64_t), seed1, _wyp); |
| 466 | const uint64_t kmh_s2 = wyhash(&kmh, sizeof(uint64_t), seed2, _wyp); |
| 467 | |
| 468 | const uint64_t min_overload_bits = NB_BITS_BLOCK * 0.65; |
| 469 | |
| 470 | uint64_t nb_overflow = 0; |
| 471 | uint64_t minh1 = minh_s1; |
| 472 | |
| 473 | int i = 0, j = 0; |
| 474 | |
| 475 | while (i != k_) { |
| 476 | |
| 477 | uint64_t kmh1 = kmh_s1, kmh2 = kmh_s1; |
| 478 | uint64_t minh2 = minh1 + minh_s2; |
| 479 | |
| 480 | uint64_t bid1 = minh1 - (minh1 / fast_div_) * blocks_; |
| 481 | uint64_t bid2 = minh2 - (minh2 / fast_div_) * blocks_; |
| 482 | |
| 483 | for (i = 0; i != k_; ++i, kmh1 += kmh_s2) { |
| 484 | |
| 485 | if ((table_[bid1].block[(kmh1 & MASK_BITS_BLOCK) >> 6] & (1ULL << (kmh1 & 0x3fULL))) == 0) break; |
| 486 | } |
| 487 | |
| 488 | if (i != k_){ |
| 489 | |
| 490 | if (bid2 == bid1){ |
| 491 | |
| 492 | j = i; |
| 493 | kmh2 = kmh1; |
| 494 | } |
| 495 | else { |
| 496 | |
| 497 | for (j = 0; j != k_; ++j, kmh2 += kmh_s2) { |
| 498 | |
| 499 | if ((table_[bid2].block[(kmh2 & MASK_BITS_BLOCK) >> 6] & (1ULL << (kmh2 & 0x3fULL))) == 0) break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (j != k_){ |
| 504 | |
| 505 | if ((table_[bid1].bits_occupancy < min_overload_bits) || (table_[bid2].bits_occupancy < min_overload_bits)) { |
| 506 | |
| 507 | uint64_t nb_inserted_bits = 0; |
| 508 | |
| 509 | if (table_[bid2].bits_occupancy < table_[bid1].bits_occupancy){ |
| 510 | |
| 511 | i = j; |
| 512 | kmh1 = kmh2; |
| 513 | |
| 514 | std::swap(bid1, bid2); |
| 515 | } |
| 516 | |
| 517 | for (; i != k_; ++i, kmh1 += kmh_s2) { |
| 518 | |
| 519 | const uint64_t div = (kmh1 & MASK_BITS_BLOCK) >> 6; |
| 520 | const uint64_t mod = 1ULL << (kmh1 & 0x3fULL); |
| 521 | |
| 522 | nb_inserted_bits += static_cast<uint64_t>((table_[bid1].block[div] & mod) == 0); |
| 523 | table_[bid1].block[div] |= mod; |
| 524 | } |
| 525 | |
| 526 | table_[bid1].bits_occupancy += nb_inserted_bits; |
| 527 | |
| 528 | return (((bid1 + 1) << 1) | 0x1ULL); |
| 529 | } |
| 530 | else if (nb_overflow == 7) { |
| 531 | |
| 532 | const bool inserted = ush.insert(kmh).second; |
| 533 | |
| 534 | return static_cast<uint64_t>(inserted); |
| 535 | } |
| 536 | } |
| 537 | else i = j; |
| 538 | } |
| 539 | |
| 540 | minh1 += minh_s2 + minh_s2; |
| 541 | |
| 542 | ++nb_overflow; |
| 543 | } |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | CountingBlockedBloomFilter::CountingBlockedBloomFilter() : BlockedBloomFilter::BlockedBloomFilter(), hashbit(nullptr), counts(nullptr) { |
| 549 | |
| 550 | clear(); |
| 551 | } |
| 552 | |
| 553 | CountingBlockedBloomFilter::CountingBlockedBloomFilter(size_t nb_elem, size_t bits_per_elem) : BlockedBloomFilter::BlockedBloomFilter(nb_elem, bits_per_elem), |
| 554 | hashbit(nullptr), counts(nullptr), elems_per_block(0) { |
| 555 | |
| 556 | if ((BlockedBloomFilter::blocks_ != 0) && (nb_elem != 0)) { |
| 557 | |
| 558 | elems_per_block = std::max(static_cast<uint64_t>(nb_elem / BlockedBloomFilter::blocks_), static_cast<uint64_t>(1)); |
| 559 | elems_per_block = ((elems_per_block + 15) / 16) * 16; // Round it to largest multiple of 16 |
| 560 | |
| 561 | init_arrays(); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | CountingBlockedBloomFilter::CountingBlockedBloomFilter(const CountingBlockedBloomFilter& o) : BlockedBloomFilter::BlockedBloomFilter(o), |
| 566 | hashbit(nullptr), counts(nullptr), elems_per_block(o.elems_per_block) { |
| 567 | |
| 568 | if ((BlockedBloomFilter::blocks_ != 0) && (elems_per_block != 0)) { |
| 569 | |
| 570 | init_arrays(); |
| 571 | |
| 572 | memcpy(hashbit, o.hashbit, BlockedBloomFilter::blocks_ * ((elems_per_block + 15) / 16) * sizeof(uint64_t)); // 4 bits per elem -> 16 elem in a uint64_t |
| 573 | memcpy(counts, o.counts, BlockedBloomFilter::blocks_ * elems_per_block * sizeof(uint8_t)); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | CountingBlockedBloomFilter::CountingBlockedBloomFilter(CountingBlockedBloomFilter&& o) : BlockedBloomFilter::BlockedBloomFilter(std::move(o)), |
| 578 | hashbit(o.hashbit), counts(o.counts), elems_per_block(o.elems_per_block) { |
| 579 | o.hashbit = nullptr; |
| 580 | o.counts = nullptr; |
| 581 | |
| 582 | o.clear(); |
| 583 | } |
| 584 | |
| 585 | CountingBlockedBloomFilter::~CountingBlockedBloomFilter() { |
| 586 | |
| 587 | clear(); |
| 588 | } |
| 589 | |
| 590 | CountingBlockedBloomFilter& CountingBlockedBloomFilter::operator=(const CountingBlockedBloomFilter& o) { |
| 591 | |
| 592 | if (this != &o) { |
| 593 | |
| 594 | clear(); |
| 595 | |
| 596 | BlockedBloomFilter::operator=(o); |
| 597 | |
| 598 | elems_per_block = o.elems_per_block; |
| 599 | |
| 600 | if ((BlockedBloomFilter::blocks_ != 0) && (elems_per_block != 0)) { |
| 601 | |
| 602 | init_arrays(); |
| 603 | |
| 604 | memcpy(hashbit, o.hashbit, BlockedBloomFilter::blocks_ * ((elems_per_block + 15) / 16) * sizeof(uint64_t)); // 4 bits per elem -> 16 elem in a uint64_t |
| 605 | memcpy(counts, o.counts, BlockedBloomFilter::blocks_ * elems_per_block * sizeof(uint8_t)); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return *this; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | CountingBlockedBloomFilter& CountingBlockedBloomFilter::operator=(CountingBlockedBloomFilter&& o) { |
| 614 | |
| 615 | if (this != &o) { |
| 616 | |
| 617 | clear(); |
| 618 | |
| 619 | BlockedBloomFilter::operator=(std::move(o)); |
| 620 | |
| 621 | hashbit = o.hashbit; |
| 622 | counts = o.counts; |
| 623 | |
| 624 | elems_per_block = o.elems_per_block; |
| 625 | |
| 626 | o.hashbit = nullptr; |
| 627 | o.counts = nullptr; |
| 628 | |
| 629 | o.clear(); |
| 630 | } |
| 631 | |
| 632 | return *this; |
| 633 | } |
| 634 | |
| 635 | void CountingBlockedBloomFilter::clear() { |
| 636 | |
| 637 | if (hashbit != nullptr){ |
| 638 | |
| 639 | delete[] hashbit; |
| 640 | hashbit = nullptr; |
| 641 | } |
| 642 | |
| 643 | if (counts != nullptr){ |
| 644 | |
| 645 | delete[] counts; |
| 646 | counts = nullptr; |
| 647 | } |
| 648 | |
| 649 | elems_per_block = 0; |
| 650 | |
| 651 | BlockedBloomFilter::clear(); |
| 652 | } |
| 653 | |
| 654 | void CountingBlockedBloomFilter::init_arrays() { |
| 655 | |
| 656 | hashbit = new uint64_t[blocks_ * ((elems_per_block + 15) / 16)](); |
| 657 | counts = new uint8_t[blocks_ * elems_per_block](); |
| 658 | } |
| 659 | |
| 660 | uint64_t CountingBlockedBloomFilter::insert_par(const uint64_t kmh, const uint64_t minh) { |
| 661 | |
| 662 | const uint64_t bid_ins = BlockedBloomFilter::insert_par(kmh, minh); // Insert in the bloom filter |
| 663 | const uint64_t bid = bid_ins >> 1; // Block ID of insertion |
| 664 | |
| 665 | const bool ins = static_cast<bool>(bid_ins & 0x1ULL); // Boolean indicating whether insertion took place |
| 666 | |
| 667 | if (bid != 0) { // bid == 0 means all buckets to insert were overflowing |
| 668 | |
| 669 | const uint64_t pos_start_block = (bid - 1) * elems_per_block; |
| 670 | |
| 671 | const uint64_t kmh_s1 = wyhash(&kmh, sizeof(uint64_t), seed1, _wyp); |
| 672 | const uint64_t kmh_s2 = wyhash(&kmh, sizeof(uint64_t), seed2, _wyp); |
| 673 | |
| 674 | uint64_t kmh1 = kmh_s1; |
| 675 | uint64_t count_pos = 0xFFFFFFFFFFFFFFFFULL; |
| 676 | |
| 677 | table_[bid].lock(); |
| 678 | |
| 679 | if (ins) { // Element was just inserted so it was not present in the graph before |
| 680 | |
| 681 | for (size_t i = 0; i < 4; ++i, kmh1 += kmh_s2) { |
| 682 | |
| 683 | const uint64_t pos_within_block = kmh1 % elems_per_block; |
| 684 | const uint64_t pos_abs = pos_start_block + pos_within_block; |
| 685 | |
| 686 | const uint64_t pos_div = pos_abs >> 4; // Div by 16 |
| 687 | const uint64_t pos_mod = ((pos_abs & 0xFULL) << 2) + i; |
| 688 | const uint64_t mask_ins = 0x1ULL << pos_mod; |
| 689 | |
| 690 | if ((hashbit[pos_div] & mask_ins) == 0){ |
| 691 | |
| 692 | hashbit[pos_div] |= mask_ins; |
| 693 | count_pos = pos_abs; |
| 694 | |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | if (!ins || (count_pos == 0xFFFFFFFFFFFFFFFFULL)) { |
| 701 | |
| 702 | uint8_t count = 0xFF; |
| 703 | |
| 704 | for (size_t i = 0; i < 4; ++i, kmh1 += kmh_s2) { |
| 705 | |
| 706 | const uint64_t pos_within_block = kmh1 % elems_per_block; |
| 707 | const uint64_t pos_abs = pos_start_block + pos_within_block; |
| 708 | |
| 709 | const uint64_t pos_div = pos_abs >> 4; // Div by 16 |
| 710 | const uint64_t pos_mod = ((pos_abs & 0xFULL) << 2) + i; |
| 711 | const uint64_t mask_ins = 0x1ULL << pos_mod; |
| 712 | |
| 713 | if ((hashbit[pos_div] & mask_ins) == 0) break; |
| 714 | else if (counts[pos_abs] <= count) { |
| 715 | |
| 716 | count = counts[pos_abs]; |
| 717 | count_pos = pos_abs; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | counts[count_pos] += static_cast<uint8_t>(counts[count_pos] != 0xFF); |
| 723 | |
| 724 | table_[bid].unlock(); |
| 725 | } |
| 726 | |
| 727 | return bid_ins; |
| 728 | } |
| 729 | |
| 730 | uint64_t CountingBlockedBloomFilter::insert_unpar(const uint64_t kmh, const uint64_t minh) { |
| 731 | |
| 732 | const uint64_t bid_ins = BlockedBloomFilter::insert_unpar(kmh, minh); // Insert in the bloom filter |
| 733 | const uint64_t bid = bid_ins >> 1; // Block ID of insertion |
| 734 | |
| 735 | const bool ins = static_cast<bool>(bid_ins & 0x1ULL); // Boolean indicating whether insertion took place |
| 736 | |
| 737 | if (bid != 0) { // bid == 0 means all buckets to insert were overflowing |
| 738 | |
| 739 | const uint64_t pos_start_block = (bid - 1) * elems_per_block; |
| 740 | |
| 741 | const uint64_t kmh_s1 = wyhash(&kmh, sizeof(uint64_t), seed1, _wyp); |
| 742 | const uint64_t kmh_s2 = wyhash(&kmh, sizeof(uint64_t), seed2, _wyp); |
| 743 | |
| 744 | uint64_t kmh1 = kmh_s1; |
| 745 | uint64_t count_pos = 0xFFFFFFFFFFFFFFFFULL; |
| 746 | |
| 747 | if (ins) { // Element was just inserted so it was not present in the graph before |
| 748 | |
| 749 | for (size_t i = 0; i < 4; ++i, kmh1 += kmh_s2) { |
| 750 | |
| 751 | const uint64_t pos_within_block = kmh1 % elems_per_block; |
| 752 | const uint64_t pos_abs = pos_start_block + pos_within_block; |
| 753 | |
| 754 | const uint64_t pos_div = pos_abs >> 4; // Div by 16 |
| 755 | const uint64_t pos_mod = ((pos_abs & 0xFULL) << 2) + i; |
| 756 | const uint64_t mask_ins = 0x1ULL << pos_mod; |
| 757 | |
| 758 | if ((hashbit[pos_div] & mask_ins) == 0){ |
| 759 | |
| 760 | hashbit[pos_div] |= mask_ins; |
| 761 | count_pos = pos_abs; |
| 762 | |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | if (!ins || (count_pos == 0xFFFFFFFFFFFFFFFFULL)) { |
| 769 | |
| 770 | uint8_t count = 0xFF; |
| 771 | |
| 772 | for (size_t i = 0; i < 4; ++i, kmh1 += kmh_s2) { |
| 773 | |
| 774 | const uint64_t pos_within_block = kmh1 % elems_per_block; |
| 775 | const uint64_t pos_abs = pos_start_block + pos_within_block; |
| 776 | |
| 777 | const uint64_t pos_div = pos_abs >> 4; // Div by 16 |
| 778 | const uint64_t pos_mod = ((pos_abs & 0xFULL) << 2) + i; |
| 779 | const uint64_t mask_ins = 0x1ULL << pos_mod; |
| 780 | |
| 781 | if ((hashbit[pos_div] & mask_ins) == 0) break; |
| 782 | else if (counts[pos_abs] <= count) { |
| 783 | |
| 784 | count = counts[pos_abs]; |
| 785 | count_pos = pos_abs; |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | counts[count_pos] += static_cast<uint8_t>(counts[count_pos] != 0xFF); |
| 791 | } |
| 792 | |
| 793 | return bid_ins; |
| 794 | } |
| 795 | |
| 796 | int64_t CountingBlockedBloomFilter::contains_bids(const uint64_t kmh, const uint64_t minh, const uint64_t min_count) const { |
| 797 | |
| 798 | const int64_t bid = BlockedBloomFilter::contains_bids(kmh, minh); |
| 799 | |
| 800 | if (bid != -1) { |
| 801 | |
| 802 | if (bid == 0) return 0; |
| 803 | else { |
| 804 | |
| 805 | const uint64_t pos_start_block = (bid - 1) * elems_per_block; |
| 806 | |
| 807 | const uint64_t kmh_s1 = wyhash(&kmh, sizeof(uint64_t), seed1, _wyp); |
| 808 | const uint64_t kmh_s2 = wyhash(&kmh, sizeof(uint64_t), seed2, _wyp); |
| 809 | |
| 810 | uint64_t kmh1 = kmh_s1; |
| 811 | |
| 812 | uint8_t count = 0; |
| 813 | |
| 814 | for (size_t i = 0; (i < 4) && (count < min_count); ++i, kmh1 += kmh_s2) { |
| 815 | |
| 816 | const uint64_t pos_within_block = kmh1 % elems_per_block; |
| 817 | const uint64_t pos_abs = pos_start_block + pos_within_block; |
| 818 | |
| 819 | const uint64_t pos_div = pos_abs >> 4; // Div by 16 |
| 820 | const uint64_t pos_mod = ((pos_abs & 0xFULL) << 2) + i; |
| 821 | const uint64_t mask_ins = 0x1ULL << pos_mod; |
| 822 | |
| 823 | if ((hashbit[pos_div] & mask_ins) != 0) count += counts[pos_abs]; |
| 824 | else break; |
| 825 | } |
| 826 | |
| 827 | if (count >= min_count) return bid; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | return -1; |
| 832 | } |
| 833 | |
| 834 | int CountingBlockedBloomFilter::contains(const uint64_t (&kmh)[4], const uint64_t minh, bool (&pres)[4], const int limit, const uint64_t min_count) const { |
| 835 | |
| 836 | const std::array<int64_t, 4> bids = BlockedBloomFilter::contains_bids(kmh, minh, 4); |
| 837 | |
| 838 | int cpt = 0; |
| 839 | |
| 840 | pres[0] = false; |
| 841 | pres[1] = false; |
| 842 | pres[2] = false; |
| 843 | pres[3] = false; |
| 844 | |
| 845 | for (size_t i = 0; (i < 4) && (cpt < limit); ++i) { |
| 846 | |
| 847 | if (bids[i] != -1) { |
| 848 | |
| 849 | if (bids[i] == 0) { |
| 850 | |
| 851 | pres[i] = true; |
| 852 | ++cpt; |
| 853 | } |
| 854 | else { |
| 855 | |
| 856 | const uint64_t pos_start_block = (bids[i] - 1) * elems_per_block; |
| 857 | |
| 858 | const uint64_t kmh_s1 = wyhash(&kmh[i], sizeof(uint64_t), seed1, _wyp); |
| 859 | const uint64_t kmh_s2 = wyhash(&kmh[i], sizeof(uint64_t), seed2, _wyp); |
| 860 | |
| 861 | uint64_t kmh1 = kmh_s1; |
| 862 | |
| 863 | uint8_t count = 0; |
| 864 | |
| 865 | for (size_t ii = 0; (ii < 4) && (count < min_count); ++ii, kmh1 += kmh_s2) { |
| 866 | |
| 867 | const uint64_t pos_within_block = kmh1 % elems_per_block; |
| 868 | const uint64_t pos_abs = pos_start_block + pos_within_block; |
| 869 | |
| 870 | const uint64_t pos_div = pos_abs >> 4; // Div by 16 |
| 871 | const uint64_t pos_mod = ((pos_abs & 0xFULL) << 2) + ii; |
| 872 | const uint64_t mask_ins = 0x1ULL << pos_mod; |
| 873 | |
| 874 | if ((hashbit[pos_div] & mask_ins) != 0) count += counts[pos_abs]; |
| 875 | else break; |
| 876 | } |
| 877 | |
| 878 | if (count >= min_count) { |
| 879 | |
| 880 | pres[i] = true; |
| 881 | ++cpt; |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | return cpt; |
| 888 | } |
| 889 | |
| 890 | bool CountingBlockedBloomFilter::WriteBloomFilter(FILE *fp) const { |
| 891 | |
| 892 | if (!BlockedBloomFilter::WriteBloomFilter(fp)) return false; |
| 893 | |
| 894 | if (fwrite(&elems_per_block, sizeof(uint64_t), 1, fp) != 1) return false; |
| 895 | |
| 896 | if (fwrite(hashbit, sizeof(uint64_t), blocks_ * ((elems_per_block + 15) / 16), fp) != (blocks_ * ((elems_per_block + 15) / 16))) return false; |
| 897 | if (fwrite(counts, sizeof(uint8_t), blocks_ * elems_per_block, fp) != (blocks_ * elems_per_block)) return false; |
| 898 | |
| 899 | return true; |
| 900 | } |
| 901 | |
| 902 | bool CountingBlockedBloomFilter::ReadBloomFilter(FILE *fp) { |
| 903 | |
| 904 | clear(); |
| 905 | |
| 906 | if (!BlockedBloomFilter::ReadBloomFilter(fp)) return false; |
| 907 | |
| 908 | if (fread(&elems_per_block, sizeof(uint64_t), 1, fp) != 1) return false; |
| 909 | |
| 910 | init_arrays(); |
| 911 | |
| 912 | if (fread(hashbit, sizeof(uint64_t), BlockedBloomFilter::blocks_ * ((elems_per_block + 15) / 16), fp) != (blocks_ * ((elems_per_block + 15) / 16))) return false; |
| 913 | if (fread(counts, sizeof(uint8_t), BlockedBloomFilter::blocks_ * elems_per_block, fp) != (blocks_ * elems_per_block)) return false; |
| 914 | |
| 915 | return true; |
| 916 | } |
| 917 | |
| 918 | //#endif |