Skip to content

Commit

Permalink
ae.utils.math.mixed_radix: Add explicit VariableLengthEncoder
Browse files Browse the repository at this point in the history
Avoid magic numbers (-1) in the public interface.
  • Loading branch information
CyberShadow committed Jan 21, 2024
1 parent 5976bf4 commit af81fdd
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions utils/math/mixed_radix.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ template MixedRadixCoder(
/// until `.finish` is called.
struct Encoder(
/// Maximum number of encoded items.
/// If -1, a dynamic array will be used.
size_t maxSize = -1,
size_t maxSize,
)
{
struct Item { I n, max; }
Expand All @@ -53,6 +52,9 @@ template MixedRadixCoder(
}
}

/// As above. This will allocate the items dynamically.
alias VariableLengthEncoder = Encoder!(-1);

/// Like `Encoder`, but does not use a temporary buffer.
/// Instead, the user is expected to put the items in reverse order.
struct RetroEncoder
Expand Down Expand Up @@ -123,7 +125,10 @@ unittest
}
else
{
Coder.Encoder!(mode == Mode.dynamicSize ? -1 : 2) encoder;
static if (mode == Mode.dynamicSize)
Coder.VariableLengthEncoder encoder;
else
Coder.Encoder!2 encoder;

encoder.put(5, 8);
encoder.put(1, 2);
Expand Down

0 comments on commit af81fdd

Please sign in to comment.