Skip to content
Merged
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
4 changes: 2 additions & 2 deletions sampling/include/var_opt_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ var_opt_sketch<T, A> var_opt_sketch<T, A>::deserialize(const void* bytes, size_t
ptr += sd.deserialize(ptr, end_ptr - ptr, &(items.get()[h + 1]), r);
items.get_deleter().set_r(r); // serde didn't throw, so the items are now valid

return var_opt_sketch(k, h, (r > 0 ? 1 : 0), r, n, total_wt_r, rf, array_size, false,
return var_opt_sketch(k, h, 0, r, n, total_wt_r, rf, array_size, false,
std::move(items), std::move(weights), num_marks_in_h, std::move(marks), allocator);
}

Expand Down Expand Up @@ -635,7 +635,7 @@ var_opt_sketch<T, A> var_opt_sketch<T, A>::deserialize(std::istream& is, const S
if (!is.good())
throw std::runtime_error("error reading from std::istream");

return var_opt_sketch(k, h, (r > 0 ? 1 : 0), r, n, total_wt_r, rf, array_size, false,
return var_opt_sketch(k, h, 0, r, n, total_wt_r, rf, array_size, false,
std::move(items), std::move(weights), num_marks_in_h, std::move(marks), allocator);
}

Expand Down
65 changes: 65 additions & 0 deletions sampling/test/var_opt_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,69 @@ TEST_CASE("varopt sketch: estimate subset sum", "[var_opt_sketch]") {
REQUIRE(summary.estimate < total_weight); // exact mode, so know it must be strictly less
}

TEST_CASE("varopt sketch: update after deserialize in estimation mode", "[var_opt_sketch]") {
const uint32_t k = 10;
var_opt_sketch<int> sk(k);
double total_weight = 0.0;
for (int i = 1; i <= 100; ++i) {
sk.update(i, static_cast<double>(i));
total_weight += i;
}
REQUIRE(sk.get_num_samples() == k);

auto bytes = sk.serialize();
auto sk_bytes = var_opt_sketch<int>::deserialize(bytes.data(), bytes.size());
std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary);
sk.serialize(ss);
auto sk_stream = var_opt_sketch<int>::deserialize(ss);

const double light_weight = 0.5;
const double heavy_weight = 1e6;
total_weight += light_weight + heavy_weight;

for (var_opt_sketch<int>* d : {&sk_bytes, &sk_stream}) {
REQUIRE_NOTHROW(d->update(1000, light_weight));
REQUIRE_NOTHROW(d->update(1001, heavy_weight));
REQUIRE(d->get_n() == 102);
REQUIRE(d->get_num_samples() == k);
auto summary = d->estimate_subset_sum([](int) { return true; });
REQUIRE(summary.total_sketch_weight == Approx(total_weight).margin(EPS));
bool heavy_kept_exactly = false;
for (const auto& sample : *d) {
if (sample.first == 1001) heavy_kept_exactly = (sample.second == heavy_weight);
}
REQUIRE(heavy_kept_exactly);
}
}

TEST_CASE("varopt sketch: update after deserialize in exact mode", "[var_opt_sketch]") {
const uint32_t k = 10;
const uint32_t n = GENERATE(0u, 5u, 10u);
var_opt_sketch<int> sk(k);
double total_weight = 0.0;
for (uint32_t i = 1; i <= n; ++i) {
sk.update(static_cast<int>(i), static_cast<double>(i));
total_weight += i;
}
REQUIRE(sk.get_num_samples() == n);

auto bytes = sk.serialize();
auto sk_bytes = var_opt_sketch<int>::deserialize(bytes.data(), bytes.size());
std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary);
sk.serialize(ss);
auto sk_stream = var_opt_sketch<int>::deserialize(ss);

const double weight = 0.5;
total_weight += weight;
const uint32_t expected_samples = n < k ? n + 1 : k;

for (var_opt_sketch<int>* d : {&sk_bytes, &sk_stream}) {
REQUIRE_NOTHROW(d->update(1000, weight));
REQUIRE(d->get_n() == n + 1);
REQUIRE(d->get_num_samples() == expected_samples);
auto summary = d->estimate_subset_sum([](int) { return true; });
REQUIRE(summary.total_sketch_weight == Approx(total_weight).margin(EPS));
}
}

}
47 changes: 47 additions & 0 deletions sampling/test/var_opt_union_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,51 @@ TEST_CASE("varopt union: deserialize truncated non-empty preamble", "[var_opt_un
}
}

TEST_CASE("varopt union: update after deserialize in estimation mode", "[var_opt_union]") {
const uint32_t k = 10;
var_opt_union<int> u(k);
u.update(create_unweighted_sketch(k, 100));
u.update(create_unweighted_sketch(k, 100));

auto bytes = u.serialize();
auto u_bytes = var_opt_union<int>::deserialize(bytes.data(), bytes.size());
std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary);
u.serialize(ss);
auto u_stream = var_opt_union<int>::deserialize(ss);

for (var_opt_union<int>* du : {&u_bytes, &u_stream}) {
REQUIRE_NOTHROW(du->update(create_unweighted_sketch(k, 50)));
auto result = du->get_result();
REQUIRE(result.get_n() == 250);
REQUIRE(result.get_num_samples() == k);
auto summary = result.estimate_subset_sum([](int) { return true; });
REQUIRE(summary.total_sketch_weight == Approx(250.0).margin(EPS));
}
}

TEST_CASE("varopt union: update after deserialize in exact mode", "[var_opt_union]") {
const uint32_t k = 10;
const uint64_t extra = GENERATE(3u, 10u);
var_opt_union<int> u(k);
u.update(create_unweighted_sketch(k, 5));

auto bytes = u.serialize();
auto u_bytes = var_opt_union<int>::deserialize(bytes.data(), bytes.size());
std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary);
u.serialize(ss);
auto u_stream = var_opt_union<int>::deserialize(ss);

const uint64_t total = 5 + extra;
const uint32_t expected_samples = total < k ? static_cast<uint32_t>(total) : k;

for (var_opt_union<int>* du : {&u_bytes, &u_stream}) {
REQUIRE_NOTHROW(du->update(create_unweighted_sketch(k, extra)));
auto result = du->get_result();
REQUIRE(result.get_n() == total);
REQUIRE(result.get_num_samples() == expected_samples);
auto summary = result.estimate_subset_sum([](int) { return true; });
REQUIRE(summary.total_sketch_weight == Approx(static_cast<double>(total)).margin(EPS));
}
}

}
Loading