From bbe41088c61f2ad328766e851ffe6169aa80935a Mon Sep 17 00:00:00 2001 From: Peter Todd Date: Fri, 17 Jul 2015 06:42:43 -0400 Subject: [PATCH] Add uint256 support to CRollingBloomFilter --- src/bloom.cpp | 22 ++++++++++++++++++++++ src/bloom.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/bloom.cpp b/src/bloom.cpp index 36cba491c..e15bc32f9 100644 --- a/src/bloom.cpp +++ b/src/bloom.cpp @@ -234,6 +234,20 @@ void CRollingBloomFilter::insert(const std::vector& vKey) } } +void CRollingBloomFilter::insert(const uint256& hash) +{ + if (nInsertions == 0) { + b1.clear(); + } else if (nInsertions == nBloomSize / 2) { + b2.clear(); + } + b1.insert(hash); + b2.insert(hash); + if (++nInsertions == nBloomSize) { + nInsertions = 0; + } +} + bool CRollingBloomFilter::contains(const std::vector& vKey) const { if (nInsertions < nBloomSize / 2) { @@ -242,6 +256,14 @@ bool CRollingBloomFilter::contains(const std::vector& vKey) const return b1.contains(vKey); } +bool CRollingBloomFilter::contains(const uint256& hash) const +{ + if (nInsertions < nBloomSize / 2) { + return b2.contains(hash); + } + return b1.contains(hash); +} + void CRollingBloomFilter::clear() { b1.clear(); diff --git a/src/bloom.h b/src/bloom.h index bb17f59c8..0daa3728e 100644 --- a/src/bloom.h +++ b/src/bloom.h @@ -114,7 +114,9 @@ public: CRollingBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak); void insert(const std::vector& vKey); + void insert(const uint256& hash); bool contains(const std::vector& vKey) const; + bool contains(const uint256& hash) const; void clear();