Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
quotient-filter.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
118#ifndef QUOTIENT_FILTER_H
119#define QUOTIENT_FILTER_H
120
121#include <cstdint>
122#include <cstddef>
123#include <cmath>
124#include <algorithm>
125#include <utility>
126
127#include <ah-errors.H>
128#include <tpl_array.H>
129#include <hash-fct.H>
130
131namespace Aleph {
132
144template <typename T> class Quotient_Filter
145{
146 uint8_t q_;
147 uint8_t r_;
150 uint32_t seed_;
151
152 // Per-slot storage packed into a uint64_t:
153 // bit 0: is_occupied (belongs to the SLOT INDEX, not shifted data)
154 // bit 1: is_continuation
155 // bit 2: is_shifted
156 // bits 3..(r+2): remainder
158
159 static constexpr uint64_t OCC_BIT = 1ULL << 0;
160 static constexpr uint64_t CONT_BIT = 1ULL << 1;
161 static constexpr uint64_t SHFT_BIT = 1ULL << 2;
162 static constexpr uint64_t META_MASK = 0x7ULL;
163
164 [[nodiscard]] uint64_t rem_mask() const noexcept
165 {
166 return (1ULL << r_) - 1;
167 }
168
169 [[nodiscard]] bool get_occ(size_t i) const noexcept
170 {
171 return (slots_(i) & OCC_BIT) != 0;
172 }
173
174 [[nodiscard]] bool get_cont(size_t i) const noexcept
175 {
176 return (slots_(i) & CONT_BIT) != 0;
177 }
178
179 [[nodiscard]] bool get_shft(size_t i) const noexcept
180 {
181 return (slots_(i) & SHFT_BIT) != 0;
182 }
183
184 // A slot is truly empty when it holds no data and no occupied marker.
185 [[nodiscard]] bool slot_empty(size_t i) const noexcept
186 {
187 return slots_(i) == 0;
188 }
189
190 // A slot holds data if continuation or shifted is set, or if it
191 // is the unshifted start of a run (occupied and not empty remainder
192 // region... actually simplest: it holds data iff it is not
193 // "metadata-only"). A slot is "has element" iff is_shifted or
194 // is_continuation is set, OR (not shifted and not continuation and
195 // the slot is occupied — i.e., it's a run start at its canonical
196 // position). Equivalently: a slot has an element stored iff
197 // is_continuation or is_shifted, or (is_occupied and not shifted
198 // and not continuation), which simplifies to: the slot is not
199 // completely empty AND (if only occupied is set, there IS data
200 // here — it's the canonical run start).
201 //
202 // Actually the precise check: a slot has data iff any of the three
203 // bits is set (since occupied-only slots DO store data: the first
204 // element of an unshifted run).
205 // CORRECTION: that's wrong. A slot can have occupied=1 but hold
206 // another quotient's shifted data. The occupied bit marks that
207 // SOME run for this quotient exists, stored here or elsewhere.
208 //
209 // The simplest correct check: a slot holds element data iff
210 // shifted or continuation is set, OR (it is unshifted and not a
211 // continuation — i.e., it is a cluster start, meaning it stores
212 // the first element of a run at its canonical position).
213 //
214 // Even simpler: a slot is "element-occupied" iff it is not fully
215 // empty (meta != 0) and not "occupied-only" (meta == OCC_BIT).
216 // Wait, that's also wrong. A slot with only OCC_BIT set has the
217 // run start element at its canonical position!
218 //
219 // Let me think again. Slots that have data:
220 // 1. occ=1, cont=0, shft=0: canonical run start (has data)
221 // 2. occ=X, cont=0, shft=1: shifted run start (has data)
222 // 3. occ=X, cont=1, shft=1: shifted continuation (has data)
223 // 4. occ=X, cont=1, shft=0: impossible (continuation must be shifted
224 // unless it's at its canonical position, but continuation means
225 // it's not the first in its run, so it must be shifted)
226 // Actually wait — cont=1, shft=0: this IS possible if the
227 // continuation happens to land at its original quotient's
228 // canonical position... no, cont=1 means it's not the FIRST
229 // in the run, and shft=0 means it IS at its canonical position.
230 // Can this happen? Only if two elements have the same quotient
231 // and the continuation of the run happens to be at the same
232 // canonical slot. But runs are contiguous and sorted by
233 // remainder, and for the same quotient all have the same
234 // canonical slot. So the 2nd element of a run at slot fq+1
235 // would have cont=1, shft=1 (since it's shifted from fq).
236 // Unless fq itself has two elements? No, each slot holds one
237 // element. The run for quotient fq starts at slot fq (if
238 // unshifted), and continues at fq+1, fq+2, etc. with cont=1,
239 // shft=1.
240 //
241 // Bottom line: a slot has data stored iff (cont or shft) is set,
242 // OR (both are clear AND occ is set — canonical unshifted run start).
243 // Equivalently: (slots_[i] & META_MASK) != 0 and it's not the case
244 // that the slot has ONLY the occupied bit with nothing stored.
245 //
246 // SIMPLIFICATION: In practice, a slot "has data" iff
247 // (slots_[i] & (CONT_BIT | SHFT_BIT)) != 0 ||
248 // slot is a canonical start (occ=1, cont=0, shft=0)
249 // Which is the same as (slots_[i] & META_MASK) != 0 (any bit set).
250 // But the problem is occ-only: does occ=1, cont=0, shft=0 ALWAYS
251 // mean data is stored? YES. If occupied is set and the element is
252 // not shifted, it means this slot IS the start of the run, so data
253 // is here.
254 //
255 // FINAL ANSWER: has_element(i) iff (slots_[i] & META_MASK) != 0.
256 // This is the same as !slot_empty(i).
257 [[nodiscard]] bool has_element(const size_t i) const noexcept
258 {
259 return (slots_(i) & META_MASK) != 0;
260 }
261
262 // A slot is a "run start" if it has data and is_continuation is false.
263 [[nodiscard]] bool is_run_start(const size_t i) const noexcept
264 {
265 return has_element(i) and not get_cont(i);
266 }
267
268 void set_occ(const size_t i, const bool v) noexcept
269 {
270 if (v)
271 slots_(i) |= OCC_BIT;
272 else
273 slots_(i) &= ~OCC_BIT;
274 }
275
276 void set_cont(const size_t i, const bool v) noexcept
277 {
278 if (v)
279 slots_(i) |= CONT_BIT;
280 else
281 slots_(i) &= ~CONT_BIT;
282 }
283
284 void set_shft(const size_t i, const bool v) noexcept
285 {
286 if (v)
287 slots_(i) |= SHFT_BIT;
288 else
289 slots_(i) &= ~SHFT_BIT;
290 }
291
292 [[nodiscard]] uint64_t get_rem(const size_t i) const noexcept
293 {
294 return (slots_(i) >> 3) & rem_mask();
295 }
296
297 void set_rem(const size_t i, const uint64_t rem) noexcept
298 {
299 slots_(i) = (slots_(i) & META_MASK) | ((rem & rem_mask()) << 3);
300 }
301
302 // Set the full "element data" portion of a slot (cont, shft, rem),
303 // but preserve the occupied bit which belongs to the slot index.
304 void set_element(const size_t i, const bool cont, const bool shft, const uint64_t rem) noexcept
305 {
306 const bool occ = get_occ(i);
307 slots_(i) = ((rem & rem_mask()) << 3) | (shft ? SHFT_BIT : 0) | (cont ? CONT_BIT : 0) | (occ ? OCC_BIT : 0);
308 }
309
310 // Clear the element data (cont, shft, rem) but preserve occupied.
311 void clear_element(const size_t i) noexcept
312 {
313 const bool occ = get_occ(i);
314 slots_(i) = occ ? OCC_BIT : 0;
315 }
316
317 [[nodiscard]] size_t incr(const size_t i) const noexcept
318 {
319 return (i + 1) & (num_slots_ - 1);
320 }
321
322 [[nodiscard]] size_t decr(const size_t i) const noexcept
323 {
324 return (i - 1) & (num_slots_ - 1);
325 }
326
327 [[nodiscard]] std::pair<size_t, uint64_t> fingerprint(const T &item) const
328 {
329 const size_t h = murmur3hash(item, seed_);
330 const size_t fq = h & (num_slots_ - 1);
331 const uint64_t fr = (static_cast<uint64_t>(h) >> q_) & rem_mask();
332 return {fq, fr};
333 }
334
335 // Find the start of the run whose canonical slot is `fq`.
336 // Precondition: is_occupied(fq) is true.
337 [[nodiscard]] size_t find_run_start(size_t fq) const noexcept
338 {
339 // 1. Walk backward from fq to find the cluster start.
340 size_t j = fq;
341 while (get_shft(j))
342 j = decr(j);
343
344 // 2. Walk forward: for each occupied canonical slot between
345 // j and fq, skip past its run.
346 size_t i = j;
347 while (j != fq)
348 {
349 do // skip current run
350 i = incr(i);
351 while (get_cont(i));
352
353 do // advance to next occupied canonical slot
354 j = incr(j);
355 while (not get_occ(j));
356 }
357 return i;
358 }
359
360 // Find the first slot at or after `start` that has no element data.
361 [[nodiscard]] size_t find_first_unused(size_t start) const noexcept
362 {
363 size_t i = start;
364 while (has_element(i))
365 i = incr(i);
366 return i;
367 }
368
369 // Shift element data one slot to the right, from `pos` up to (but
370 // not including) `empty`. The occupied bit is never shifted — it
371 // belongs to the slot index and is preserved in place.
372 void shift_elements_right(size_t pos, size_t empty) noexcept
373 {
374 while (empty != pos)
375 {
376 const size_t prev = decr(empty);
377 const bool occ_at_empty = get_occ(empty);
378 slots_(empty) = slots_(prev);
379 set_occ(empty, occ_at_empty); // restore occupied at destination
380 set_shft(empty, true); // everything shifted is shifted
381 empty = prev;
382 }
383 }
384
385 // Whether the run of the quotient `fq` holds the remainder `fr`.
386 [[nodiscard]] bool holds(const size_t fq, const uint64_t fr) const noexcept
387 {
388 if (not get_occ(fq))
389 return false;
390
391 size_t s = find_run_start(fq);
392 do
393 {
394 const uint64_t cur = get_rem(s);
395 if (cur == fr)
396 return true;
397 if (cur > fr)
398 return false;
399 s = incr(s);
400 } while (get_cont(s));
401
402 return false;
403 }
404
405 // Calls op(fq, fr) with the quotient and the remainder of each element.
406 template <class Op> void for_each_fingerprint(Op op) const
407 {
408 for (size_t fq = 0; fq < num_slots_; ++fq)
409 {
410 if (not get_occ(fq))
411 continue;
412
413 size_t s = find_run_start(fq);
414 do
415 {
416 op(fq, get_rem(s));
417 s = incr(s);
418 } while (get_cont(s));
419 }
420 }
421
422 enum class Insert_Result
423 {
424 inserted,
425 duplicate,
426 full
427 };
428
429 // Core insert of a pre-computed (fq, fr) pair. The remainder is looked
430 // for before the room: a filter holds its elements once, full or not.
431 [[nodiscard]] Insert_Result do_insert(size_t fq, uint64_t fr)
432 {
433 // Case 1: canonical slot is completely empty. A full filter has none.
434 if (slot_empty(fq))
435 {
436 slots_(fq) = OCC_BIT | ((fr & rem_mask()) << 3);
437 ++num_elems_;
439 }
440
441 if (get_occ(fq))
442 {
443 // A run for this quotient already exists.
444 const size_t s = find_run_start(fq);
445
446 // Scan the run for a duplicate or ordered insertion point.
447 size_t pos = s;
448 do
449 {
450 const uint64_t cur = get_rem(pos);
451 if (cur == fr)
453 if (cur > fr)
454 break;
455 pos = incr(pos);
456 } while (get_cont(pos));
457
458 if (num_elems_ >= num_slots_)
459 return Insert_Result::full;
460
461 // `pos` is where we insert (before element at pos).
462 // If we scanned past the last continuation without finding
463 // cur > fr, pos is one past the run's end — still correct.
464
465 const size_t empty = find_first_unused(pos);
466 shift_elements_right(pos, empty);
467
468 const bool at_run_start = (pos == s);
469 set_element(pos, not at_run_start, pos != fq, fr);
470
471 // If we displaced the old run start, it becomes a continuation.
472 if (at_run_start)
473 set_cont(incr(pos), true);
474
475 ++num_elems_;
477 }
478
479 if (num_elems_ >= num_slots_)
480 return Insert_Result::full;
481
482 // No run for this quotient exists yet. We must find where the
483 // new single-element run should be inserted within the cluster.
484 // With occupied(fq) set, find_run_start locates the correct
485 // position for this new run.
486 set_occ(fq, true);
487 const size_t s = find_run_start(fq);
488
489 const size_t empty = find_first_unused(s);
490 shift_elements_right(s, empty);
491
492 set_element(s, false, s != fq, fr);
493 ++num_elems_;
495 }
496
497 // Core delete: shift one slot to the left the elements that follow the
498 // removed one, at `pos`, to the end of its cluster. The occupied bit is
499 // preserved at each index.
500 //
501 // `quot` is the quotient of the run of the removed element, and `promote`
502 // says that the element next to it is a continuation of that run, which
503 // takes its place as the start of the run. A run start is no longer
504 // shifted when it lands on the slot of its own quotient, and the quotient
505 // of each run that moves is the next occupied one after that of the run
506 // before it. The occupied bit of the slot does not tell it: it may be
507 // that of a later run, whose elements are further to the right.
508 void shift_elements_left(size_t pos, size_t quot, bool promote) noexcept
509 {
510 size_t curr = pos;
511 size_t nxt = incr(curr);
512
513 while (has_element(nxt) and get_shft(nxt))
514 {
515 if (not get_cont(nxt)) // the start of the run of the next quotient
516 do
517 quot = incr(quot);
518 while (not get_occ(quot));
519
520 const bool occ_at_curr = get_occ(curr);
521 slots_(curr) = slots_(nxt);
522 set_occ(curr, occ_at_curr);
523
524 if (promote)
525 {
526 set_cont(curr, false);
527 promote = false;
528 }
529
530 if (not get_cont(curr) and quot == curr)
531 set_shft(curr, false);
532
533 curr = nxt;
534 nxt = incr(nxt);
535 }
536
537 // Clear the last vacated slot, preserving its occupied bit.
538 clear_element(curr);
539 }
540
541public:
549 Quotient_Filter(uint8_t q, uint8_t r, uint32_t seed = 0x5F3759DF)
550 : q_(q), r_(r), num_slots_(0), num_elems_(0), seed_(seed), slots_()
551 {
552 ah_domain_error_if(q < 1 or q > 32) << "Quotient_Filter: q must be in [1, 32] (got " << static_cast<int>(q) << ")";
553 ah_domain_error_if(r < 1 or r > 60) << "Quotient_Filter: r must be in [1, 60] (got " << static_cast<int>(r) << ")";
554 ah_domain_error_if(q + r > 64) << "Quotient_Filter: q + r must be <= 64 (got " << static_cast<int>(q + r) << ")";
555
556 num_slots_ = size_t{1} << q;
557 slots_ = Array<uint64_t>(num_slots_, uint64_t{0});
558 }
559
564 Quotient_Filter(const Quotient_Filter & other) = default;
565
581 {
582 swap(other);
583 }
584
590 Quotient_Filter &operator=(const Quotient_Filter & other) = default;
591
599 {
600 swap(other);
601 return *this;
602 }
603
615 static Quotient_Filter from_capacity(size_t expected_n, double fp_rate, uint32_t seed = 0x5F3759DF)
616 {
617 ah_domain_error_if(expected_n == 0) << "Quotient_Filter::from_capacity: expected_n must be > 0";
618 ah_domain_error_if(not (fp_rate > 0.0 and fp_rate < 1.0))
619 << "Quotient_Filter::from_capacity: fp_rate must be in (0, 1) (got " << fp_rate << ")";
620
621 const auto [q_v, r_v] = estimate(expected_n, fp_rate);
622 return Quotient_Filter(q_v, r_v, seed);
623 }
624
634 {
635 auto [fq, fr] = fingerprint(item);
637 << "Quotient_Filter::insert: filter is full (" << num_elems_ << "/" << num_slots_ << ")";
638 return *this;
639 }
640
645 [[nodiscard]] bool contains(const T &item) const noexcept
646 {
647 auto [fq, fr] = fingerprint(item);
648 return holds(fq, fr);
649 }
650
655 bool remove(const T &item) noexcept
656 {
657 auto [fq, fr] = fingerprint(item);
658
659 if (not get_occ(fq))
660 return false;
661
662 size_t s = find_run_start(fq);
663 size_t pos = s;
664
665 // Locate the remainder within the run.
666 do
667 {
668 if (get_rem(pos) == fr)
669 goto found;
670 if (get_rem(pos) > fr)
671 return false;
672 pos = incr(pos);
673 } while (get_cont(pos));
674 return false;
675
676 found: {
677 const bool was_run_start = not get_cont(pos);
678 size_t nxt = incr(pos);
679 bool nxt_is_cont = has_element(nxt) and get_cont(nxt);
680
681 if (was_run_start and not nxt_is_cont)
682 set_occ(fq, false); // run becomes empty
683
684 // the next of the run, if any, takes the place of its start
685 shift_elements_left(pos, fq, was_run_start and nxt_is_cont);
686 --num_elems_;
687 return true;
688 }
689 }
690
692 [[nodiscard]] size_t size() const noexcept
693 {
694 return num_elems_;
695 }
696
698 [[nodiscard]] size_t capacity() const noexcept
699 {
700 return num_slots_;
701 }
702
704 [[nodiscard]] double load_factor() const noexcept
705 {
706 return static_cast<double>(num_elems_) / num_slots_;
707 }
708
710 [[nodiscard]] bool is_empty() const noexcept
711 {
712 return num_elems_ == 0;
713 }
714
716 [[nodiscard]] uint8_t q() const noexcept
717 {
718 return q_;
719 }
720
722 [[nodiscard]] uint8_t r() const noexcept
723 {
724 return r_;
725 }
726
728 [[nodiscard]] double false_positive_rate() const noexcept
729 {
730 return std::ldexp(1.0, -static_cast<int>(r_));
731 }
732
734 [[nodiscard]] uint32_t seed() const noexcept
735 {
736 return seed_;
737 }
738
740 [[nodiscard]] size_t memory_usage() const noexcept
741 {
742 return num_slots_ * sizeof(uint64_t);
743 }
744
746 void clear() noexcept
747 {
748 for (size_t i = 0; i < num_slots_; ++i)
749 slots_(i) = 0;
750 num_elems_ = 0;
751 }
752
770 void merge(const Quotient_Filter &other)
771 {
772 ah_domain_error_if(q_ != other.q_ or r_ != other.r_) << "Quotient_Filter::merge: parameter mismatch";
773 ah_domain_error_if(seed_ != other.seed_) << "Quotient_Filter::merge: seed mismatch";
774 if (this == &other) // it holds its elements already
775 return;
776
777 // The elements of the other that this one does not hold are counted
778 // before any of them enters, when the two sizes say that they may not
779 // fit: a filter that cannot take them all is left as it was.
780 const size_t room = num_slots_ - num_elems_;
781 if (other.num_elems_ > room)
782 {
783 size_t missing = 0;
784 other.for_each_fingerprint([this, &missing](const size_t fq, const uint64_t fr)
785 {
786 if (not holds(fq, fr))
787 ++missing;
788 });
789 ah_overflow_error_if(missing > room)
790 << "Quotient_Filter::merge: " << missing << " elements of the other filter do not fit in the " << room
791 << " free slots of this one";
792 }
793
794 other.for_each_fingerprint([this](const size_t fq, const uint64_t fr) { (void) do_insert(fq, fr); });
795 }
796
812 [[nodiscard]] static std::pair<uint8_t, uint8_t> estimate(size_t n, double fp_rate)
813 {
814 ah_domain_error_if(n == 0) << "Quotient_Filter::estimate: n must be > 0";
815 ah_domain_error_if(not (fp_rate > 0.0 and fp_rate < 1.0))
816 << "Quotient_Filter::estimate: fp_rate must be in (0, 1) (got " << fp_rate << ")";
817
818 // as int: a rate may ask for up to 1074 bits, more than a uint8_t holds.
819 // They are counted in its exponent, which is exact: a logarithm rounds
820 // a rate just under a power of two to the power, one bit short
821 const double slots = static_cast<double>(n) / 0.75;
822 const int qv = static_cast<int>(std::max(1.0, std::ceil(std::log2(slots))));
823 const int rv = -std::ilogb(fp_rate);
824
825 ah_domain_error_if(rv > 60) << "Quotient_Filter::estimate: a rate of " << fp_rate << " asks for " << rv
826 << " remainder bits, and a filter has 60 at most";
827 ah_domain_error_if(qv > 32) << "Quotient_Filter::estimate: " << n << " elements ask for 2^" << qv
828 << " slots, and a filter has 2^32 at most";
829 ah_domain_error_if(qv + rv > 64) << "Quotient_Filter::estimate: " << n << " elements at a rate of " << fp_rate
830 << " ask for " << qv + rv << " bits of the hash, which has 64";
831 return {static_cast<uint8_t>(qv), static_cast<uint8_t>(rv)};
832 }
833
835 void swap(Quotient_Filter &other) noexcept
836 {
837 std::swap(q_, other.q_);
838 std::swap(r_, other.r_);
839 std::swap(num_slots_, other.num_slots_);
840 std::swap(num_elems_, other.num_elems_);
841 std::swap(seed_, other.seed_);
842 slots_.swap(other.slots_);
843 }
844};
845
846} // end namespace Aleph
847
848#endif // QUOTIENT_FILTER_H
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
long double h
Definition btreepic.C:154
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
Quotient filter for probabilistic set membership with deletion.
bool get_occ(size_t i) const noexcept
static constexpr uint64_t CONT_BIT
bool holds(const size_t fq, const uint64_t fr) const noexcept
Quotient_Filter(Quotient_Filter &&other)
Take the elements of another filter, which is left as an empty filter of two slots (q = 1),...
void shift_elements_left(size_t pos, size_t quot, bool promote) noexcept
Array< uint64_t > slots_
size_t decr(const size_t i) const noexcept
size_t incr(const size_t i) const noexcept
Quotient_Filter & insert(const T &item)
Insert an element.
static Quotient_Filter from_capacity(size_t expected_n, double fp_rate, uint32_t seed=0x5F3759DF)
Construct from desired capacity and false positive rate.
double load_factor() const noexcept
Current load factor.
Quotient_Filter(const Quotient_Filter &other)=default
Copy the parameters and the slots of another filter.
bool has_element(const size_t i) const noexcept
size_t memory_usage() const noexcept
Memory usage in bytes.
uint8_t q() const noexcept
Quotient bits.
bool get_shft(size_t i) const noexcept
bool is_empty() const noexcept
True if empty.
void set_rem(const size_t i, const uint64_t rem) noexcept
double false_positive_rate() const noexcept
Theoretical FP probability ~ 2^{-r}.
Quotient_Filter & operator=(Quotient_Filter &&other) noexcept
Exchange this filter with another, which is left with the previous state of this one.
void set_shft(const size_t i, const bool v) noexcept
Insert_Result do_insert(size_t fq, uint64_t fr)
Quotient_Filter & operator=(const Quotient_Filter &other)=default
Copy the parameters and the slots of another filter.
size_t capacity() const noexcept
Number of slots (2^q).
uint8_t r() const noexcept
Remainder bits.
uint64_t get_rem(const size_t i) const noexcept
void set_occ(const size_t i, const bool v) noexcept
void merge(const Quotient_Filter &other)
Merge another filter (same q, r, seed).
size_t find_first_unused(size_t start) const noexcept
uint64_t rem_mask() const noexcept
bool is_run_start(const size_t i) const noexcept
size_t size() const noexcept
Number of elements.
bool contains(const T &item) const noexcept
Test membership.
bool get_cont(size_t i) const noexcept
void for_each_fingerprint(Op op) const
void set_element(const size_t i, const bool cont, const bool shft, const uint64_t rem) noexcept
std::pair< size_t, uint64_t > fingerprint(const T &item) const
static constexpr uint64_t SHFT_BIT
static constexpr uint64_t OCC_BIT
static constexpr uint64_t META_MASK
uint32_t seed() const noexcept
Hash seed.
bool slot_empty(size_t i) const noexcept
Quotient_Filter(uint8_t q, uint8_t r, uint32_t seed=0x5F3759DF)
Construct a quotient filter.
void shift_elements_right(size_t pos, size_t empty) noexcept
static std::pair< uint8_t, uint8_t > estimate(size_t n, double fp_rate)
The quotient and the remainder bits of a filter for n elements and a target FP rate.
size_t find_run_start(size_t fq) const noexcept
void set_cont(const size_t i, const bool v) noexcept
bool remove(const T &item) noexcept
Remove an element.
void clear() noexcept
Remove all elements.
void clear_element(const size_t i) noexcept
void swap(Quotient_Filter &other) noexcept
Swap in O(1).
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
size_t murmur3hash(const Key &key, std::uint32_t seed)
Definition hash-fct.H:361
Dynamic array container with automatic resizing.