Bluenet
5.7.0
Bluenet, firmware for nRF52 smart home devices
|
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: 02 Apr., 2021 License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed) More...
#include <cs_CuckooFilter.h>
Public Member Functions | |
bool | isValid () override |
bool | contains (const void *key, size_t keyLengthInBytes) override |
constexpr size_t | size () |
Total number of bytes this instance occypies. More... | |
bool | add (cuckoo_fingerprint_t finger, cuckoo_index_t bucketIndex) |
bool | remove (cuckoo_fingerprint_t finger, cuckoo_index_t bucketIndex) |
bool | contains (cuckoo_fingerprint_t finger, cuckoo_index_t bucketIndex) |
bool | add (cuckoo_key_t key, size_t keyLengthInBytes) |
bool | remove (cuckoo_key_t key, size_t keyLengthInBytes) |
cuckoo_compressed_fingerprint_t | getCompressedFingerprint (cuckoo_key_t key, size_t keyLengthInBytes) |
Reduces a key (element) to a compressed fingerprint, consisting of the fingerprint of the key and its associated primary position in the fingerprint array. More... | |
CuckooFilter (cuckoo_filter_data_t *data) | |
Wraps a data struct into a CuckooFilter object. More... | |
CuckooFilter () | |
Use this with loads of care, _data is never checked to be non-nullptr in this class. More... | |
void | clear () |
Memsets the bucket_array to 0x00 and sets victim to 0. More... | |
void | init (cuckoo_index_t bucketCount, cuckoo_index_t nestsPerBucket) |
Sets the size parameters and then clear()s this object so that the bucket_array is filled with precisely enough 0x00s to represent an empty filter. More... | |
constexpr size_t | bucketCount () |
Actual bucket count value may be bigger than a cuckoo_index_t can hold. More... | |
constexpr size_t | bufferSize () |
![]() | |
virtual | ~FilterInterface ()=default |
virtual bool | contains (const void *key, size_t keyLengthInBytes)=0 |
virtual size_t | size ()=0 |
virtual bool | isValid ()=0 |
Static Public Member Functions | |
static constexpr size_t | fingerprintCount (size_t bucketCount, cuckoo_index_t nestsPerBucket) |
static constexpr size_t | bufferSize (size_t bucketCount, cuckoo_index_t nestsPerBucket) |
Size of the byte buffer in bytes. More... | |
static constexpr size_t | size (cuckoo_index_t bucketCount, cuckoo_index_t nestsPerBucket) |
Total number of bytes a CuckooFilter with the given parameters would occupy. More... | |
Private Member Functions | |
cuckoo_extended_fingerprint_t | getExtendedFingerprint (cuckoo_key_t key, size_t keyLengthInBytes) |
Reduces a key (element) to an extended fingerprint, consisting of the fingerprint of the key and its associated positions in the fingerprint array. More... | |
cuckoo_extended_fingerprint_t | getExtendedFingerprint (cuckoo_fingerprint_t fingerprint, cuckoo_index_t bucketIndex) |
Expands a fingerprint and the index where it is placed into an extended fingerprint, by computing the alternative index in the fingerprint array. More... | |
cuckoo_fingerprint_t | filterHash () |
A crc16 hash of this filters current contents (_data). More... | |
cuckoo_fingerprint_t | hashToFingerprint (cuckoo_key_t key, size_t keyLengthInBytes) |
Hashes the given key into a fingerprint. More... | |
cuckoo_fingerprint_t | hashToBucket (cuckoo_key_t key, size_t keyLengthInBytes) |
Hashes the given key to obtain an (untruncated) bucket index. More... | |
cuckoo_fingerprint_t & | lookupFingerprint (cuckoo_index_t bucketIndex, cuckoo_index_t fingerIndex) |
Returns a reference to the fingerprint at the given coordinates. More... | |
bool | addFingerprintToBucket (cuckoo_fingerprint_t fingerprint, cuckoo_index_t bucketIndex) |
Returns true if there was an empty space in the bucket and placement was successful, returns false otherwise. More... | |
bool | removeFingerprintFromBucket (cuckoo_fingerprint_t fingerprint, cuckoo_index_t bucketIndex) |
Returns true if found and removed, returns false if not found. More... | |
bool | move (cuckoo_extended_fingerprint_t entryToInsert) |
bool | add (cuckoo_extended_fingerprint_t efp) |
bool | remove (cuckoo_extended_fingerprint_t efp) |
bool | contains (cuckoo_extended_fingerprint_t efp) |
Private Attributes | |
cuckoo_filter_data_t * | _data |
Static Private Attributes | |
static constexpr size_t | MAX_KICK_ATTEMPTS = 100 |
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: 02 Apr., 2021 License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)
This library is forked from the public github repository and initially added to bluenet on 19-02-2021. Code has been extensively refactored for idiomatic use of C++ and many implementation details have changed e.g. to fix implicit narrowing/widening of integers, large recursion and type punning in allocations.
Those changes have been made by the Crownstone Team and fall under the project license mentioned above.
The original code and the extent to which is required by applicable law is left under its original license included below and is attributed to the original author Jonah H. Harris jonah.nosp@m..har.nosp@m.ris@g.nosp@m.mail.nosp@m..com.
The MIT License
Copyright (c) 2015 Jonah H. Harris
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. A CuckooFilter is a probabilistic datatype is made for approximate membership queries.
This implementation consists of the POD data structure cuckoo_filter_data_t
and a 'view class' Cuckoofilter
, which is used to manipulate the data struct.
The cuckoo_filter_data_t struct is not aware of its size. That must be managed by the owning scope, which can query the required size in bytes for the given amount of fingerprints through the CuckooFilter class.
The 'elements' that one can add
to a CuckooFilter
are of type cuckoo_key_t
which are simply sequences of bytes. To check if an element is contained in a filter, refer to the contains
methods.
CuckooFilter data contents are stored in an array of fingerprints which is segmented into 'buckets'. These buckets are used for indexing, and although for an element, the bucket index and fingerprint are deterministic, the index inside the bucket is dependent on order of insertion.
More details can be found in https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf
. "Cuckoo Filter: Better Than Bloom" by Bin Fan, Dave Andersen, and Michael Kaminsky
|
inline |
Wraps a data struct into a CuckooFilter object.
|
inline |
Use this with loads of care, _data is never checked to be non-nullptr in this class.
|
private |
|
inline |
|
inline |
|
private |
Returns true if there was an empty space in the bucket and placement was successful, returns false otherwise.
Does not check for duplicates.
|
inlineconstexpr |
Actual bucket count value may be bigger than a cuckoo_index_t can hold.
|
inlineconstexpr |
|
inlinestaticconstexpr |
Size of the byte buffer in bytes.
void CuckooFilter::clear | ( | ) |
Memsets the bucket_array to 0x00 and sets victim to 0.
No changes are made to size.
|
inlineoverridevirtual |
Implements FilterInterface.
|
private |
|
inline |
|
private |
A crc16 hash of this filters current contents (_data).
|
inlinestaticconstexpr |
cuckoo_compressed_fingerprint_t CuckooFilter::getCompressedFingerprint | ( | cuckoo_key_t | key, |
size_t | keyLengthInBytes | ||
) |
Reduces a key (element) to a compressed fingerprint, consisting of the fingerprint of the key and its associated primary position in the fingerprint array.
|
private |
Expands a fingerprint and the index where it is placed into an extended fingerprint, by computing the alternative index in the fingerprint array.
Note: bucketCount must be a power of two. (which is validated at construction/init)
|
private |
Reduces a key (element) to an extended fingerprint, consisting of the fingerprint of the key and its associated positions in the fingerprint array.
Note: bucketCount must be a power of two. (which is validated at construction/init)
|
private |
Hashes the given key to obtain an (untruncated) bucket index.
|
private |
Hashes the given key into a fingerprint.
void CuckooFilter::init | ( | cuckoo_index_t | bucketCount, |
cuckoo_index_t | nestsPerBucket | ||
) |
Sets the size parameters and then clear()s this object so that the bucket_array is filled with precisely enough 0x00s to represent an empty filter.
[in] | bucket_count_log2 | number of buckets allocated will be rounde up to nearest power of 2 because of theoretical requirements. |
|
overridevirtual |
Implements FilterInterface.
|
inlineprivate |
Returns a reference to the fingerprint at the given coordinates.
|
private |
|
private |
|
inline |
|
inline |
|
private |
Returns true if found and removed, returns false if not found.
|
inlineconstexprvirtual |
Total number of bytes this instance occypies.
Use this function instead of sizeof for this class to take the buffer into account.
Implements FilterInterface.
|
inlinestaticconstexpr |
Total number of bytes a CuckooFilter with the given parameters would occupy.
As per cppreference.com: "sizeof, and the assignment operator ignore the flexible array member." https://en.cppreference.com/w/c/language/struct
|
private |
|
staticconstexprprivate |