Skip to content

Commit 59f69dc

Browse files
committed
Encode/decode index mapping
1 parent f6ed4fa commit 59f69dc

5 files changed

Lines changed: 44 additions & 16 deletions

File tree

ddsketch/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ install(FILES
6363
include/quadratically_interpolated_mapping.hpp
6464
include/quadratically_interpolated_mapping_impl.hpp
6565
include/store.hpp
66+
include/index_mapping_decode.hpp
6667
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/DataSketches")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Created by Andrea Novellini on 13/09/2025.
3+
//
4+
5+
#ifndef DATASKETCHES_INDEX_MAPPING_DECODE_HPP
6+
#define DATASKETCHES_INDEX_MAPPING_DECODE_HPP
7+
8+
#include "index_mapping.hpp"
9+
#include "logarithmic_mapping.hpp"
10+
#include "quadratically_interpolated_mapping.hpp"
11+
12+
namespace datasketches {
13+
14+
template<class Derived>
15+
template<IndexMappingLayout layout>
16+
IndexMapping<Derived>* IndexMapping<Derived>::decode(std::istream& is) const {
17+
const auto gamma = read<double>(is);
18+
const auto index_offset = read<double>(is);
19+
20+
switch (layout) {
21+
case IndexMappingLayout::LOG:
22+
return new LogarithmicMapping(gamma, index_offset);
23+
case IndexMappingLayout::LOG_LINEAR:
24+
return new LinearlyInterpolatedMapping(gamma, index_offset);
25+
case IndexMappingLayout::LOG_QUADRATIC:
26+
return new QuadraticallyInterpolatedMapping(gamma, index_offset);
27+
case IndexMappingLayout::LOG_QUARTIC:
28+
return new QuadraticallyInterpolatedMapping(gamma, index_offset);
29+
default:
30+
throw std::invalid_argument("Invalid index mapping layout");
31+
}
32+
}
33+
}
34+
35+
#endif //DATASKETCHES_INDEX_MAPPING_DECODE_HPP

ddsketch/include/index_mapping_impl.hpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
#include <istream>
2424

25+
#include "common_defs.hpp"
2526
#include "index_mapping.hpp"
27+
#include "linearly_interpolated_mapping.hpp"
2628

2729
namespace datasketches {
2830

@@ -42,18 +44,6 @@ inline std::ostream& operator<<(std::ostream& os, const IndexMappingLayout& obj)
4244
return os << "INVALID";
4345
}
4446
}
45-
46-
template<class Derived>
47-
template<IndexMappingLayout layout>
48-
IndexMapping<Derived>* IndexMapping<Derived>::decode(std::istream& is) {
49-
switch (layout) {
50-
case IndexMappingLayout::LOG_LINEAR:
51-
return nullptr;
52-
default:
53-
return nullptr;
54-
}
55-
}
56-
5747
}
5848

5949

ddsketch/include/log_like_index_mapping.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class LogLikeIndexMapping : public IndexMapping<Derived> {
3838
double get_relative_accuracy() const;
3939
double min_indexable_value() const;
4040
double max_indexable_value() const;
41-
void encode(std::ostream& os);
41+
void serialize(std::ostream& os) const;
4242

4343
bool operator==(const LogLikeIndexMapping<Derived>& other) const;
4444

ddsketch/include/log_like_index_mapping_impl.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#define LOG_LIKE_INDEX_MAPPING_IMPL_HPP
2222
#include "log_like_index_mapping.hpp"
2323
#include <cmath>
24-
#include <iomanip>
24+
#include "common_defs.hpp"
2525

2626
namespace datasketches {
2727

@@ -102,8 +102,10 @@ double LogLikeIndexMapping<Derived>::max_indexable_value() const {
102102
}
103103

104104
template<class Derived>
105-
void LogLikeIndexMapping<Derived>::encode(std::ostream &os) {
106-
// TODO implement
105+
void LogLikeIndexMapping<Derived>::serialize(std::ostream &os) const {
106+
// write(os, layout());
107+
write(os, gamma);
108+
write(os, index_offset);
107109
}
108110

109111
template<class Derived>

0 commit comments

Comments
 (0)