Huffman encoder/decoder built in C++ (C++23), based on Coding Challenges by John Crickett.
| Dependency | Version / notes |
|---|---|
| CMake | 3.15 or later |
| Build system | Ninja (used by presets) |
| C++ compiler | Any compiler with C++23 support (e.g. Clang 16+, GCC 13+) |
| Boost | Required; iostreams and headers (Boost.Multiprecision with GMP). Use a recent release (e.g. 1.70+). |
| GMP | Required (used by Boost.Multiprecision for arbitrary-precision counts). |
| Googletest | Fetched automatically by CMake (no manual install) |
You must install Boost (with iostreams) and GMP on your system before configuring. Everything else is handled by CMake.
macOS (Homebrew):
brew install cmake ninja boost gmp
# Use system Clang (Xcode Command Line Tools) or:
brew install llvmLinux (apt):
sudo apt update
sudo apt install cmake ninja-build build-essential libboost-all-dev libgmp-dev
# Or for a newer GCC: sudo apt install gcc-13 g++-13Windows:
Install CMake, Ninja, and a C++23-capable compiler (e.g. Visual Studio 2022 or LLVM/Clang). For Boost and GMP, use vcpkg (e.g. vcpkg install boost-iostreams gmp) or the official Boost binaries and GMP; set BOOST_ROOT or pass -DBOOST_ROOT=... when configuring if needed.
The repo includes a .clangd file so editors using clangd get consistent warnings and include paths. It uses machine-specific paths (e.g. Homebrew LLVM, Boost, and GMP on macOS). If you’re on a different OS or layout, either:
- Remove or rename
.clangdand rely oncompile_commands.jsonfrom the build (see below), or - Edit
.clangdand update:-I...for the project include directory (use your project root path).-isystempaths for LLVM libc++, Boost, and GMP to match your install.
After configuring with CMake, the build directory contains compile_commands.json, which many IDEs and clangd can use automatically, often making .clangd optional.
From the project root:
# Configure (pick one preset)
cmake --preset clang-debug # Debug build
# or
cmake --preset clang-release # Release build
# Build (use the same preset name as above)
cmake --build out/build/clang-debug
# or
cmake --build out/build/clang-release
# Run tests
ctest --test-dir out/build/clang-debug --output-on-failure
# or
ctest --test-dir out/build/clang-release --output-on-failureOne-liner for a Release build and test:
cmake --preset clang-release && cmake --build out/build/clang-release && ctest --test-dir out/build/clang-release --output-on-failureTests include unit tests (Googletest) and a round-trip test (RoundTripEncodeDecode): the tool encodes sample/test.txt to a compressed file, decodes it back, and diff confirms the decoded file matches the original. This test requires sample/test.txt to exist.
The main executable (after building) is:
- Debug:
out/build/clang-debug/cc-compression-tool-cpp - Release:
out/build/clang-release/cc-compression-tool-cpp
The tool supports compress (default) and decompress. Output paths are optional; defaults are output.compressed (compress) and output.decompressed (decompress).
Compress (default):
./cc-compression-tool-cpp <input_file> [output_file]
./cc-compression-tool-cpp --compress <input_file> [output_file]Decompress:
./cc-compression-tool-cpp --decompress <compressed_file> [output_file]Examples:
./cc-compression-tool-cpp sample/test.txt # -> output.compressed
./cc-compression-tool-cpp sample/test.txt out.compressed # -> out.compressed
./cc-compression-tool-cpp --decompress out.compressed # -> output.decompressed
./cc-compression-tool-cpp --decompress out.compressed out.txtInput and output paths must exist (input) or not exist (output) as appropriate. The compressed format is custom: header (prefix code table) then packed bit stream.
├── CMakeLists.txt # Main build and test definition
├── CMakePresets.json # clang-debug / clang-release presets
├── .clangd # Optional; adjust paths for your machine
├── include/ # Project include path (optional)
├── src/ # Application source and headers
│ ├── main.cpp
│ ├── compress.hpp # Compression entry (uses progress, file_handler, frequency_table, prefix_codes)
│ ├── decompress.hpp # Decompression entry (read_header, decode_and_write, progress)
│ ├── progress.hpp # Shared progress bar (used by compress and decompress)
│ ├── argument/ # CLI (--compress, --decompress, paths)
│ ├── exceptions/ # Custom exceptions
│ ├── file_handler/ # Input/output files; write_header, read_header, decode_and_write
│ ├── frequency_table/# Character frequency table (Huffman)
│ └── prefix_codes/ # Huffman prefix code generation
├── test/ # Googletest + round-trip CTest
│ ├── *_test.cpp # Unit tests (arguments, file_handler, output_file, read_header, decode_and_write, decompress, …)
│ └── roundtrip_encode_decode.cmake # CTest: encode sample/test.txt, decode, diff
├── sample/ # Sample data (test.txt used by round-trip test)
└── out/ # Build and install outputs (generated)
- Install CMake, Ninja, Boost (iostreams), GMP, and a C++23 compiler (see "Setup on your system").
- Configure, build, and test using the commands in “Build and test” (no code changes needed if your toolchain is standard).
- If you use clangd/IDE: update or disable
.clangdso include paths and compiler flags match your system or rely oncompile_commands.jsongenerated in the build directory.