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/03/25 21:40:58 UTC
[datasketches-cpp] 02/04: support serde instance
This is an automated email from the ASF dual-hosted git repository.
alsay pushed a commit to branch serde_instance
in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git
commit f8d5a21d913d07084fa4c011a9942d0513fc4f1e
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Fri Mar 25 13:17:23 2022 -0700
support serde instance
---
fi/include/frequent_items_sketch.hpp | 4 +++-
fi/include/frequent_items_sketch_impl.hpp | 5 +++--
python/src/fi_wrapper.cpp | 7 ++++++-
3 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/fi/include/frequent_items_sketch.hpp b/fi/include/frequent_items_sketch.hpp
index 76be516..ac524a3 100644
--- a/fi/include/frequent_items_sketch.hpp
+++ b/fi/include/frequent_items_sketch.hpp
@@ -225,9 +225,11 @@ public:
/**
* Computes size needed to serialize the current state of the sketch.
* This can be expensive since every item needs to be looked at.
+ * @param instance of a SerDe
* @return size in bytes needed to serialize this sketch
*/
- size_t get_serialized_size_bytes() const;
+ template<typename SerDe = S>
+ size_t get_serialized_size_bytes(const SerDe& sd = SerDe()) const;
/**
* This method serializes the sketch into a given stream in a binary form
diff --git a/fi/include/frequent_items_sketch_impl.hpp b/fi/include/frequent_items_sketch_impl.hpp
index f7c09e8..8c025ee 100644
--- a/fi/include/frequent_items_sketch_impl.hpp
+++ b/fi/include/frequent_items_sketch_impl.hpp
@@ -212,10 +212,11 @@ void frequent_items_sketch<T, W, H, E, S, A>::serialize(std::ostream& os, const
}
template<typename T, typename W, typename H, typename E, typename S, typename A>
-size_t frequent_items_sketch<T, W, H, E, S, A>::get_serialized_size_bytes() const {
+template<typename SerDe>
+size_t frequent_items_sketch<T, W, H, E, S, A>::get_serialized_size_bytes(const SerDe& sd) const {
if (is_empty()) return PREAMBLE_LONGS_EMPTY * sizeof(uint64_t);
size_t size = PREAMBLE_LONGS_NONEMPTY * sizeof(uint64_t) + map.get_num_active() * sizeof(W);
- for (auto it: map) size += S().size_of_item(it.first);
+ for (auto it: map) size += sd.size_of_item(it.first);
return size;
}
diff --git a/python/src/fi_wrapper.cpp b/python/src/fi_wrapper.cpp
index c4066b7..1c217e1 100644
--- a/python/src/fi_wrapper.cpp
+++ b/python/src/fi_wrapper.cpp
@@ -64,6 +64,11 @@ py::list fi_sketch_get_frequent_items(const frequent_items_sketch<T>& sk,
return list;
}
+template<typename T>
+size_t fi_sketch_get_serialized_size_bytes(const frequent_items_sketch<T>& sk) {
+ return sk.get_serialized_size_bytes();
+}
+
}
}
@@ -104,7 +109,7 @@ void bind_fi_sketch(py::module &m, const char* name) {
"Returns the epsilon value used to compute a priori error for a given log2(max_map_size)")
.def_static("get_apriori_error", &frequent_items_sketch<T>::get_apriori_error, py::arg("lg_max_map_size"), py::arg("estimated_total_weight"),
"Returns the estimated a priori error given the max_map_size for the sketch and the estimated_total_stream_weight.")
- .def("get_serialized_size_bytes", &frequent_items_sketch<T>::get_serialized_size_bytes,
+ .def("get_serialized_size_bytes", &dspy::fi_sketch_get_serialized_size_bytes<T>,
"Computes the size needed to serialize the current state of the sketch. This can be expensive since every item needs to be looked at.")
.def("serialize", &dspy::fi_sketch_serialize<T>, "Serializes the sketch into a bytes object")
.def_static("deserialize", &dspy::fi_sketch_deserialize<T>, "Reads a bytes object and returns the corresponding frequent_strings_sketch")
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org