Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mavam/libbf
Browse files Browse the repository at this point in the history
  • Loading branch information
mavam committed Jun 28, 2017
2 parents 2b342b6 + 9837a74 commit 802d395
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bf/bitvector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class bitvector
bitvector& operator=(bitvector other);

/// Swaps two bit vectors.
friend void swap(bitvector x, bitvector y);
friend void swap(bitvector& x, bitvector& y);

//
// Bitwise operations
Expand Down
9 changes: 9 additions & 0 deletions bf/bloom_filter/basic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class basic_bloom_filter : public bloom_filter
basic_bloom_filter(double fp, size_t capacity, size_t seed = 0,
bool double_hashing = true, bool partition = true);

/// Constructs a basic Bloom filter given a hasher and a bitvector.
///
/// @param hasher The hasher to use.
/// @param bitvector the underlying bitvector of the bf.
basic_bloom_filter(hasher h, bitvector b);

basic_bloom_filter(basic_bloom_filter&&);

using bloom_filter::add;
Expand All @@ -82,6 +88,9 @@ class basic_bloom_filter : public bloom_filter
/// Returns the underlying storage of the Bloom filter.
bitvector const& storage() const;

/// Returns the hasher of the Bloom filter.
hasher const& hasher_function() const;

private:
hasher hasher_;
bitvector bits_;
Expand Down
2 changes: 1 addition & 1 deletion src/bitvector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ bitvector& bitvector::operator=(bitvector other)
return *this;
}

void swap(bitvector x, bitvector y)
void swap(bitvector& x, bitvector& y)
{
using std::swap;
swap(x.bits_, y.bits_);
Expand Down
11 changes: 11 additions & 0 deletions src/bloom_filter/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ basic_bloom_filter::basic_bloom_filter(double fp, size_t capacity, size_t seed,
hasher_ = make_hasher(optimal_k, seed, double_hashing);
}

basic_bloom_filter::basic_bloom_filter(hasher h, bitvector b)
: hasher_(std::move(h))
, bits_(std::move(b))
{
}

basic_bloom_filter::basic_bloom_filter(basic_bloom_filter&& other)
: hasher_(std::move(other.hasher_)),
bits_(std::move(other.bits_))
Expand Down Expand Up @@ -100,5 +106,10 @@ bitvector const& basic_bloom_filter::storage() const
{
return bits_;
}
hasher const& basic_bloom_filter::hasher_function() const
{
return hasher_;
}


} // namespace bf
14 changes: 14 additions & 0 deletions test/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ TEST(bloom_filter_basic) {
// False-positives
CHECK_EQUAL(bf.lookup("corge"), 1u);
CHECK_EQUAL(bf.lookup('a'), 1u);

// another filter
basic_bloom_filter obf(0.8, 10);
obf.swap(bf);

CHECK_EQUAL(obf.lookup("foo"), 1u);

// Make bf using another filter's storage
hasher h = obf.hasher_function();
bitvector b = obf.storage();
basic_bloom_filter obfc(h, b);
CHECK_EQUAL(obfc.storage(), b);
CHECK_EQUAL(obfc.lookup("foo"), 1u);

}

TEST(bloom_filter_counting) {
Expand Down

0 comments on commit 802d395

Please sign in to comment.