Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
bloom_filter.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2014 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
29
******************************************************************************/
30
31
#ifndef ETL_BLOOM_FILTER_INCLUDED
32
#define ETL_BLOOM_FILTER_INCLUDED
33
34
#include "
platform.h
"
35
#include "
binary.h
"
36
#include "
bitset.h
"
37
#include "
log.h
"
38
#include "
parameter_type.h
"
39
#include "
power.h
"
40
#include "
type_traits.h
"
41
45
46
namespace
etl
47
{
48
namespace
private_bloom_filter
49
{
50
// Placeholder null hash for defaulted template parameters.
51
struct
null_hash
52
{
53
template
<
typename
T>
54
size_t
operator()(T)
55
{
56
return
0;
57
}
58
};
59
}
// namespace private_bloom_filter
60
61
//***************************************************************************
72
//***************************************************************************
73
template
<
size_t
Desired_Width,
typename
THash1,
typename
THash2 =
private_bloom_filter::null_hash
,
74
typename
THash3 =
private_bloom_filter::null_hash
>
75
class
bloom_filter
76
{
77
private
:
78
79
typedef
typename
etl::parameter_type<typename THash1::argument_type>::type
parameter_t;
80
typedef
private_bloom_filter::null_hash
null_hash;
81
82
public
:
83
84
enum
85
{
86
// Make the most efficient use of the bitset.
87
WIDTH = etl::bitset<Desired_Width>::Allocated_Bits
88
};
89
90
//***************************************************************************
92
//***************************************************************************
93
void
clear
()
94
{
95
flags.reset();
96
}
97
98
//***************************************************************************
101
//***************************************************************************
102
void
add
(parameter_t key)
103
{
104
flags.set(get_hash<THash1>(key));
105
106
if
(!etl::is_same<THash2, null_hash>::value)
107
{
108
flags.set(get_hash<THash2>(key));
109
}
110
111
if
(!etl::is_same<THash3, null_hash>::value)
112
{
113
flags.set(get_hash<THash3>(key));
114
}
115
}
116
117
//***************************************************************************
121
//***************************************************************************
122
bool
exists
(parameter_t key)
const
123
{
124
bool
exists1 = flags[get_hash<THash1>(key)];
125
bool
exists2 =
true
;
126
bool
exists3 =
true
;
127
128
// Do we have a second hash?
129
if
(!etl::is_same<THash2, null_hash>::value)
130
{
131
exists2 = flags[get_hash<THash2>(key)];
132
}
133
134
// Do we have a third hash?
135
if
(!etl::is_same<THash3, null_hash>::value)
136
{
137
exists3 = flags[get_hash<THash3>(key)];
138
}
139
140
return
exists1 && exists2 && exists3;
141
}
142
143
//***************************************************************************
145
//***************************************************************************
146
size_t
width
()
const
147
{
148
return
WIDTH;
149
}
150
151
//***************************************************************************
153
//***************************************************************************
154
size_t
usage
()
const
155
{
156
return
(100 *
count
()) / WIDTH;
157
}
158
159
//***************************************************************************
161
//***************************************************************************
162
size_t
count
()
const
163
{
164
return
flags.count();
165
}
166
167
private
:
168
169
//***************************************************************************
173
//***************************************************************************
174
template
<
typename
THash>
175
size_t
get_hash(parameter_t key)
const
176
{
177
size_t
hash = THash()(key);
178
179
// Fold the hash down to fit the width.
180
return
fold_bits<size_t, etl::log2<WIDTH>::value
>(hash);
181
}
182
184
etl::bitset<WIDTH>
flags;
185
};
186
}
// namespace etl
187
188
#endif
binary.h
bitset.h
etl::fold_bits
ETL_CONSTEXPR14 TReturn fold_bits(TValue value)
Definition
binary.h:243
etl::bitset
Bitset forward declaration.
Definition
bitset_legacy.h:1124
etl::bloom_filter::width
size_t width() const
Returns the width of the Bloom filter.
Definition
bloom_filter.h:146
etl::bloom_filter::count
size_t count() const
Returns the number of filter flags set.
Definition
bloom_filter.h:162
etl::bloom_filter::clear
void clear()
Clears the bloom filter of all entries.
Definition
bloom_filter.h:93
etl::bloom_filter::exists
bool exists(parameter_t key) const
Definition
bloom_filter.h:122
etl::bloom_filter::usage
size_t usage() const
Returns the percentage of usage. Range 0 to 100.
Definition
bloom_filter.h:154
etl::bloom_filter::add
void add(parameter_t key)
Definition
bloom_filter.h:102
etl::bloom_filter
Definition
bloom_filter.h:76
log.h
etl
Definition
absolute.h:40
parameter_type.h
platform.h
power.h
etl::parameter_type::type
etl::conditional< etl::is_fundamental< T >::value||etl::is_pointer< T >::value, T, constT & >::type type
By default fundamental and pointer types are passed by value.
Definition
parameter_type.h:46
etl::private_bloom_filter::null_hash
Definition
bloom_filter.h:52
type_traits.h
include
etl
bloom_filter.h
Generated on
for Embedded Template Library by
1.17.0