diff --git a/fi/include/frequent_items_sketch.hpp b/fi/include/frequent_items_sketch.hpp index 2dd337e5..a1b1a6db 100644 --- a/fi/include/frequent_items_sketch.hpp +++ b/fi/include/frequent_items_sketch.hpp @@ -244,6 +244,8 @@ class frequent_items_sketch { * There may be items omitted from the set with true frequencies greater than the * threshold (false negatives).

* + *

If the threshold is lower than get_maximum_error(), then get_maximum_error() will be used instead.

+ * * @param err_type determines whether no false positives or no false negatives are desired. * @param threshold to include items in the result list * @return an array of frequent items diff --git a/fi/include/frequent_items_sketch_impl.hpp b/fi/include/frequent_items_sketch_impl.hpp index ae77ae77..13ec1dc5 100644 --- a/fi/include/frequent_items_sketch_impl.hpp +++ b/fi/include/frequent_items_sketch_impl.hpp @@ -158,6 +158,7 @@ auto frequent_items_sketch::get_frequent_items(frequent_items_err template auto frequent_items_sketch::get_frequent_items(frequent_items_error_type err_type, W threshold) const -> vector_row { vector_row items(map.get_allocator()); + threshold = std::max(threshold, offset); for (auto it: map) { const W lb = it.second; const W ub = it.second + offset; diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp index b2035439..d6efa7ab 100644 --- a/fi/test/frequent_items_sketch_test.cpp +++ b/fi/test/frequent_items_sketch_test.cpp @@ -133,6 +133,25 @@ TEST_CASE("frequent items: estimation mode", "[frequent_items_sketch]") { REQUIRE(12 >= items.size()); // but not more than 12 items } +TEST_CASE("frequent items: threshold below the maximum error is raised to it", "[frequent_items_sketch]") { + struct identity_hash { + size_t operator()(int64_t v) const { return static_cast(v); } + }; + frequent_items_sketch sketch(3); + for (int64_t i = 1; i <= 8; ++i) sketch.update(i); + REQUIRE(sketch.get_maximum_error() == 1); // capacity is 6, so the 7th distinct item purges every count of 1 + + // threshold 0 is raised to 1, so item 8 (lower bound 1) is not returned + auto items = sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 0); + REQUIRE(items.empty()); + REQUIRE(items.size() == sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES).size()); + + // item 8 has upper bound 2, which is above 1 + items = sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 0); + REQUIRE(items.size() == 1); + REQUIRE(items[0].get_item() == 8); +} + TEST_CASE("frequent items: purge clearing all counters is not empty", "[frequent_items_sketch]") { // lg_k=8 -> capacity 192; the 193rd distinct item triggers a purge whose // median (1) wipes every counter. The sketch still holds observations: