Skip to content

Commit

Permalink
chore: polish lossless coding
Browse files Browse the repository at this point in the history
  • Loading branch information
rwnobrega committed Dec 31, 2024
1 parent d4cb1f0 commit 22da35d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/komm/_lossless_coding/FixedToVariableCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def encode(self, input: npt.ArrayLike) -> npt.NDArray[np.integer]:
>>> code.encode([0, 1, 0]) # Not a multiple of the source block size
Traceback (most recent call last):
...
ValueError: length of 'input' must be a multiple of block size 2 (got 3)
ValueError: length of input must be a multiple of block size 2 (got 3)
>>> code.encode([0, 7, 0, 0]) # 07 is not a valid source word
Traceback (most recent call last):
Expand Down
2 changes: 1 addition & 1 deletion src/komm/_lossless_coding/VariableToFixedCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def decode(self, input: npt.ArrayLike) -> npt.NDArray[np.integer]:
>>> code.decode([1, 1, 0, 0, 1]) # Not a multiple of target block size
Traceback (most recent call last):
...
ValueError: length of 'input' must be a multiple of block size 2 (got 5)
ValueError: length of input must be a multiple of block size 2 (got 5)
>>> code.decode([0, 0, 1, 1]) # 11 is not a valid target word
Traceback (most recent call last):
Expand Down
18 changes: 8 additions & 10 deletions src/komm/_lossless_coding/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def parse_fixed_length(
) -> npt.NDArray[np.integer]:
if input.size % block_size != 0:
raise ValueError(
"length of 'input' must be a multiple of block size"
"length of input must be a multiple of block size"
f" {block_size} (got {len(input)})"
)
try:
Expand All @@ -63,23 +63,21 @@ def parse_prefix_free(
allow_incomplete: bool,
) -> npt.NDArray[np.integer]:
output: list[int] = []
i, j = 0, 0
while j < len(input):
j += 1
key = tuple(input[i:j])
i = 0
for j in range(len(input)):
key = tuple(input[i : j + 1])
if key in dictionary:
output.extend(dictionary[key])
i = j
i = j + 1

if i == len(input):
return np.asarray(output)

if not allow_incomplete:
elif not allow_incomplete:
raise ValueError("input contains invalid word")

remaining = tuple(input[i:])
remainder = tuple(input[i:])
for key, value in dictionary.items():
if is_prefix_of(remaining, key):
if is_prefix_of(remainder, key):
output.extend(value)
return np.asarray(output)

Expand Down

0 comments on commit 22da35d

Please sign in to comment.