Skip to content

Commit bbe4108

Browse files
petertoddsipa
authored andcommitted
Add uint256 support to CRollingBloomFilter
1 parent 08e9c57 commit bbe4108

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/bloom.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
234234
}
235235
}
236236

237+
void CRollingBloomFilter::insert(const uint256& hash)
238+
{
239+
if (nInsertions == 0) {
240+
b1.clear();
241+
} else if (nInsertions == nBloomSize / 2) {
242+
b2.clear();
243+
}
244+
b1.insert(hash);
245+
b2.insert(hash);
246+
if (++nInsertions == nBloomSize) {
247+
nInsertions = 0;
248+
}
249+
}
250+
237251
bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
238252
{
239253
if (nInsertions < nBloomSize / 2) {
@@ -242,6 +256,14 @@ bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
242256
return b1.contains(vKey);
243257
}
244258

259+
bool CRollingBloomFilter::contains(const uint256& hash) const
260+
{
261+
if (nInsertions < nBloomSize / 2) {
262+
return b2.contains(hash);
263+
}
264+
return b1.contains(hash);
265+
}
266+
245267
void CRollingBloomFilter::clear()
246268
{
247269
b1.clear();

src/bloom.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ class CRollingBloomFilter
114114
CRollingBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak);
115115

116116
void insert(const std::vector<unsigned char>& vKey);
117+
void insert(const uint256& hash);
117118
bool contains(const std::vector<unsigned char>& vKey) const;
119+
bool contains(const uint256& hash) const;
118120

119121
void clear();
120122

0 commit comments

Comments
 (0)