From 56258ab91a16ff2126631dca701ee58e2c7fea49 Mon Sep 17 00:00:00 2001 From: AlexanderSaydakov Date: Wed, 26 Jun 2019 11:59:27 -0700 Subject: [PATCH 1/2] common partial specialization for arithmetic types --- common/include/serde.hpp | 82 ++++++++++------------------------------ 1 file changed, 20 insertions(+), 62 deletions(-) diff --git a/common/include/serde.hpp b/common/include/serde.hpp index cda35271..49961c32 100644 --- a/common/include/serde.hpp +++ b/common/include/serde.hpp @@ -26,7 +26,7 @@ namespace datasketches { // serialize and deserialize -template struct serde { +template struct serde { // stream void serialize(std::ostream& os, const T* items, unsigned num); void deserialize(std::istream& is, T* items, unsigned num); // items are not initialized @@ -36,69 +36,27 @@ template struct serde { size_t deserialize(const char* ptr, T* items, unsigned num); // items are not initialized }; -template<> -struct serde { - void serialize(std::ostream& os, const int32_t* items, unsigned num) { - os.write((char*)items, sizeof(int32_t) * num); - } - void deserialize(std::istream& is, int32_t* items, unsigned num) { - is.read((char*)items, sizeof(int32_t) * num); - } - size_t size_of_item(int32_t item) { - return sizeof(int32_t); - } - size_t serialize(char* ptr, const int32_t* items, unsigned num) { - memcpy(ptr, items, sizeof(int32_t) * num); - return sizeof(int32_t) * num; - } - size_t deserialize(const char* ptr, int32_t* items, unsigned num) { - memcpy(items, ptr, sizeof(int32_t) * num); +// serde for all fixed-size arithmetic types (int and float of different sizes) +// in particular, serde for signed 64-bit integers should produce sketches binary-compatible +// with LongsSketch and ItemsSketch with ArrayOfLongsSerDe in Java +template +struct serde::value>::type> { + void serialize(std::ostream& os, const T* items, unsigned num) { + os.write((char*)items, sizeof(T) * num); + } + void deserialize(std::istream& is, T* items, unsigned num) { + is.read((char*)items, sizeof(T) * num); + } + size_t size_of_item(T item) { + return sizeof(T); + } + size_t serialize(char* ptr, const T* items, unsigned num) { + memcpy(ptr, items, sizeof(T) * num); return sizeof(int32_t) * num; } -}; - -// serde for signed 64-bit integers -// this should produce sketches binary-compatible with LongsSketch -// and ItemsSketch with ArrayOfLongsSerDe in Java -template<> -struct serde { - void serialize(std::ostream& os, const int64_t* items, unsigned num) { - os.write((char*)items, sizeof(int64_t) * num); - } - void deserialize(std::istream& is, int64_t* items, unsigned num) { - is.read((char*)items, sizeof(int64_t) * num); - } - size_t size_of_item(int64_t item) { - return sizeof(int64_t); - } - size_t serialize(char* ptr, const int64_t* items, unsigned num) { - memcpy(ptr, items, sizeof(int64_t) * num); - return sizeof(int64_t) * num; - } - size_t deserialize(const char* ptr, int64_t* items, unsigned num) { - memcpy(items, ptr, sizeof(int64_t) * num); - return sizeof(int64_t) * num; - } -}; - -template<> -struct serde { - void serialize(std::ostream& os, const float* items, unsigned num) { - os.write((char*)items, sizeof(float) * num); - } - void deserialize(std::istream& is, float* items, unsigned num) { - is.read((char*)items, sizeof(float) * num); - } - size_t size_of_item(float item) { - return sizeof(float); - } - size_t serialize(char* ptr, const float* items, unsigned num) { - memcpy(ptr, items, sizeof(float) * num); - return sizeof(float) * num; - } - size_t deserialize(const char* ptr, float* items, unsigned num) { - memcpy(items, ptr, sizeof(float) * num); - return sizeof(float) * num; + size_t deserialize(const char* ptr, T* items, unsigned num) { + memcpy(items, ptr, sizeof(T) * num); + return sizeof(T) * num; } }; From 4c9dc5ae9681bfe61b7eb20b3b1f29130305c5ff Mon Sep 17 00:00:00 2001 From: AlexanderSaydakov Date: Wed, 26 Jun 2019 12:02:05 -0700 Subject: [PATCH 2/2] clarification in comment --- common/include/serde.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/include/serde.hpp b/common/include/serde.hpp index 49961c32..1e27acdf 100644 --- a/common/include/serde.hpp +++ b/common/include/serde.hpp @@ -37,7 +37,7 @@ template struct serde { }; // serde for all fixed-size arithmetic types (int and float of different sizes) -// in particular, serde for signed 64-bit integers should produce sketches binary-compatible +// in particular, kll_sketch should produce sketches binary-compatible // with LongsSketch and ItemsSketch with ArrayOfLongsSerDe in Java template struct serde::value>::type> {