Skip to content

Commit ba7905f

Browse files
committed
Merge #40: Add support for 0 capacity sketches
5487f1f Add support for 0 capacity sketches (Pieter Wuille) Pull request description: Make `minisketch_create` return a functioning `minisketch*` when capacity=0 is passed, to remove the need for dealing with this edge case from the user. See bitcoin/bitcoin#21859 (comment) for discussion. ACKs for top commit: gmaxwell: utACK 5487f1f Tree-SHA512: 3e325877d3d02dd80450bf64aa6792faa9ef79008f56a39f3be595345b5a219538eee4b25fd8025fb582e60079933fc2f35cc22d36713aff3de0d3357320efb0
2 parents 2bec6d9 + 5487f1f commit ba7905f

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

include/minisketch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ int minisketch_implementation_supported(uint32_t bits, uint32_t implementation);
4242

4343
/** Construct a sketch for a given element size, implementation and capacity.
4444
*
45-
* If the combination of `bits` and `implementation` is unavailable, or if
46-
* `capacity` is 0, NULL is returned. If minisketch_implementation_supported
45+
* If the combination of `bits` and `implementation` is unavailable, or when
46+
* OOM occurs, NULL is returned. If minisketch_implementation_supported
4747
* returns 1 for the specified bits and implementation, this will always succeed
4848
* (except when allocation fails).
4949
*

src/minisketch.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,6 @@ int minisketch_implementation_supported(uint32_t bits, uint32_t implementation)
337337
}
338338

339339
minisketch* minisketch_create(uint32_t bits, uint32_t implementation, size_t capacity) {
340-
if (capacity == 0) {
341-
return nullptr;
342-
}
343340
try {
344341
Sketch* sketch = Construct(bits, implementation);
345342
if (sketch) {

src/test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void TestExhaustive(uint32_t bits, size_t capacity) {
123123
/** Test properties of sketches with random elements put in. */
124124
void TestRandomized(uint32_t bits, size_t max_capacity, size_t iter) {
125125
std::random_device rnd;
126-
std::uniform_int_distribution<uint64_t> capacity_dist(1, std::min<uint64_t>(std::numeric_limits<uint64_t>::max() >> (64 - bits), max_capacity));
126+
std::uniform_int_distribution<uint64_t> capacity_dist(0, std::min<uint64_t>(std::numeric_limits<uint64_t>::max() >> (64 - bits), max_capacity));
127127
std::uniform_int_distribution<uint64_t> element_dist(1, std::numeric_limits<uint64_t>::max() >> (64 - bits));
128128
std::uniform_int_distribution<uint64_t> rand64(0, std::numeric_limits<uint64_t>::max());
129129
std::uniform_int_distribution<int64_t> size_offset_dist(-3, 3);
@@ -298,8 +298,11 @@ int main(int argc, char** argv) {
298298
TestRandomized(j, 4096, test_complexity / j);
299299
}
300300

301-
for (int weight = 2; weight <= 40; ++weight) {
302-
for (int bits = 2; bits <= 32 && bits <= weight; ++bits) {
301+
// Test capacity==0 together with all field sizes, and then
302+
// all combinations of bits and capacity up to a certain bits*capacity,
303+
// depending on test_complexity.
304+
for (int weight = 0; weight <= 40; ++weight) {
305+
for (int bits = 2; weight == 0 ? bits <= 64 : (bits <= 32 && bits <= weight); ++bits) {
303306
int capacity = weight / bits;
304307
if (capacity * bits != weight) continue;
305308
TestExhaustive(bits, capacity);

0 commit comments

Comments
 (0)