Exemple #1
0
            public void ReturnsPointer_257_2_From_10001000000010001()
            {
                var lzByteConverter = new LZByteConverter();
                var expected        = new PointerByte(258, 2);
                var ub = new UnevenByte(69649, 17);

                var actual = lzByteConverter.ToEncodedByte(ub) as PointerByte;

                Assert.AreEqual(expected, actual);
            }
        public UnevenByte GetRange(int index, int length)
        {
            var ub = new UnevenByte(0, 0);

            for (var i = 0; i < length; ++i)
            {
                ub += this[index + i];
            }

            return(ub);
        }
 /// <summary>
 /// This method is a recursive method. It creates a decoding dictionary from BitIndexer until
 /// the Huffman tree is complete. A 0 bit indicate a 'branch' and a 1 bit indicate a 'leaf'.
 /// </summary>
 /// <param name="code"> It is the decoding code that gets inherit to a 'leaf'. </param>
 private void AddDictionaryEntries(UnevenByte code)
 {
     if (_bitIndexer.GetNext() == UnevenByte.Zero)
     {
         AddDictionaryEntries(code + UnevenByte.Zero);
         AddDictionaryEntries(code + UnevenByte.One);
     }
     else
     {
         byte b = (byte)_bitIndexer.GetNextRange(8).Data;
         _decodeDictionary.Add(code, b);
     }
 }
Exemple #4
0
        /// <summary>
        ///     This method encodes the dictionary for the decoder. The method is rucursive and it calls
        ///     itself until it meets a LeafNode.
        /// </summary>
        /// <param name="node"> Indicates what node it is in the RootNode </param>
        private void EncodeTree(Node node)
        {
            if (node is BranchNode branchNode)
            {
                EncodedTreeList.Add(UnevenByte.Zero);

                EncodeTree(branchNode.LeftNode);
                EncodeTree(branchNode.RightNode);
            }
            else if (node is LeafNode leafNode)
            {
                var symbolAsUB = new UnevenByte(leafNode.Symbol, 8);
                EncodedTreeList.Add(UnevenByte.One + symbolAsUB);
            }
        }
        /// <summary>
        ///     Converts an UnevenByte to EncodedLZByte based on the control bit-
        /// </summary>
        /// <param name="unevenByte"> UnevenByte to convert. </param>
        public EncodedLZByte ToEncodedByte(UnevenByte unevenByte)
        {
            // Check if control bit is 1
            if (unevenByte[0] == 1)
            {
                unevenByte -= 1;
                var pointerData = unevenByte.GetBits(PointerByte.POINTER_SIZE);
                unevenByte -= PointerByte.POINTER_SIZE;
                var lengthData = unevenByte.GetBits(PointerByte.LENGTH_SIZE);
                return(new PointerByte(pointerData + 1, lengthData + 1));
            }

            unevenByte -= 1;
            return(new RawByte((byte)unevenByte.Data));
        }
 public int GetUnevenByteLength(UnevenByte controlBit)
 {
     return(controlBit == UnevenByte.One
         ? 1 + PointerByte.POINTER_SIZE + PointerByte.LENGTH_SIZE
         : 1 + RawByte.RAW_SIZE);
 }