/* 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 . */ /** @file BloomFilter.cpp * @author Vladislav Gluhovsky * @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_match = ~unsigned(0); unsigned topic = AbridgedTopic::Arith(_t).convert_to(); unsigned matchingBits = m_filter & topic; matchingBits |= ~topic; return (c_match == matchingBits); } void SharedBloomFilter::add(AbridgedTopic const& _t) { unsigned const topic = AbridgedTopic::Arith(_t).convert_to(); m_filter |= topic; unsigned nextBit = 1; for (unsigned i = 0; i < ArrSize; ++i, nextBit <<= 1) if (topic & nextBit) if (m_refCounter[i] != numeric_limits::max()) m_refCounter[i]++; //else: overflow // in order to encounter overflow, you have to set at least 65536 filters simultaneously. // even then, the problem will only arise after at least 65536 filters will be be removed. // we assume, it will never happen. } void SharedBloomFilter::remove(AbridgedTopic const& _t) { unsigned const topic = AbridgedTopic::Arith(_t).convert_to(); 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; } }