From 447494138078ab1b67c4080daa45769924d5117c Mon Sep 17 00:00:00 2001 From: Vlad Gluhovsky Date: Thu, 18 Jun 2015 15:29:05 +0200 Subject: [PATCH] BloomFilter refactoring: template class --- libwhisper/BloomFilter.cpp | 12 ++++--- libwhisper/BloomFilter.h | 65 +++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/libwhisper/BloomFilter.cpp b/libwhisper/BloomFilter.cpp index cff69c664..9680ded66 100644 --- a/libwhisper/BloomFilter.cpp +++ b/libwhisper/BloomFilter.cpp @@ -25,9 +25,11 @@ using namespace std; using namespace dev; using namespace dev::shh; +/* static unsigned const c_mask[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; -void TopicBloomFilter::addRaw(AbridgedTopic const& _h) +template +void TopicBloomFilterBase::addRaw(FixedHash const& _h) { *this |= _h; for (unsigned i = 0; i < CounterSize; ++i) @@ -40,7 +42,8 @@ void TopicBloomFilter::addRaw(AbridgedTopic const& _h) } } -void TopicBloomFilter::removeRaw(AbridgedTopic const& _h) +template +void TopicBloomFilterBase::removeRaw(FixedHash const& _h) { for (unsigned i = 0; i < CounterSize; ++i) if (isBitSet(_h, i)) @@ -53,12 +56,13 @@ void TopicBloomFilter::removeRaw(AbridgedTopic const& _h) } } -bool TopicBloomFilter::isBitSet(AbridgedTopic const& _h, unsigned _index) +template +bool TopicBloomFilterBase::isBitSet(FixedHash const& _h, unsigned _index) { unsigned iByte = _index / 8; unsigned iBit = _index % 8; return (_h[iByte] & c_mask[iBit]) != 0; } - +*/ diff --git a/libwhisper/BloomFilter.h b/libwhisper/BloomFilter.h index 0ca460805..cc236c255 100644 --- a/libwhisper/BloomFilter.h +++ b/libwhisper/BloomFilter.h @@ -28,30 +28,73 @@ namespace dev namespace shh { -class TopicBloomFilter: public AbridgedTopic +//enum { BloomSize = 4 }; + +template +class TopicBloomFilterBase: public FixedHash { public: - TopicBloomFilter() { init(); } - TopicBloomFilter(AbridgedTopic const& _h): AbridgedTopic(_h) { init(); } + TopicBloomFilterBase() { init(); } + TopicBloomFilterBase(FixedHash const& _h): FixedHash(_h) { init(); } - void addBloom(AbridgedTopic const& _h) { addRaw(_h.template bloomPart()); } - void removeBloom(AbridgedTopic const& _h) { removeRaw(_h.template bloomPart()); } - bool containsBloom(AbridgedTopic const& _h) const { return contains(_h.template bloomPart()); } + void addBloom(AbridgedTopic const& _h) { addRaw(_h.template bloomPart()); } + void removeBloom(AbridgedTopic const& _h) { removeRaw(_h.template bloomPart()); } + bool containsBloom(AbridgedTopic const& _h) const { return contains(_h.template bloomPart()); } - void addRaw(AbridgedTopic const& _h); - void removeRaw(AbridgedTopic const& _h); - bool containsRaw(AbridgedTopic const& _h) const { return contains(_h); } + void addRaw(FixedHash const& _h); + void removeRaw(FixedHash const& _h); + bool containsRaw(FixedHash const& _h) const { return contains(_h); } enum { BitsPerBloom = 3 }; private: void init() { for (unsigned i = 0; i < CounterSize; ++i) m_refCounter[i] = 0; } - static bool isBitSet(AbridgedTopic const& _h, unsigned _index); + static bool isBitSet(FixedHash const& _h, unsigned _index); - enum { CounterSize = 8 * TopicBloomFilter::size }; + enum { CounterSize = 8 * TopicBloomFilterBase::size }; std::array m_refCounter; }; +static unsigned const c_mask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 }; + +template +void TopicBloomFilterBase::addRaw(FixedHash const& _h) +{ + *this |= _h; + for (unsigned i = 0; i < CounterSize; ++i) + if (isBitSet(_h, i)) + { + if (m_refCounter[i] != numeric_limits::max()) + m_refCounter[i]++; + else + BOOST_THROW_EXCEPTION(Overflow()); + } +} + +template +void TopicBloomFilterBase::removeRaw(FixedHash const& _h) +{ + for (unsigned i = 0; i < CounterSize; ++i) + if (isBitSet(_h, i)) + { + if (m_refCounter[i]) + m_refCounter[i]--; + + if (!m_refCounter[i]) + (*this)[i / 8] &= ~c_mask[i % 8]; + } +} + +template +bool TopicBloomFilterBase::isBitSet(FixedHash const& _h, unsigned _index) +{ + unsigned iByte = _index / 8; + unsigned iBit = _index % 8; + return (_h[iByte] & c_mask[iBit]) != 0; +} + +using TopicBloomFilter = TopicBloomFilterBase<4>; + } }