Are you an LLM? You can read better optimized documentation at /compress-kit/en/api/cpp.md for this page in Markdown format
C++ Implementation Reference
See also: Streaming API
All C++ implementations keep single-file algorithm cores, but now depend on a shared streaming/buffer facade under algorithms/shared/cpp/include/compresskit/.
Compilation
bash
g++ -std=c++17 -O2 -Wall -Wextra -o <binary> main.cpp1
Recommended Flags
| Flag | Purpose |
|---|---|
-std=c++17 | Enable C++17 features |
-O2 | Optimization level |
-Wall -Wextra | Warnings |
-fsanitize=address | AddressSanitizer (debug builds) |
-fsanitize=undefined | UBSan (debug builds) |
Huffman (algorithms/huffman/cpp/main.cpp)
Usage
bash
./huffman_cpp encode input.bin output.huf
./huffman_cpp decode output.huf decoded.bin1
2
2
Internal Structure
BitWriter/BitReader— bit-level I/O classesNode— Huffman tree node with custom deletercompress_file()/decompress_file()— main encode/decode functions
File Format
| Offset | Size | Field |
|---|---|---|
| 0 | 4B | Magic: HFMN |
| 4 | 4B | Frequency table size (always 257) |
| 8 | 1028B | Frequency table (257 × uint32 LE) |
| 1036+ | Variable | Encoded bit stream |
Arithmetic (algorithms/arithmetic/cpp/main.cpp)
Usage
bash
./arithmetic_cpp encode input.bin output.aenc
./arithmetic_cpp decode output.aenc decoded.bin1
2
2
Key Classes
ArithmeticEncoder— state machine withlow,high,pendingBitsArithmeticDecoder— decoder withcodeinitialization
Range Coder (algorithms/range/cpp/main.cpp)
Usage
bash
./rangecoder_cpp encode input.bin output.rcnc
./rangecoder_cpp decode output.rcnc decoded.bin1
2
2
File Format
| Offset | Size | Field |
|---|---|---|
| 0 | 4B | Magic: RCNC |
| 4 | 4B | Frequency table size |
| 8 | Variable | Frequency table |
| ... | Variable | Byte stream (renormalized intervals) |
RLE (algorithms/rle/cpp/main.cpp)
Usage
bash
./rle_cpp encode input.bin output.rle
./rle_cpp decode output.rle decoded.bin1
2
2
File Format
| Offset | Size | Field |
|---|---|---|
| 0 | 4B | Magic: RLE\x00 |
| 4+ | Variable | Repeated (count: uint32 LE, value: byte) pairs |
Common Patterns
| Pattern | Description |
|---|---|
| Single-file core | Each algorithm core in one main.cpp |
| Shared dependencies | Uses common code from algorithms/shared/cpp/ |
| Error handling | fprintf(stderr, ...) + exit(1) |
| Memory management | std::unique_ptr with custom deleters |
Shared Streaming Facade
The streaming and buffer helpers live in:
compresskit/result.hppcompresskit/encoder.hppcompresskit/buffer_api.hppcompresskit/algorithms.hpp
These headers provide a common Encoder / Decoder lifecycle and algorithm factories such as compresskit::make_huffman_encoder().