1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-24 12:30:17 +00:00

Merge branch 'RP5C01' of github.com:TomHarte/CLK into RP5C01

This commit is contained in:
Thomas Harte 2023-01-17 18:53:30 -05:00
commit 9588c9bee2

View File

@ -23,19 +23,20 @@ template <int... Sizes> class NumericCoder {
/// Modifies @c target to hold @c value at @c index. /// Modifies @c target to hold @c value at @c index.
template <int index> static void encode(int &target, int value) { template <int index> static void encode(int &target, int value) {
static_assert(index < sizeof...(Sizes), "Index must be within range"); static_assert(index < sizeof...(Sizes), "Index must be within range");
NumericEncoderImp<Sizes...>::template encode<index, 0, 1>(target, value); NumericEncoder<Sizes...>::template encode<index>(target, value);
} }
/// @returns The value from @c source at @c index. /// @returns The value from @c source at @c index.
template <int index> static int decode(int source) { template <int index> static int decode(int source) {
static_assert(index < sizeof...(Sizes), "Index must be within range"); static_assert(index < sizeof...(Sizes), "Index must be within range");
return NumericEncoderImp<Sizes...>::template decode<index, 0, 1>(source); return NumericDecoder<Sizes...>::template decode<index>(source);
} }
private: private:
template <int size, int... Tail> template <int size, int... Tail>
struct NumericEncoderImp { struct NumericEncoder {
template <int index, int i, int divider> static void encode(int &target, int value) { template <int index, int i = 0, int divider = 1> static void encode(int &target, int value) {
if constexpr (i == index) { if constexpr (i == index) {
const int suffix = target % divider; const int suffix = target % divider;
target /= divider; target /= divider;
@ -44,18 +45,20 @@ template <int... Sizes> class NumericCoder {
target *= divider; target *= divider;
target += suffix; target += suffix;
} else { } else {
NumericEncoderImp<Tail...>::template encode<index, i+1, divider*size>(target, value); NumericEncoder<Tail...>::template encode<index, i+1, divider*size>(target, value);
} }
} }
};
template <int index, int i, int divider> static int decode(int source) { template <int size, int... Tail>
struct NumericDecoder {
template <int index, int i = 0, int divider = 1> static int decode(int source) {
if constexpr (i == index) { if constexpr (i == index) {
return (source / divider) % size; return (source / divider) % size;
} else { } else {
return NumericEncoderImp<Tail...>::template decode<index, i+1, divider*size>(source); return NumericDecoder<Tail...>::template decode<index, i+1, divider*size>(source);
} }
} }
}; };
}; };