Lossless text compression using adaptive Splay Tree prefix coding
Project description
Splay Tree Compression
Lossless text compression using adaptive Splay Tree prefix coding.
This package implements an optimized Splay Tree-based compression algorithm with various performance improvements including:
- Array-based storage for better cache locality
- Semi-splaying strategies to reduce rotation overhead
- Block reset mechanisms for multi-domain data
- Configurable splay frequency
Installation
From local source
# Navigate to package directory
cd package
# Install in development mode (editable)
pip install -e .
# Or install normally
pip install .
From PyPI (if published)
pip install splaytree-compression
Quick Start
Command Line Interface
# Compress a file
splay-compress compress input.txt -o output.splay
# Compress with preset
splay-compress compress input.txt --preset fast
# Decompress
splay-compress decompress output.splay -o input.txt
Python API
from splaytree_compression import SplayCompressor
# Using preset
compressor = SplayCompressor(preset='balanced')
compressed = compressor.compress(data)
decompressed = compressor.decompress(compressed)
# Custom parameters
compressor = SplayCompressor.create_custom(
splay_every=4,
depth_threshold=10,
target_depth=1,
reset_block_bytes=64*1024
)
compressed = compressor.compress(data)
Low-level API
from splaytree_compression import SplayPrefixCoder
# Create coder
coder = SplayPrefixCoder(
alphabet_size=256,
splay_every=4,
depth_threshold=10,
target_depth=1,
reset_block_bytes=64*1024
)
# Compress
compressed = coder.compress(data)
# Decompress (parameters are stored in header)
decompressed = SplayPrefixCoder.decompress(compressed)
Compression Presets
-
fast: Optimized for speedsplay_every=8,target_depth=2- Best for large files where speed matters
-
balanced: Balanced speed/ratio (default)splay_every=4,depth_threshold=10,target_depth=1,reset_block_bytes=64KB- Good general-purpose setting
-
best_ratio: Best compression ratiosplay_every=1,target_depth=0(full splay)- Best compression but slower
Parameters
alphabet_size(int, default 256): Size of alphabet (2-256)splay_every(int, default 1): Splay every k symbols (k>=1)depth_threshold(int, optional): Splay when depth > thresholdtarget_depth(int, default 0): Target depth for semi-splay (0 = full splay to root)reset_block_bytes(int, optional): Reset tree every N bytes
Performance
Based on benchmarks, the optimized version achieves:
- ~3.75x speedup compared to baseline
- Compression ratio: ~0.75-0.78 (depending on data)
- Suitable for educational/research purposes
Note: This algorithm is not intended to compete with industrial compression algorithms like zlib or LZMA, but rather serves as a research tool to understand adaptive prefix coding with Splay Trees.
Documentation
INSTALL.md: Detailed installation instructionsPACKAGE_USAGE.md: Usage guide with examplesPUBLISH.md: Guide for publishing to PyPI
Examples
Compress with custom parameters
splay-compress compress input.txt \
--splay-every 8 \
--target-depth 2 \
-o output.splay
Batch compression
from pathlib import Path
from splaytree_compression import SplayCompressor
compressor = SplayCompressor(preset='balanced')
for file in Path('data').glob('*.txt'):
with open(file, 'rb') as f:
data = f.read()
compressed = compressor.compress(data)
with open(file.with_suffix('.splay'), 'wb') as f:
f.write(compressed)
print(f"Compressed {file}: {len(data)} -> {len(compressed)} bytes")
Development
# Install in development mode with dev dependencies
pip install -e ".[dev]"
# Run tests
python test_package.py
# Format code
black splaytree_compression/
# Type checking
mypy splaytree_compression/
License
MIT License
References
Based on research paper: "Advanced Lossless Text Compression Algorithm Based on Splay Tree Adaptive Methods"
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file splaytree_compression-1.0.0.tar.gz.
File metadata
- Download URL: splaytree_compression-1.0.0.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c499d0ae1cf9d6a0148d80ba8b338693ccb974233abe0185b6a7a25d522a997
|
|
| MD5 |
b56799825ba343b5701aec160087d155
|
|
| BLAKE2b-256 |
2a934bb871205b7a85589671e9fd8c919e4f68dd572854cc8321b15d364d0697
|
File details
Details for the file splaytree_compression-1.0.0-py3-none-any.whl.
File metadata
- Download URL: splaytree_compression-1.0.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b40d380d07c5fde78293967ce8c042734a84e77279e1cd36df060eb455e1c474
|
|
| MD5 |
e48494ba29b7e21ba933f4f9b297da58
|
|
| BLAKE2b-256 |
3ecf982c1f5c149a8b1ef760075be2daf17c7f79eb15f9a1803f3cfbb7dd526f
|