/
PrimitiveBloomFilter.h
93 lines (77 loc) · 2.6 KB
/
PrimitiveBloomFilter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* \file PrimitiveBloomFilter.h
*
* BloomFilter wrapper template for primitive types.
*
* This code is provided free of charge and without restrictions. It may be
* used for commercial products. It may be redistributed. It may be modified.
* IT COMES WITH NO WARRANTY OF ANY SORT.
*
*/
#ifndef PRIMITIVEBLOOMFILTER_H
#define PRIMITIVEBLOOMFILTER_H
#include "BloomFilter.h"
/**
* Bloom filter wrapper for primitive types.
*
* This provides a more convenient interface for adding and checking elements in
* a bloom filter. It will work with any class that can be properly hashed
* using only a pointer to its raw memory region and sizeof().
*/
template< typename PrimitiveT, std::size_t NumBits >
class PrimitiveBloomFilter : public BloomFilter<NumBits> {
public:
/**
* Constructor.
*
* \param expectedElemCnt Expected element count.
* \param numHashes Number of hashes to use.
*/
PrimitiveBloomFilter( std::size_t expectedElemCnt, int numHashes = -1 )
: BloomFilter<NumBits>( expectedElemCnt, numHashes ) {
}
/**
* Copy ctor.
*
* \param src Source to copy.
*/
PrimitiveBloomFilter( const PrimitiveBloomFilter<PrimitiveT,NumBits>& src )
: BloomFilter<NumBits>( src ) {
}
/**
* Assignment operator.
*
* \param rhs Right hand side.
*
* \return reference to updated self.
*/
PrimitiveBloomFilter<PrimitiveT,NumBits> operator=(
const PrimitiveBloomFilter<PrimitiveT,NumBits>& rhs ) {
BloomFilter<NumBits>::operator=( rhs );
return *this;
}
/**
* Add element to filter.
*
* \param element Element to add.
*/
void addElement( PrimitiveT element ) {
BloomFilter<NumBits>::addElement(
reinterpret_cast<char*>( &element ),
sizeof( PrimitiveT ) );
}
/**
* Check if element is in filter.
*
* \param element Element to check.
*
* \return True if element is possibly in filter; false if element is
* definitely not inside filter.
*/
bool containsElement( PrimitiveT element ) const {
return BloomFilter<NumBits>::containsElement(
reinterpret_cast<char*>( &element ),
sizeof( PrimitiveT ) );
}
};
#endif // PRIMITIVEBLOOMFILTER_H