1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-06-01 22:41:32 +00:00

Split encode and decode for clearer naming.

This commit is contained in:
Thomas Harte 2023-01-17 17:33:52 -05:00
parent f6e601daff
commit 83cf4497dd

View File

@ -23,18 +23,18 @@ template <int... Sizes> class NumericCoder {
/// 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");
NumericEncoderImp<Sizes...>::template encode<index, 0, 1>(target, value);
NumericEncoder<Sizes...>::template encode<index, 0, 1>(target, value);
}
/// @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");
return NumericEncoderImp<Sizes...>::template decode<index, 0, 1>(source);
return NumericDecoder<Sizes...>::template decode<index, 0, 1>(source);
}
private:
template <int size, int... Tail>
struct NumericEncoderImp {
struct NumericEncoder {
template <int index, int i, int divider> static void encode(int &target, int value) {
if constexpr (i == index) {
const int suffix = target % divider;
@ -44,18 +44,20 @@ template <int... Sizes> class NumericCoder {
target *= divider;
target += suffix;
} 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 size, int... Tail>
struct NumericDecoder {
template <int index, int i, int divider> static int decode(int source) {
if constexpr (i == index) {
return (source / divider) % size;
} else {
return NumericEncoderImp<Tail...>::template decode<index, i+1, divider*size>(source);
return NumericDecoder<Tail...>::template decode<index, i+1, divider*size>(source);
}
}
};
};