const int ChunkSize = 128 * 1024; // 128Kb throttling

        public EncodingToken CreateEncodingToken(IEncodingInputStream inputStream, CancellationToken cancellationToken)
        {
            Guard.IsNotNull(inputStream, nameof(inputStream));

            WeightsTable    weightsTable = BuildWeightsTable(inputStream, cancellationToken);
            HuffmanTreeBase huffmanTree  = BuildHuffmanTree(weightsTable);
            CodingTable     codingTable  = BuildCodingTable(huffmanTree);

            return(new EncodingToken(weightsTable, huffmanTree, codingTable));
        }
        public long Encode(IEncodingInputStream inputStream, IEncodingOutputStream outputStream, EncodingToken encodingToken, CancellationToken cancellationToken, IProgressHandler progress)
        {
            Guard.IsNotNull(inputStream, nameof(inputStream));
            Guard.IsNotNull(outputStream, nameof(outputStream));
            Guard.IsNotNull(encodingToken, nameof(encodingToken));

            CodingTable codingTable = encodingToken.CodingTable;

            inputStream.Reset();
            long sequenceLength = 0;
            long progressValue  = progress?.State.CastTo <CodingProgressState>()?.Value ?? 0;

            while (inputStream.ReadSymbol(out byte symbol))
            {
                BitSequence codingSequence = codingTable[symbol];
                for (int n = 0; n < codingSequence.Size; n++)
                {
                    Bit bit = codingSequence[n];
                    sequenceLength++;
                    outputStream.WriteBit(bit);
                }
                // Throttling
                if (++progressValue == ChunkSize)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    progress?.Report(ChunkSize, inputStream.Path);
                    progressValue = 0;
                }
            }
            if (progress != null)
            {
                CodingProgressState progressState = (CodingProgressState)progress.State ?? new CodingProgressState();
                progress.State = progressState.WithValue(progressValue);
            }
            return(sequenceLength);
        }
 public EncodingToken(WeightsTable weightsTable, HuffmanTreeBase huffmanTree, CodingTable codingTable)
 {
     CodingTable  = codingTable;
     WeightsTable = weightsTable;
     HuffmanTree  = huffmanTree;
 }
 public TreeVisitor()
 {
     this.codingTable = new CodingTable();
     this.bitSequence = new BitSequence();
 }