Skip to content

Commit d53a4e3

Browse files
committed
Index mapping encoding and decoding
1 parent 59f69dc commit d53a4e3

5 files changed

Lines changed: 54 additions & 41 deletions

File tree

ddsketch/include/index_mapping.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ class IndexMapping {
4343
double get_relative_accuracy() const;
4444
double min_indexable_value() const;
4545
double max_indexable_value() const;
46-
void encode(std::ostream& os);
46+
void serialize(std::ostream& os) const;
4747

48-
template<IndexMappingLayout layout>
49-
static IndexMapping* decode(std::istream& is);
48+
static Derived deserialize(std::istream& is);
5049

5150
~IndexMapping() = default;
51+
52+
protected:
53+
Derived& derived();
54+
const Derived& derived() const;
5255
};
5356

5457
}

ddsketch/include/index_mapping_decode.hpp

Lines changed: 0 additions & 35 deletions
This file was deleted.

ddsketch/include/index_mapping_impl.hpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#include "common_defs.hpp"
2626
#include "index_mapping.hpp"
27-
#include "linearly_interpolated_mapping.hpp"
2827

2928
namespace datasketches {
3029

@@ -44,6 +43,35 @@ inline std::ostream& operator<<(std::ostream& os, const IndexMappingLayout& obj)
4443
return os << "INVALID";
4544
}
4645
}
46+
47+
template<class Derived>
48+
Derived IndexMapping<Derived>::deserialize(std::istream &is) {
49+
const auto gamma = read<double>(is);
50+
const auto index_offset = read<double>(is);
51+
52+
53+
return Derived(gamma, index_offset);
54+
}
55+
56+
template<class Derived>
57+
Derived& IndexMapping<Derived>::derived() {
58+
return *static_cast<Derived*>(this);
59+
}
60+
61+
template<class Derived>
62+
const Derived& IndexMapping<Derived>::derived() const {
63+
return *static_cast<const Derived*>(this);
64+
}
65+
66+
template<class Derived>
67+
void IndexMapping<Derived>::serialize(std::ostream &os) const {
68+
derived().serialize(os);
69+
}
70+
71+
template<class Derived>
72+
double IndexMapping<Derived>::get_relative_accuracy() const {
73+
return derived().get_relative_accuracy();
74+
}
4775
}
4876

4977

ddsketch/include/log_like_index_mapping_impl.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ double LogLikeIndexMapping<Derived>::max_indexable_value() const {
102102
}
103103

104104
template<class Derived>
105-
void LogLikeIndexMapping<Derived>::serialize(std::ostream &os) const {
106-
// write(os, layout());
105+
void LogLikeIndexMapping<Derived>::serialize(std::ostream& os) const {
107106
write(os, gamma);
108107
write(os, index_offset);
109108
}

ddsketch/test/IndexMappingTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,22 @@ TEMPLATE_TEST_CASE("test index mapping validity", "[indexmappingtest}",
9494
bound = mapping->upper_bound(index);
9595
}
9696
}
97+
98+
TEMPLATE_TEST_CASE("encode - decode", "[indexmappingtest",
99+
LinearlyInterpolatedMapping,
100+
LogarithmicMapping,
101+
QuadraticallyInterpolatedMapping,
102+
QuarticallyInterpolatedMapping
103+
) {
104+
const double gamma = 42;
105+
const double index_offset = 4321;
106+
TestType mapping(gamma, index_offset);
107+
108+
std::stringstream ss;
109+
mapping.serialize(ss);
110+
111+
const TestType decoded_mapping = IndexMapping<TestType>::deserialize(ss);
112+
113+
REQUIRE(mapping.get_relative_accuracy() == decoded_mapping.get_relative_accuracy());
114+
}
97115
}

0 commit comments

Comments
 (0)