Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fi/include/frequent_items_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ class frequent_items_sketch {
* There may be items omitted from the set with true frequencies greater than the
* threshold (false negatives).</p>
*
* <p>If the threshold is lower than get_maximum_error(), then get_maximum_error() will be used instead.
* For integer weights, it has no effect on NO_FALSE_NEGATIVES.</p>
*
* @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
Expand Down
2 changes: 2 additions & 0 deletions fi/include/frequent_items_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ auto frequent_items_sketch<T, W, H, E, A>::get_frequent_items(frequent_items_err
template<typename T, typename W, typename H, typename E, typename A>
auto frequent_items_sketch<T, W, H, E, A>::get_frequent_items(frequent_items_error_type err_type, W threshold) const -> vector_row {
vector_row items(map.get_allocator());
// Below the offset, a listed item can be less frequent than a dropped item.
threshold = std::max(threshold, offset);
for (auto it: map) {
const W lb = it.second;
const W ub = it.second + offset;
Expand Down
31 changes: 31 additions & 0 deletions fi/test/frequent_items_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ 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<size_t>(v); }
};
frequent_items_sketch<int64_t, uint64_t, identity_hash> 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(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES).empty());

items = sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 0);
REQUIRE(items.size() == 1);
REQUIRE(items[0].get_item() == 8);

// thresholds at or above the maximum error are used as given
sketch.update(8); // item 8 now has lower bound 2
REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 1).size() == 1);
REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 2).empty());
REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 2).size() == 1);
REQUIRE(sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 3).empty());

// any threshold below the maximum error is raised, not only 0
frequent_items_sketch<int64_t, uint64_t, identity_hash> sketch2(3);
for (int64_t i = 1; i <= 8; ++i) sketch2.update(i, 2);
REQUIRE(sketch2.get_maximum_error() == 2);
REQUIRE(sketch2.get_frequent_items(frequent_items_error_type::NO_FALSE_POSITIVES, 1).empty());
}

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:
Expand Down