2023-01-17 18:26:11 +00:00
|
|
|
//
|
|
|
|
// NumericCoder.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 17/01/2023.
|
|
|
|
// Copyright © 2023 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef NumericCoder_hpp
|
|
|
|
#define NumericCoder_hpp
|
|
|
|
|
|
|
|
namespace Numeric {
|
|
|
|
|
|
|
|
/// Stores and retrieves an arbitrary number of fields into an int with arbitrary modulos.
|
|
|
|
///
|
2023-01-18 01:14:32 +00:00
|
|
|
/// E.g. NumericCoder<8, 3, 14> establishes an encoder and decoder for three fields,
|
2023-01-17 18:26:11 +00:00
|
|
|
/// the first is modulo 8, the second is modulo 3, the third is modulo 14.
|
|
|
|
///
|
2023-01-18 01:14:32 +00:00
|
|
|
/// NumericCoder<8, 3, 14>::encode<2>(v, 9) will mutate v so that the third field
|
2023-01-17 18:26:11 +00:00
|
|
|
/// (i.e. field 2) has value 9.
|
2023-01-18 01:14:32 +00:00
|
|
|
///
|
|
|
|
/// The first given field will occupy the least significant part of the target int; e.g.
|
|
|
|
/// a NumericCoder<3, 4, 6> with ::encode<0>(v, 2), ::encode<1>(v, 1) and
|
|
|
|
/// ::encode<2>(v, 5) will have a final value of:
|
|
|
|
///
|
|
|
|
/// [value] 2 +
|
|
|
|
/// [value] 1 * [product of previous field sizes] 3 +
|
|
|
|
/// 5 * 12
|
|
|
|
/// = 65
|
|
|
|
///
|
2023-01-17 18:26:11 +00:00
|
|
|
template <int... Sizes> class NumericCoder {
|
|
|
|
public:
|
|
|
|
/// Modifies @c target to hold @c value at @c index.
|
|
|
|
template <int index> static void encode(int &target, int value) {
|
|
|
|
static_assert(index < sizeof...(Sizes), "Index must be within range");
|
2023-01-17 22:36:15 +00:00
|
|
|
NumericEncoder<Sizes...>::template encode<index>(target, value);
|
2023-01-17 18:26:11 +00:00
|
|
|
}
|
2023-01-17 22:36:15 +00:00
|
|
|
|
2023-01-17 18:26:11 +00:00
|
|
|
/// @returns The value from @c source at @c index.
|
|
|
|
template <int index> static int decode(int source) {
|
|
|
|
static_assert(index < sizeof...(Sizes), "Index must be within range");
|
2023-01-17 22:36:15 +00:00
|
|
|
return NumericDecoder<Sizes...>::template decode<index>(source);
|
2023-01-17 18:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
template <int size, int... Tail>
|
2023-01-17 22:33:52 +00:00
|
|
|
struct NumericEncoder {
|
2023-01-17 22:36:15 +00:00
|
|
|
template <int index, int i = 0, int divider = 1> static void encode(int &target, int value) {
|
2023-01-17 18:26:11 +00:00
|
|
|
if constexpr (i == index) {
|
|
|
|
const int suffix = target % divider;
|
|
|
|
target /= divider;
|
|
|
|
target -= target % size;
|
|
|
|
target += value % size;
|
|
|
|
target *= divider;
|
|
|
|
target += suffix;
|
|
|
|
} else {
|
2023-01-17 22:33:52 +00:00
|
|
|
NumericEncoder<Tail...>::template encode<index, i+1, divider*size>(target, value);
|
2023-01-17 18:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 22:33:52 +00:00
|
|
|
};
|
2023-01-17 18:26:11 +00:00
|
|
|
|
2023-01-17 22:33:52 +00:00
|
|
|
template <int size, int... Tail>
|
|
|
|
struct NumericDecoder {
|
2023-01-17 22:36:15 +00:00
|
|
|
template <int index, int i = 0, int divider = 1> static int decode(int source) {
|
2023-01-17 18:26:11 +00:00
|
|
|
if constexpr (i == index) {
|
|
|
|
return (source / divider) % size;
|
|
|
|
} else {
|
2023-01-17 22:33:52 +00:00
|
|
|
return NumericDecoder<Tail...>::template decode<index, i+1, divider*size>(source);
|
2023-01-17 18:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* NumericCoder_hpp */
|