TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
data_structures::BloomFilter< T > Class Template Reference

Bloom filter template class. More...

Collaboration diagram for data_structures::BloomFilter< T >:
[legend]

Public Member Functions

 BloomFilter (std::size_t, std::initializer_list< std::function< std::size_t(T)> >)
 Constructor for Bloom filter.
 
void add (T)
 Add function for Bloom filter.
 
bool contains (T)
 Check element function for Bloom filter.
 

Private Attributes

Bitset set
 inner bitset for elements
 
std::vector< std::function< std::size_t(T)> > hashFunks
 hash functions for T type
 

Detailed Description

template<typename T>
class data_structures::BloomFilter< T >

Bloom filter template class.

Template Parameters
Ttype of elements that we need to filter

Definition at line 99 of file bloom_filter.cpp.

Constructor & Destructor Documentation

◆ BloomFilter()

template<typename T >
data_structures::BloomFilter< T >::BloomFilter ( std::size_t size,
std::initializer_list< std::function< std::size_t(T)> > funks )

Constructor for Bloom filter.

Template Parameters
Ttype of elements that we need to filter
Parameters
sizeinitial size of Bloom filter
funkshash functions for T type
Returns
none

Definition at line 121 of file bloom_filter.cpp.

124 : set(size), hashFunks(funks) {}
std::vector< std::function< std::size_t(T)> > hashFunks
hash functions for T type
Bitset set
inner bitset for elements

Member Function Documentation

◆ add()

template<typename T >
void data_structures::BloomFilter< T >::add ( T x)

Add function for Bloom filter.

Template Parameters
Ttype of elements that we need to filter
Parameters
xelement to add to filter
Returns
void

Definition at line 134 of file bloom_filter.cpp.

134 {
135 for (std::size_t i = 0; i < hashFunks.size(); i++) {
136 set.add(hashFunks[i](x) % (sizeof(std::size_t) * set.size()));
137 }
138}
void add(std::size_t)
Turn bit on position x to 1s.
std::size_t size()
Utility function to return the size of the inner array.

◆ contains()

template<typename T >
bool data_structures::BloomFilter< T >::contains ( T x)

Check element function for Bloom filter.

Template Parameters
Ttype of elements that we need to filter
Parameters
xelement to check in filter
Returns
true if the element probably appears in the filter
false if the element certainly does not appear in the filter

Definition at line 149 of file bloom_filter.cpp.

149 {
150 for (std::size_t i = 0; i < hashFunks.size(); i++) {
151 if (set.contains(hashFunks[i](x) %
152 (sizeof(std::size_t) * set.size())) == false) {
153 return false;
154 }
155 }
156 return true;
157}
bool contains(std::size_t)
Doest bitset contains element x.

Member Data Documentation

◆ hashFunks

template<typename T >
std::vector<std::function<std::size_t(T)> > data_structures::BloomFilter< T >::hashFunks
private

hash functions for T type

Definition at line 103 of file bloom_filter.cpp.

◆ set

template<typename T >
Bitset data_structures::BloomFilter< T >::set
private

inner bitset for elements

Definition at line 101 of file bloom_filter.cpp.


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