Embedded Template Library 1.0
Loading...
Searching...
No Matches
bloom_filter.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
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
46namespace 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>
76 {
77 private:
78
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.
181 }
182
184 etl::bitset<WIDTH> flags;
185 };
186} // namespace etl
187
188#endif
ETL_CONSTEXPR14 TReturn fold_bits(TValue value)
Definition binary.h:243
Bitset forward declaration.
Definition bitset_legacy.h:1124
size_t width() const
Returns the width of the Bloom filter.
Definition bloom_filter.h:146
size_t count() const
Returns the number of filter flags set.
Definition bloom_filter.h:162
void clear()
Clears the bloom filter of all entries.
Definition bloom_filter.h:93
bool exists(parameter_t key) const
Definition bloom_filter.h:122
size_t usage() const
Returns the percentage of usage. Range 0 to 100.
Definition bloom_filter.h:154
void add(parameter_t key)
Definition bloom_filter.h:102
Definition bloom_filter.h:76
bitset_ext
Definition absolute.h:40
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
Definition bloom_filter.h:52