This is an automated email from the ASF dual-hosted git repository. alsay pushed a commit to branch cleanup_warnings in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git
commit 32942249ad6948759b0b5095a1a24f18cc67e64b Author: Alexander Saydakov <[email protected]> AuthorDate: Thu Apr 1 12:12:50 2021 -0700 types and casts --- kll/include/kll_helper.hpp | 9 ++-- kll/include/kll_helper_impl.hpp | 12 ++--- kll/include/kll_quantile_calculator.hpp | 2 +- kll/include/kll_quantile_calculator_impl.hpp | 10 ++-- kll/include/kll_sketch.hpp | 2 +- kll/include/kll_sketch_impl.hpp | 4 +- kll/test/kll_sketch_test.cpp | 79 ++++++++++++++-------------- 7 files changed, 59 insertions(+), 59 deletions(-) diff --git a/kll/include/kll_helper.hpp b/kll/include/kll_helper.hpp index 4857f51..c026538 100644 --- a/kll/include/kll_helper.hpp +++ b/kll/include/kll_helper.hpp @@ -26,7 +26,8 @@ namespace datasketches { -static std::independent_bits_engine<std::mt19937, 1, uint32_t> random_bit(std::chrono::system_clock::now().time_since_epoch().count()); +static std::independent_bits_engine<std::mt19937, 1, uint32_t> + random_bit(static_cast<uint32_t>(std::chrono::system_clock::now().time_since_epoch().count())); #ifdef KLL_VALIDATION extern uint32_t kll_next_offset; @@ -46,9 +47,9 @@ class kll_helper { static inline uint8_t floor_of_log2_of_fraction(uint64_t numer, uint64_t denom); static inline uint8_t ub_on_num_levels(uint64_t n); static inline uint32_t compute_total_capacity(uint16_t k, uint8_t m, uint8_t num_levels); - static inline uint32_t level_capacity(uint16_t k, uint8_t numLevels, uint8_t height, uint8_t min_wid); - static inline uint32_t int_cap_aux(uint16_t k, uint8_t depth); - static inline uint32_t int_cap_aux_aux(uint16_t k, uint8_t depth); + static inline uint16_t level_capacity(uint16_t k, uint8_t numLevels, uint8_t height, uint8_t min_wid); + static inline uint16_t int_cap_aux(uint16_t k, uint8_t depth); + static inline uint16_t int_cap_aux_aux(uint16_t k, uint8_t depth); static inline uint64_t sum_the_sample_weights(uint8_t num_levels, const uint32_t* levels); /* diff --git a/kll/include/kll_helper_impl.hpp b/kll/include/kll_helper_impl.hpp index 011a78d..caa611b 100644 --- a/kll/include/kll_helper_impl.hpp +++ b/kll/include/kll_helper_impl.hpp @@ -55,28 +55,28 @@ uint32_t kll_helper::compute_total_capacity(uint16_t k, uint8_t m, uint8_t num_l return total; } -uint32_t kll_helper::level_capacity(uint16_t k, uint8_t numLevels, uint8_t height, uint8_t min_wid) { +uint16_t kll_helper::level_capacity(uint16_t k, uint8_t numLevels, uint8_t height, uint8_t min_wid) { if (height >= numLevels) throw std::invalid_argument("height >= numLevels"); const uint8_t depth = numLevels - height - 1; - return std::max((uint32_t) min_wid, int_cap_aux(k, depth)); + return std::max<uint16_t>(min_wid, int_cap_aux(k, depth)); } -uint32_t kll_helper::int_cap_aux(uint16_t k, uint8_t depth) { +uint16_t kll_helper::int_cap_aux(uint16_t k, uint8_t depth) { if (depth > 60) throw std::invalid_argument("depth > 60"); if (depth <= 30) return int_cap_aux_aux(k, depth); const uint8_t half = depth / 2; const uint8_t rest = depth - half; - const uint32_t tmp = int_cap_aux_aux(k, half); + const uint16_t tmp = int_cap_aux_aux(k, half); return int_cap_aux_aux(tmp, rest); } -uint32_t kll_helper::int_cap_aux_aux(uint16_t k, uint8_t depth) { +uint16_t kll_helper::int_cap_aux_aux(uint16_t k, uint8_t depth) { if (depth > 30) throw std::invalid_argument("depth > 30"); const uint64_t twok = k << 1; // for rounding, we pre-multiply by 2 const uint64_t tmp = (uint64_t) (((uint64_t) twok << depth) / powers_of_three[depth]); const uint64_t result = (tmp + 1) >> 1; // then here we add 1 and divide by 2 if (result > k) throw std::logic_error("result > k"); - return result; + return static_cast<uint16_t>(result); } uint64_t kll_helper::sum_the_sample_weights(uint8_t num_levels, const uint32_t* levels) { diff --git a/kll/include/kll_quantile_calculator.hpp b/kll/include/kll_quantile_calculator.hpp index 5114399..86dfe98 100644 --- a/kll/include/kll_quantile_calculator.hpp +++ b/kll/include/kll_quantile_calculator.hpp @@ -45,7 +45,7 @@ class kll_quantile_calculator { T approximately_answer_positional_query(uint64_t pos) const; void convert_to_preceding_cummulative(); uint32_t chunk_containing_pos(uint64_t pos) const; - uint32_t search_for_chunk_containing_pos(uint64_t pos, uint32_t l, uint32_t r) const; + uint32_t search_for_chunk_containing_pos(uint64_t pos, uint64_t l, uint64_t r) const; static void merge_sorted_blocks(Container& entries, const uint32_t* levels, uint8_t num_levels, uint32_t num_items); static void merge_sorted_blocks_direct(Container& orig, Container& temp, const uint32_t* levels, uint8_t starting_level, uint8_t num_levels); static void merge_sorted_blocks_reversed(Container& orig, Container& temp, const uint32_t* levels, uint8_t starting_level, uint8_t num_levels); diff --git a/kll/include/kll_quantile_calculator_impl.hpp b/kll/include/kll_quantile_calculator_impl.hpp index ff6e547..5bd4294 100644 --- a/kll/include/kll_quantile_calculator_impl.hpp +++ b/kll/include/kll_quantile_calculator_impl.hpp @@ -35,7 +35,7 @@ n_(n), levels_(num_levels + 1, 0, allocator), entries_(allocator) const uint32_t num_items = levels[num_levels] - levels[0]; entries_.reserve(num_items); populate_from_sketch(items, levels, num_levels); - merge_sorted_blocks(entries_, levels_.data(), levels_.size() - 1, num_items); + merge_sorted_blocks(entries_, levels_.data(), static_cast<uint8_t>(levels_.size()) - 1, num_items); if (!is_sorted(entries_.begin(), entries_.end(), compare_pair_by_first<C>())) throw std::logic_error("entries must be sorted"); convert_to_preceding_cummulative(); } @@ -89,7 +89,7 @@ void kll_quantile_calculator<T, C, A>::convert_to_preceding_cummulative() { template <typename T, typename C, typename A> uint64_t kll_quantile_calculator<T, C, A>::pos_of_phi(double phi, uint64_t n) { - const uint64_t pos = std::floor(phi * n); + const uint64_t pos = static_cast<uint64_t>(std::floor(phi * n)); return (pos == n) ? n - 1 : pos; } @@ -102,11 +102,11 @@ uint32_t kll_quantile_calculator<T, C, A>::chunk_containing_pos(uint64_t pos) co } template <typename T, typename C, typename A> -uint32_t kll_quantile_calculator<T, C, A>::search_for_chunk_containing_pos(uint64_t pos, uint32_t l, uint32_t r) const { +uint32_t kll_quantile_calculator<T, C, A>::search_for_chunk_containing_pos(uint64_t pos, uint64_t l, uint64_t r) const { if (l + 1 == r) { - return l; + return static_cast<uint32_t>(l); } - const uint32_t m(l + (r - l) / 2); + const uint64_t m = l + (r - l) / 2; if (entries_[m].second <= pos) { return search_for_chunk_containing_pos(pos, m, r); } diff --git a/kll/include/kll_sketch.hpp b/kll/include/kll_sketch.hpp index bbca76f..6447a26 100644 --- a/kll/include/kll_sketch.hpp +++ b/kll/include/kll_sketch.hpp @@ -296,7 +296,7 @@ class kll_sketch { * * @return array of approximations to the given number of evenly-spaced fractional ranks. */ - std::vector<T, A> get_quantiles(size_t num) const; + std::vector<T, A> get_quantiles(uint32_t num) const; /** * Returns an approximation to the normalized (fractional) rank of the given value from 0 to 1, diff --git a/kll/include/kll_sketch_impl.hpp b/kll/include/kll_sketch_impl.hpp index 9caf14e..0115d75 100644 --- a/kll/include/kll_sketch_impl.hpp +++ b/kll/include/kll_sketch_impl.hpp @@ -303,7 +303,7 @@ std::vector<T, A> kll_sketch<T, C, S, A>::get_quantiles(const double* fractions, } template<typename T, typename C, typename S, typename A> -std::vector<T, A> kll_sketch<T, C, S, A>::get_quantiles(size_t num) const { +std::vector<T, A> kll_sketch<T, C, S, A>::get_quantiles(uint32_t num) const { if (is_empty()) return std::vector<T, A>(allocator_); if (num == 0) { throw std::invalid_argument("num must be > 0"); @@ -555,7 +555,7 @@ kll_sketch<T, C, S, A> kll_sketch<T, C, S, A>::deserialize(const void* bytes, si check_preamble_ints(preamble_ints, flags_byte); check_serial_version(serial_version); check_family_id(family_id); - ensure_minimum_memory(size, 1 << preamble_ints); + ensure_minimum_memory(size, 1ULL << preamble_ints); const bool is_empty(flags_byte & (1 << flags::IS_EMPTY)); if (is_empty) return kll_sketch<T, C, S, A>(k, allocator); diff --git a/kll/test/kll_sketch_test.cpp b/kll/test/kll_sketch_test.cpp index b51f98b..f7afcbe 100644 --- a/kll/test/kll_sketch_test.cpp +++ b/kll/test/kll_sketch_test.cpp @@ -70,7 +70,6 @@ TEST_CASE("kll sketch", "[kll_sketch]") { REQUIRE(sketch.get_PMF(split_points, 1).size() == 0); REQUIRE(sketch.get_CDF(split_points, 1).size() == 0); - int count = 0; for (auto it: sketch) { (void) it; // to suppress "unused" warning FAIL("should be no iterations over an empty sketch"); @@ -85,13 +84,13 @@ TEST_CASE("kll sketch", "[kll_sketch]") { SECTION("one item") { kll_float_sketch sketch(200, 0); - sketch.update(1); + sketch.update(1.0f); REQUIRE_FALSE(sketch.is_empty()); REQUIRE_FALSE(sketch.is_estimation_mode()); REQUIRE(sketch.get_n() == 1); REQUIRE(sketch.get_num_retained() == 1); - REQUIRE(sketch.get_rank(1) == 0.0); - REQUIRE(sketch.get_rank(2) == 1.0); + REQUIRE(sketch.get_rank(1.0f) == 0.0); + REQUIRE(sketch.get_rank(2.0f) == 1.0); REQUIRE(sketch.get_min_value() == 1.0); REQUIRE(sketch.get_max_value() == 1.0); REQUIRE(sketch.get_quantile(0.5) == 1.0); @@ -115,7 +114,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { sketch.update(std::numeric_limits<float>::quiet_NaN()); REQUIRE(sketch.is_empty()); - sketch.update(0.0); + sketch.update(0); sketch.update(std::numeric_limits<float>::quiet_NaN()); REQUIRE(sketch.get_n() == 1); } @@ -124,7 +123,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { kll_float_sketch sketch(200, 0); const uint32_t n = 200; for (uint32_t i = 0; i < n; i++) { - sketch.update(i); + sketch.update(static_cast<float>(i)); REQUIRE(sketch.get_n() == i + 1); } REQUIRE_FALSE(sketch.is_empty()); @@ -144,7 +143,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { for (uint32_t i = 0; i < n; i++) { const double trueRank = (double) i / n; - REQUIRE(sketch.get_rank(i) == trueRank); + REQUIRE(sketch.get_rank(static_cast<float>(i)) == trueRank); } // the alternative method must produce the same result @@ -157,16 +156,16 @@ TEST_CASE("kll sketch", "[kll_sketch]") { SECTION("10 items") { kll_float_sketch sketch(200, 0); - sketch.update(1); - sketch.update(2); - sketch.update(3); - sketch.update(4); - sketch.update(5); - sketch.update(6); - sketch.update(7); - sketch.update(8); - sketch.update(9); - sketch.update(10); + sketch.update(1.0f); + sketch.update(2.0f); + sketch.update(3.0f); + sketch.update(4.0f); + sketch.update(5.0f); + sketch.update(6.0f); + sketch.update(7.0f); + sketch.update(8.0f); + sketch.update(9.0f); + sketch.update(10.0f); REQUIRE(sketch.get_quantile(0) == 1.0); REQUIRE(sketch.get_quantile(0.5) == 6.0); REQUIRE(sketch.get_quantile(0.99) == 10.0); @@ -175,7 +174,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { SECTION("100 items") { kll_float_sketch sketch(200, 0); - for (int i = 0; i < 100; ++i) sketch.update(i); + for (int i = 0; i < 100; ++i) sketch.update(static_cast<float>(i)); REQUIRE(sketch.get_quantile(0) == 0); REQUIRE(sketch.get_quantile(0.01) == 1); REQUIRE(sketch.get_quantile(0.5) == 50); @@ -185,9 +184,9 @@ TEST_CASE("kll sketch", "[kll_sketch]") { SECTION("many items, estimation mode") { kll_float_sketch sketch(200, 0); - const int n(1000000); + const int n = 1000000; for (int i = 0; i < n; i++) { - sketch.update(i); + sketch.update(static_cast<float>(i)); REQUIRE(sketch.get_n() == static_cast<uint64_t>(i + 1)); } REQUIRE_FALSE(sketch.is_empty()); @@ -200,7 +199,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { // test rank for (int i = 0; i < n; i++) { const double trueRank = (double) i / n; - REQUIRE(sketch.get_rank(i) == Approx(trueRank).margin(RANK_EPS_FOR_K_200)); + REQUIRE(sketch.get_rank(static_cast<float>(i)) == Approx(trueRank).margin(RANK_EPS_FOR_K_200)); } // test quantiles at every 0.1 percentage point @@ -239,8 +238,8 @@ TEST_CASE("kll sketch", "[kll_sketch]") { const int n = 1000; float values[n]; for (int i = 0; i < n; i++) { - sketch.update(i); - values[i] = i; + sketch.update(static_cast<float>(i)); + values[i] = static_cast<float>(i); } const auto ranks(sketch.get_CDF(values, n)); @@ -307,7 +306,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { SECTION("serialize deserialize one item") { kll_float_sketch sketch(200, 0); - sketch.update(1); + sketch.update(1.0f); std::stringstream s(std::ios::in | std::ios::out | std::ios::binary); sketch.serialize(s); REQUIRE(static_cast<size_t>(s.tellp()) == sketch.get_serialized_size_bytes()); @@ -340,8 +339,8 @@ TEST_CASE("kll sketch", "[kll_sketch]") { SECTION("stream serialize deserialize many floats") { kll_float_sketch sketch(200, 0); - const int n(1000); - for (int i = 0; i < n; i++) sketch.update(i); + const int n = 1000; + for (int i = 0; i < n; i++) sketch.update(static_cast<float>(i)); std::stringstream s(std::ios::in | std::ios::out | std::ios::binary); sketch.serialize(s); REQUIRE(static_cast<size_t>(s.tellp()) == sketch.get_serialized_size_bytes()); @@ -358,13 +357,13 @@ TEST_CASE("kll sketch", "[kll_sketch]") { REQUIRE(sketch2.get_normalized_rank_error(true) == sketch.get_normalized_rank_error(true)); REQUIRE(sketch2.get_quantile(0.5) == sketch.get_quantile(0.5)); REQUIRE(sketch2.get_rank(0) == sketch.get_rank(0)); - REQUIRE(sketch2.get_rank(n) == sketch.get_rank(n)); + REQUIRE(sketch2.get_rank(static_cast<float>(n)) == sketch.get_rank(static_cast<float>(n))); } SECTION("bytes serialize deserialize many floats") { kll_float_sketch sketch(200, 0); - const int n(1000); - for (int i = 0; i < n; i++) sketch.update(i); + const int n = 1000; + for (int i = 0; i < n; i++) sketch.update(static_cast<float>(i)); auto bytes = sketch.serialize(); REQUIRE(bytes.size() == sketch.get_serialized_size_bytes()); auto sketch2 = kll_float_sketch::deserialize(bytes.data(), bytes.size(), 0); @@ -379,7 +378,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { REQUIRE(sketch2.get_normalized_rank_error(true) == sketch.get_normalized_rank_error(true)); REQUIRE(sketch2.get_quantile(0.5) == sketch.get_quantile(0.5)); REQUIRE(sketch2.get_rank(0) == sketch.get_rank(0)); - REQUIRE(sketch2.get_rank(n) == sketch.get_rank(n)); + REQUIRE(sketch2.get_rank(static_cast<float>(n)) == sketch.get_rank(static_cast<float>(n))); REQUIRE_THROWS_AS(kll_sketch<int>::deserialize(bytes.data(), 7), std::out_of_range); REQUIRE_THROWS_AS(kll_sketch<int>::deserialize(bytes.data(), 15), std::out_of_range); REQUIRE_THROWS_AS(kll_sketch<int>::deserialize(bytes.data(), bytes.size() - 1), std::out_of_range); @@ -387,7 +386,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { SECTION("bytes serialize deserialize many ints") { kll_sketch<int> sketch; - const int n(1000); + const int n = 1000; for (int i = 0; i < n; i++) sketch.update(i); auto bytes = sketch.serialize(); REQUIRE(bytes.size() == sketch.get_serialized_size_bytes()); @@ -447,8 +446,8 @@ TEST_CASE("kll sketch", "[kll_sketch]") { kll_float_sketch sketch2(200, 0); const int n = 10000; for (int i = 0; i < n; i++) { - sketch1.update(i); - sketch2.update((2 * n) - i - 1); + sketch1.update(static_cast<float>(i)); + sketch2.update(static_cast<float>((2 * n) - i - 1)); } REQUIRE(sketch1.get_min_value() == 0.0f); @@ -470,8 +469,8 @@ TEST_CASE("kll sketch", "[kll_sketch]") { kll_float_sketch sketch2(128, 0); const int n = 10000; for (int i = 0; i < n; i++) { - sketch1.update(i); - sketch2.update((2 * n) - i - 1); + sketch1.update(static_cast<float>(i)); + sketch2.update(static_cast<float>((2 * n) - i - 1)); } REQUIRE(sketch1.get_min_value() == 0.0f); @@ -503,7 +502,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { kll_float_sketch sketch2(128, 0); const int n = 10000; for (int i = 0; i < n; i++) { - sketch1.update(i); + sketch1.update(static_cast<float>(i)); } // rank error should not be affected by a merge with an empty sketch with lower k @@ -526,8 +525,8 @@ TEST_CASE("kll sketch", "[kll_sketch]") { SECTION("merge min value from other") { kll_float_sketch sketch1(200, 0); kll_float_sketch sketch2(200, 0); - sketch1.update(1); - sketch2.update(2); + sketch1.update(1.0f); + sketch2.update(2.0f); sketch2.merge(sketch1); REQUIRE(sketch2.get_min_value() == 1.0f); REQUIRE(sketch2.get_max_value() == 2.0f); @@ -535,7 +534,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { SECTION("merge min and max values from other") { kll_float_sketch sketch1(200, 0); - for (int i = 0; i < 1000000; i++) sketch1.update(i); + for (int i = 0; i < 1000000; i++) sketch1.update(static_cast<float>(i)); kll_float_sketch sketch2(200, 0); sketch2.merge(sketch1); REQUIRE(sketch2.get_min_value() == 0.0f); @@ -548,7 +547,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { REQUIRE_THROWS_AS(sketch.get_min_value(), std::runtime_error); REQUIRE_THROWS_AS(sketch.get_max_value(), std::runtime_error); - const int n(1000); + const int n = 1000; for (int i = 0; i < n; i++) sketch.update(i); std::stringstream s(std::ios::in | std::ios::out | std::ios::binary); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
