You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.9 KiB
71 lines
1.9 KiB
/*
|
|
This file is part of cpp-ethereum.
|
|
|
|
cpp-ethereum is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
cpp-ethereum is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
/** @file BloomFilter.cpp
|
|
* @author Vladislav Gluhovsky <vlad@ethdev.com>
|
|
* @date June 2015
|
|
*/
|
|
|
|
#include "BloomFilter.h"
|
|
|
|
using namespace std;
|
|
using namespace dev;
|
|
using namespace dev::shh;
|
|
|
|
bool BloomFilter::matches(AbridgedTopic const& _t) const
|
|
{
|
|
static unsigned const c_PerfectMatch = ~unsigned(0);
|
|
unsigned topic = AbridgedTopic::Arith(_t).convert_to<unsigned>();
|
|
unsigned matchingBits = m_filter & topic;
|
|
matchingBits |= ~topic;
|
|
return (c_PerfectMatch == matchingBits);
|
|
}
|
|
|
|
void SharedBloomFilter::add(AbridgedTopic const& _t)
|
|
{
|
|
unsigned const topic = AbridgedTopic::Arith(_t).convert_to<unsigned>();
|
|
m_filter |= topic;
|
|
|
|
unsigned nextBit = 1;
|
|
for (unsigned i = 0; i < ArrSize; ++i, nextBit <<= 1)
|
|
if (topic & nextBit)
|
|
if (m_refCounter[i] != numeric_limits<unsigned short>::max())
|
|
m_refCounter[i]++;
|
|
//else: overflow
|
|
|
|
// in order to encounter overflow, you have to set 65536 filters simultaneously.
|
|
// we assume, it will never happen.
|
|
}
|
|
|
|
void SharedBloomFilter::remove(AbridgedTopic const& _t)
|
|
{
|
|
unsigned const topic = AbridgedTopic::Arith(_t).convert_to<unsigned>();
|
|
|
|
unsigned nextBit = 1;
|
|
for (unsigned i = 0; i < ArrSize; ++i, nextBit <<= 1)
|
|
if (topic & nextBit)
|
|
{
|
|
if (m_refCounter[i])
|
|
m_refCounter[i]--;
|
|
|
|
if (!m_refCounter[i])
|
|
m_filter &= ~nextBit;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|