public void Encode()
        {
            Dictionary <byte, int> symbolQuantityDic = createDictionary();
            var           builder      = new HuffmanCodecBuilder <byte>();
            var           tree         = builder.BuildTree(Comparer <byte> .Default, symbolQuantityDic);
            ICoder <byte> huffmanCoder = builder.GetCoder(tree);

            huffmanCoder.Encode(new StandardHuffmanCoderInput(inputReader), new HuffmanCoderOutput(coderOutputWriter));
            coderOutputWriter.CreateFileBytes(HuffmanEncodeModel.Standard, false, SymbolQuantityMapConverter.StandardIntToExtConvert(symbolQuantityDic, huffmanCoder.GetEncodingDictionary()));
        }
        public void GetCoder_ReturnsOneSymbolCoder_WhenOneSymbolOnly()
        {
            //given
            var builder  = new HuffmanCodecBuilder <char>();
            var treeRoot = new HuffmanTreeNode <char>(value: 'a', quantity: 1);
            //when
            var coder = builder.GetCoder(treeRoot);

            //then
            Assert.IsInstanceOfType(coder, typeof(OneSymbolCoder <char>));
        }
Esempio n. 3
0
        public void Encode()
        {
            var symbolQuantityDic = createDictionary();
            var builder           = new HuffmanCodecBuilder <Tuple <byte, DefaultableSymbol <byte> > >();
            var tree       = builder.BuildTree(new PairComparer(), symbolQuantityDic);
            var coder      = builder.GetCoder(tree);
            var coderInput = new PairHuffmanCoderInput(inputReader);

            coder.Encode(coderInput, new HuffmanCoderOutput(coderOutputWriter));
            coderOutputWriter.CreateFileBytes(HuffmanEncodeModel.Block, coderInput.isSpecialSymbol, SymbolQuantityMapConverter.PairIntToExtConvert(symbolQuantityDic, coder.GetEncodingDictionary()));
        }
        private Dictionary <DefaultableSymbol <byte>, ICoder <byte> > createCoderDictionary()
        {
            Dictionary <DefaultableSymbol <byte>, Dictionary <byte, int> > perSymbolDictionary = createSymbolDictionary();
            Dictionary <DefaultableSymbol <byte>, ICoder <byte> >          coderDictionary     = new Dictionary <DefaultableSymbol <byte>, ICoder <byte> >();

            foreach (DefaultableSymbol <byte> key in perSymbolDictionary.Keys)
            {
                var           builder      = new HuffmanCodecBuilder <byte>();
                var           tree         = builder.BuildTree(Comparer <byte> .Default, perSymbolDictionary[key]);
                ICoder <byte> huffmanCoder = builder.GetCoder(tree);
                coderDictionary.Add(key, huffmanCoder);
            }
            return(coderDictionary);
        }
        public void GetCoder_ReturnsHuffmanCoder_WhenMoreThanOneSymbol()
        {
            //given when
            var builder    = new HuffmanCodecBuilder <char>();
            var treeRoot   = new HuffmanTreeNode <char>(value: 'a', quantity: 1);
            var leftChild  = new HuffmanTreeNode <char>(value: 'b', quantity: 1);
            var rightChild = new HuffmanTreeNode <char>(value: 'c', quantity: 1);

            treeRoot.LeftChild  = leftChild;
            treeRoot.RightChild = rightChild;
            leftChild.Parent    = rightChild.Parent = treeRoot;
            //when
            var coder = builder.GetCoder(treeRoot);

            //then
            Assert.IsInstanceOfType(coder, typeof(HuffmanCoder <char>));
        }