Skip to content

Commit fff14b4

Browse files
authored
Merge pull request #495 from proost/fix-allow-positive-weight-only
fix: allow positive weight only
2 parents e3aba71 + 5e20ad0 commit fff14b4

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

sampling/include/var_opt_sketch.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class var_opt_sketch {
272272
typedef typename std::allocator_traits<A>::template rebind_alloc<double> AllocDouble;
273273
typedef typename std::allocator_traits<A>::template rebind_alloc<bool> AllocBool;
274274

275-
static const uint32_t MIN_LG_ARR_ITEMS = 3;
275+
static const uint32_t MIN_LG_ARR_ITEMS = 4;
276276

277277
static const uint8_t PREAMBLE_LONGS_EMPTY = 1;
278278
static const uint8_t PREAMBLE_LONGS_WARMUP = 3;

sampling/include/var_opt_sketch_impl.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,12 +772,11 @@ string<A> var_opt_sketch<T, A>::items_to_string(bool print_gap) const {
772772
template<typename T, typename A>
773773
template<typename O>
774774
void var_opt_sketch<T, A>::update(O&& item, double weight, bool mark) {
775-
if (weight < 0.0 || std::isnan(weight) || std::isinf(weight)) {
776-
throw std::invalid_argument("Item weights must be nonnegative and finite. Found: "
775+
if (weight <= 0.0 || std::isnan(weight) || std::isinf(weight)) {
776+
throw std::invalid_argument("Item weights must be positive and finite. Found: "
777777
+ std::to_string(weight));
778-
} else if (weight == 0.0) {
779-
return;
780778
}
779+
781780
++n_;
782781

783782
if (r_ == 0) {

sampling/test/var_opt_sketch_test.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,17 @@ TEST_CASE("varopt sketch: non-empty degenerate sketch", "[var_opt_sketch]") {
178178

179179
TEST_CASE("varopt sketch: invalid weight", "[var_opt_sketch]") {
180180
var_opt_sketch<std::string> sk(100, resize_factor::X2);
181-
REQUIRE_THROWS_AS(sk.update("invalid_weight", -1.0), std::invalid_argument);
182181

183-
// should not throw but sketch should still be empty
184-
sk.update("zero weight", 0.0);
185-
REQUIRE(sk.is_empty());
182+
// Negative
183+
REQUIRE_THROWS_AS(sk.update("invalid_weight", -1.0), std::invalid_argument);
184+
// Zero
185+
REQUIRE_THROWS_AS(sk.update("zero_weight", 0.0), std::invalid_argument);
186+
// NaN
187+
REQUIRE_THROWS_AS(sk.update("NaN_weight", std::numeric_limits<double>::quiet_NaN()), std::invalid_argument);
188+
// +Inf
189+
REQUIRE_THROWS_AS(sk.update("positive_infinity", std::numeric_limits<double>::infinity()), std::invalid_argument);
190+
// -Inf
191+
REQUIRE_THROWS_AS(sk.update("negative_infinity", -std::numeric_limits<double>::infinity()), std::invalid_argument);
186192
}
187193

188194
TEST_CASE("varopt sketch: corrupt serialized weight", "[var_opt_sketch]") {

0 commit comments

Comments
 (0)