You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2022/06/07 01:33:18 UTC
[datasketches-cpp] branch req_converting_constructor created (now 6f1dde6)
This is an automated email from the ASF dual-hosted git repository.
alsay pushed a change to branch req_converting_constructor
in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git
at 6f1dde6 type converting copy constructor
This branch includes the following new commits:
new 6f1dde6 type converting copy constructor
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org
[datasketches-cpp] 01/01: type converting copy constructor
Posted by al...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
alsay pushed a commit to branch req_converting_constructor
in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git
commit 6f1dde65fd8af91be7c3f02ba3be349fd1e5251c
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Mon Jun 6 18:33:13 2022 -0700
type converting copy constructor
---
req/include/req_compactor.hpp | 6 ++++++
req/include/req_compactor_impl.hpp | 27 +++++++++++++++++++++++++
req/include/req_sketch.hpp | 11 +++++++++++
req/include/req_sketch_impl.hpp | 31 +++++++++++++++++++++++++++--
req/test/req_sketch_test.cpp | 40 +++++++++++++++++++++++++++++++++++---
5 files changed, 110 insertions(+), 5 deletions(-)
diff --git a/req/include/req_compactor.hpp b/req/include/req_compactor.hpp
index 682aa49..c78784b 100755
--- a/req/include/req_compactor.hpp
+++ b/req/include/req_compactor.hpp
@@ -38,6 +38,9 @@ public:
req_compactor& operator=(const req_compactor& other);
req_compactor& operator=(req_compactor&& other);
+ template<typename TT, typename CC, typename AA>
+ req_compactor(const req_compactor<TT, CC, AA>& other, const Allocator& allocator);
+
bool is_sorted() const;
uint32_t get_num_items() const;
uint32_t get_nom_capacity() const;
@@ -128,6 +131,9 @@ private:
template<typename S>
static std::pair<std::unique_ptr<T, items_deleter>, size_t> deserialize_items(const void* bytes, size_t size, const S& serde, const Allocator& allocator, uint32_t num);
+ // for type converting constructor
+ template<typename TT, typename CC, typename AA>
+ friend class req_compactor;
};
} /* namespace datasketches */
diff --git a/req/include/req_compactor_impl.hpp b/req/include/req_compactor_impl.hpp
index c1cc11f..d3747be 100755
--- a/req/include/req_compactor_impl.hpp
+++ b/req/include/req_compactor_impl.hpp
@@ -132,6 +132,33 @@ req_compactor<T, C, A>& req_compactor<T, C, A>::operator=(req_compactor&& other)
return *this;
}
+template<typename T, typename C, typename A>
+template<typename TT, typename CC, typename AA>
+req_compactor<T, C, A>::req_compactor(const req_compactor<TT, CC, AA>& other, const A& allocator):
+allocator_(allocator),
+lg_weight_(other.lg_weight_),
+hra_(other.hra_),
+coin_(other.coin_),
+sorted_(other.sorted_),
+section_size_raw_(other.section_size_raw_),
+section_size_(other.section_size_),
+num_sections_(other.num_sections_),
+state_(other.state_),
+num_items_(other.num_items_),
+capacity_(other.capacity_),
+items_(nullptr)
+{
+ if (other.items_ != nullptr) {
+ items_ = allocator_.allocate(capacity_);
+ const uint32_t from = hra_ ? capacity_ - num_items_ : 0;
+ const uint32_t to = hra_ ? capacity_ : num_items_;
+ for (uint32_t i = from; i < to; ++i) new (items_ + i) T(other.items_[i]);
+ if (sorted_ && !std::is_sorted(items_ + from, items_ + to, C())) {
+ throw std::logic_error("items must be sorted");
+ }
+ }
+}
+
template<typename T, typename C, typename A>
bool req_compactor<T, C, A>::is_sorted() const {
return sorted_;
diff --git a/req/include/req_sketch.hpp b/req/include/req_sketch.hpp
index c9da26b..a71d7da 100755
--- a/req/include/req_sketch.hpp
+++ b/req/include/req_sketch.hpp
@@ -58,6 +58,14 @@ public:
req_sketch& operator=(const req_sketch& other);
req_sketch& operator=(req_sketch&& other);
+ /*
+ * Type converting constructor.
+ * @param other sketch of a different type
+ * @param allocator instance of an Allocator
+ */
+ template<typename TT, typename CC, typename SS, typename AA>
+ explicit req_sketch(const req_sketch<TT, CC, SS, AA>& other, const Allocator& allocator = Allocator());
+
/**
* Returns configured parameter K
* @return parameter K
@@ -408,6 +416,9 @@ private:
}
}
+ // for type converting constructor
+ template<typename TT, typename CC, typename SS, typename AA>
+ friend class req_sketch;
};
template<typename T, typename C, typename S, typename A>
diff --git a/req/include/req_sketch_impl.hpp b/req/include/req_sketch_impl.hpp
index 2adb89d..09a192d 100755
--- a/req/include/req_sketch_impl.hpp
+++ b/req/include/req_sketch_impl.hpp
@@ -64,8 +64,8 @@ compactors_(other.compactors_),
min_value_(nullptr),
max_value_(nullptr)
{
- if (other.min_value_ != nullptr) min_value_ = new (A().allocate(1)) T(*other.min_value_);
- if (other.max_value_ != nullptr) max_value_ = new (A().allocate(1)) T(*other.max_value_);
+ if (other.min_value_ != nullptr) min_value_ = new (allocator_.allocate(1)) T(*other.min_value_);
+ if (other.max_value_ != nullptr) max_value_ = new (allocator_.allocate(1)) T(*other.max_value_);
}
template<typename T, typename C, typename S, typename A>
@@ -113,6 +113,33 @@ req_sketch<T, C, S, A>& req_sketch<T, C, S, A>::operator=(req_sketch&& other) {
return *this;
}
+template<typename T, typename C, typename S, typename A>
+template<typename TT, typename CC, typename SS, typename AA>
+req_sketch<T, C, S, A>::req_sketch(const req_sketch<TT, CC, SS, AA>& other, const A& allocator):
+allocator_(allocator),
+k_(other.k_),
+hra_(other.hra_),
+max_nom_size_(other.max_nom_size_),
+num_retained_(other.num_retained_),
+n_(other.n_),
+compactors_(allocator),
+min_value_(nullptr),
+max_value_(nullptr)
+{
+ static_assert(
+ std::is_constructible<T, TT>::value,
+ "Type converting constructor requires new type to be constructible from existing type"
+ );
+ compactors_.reserve(other.compactors_.size());
+ for (const auto& compactor: other.compactors_) {
+ compactors_.push_back(req_compactor<T, C, A>(compactor, allocator_));
+ }
+ if (!other.is_empty()) {
+ min_value_ = new (allocator_.allocate(1)) T(other.get_min_value());
+ max_value_ = new (allocator_.allocate(1)) T(other.get_max_value());
+ }
+}
+
template<typename T, typename C, typename S, typename A>
uint16_t req_sketch<T, C, S, A>::get_k() const {
return k_;
diff --git a/req/test/req_sketch_test.cpp b/req/test/req_sketch_test.cpp
index 9705699..ba05b94 100755
--- a/req/test/req_sketch_test.cpp
+++ b/req/test/req_sketch_test.cpp
@@ -35,7 +35,7 @@ const std::string input_path = "test/";
#endif
TEST_CASE("req sketch: empty", "[req_sketch]") {
- std::cout << "sizeof(req_float_sketch)=" << sizeof(req_sketch<float>) << "\n";
+ //std::cout << "sizeof(req_float_sketch)=" << sizeof(req_sketch<float>) << "\n";
req_sketch<float> sketch(12);
REQUIRE(sketch.get_k() == 12);
REQUIRE(sketch.is_HRA());
@@ -245,7 +245,7 @@ TEST_CASE("req sketch: byte serialize-deserialize single item", "[req_sketch]")
auto bytes = sketch.serialize();
REQUIRE(bytes.size() == sketch.get_serialized_size_bytes());
auto sketch2 = req_sketch<float>::deserialize(bytes.data(), bytes.size());
- std::cout << sketch2.to_string(true);
+ //std::cout << sketch2.to_string(true);
REQUIRE(bytes.size() == sketch2.get_serialized_size_bytes());
REQUIRE(sketch2.is_empty() == sketch.is_empty());
REQUIRE(sketch2.is_estimation_mode() == sketch.is_estimation_mode());
@@ -282,7 +282,7 @@ TEST_CASE("req sketch: byte serialize-deserialize exact mode", "[req_sketch]") {
auto bytes = sketch.serialize();
REQUIRE(bytes.size() == sketch.get_serialized_size_bytes());
auto sketch2 = req_sketch<float>::deserialize(bytes.data(), bytes.size());
- std::cout << sketch2.to_string(true);
+ //std::cout << sketch2.to_string(true);
REQUIRE(bytes.size() == sketch2.get_serialized_size_bytes());
REQUIRE(sketch2.is_empty() == sketch.is_empty());
REQUIRE(sketch2.is_estimation_mode() == sketch.is_estimation_mode());
@@ -485,6 +485,40 @@ TEST_CASE("req sketch: merge incompatible HRA and LRA", "[req_sketch]") {
REQUIRE_THROWS_AS(sketch1.merge(sketch2), std::invalid_argument);
}
+TEST_CASE("req sketch: type conversion - empty", "[req_sketch]") {
+ req_sketch<double> req_double(12);
+ req_sketch<float> req_float(req_double);
+ REQUIRE(req_float.is_empty());
+ REQUIRE(req_float.get_k() == req_double.get_k());
+ REQUIRE(req_float.get_n() == 0);
+ REQUIRE(req_float.get_num_retained() == 0);
+}
+
+TEST_CASE("req sketch: type conversion - several levels", "[req_sketch]") {
+ req_sketch<double> req_double(12);
+ for (int i = 0; i < 1000; ++i) req_double.update(static_cast<double>(i));
+ req_sketch<float> req_float(req_double);
+ REQUIRE(!req_float.is_empty());
+ REQUIRE(req_float.get_k() == req_double.get_k());
+ REQUIRE(req_float.get_n() == req_double.get_n());
+ REQUIRE(req_float.get_num_retained() == req_double.get_num_retained());
+
+ auto sv_float = req_float.get_sorted_view(false);
+ auto sv_double = req_double.get_sorted_view(false);
+ auto sv_float_it = sv_float.begin();
+ auto sv_double_it = sv_double.begin();
+ while (sv_float_it != sv_float.end()) {
+ REQUIRE(sv_double_it != sv_double.end());
+ auto float_pair = *sv_float_it;
+ auto double_pair = *sv_double_it;
+ REQUIRE(float_pair.first == Approx(double_pair.first).margin(0.01));
+ REQUIRE(float_pair.second == double_pair.second);
+ ++sv_float_it;
+ ++sv_double_it;
+ }
+ REQUIRE(sv_double_it == sv_double.end());
+}
+
//TEST_CASE("for manual comparison with Java") {
// req_sketch<float> sketch(12, false);
// for (size_t i = 0; i < 100000; ++i) sketch.update(i);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org