Loading...
Searching...
No Matches
bloom_filter.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2014 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_BLOOM_FILTER_INCLUDED
#define ETL_BLOOM_FILTER_INCLUDED
#include "platform.h"
#include "binary.h"
#include "bitset.h"
#include "log.h"
#include "parameter_type.h"
#include "power.h"
#include "type_traits.h"
namespace etl
{
namespace private_bloom_filter
{
// Placeholder null hash for defaulted template parameters.
struct null_hash
{
template <typename T>
size_t operator()(T)
{
return 0;
}
};
} // namespace private_bloom_filter
//***************************************************************************
//***************************************************************************
template <size_t Desired_Width, typename THash1, typename THash2 = private_bloom_filter::null_hash,
typename THash3 = private_bloom_filter::null_hash>
class bloom_filter
{
private:
typedef typename etl::parameter_type<typename THash1::argument_type>::type parameter_t;
typedef private_bloom_filter::null_hash null_hash;
public:
enum
{
// Make the most efficient use of the bitset.
WIDTH = etl::bitset<Desired_Width>::Allocated_Bits
};
//***************************************************************************
//***************************************************************************
void clear()
{
flags.reset();
}
//***************************************************************************
//***************************************************************************
void add(parameter_t key)
{
flags.set(get_hash<THash1>(key));
if (!etl::is_same<THash2, null_hash>::value)
{
flags.set(get_hash<THash2>(key));
}
if (!etl::is_same<THash3, null_hash>::value)
{
flags.set(get_hash<THash3>(key));
}
}
//***************************************************************************
//***************************************************************************
bool exists(parameter_t key) const
{
bool exists1 = flags[get_hash<THash1>(key)];
bool exists2 = true;
bool exists3 = true;
// Do we have a second hash?
if (!etl::is_same<THash2, null_hash>::value)
{
exists2 = flags[get_hash<THash2>(key)];
}
// Do we have a third hash?
if (!etl::is_same<THash3, null_hash>::value)
{
exists3 = flags[get_hash<THash3>(key)];
}
return exists1 && exists2 && exists3;
}
//***************************************************************************
//***************************************************************************
size_t width() const
{
return WIDTH;
}
//***************************************************************************
//***************************************************************************
size_t usage() const
{
return (100 * count()) / WIDTH;
}
//***************************************************************************
//***************************************************************************
size_t count() const
{
return flags.count();
}
private:
//***************************************************************************
//***************************************************************************
template <typename THash>
size_t get_hash(parameter_t key) const
{
size_t hash = THash()(key);
// Fold the hash down to fit the width.
return fold_bits<size_t, etl::log2<WIDTH>::value>(hash);
}
etl::bitset<WIDTH> flags;
};
} // namespace etl
#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