feat: add LZ77 compression algorithm #982
Merged
+429
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds an implementation of the LZ77 compression algorithm to the
compressionmodule.What is LZ77?
LZ77 is a lossless data compression algorithm published by Abraham Lempel and Jacob Ziv in 1977. It uses a sliding-window approach to identify repeated patterns and forms the basis for many modern compression formats including:
Algorithm Details
LZ77 works by maintaining a sliding window divided into:
The algorithm encodes data as triplets (tokens):
Implementation Features
✅ Configurable window size and lookahead buffer size
✅ Token-based encoding using
(offset, length, indicator)triplets✅ Comprehensive test coverage (13+ test cases)
✅ Full rustdoc documentation with examples
✅ Zero external dependencies
✅ Efficient implementation with
O(n²)worst-case complexityAPI
Code Quality
cargo test)cargo clippy -- -D warnings)cargo fmt)Files Modified
src/compression/lz77.rs(new file, ~280 lines)src/compression/mod.rs(added module declaration and re-exports)Testing
The implementation includes extensive tests:
Example Usage
Compression Examples
References
Checklist
cargo test)cargo fmt)cargo clippy -- -D warnings)mod.rsNote: This implementation prioritizes clarity and correctness over raw performance. While the algorithm has
O(n²)worst-case complexity, it's suitable for educational purposes and moderate-sized inputs. Production implementations often use hash tables or suffix arrays for optimization.