# estoiko/huffman-codec architecture

> Architecture diagram of the GitHub repository estoiko/huffman-codec, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/estoiko/huffman-codec
- Repository: https://github.com/estoiko/huffman-codec

## Overview

This repository implements `huff`, a command-line Huffman file archiver. The main workflow parses a mode and paths, opens binary streams, then dispatches to encoding or decoding. Encoding counts byte frequencies, builds codes, writes packed bits and a frequency header; decoding validates that header, rebuilds the tree, and restores symbols. The tree, code representation, bit-stream operators and queue are the main internal layer. Source excerpts show these core relationships; no README capabilities were available to verify.

Main entry points: [`src/Encoder.cpp`](https://github.com/estoiko/huffman-codec/blob/main/src/Encoder.cpp) (Encode archive), [`src/Decoder.cpp`](https://github.com/estoiko/huffman-codec/blob/main/src/Decoder.cpp) (Decode archive), [`src/HuffmanTree.cpp`](https://github.com/estoiko/huffman-codec/blob/main/src/HuffmanTree.cpp) (Huffman tree), [`src/main.cpp`](https://github.com/estoiko/huffman-codec/blob/main/src/main.cpp) (CLI and dispatch)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI and streams"]
  node_main["CLI and dispatch<br/>[main.cpp]"]
  node_files(("Input/output files"))
end

subgraph group_encode["Encoding"]
  node_encoder["Encode archive<br/>[Encoder.cpp]"]
  node_frequency["Frequency and header<br/>[Encoder.cpp]"]
end

subgraph group_decode["Decoding"]
  node_decoder["Decode archive<br/>[Decoder.cpp]"]
  node_validation["Header validation<br/>[Decoder.cpp]"]
end

subgraph group_internals["Huffman internals"]
  node_tree["Huffman tree<br/>[HuffmanTree.cpp]"]
  node_codes["Code representation<br/>[HuffmanCode.h]"]
  node_bits["Bit stream I/O<br/>[BitOperators.cpp]"]
  node_queue["Priority queue<br/>[Queue.h]"]
  node_errors["Error types<br/>[Exceptions.h]"]
end

node_user(("User"))

node_user -->|"invokes"| node_main
node_main -->|"opens streams"| node_files
node_main -->|"dispatches encode"| node_encoder
node_main -->|"dispatches decode"| node_decoder
node_encoder -->|"counts and writes"| node_frequency
node_encoder -->|"builds codes"| node_tree
node_encoder -->|"writes bits"| node_bits
node_encoder -->|"uses codes"| node_codes
node_decoder -->|"extracts header"| node_validation
node_decoder -->|"builds decode tree"| node_tree
node_decoder -->|"reads bits"| node_bits
node_tree -->|"orders nodes"| node_queue
node_tree -->|"generates codes"| node_codes
node_encoder -->|"throws errors"| node_errors
node_decoder -->|"throws errors"| node_errors
node_tree -->|"rejects empty input"| node_errors

click node_main "https://github.com/estoiko/huffman-codec/blob/main/src/main.cpp"
click node_encoder "https://github.com/estoiko/huffman-codec/blob/main/src/Encoder.cpp"
click node_frequency "https://github.com/estoiko/huffman-codec/blob/main/src/Encoder.cpp"
click node_decoder "https://github.com/estoiko/huffman-codec/blob/main/src/Decoder.cpp"
click node_validation "https://github.com/estoiko/huffman-codec/blob/main/src/Decoder.cpp"
click node_tree "https://github.com/estoiko/huffman-codec/blob/main/src/HuffmanTree.cpp"
click node_codes "https://github.com/estoiko/huffman-codec/blob/main/src/HuffmanCode.h"
click node_bits "https://github.com/estoiko/huffman-codec/blob/main/src/BitOperators.cpp"
click node_queue "https://github.com/estoiko/huffman-codec/blob/main/src/Queue.h"
click node_errors "https://github.com/estoiko/huffman-codec/blob/main/src/Exceptions.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_files,node_user toneBlue
class node_encoder,node_frequency toneAmber
class node_decoder,node_validation toneMint
class node_tree,node_codes,node_bits,node_queue,node_errors toneRose
```

## Components

### CLI and streams

- [CLI and dispatch](https://github.com/estoiko/huffman-codec/blob/main/src/main.cpp): `src/main.cpp`
- **Input/output files**

### Encoding

- [Encode archive](https://github.com/estoiko/huffman-codec/blob/main/src/Encoder.cpp): `src/Encoder.cpp`
- [Frequency and header](https://github.com/estoiko/huffman-codec/blob/main/src/Encoder.cpp): `src/Encoder.cpp`

### Decoding

- [Decode archive](https://github.com/estoiko/huffman-codec/blob/main/src/Decoder.cpp): `src/Decoder.cpp`
- [Header validation](https://github.com/estoiko/huffman-codec/blob/main/src/Decoder.cpp): `src/Decoder.cpp`

### Huffman internals

- [Huffman tree](https://github.com/estoiko/huffman-codec/blob/main/src/HuffmanTree.cpp): `src/HuffmanTree.cpp`
- [Code representation](https://github.com/estoiko/huffman-codec/blob/main/src/HuffmanCode.h): `src/HuffmanCode.h`
- [Bit stream I/O](https://github.com/estoiko/huffman-codec/blob/main/src/BitOperators.cpp): `src/BitOperators.cpp`
- [Priority queue](https://github.com/estoiko/huffman-codec/blob/main/src/Queue.h): `src/Queue.h`
- [Error types](https://github.com/estoiko/huffman-codec/blob/main/src/Exceptions.h): `src/Exceptions.h`

### Other components

- **User**

## Connections

- User → CLI and dispatch: invokes (evidence: `src/main.cpp`)
- CLI and dispatch → Input/output files: opens streams (evidence: `src/main.cpp`)
- CLI and dispatch → Encode archive: dispatches encode (evidence: `src/main.cpp`)
- CLI and dispatch → Decode archive: dispatches decode (evidence: `src/main.cpp`)
- Encode archive → Frequency and header: counts and writes (evidence: `src/Encoder.cpp`)
- Encode archive → Huffman tree: builds codes (evidence: `src/Encoder.cpp`)
- Encode archive → Bit stream I/O: writes bits (evidence: `src/Encoder.cpp`)
- Encode archive → Code representation: uses codes (evidence: `src/Encoder.cpp`)
- Decode archive → Header validation: extracts header (evidence: `src/Decoder.cpp`)
- Decode archive → Huffman tree: builds decode tree (evidence: `src/Decoder.cpp`)
- Decode archive → Bit stream I/O: reads bits (evidence: `src/Decoder.cpp`)
- Huffman tree → Priority queue: orders nodes (evidence: `src/HuffmanTree.cpp`)
- Huffman tree → Code representation: generates codes (evidence: `src/HuffmanTree.cpp`)
- Encode archive → Error types: throws errors (evidence: `src/Encoder.cpp`)
- Decode archive → Error types: throws errors (evidence: `src/Decoder.cpp`)
- Huffman tree → Error types: rejects empty input (evidence: `src/HuffmanTree.cpp`)
