4 #include <simo/exceptions.hpp> 14 constexpr
static const int32_t CHUNK_SIZE = 5;
17 constexpr
static const int32_t CHUNK_MASK = 0x1f;
20 constexpr
static const int32_t CHUNK_THRESHOLD = 0x20;
23 constexpr
static const int32_t ASCII_OFFSET = 63;
33 std::string encode(
double coord, int32_t precision = 5)
35 assert(precision >= 0);
36 double pow10 = std::pow(10, precision);
37 auto value =
static_cast<int32_t
>(std::round(coord * pow10));
44 while (value >= CHUNK_THRESHOLD)
46 int32_t ch = ((value & CHUNK_MASK) | CHUNK_THRESHOLD) + ASCII_OFFSET;
47 res +=
static_cast<char>(ch);
50 res +=
static_cast<char>(value + ASCII_OFFSET);
62 int32_t advance(
const std::string& text,
size_t& index)
67 while (index < text.size())
69 ch = text[index++] - ASCII_OFFSET;
70 res |= (ch & CHUNK_MASK) << shift;
72 if (ch < CHUNK_THRESHOLD)
94 std::vector<double> decode(
const std::string& text, int32_t precision = 5)
96 assert(precision >= 0);
97 double pow10 = std::pow(10, precision);
98 std::vector<double> res;
99 res.reserve(text.size() / 3);
103 while (index < text.size())
105 y += advance(text, index);
106 x += advance(text, index);
107 res.push_back(x / pow10);
108 res.push_back(y / pow10);