Skip to content

Commit

Permalink
ae.utils.math.mixed_radix: Avoid using "max" when we actually mean ca…
Browse files Browse the repository at this point in the history
…rdinality

Maximum is the type's maximum value (e.g. ubyte.max == 255).
Cardinality is the number of values that a type can have, or for
non-zero contiguous integer types, one past the maximum value.
  • Loading branch information
CyberShadow committed Jan 21, 2024
1 parent af81fdd commit 5c4beee
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions utils/math/mixed_radix.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ template MixedRadixCoder(
/// until `.finish` is called.
struct Encoder(
/// Maximum number of encoded items.
size_t maxSize,
size_t maxItems,
)
{
struct Item { I n, max; }
MaybeDynamicArray!(Item, maxSize) items;
struct Item { I n, card; }
MaybeDynamicArray!(Item, maxItems) items;

void put(I n, I max)
void put(I n, I card)
in(0 <= n && n < card)
{
assert(0 <= n && n < max);
items ~= Item(n, max);
items ~= Item(n, card);
}

E finish()
{
E result = withEOF ? 1 : 0;
foreach_reverse (ref item; items)
{
result *= item.max;
result *= item.card;
result += item.n;
}
return result;
Expand All @@ -61,10 +61,10 @@ template MixedRadixCoder(
{
E encoded = withEOF ? 1 : 0;

void put(I n, I max)
void put(I n, I card)
{
assert(0 <= n && n < max);
encoded *= max;
assert(0 <= n && n < card);
encoded *= card;
encoded += n;
}

Expand All @@ -84,11 +84,11 @@ template MixedRadixCoder(
assert(encoded > 0);
}

I get(I max)
I get(I card)
in(card > 0)
{
assert(max > 0);
I value = encoded % max;
encoded /= max;
I value = encoded % card;
encoded /= card;
static if (withEOF)
assert(encoded > 0, "Decoding error");
return value;
Expand Down

0 comments on commit 5c4beee

Please sign in to comment.