예제 #1
0
        public byte[] Huff()
        {
            var         coladeprioridad = new Coladeprioridad <HuffmanNode <T> >();
            List <byte> palabras        = new List <byte>();

            //int conocer;
            //int repfrec = 1;
            //foreach (byte value in counts.Keys)
            //{
            //    conocer = counts[value];
            //    int cantidad = (conocer > 0xffffff || conocer < 0) ? 4 : (conocer < 0xffff) ? (conocer < 0xff) ? 1 : 2 : 3;
            //    if (cantidad  > repfrec)
            //    {
            //        repfrec = conocer;
            //    }
            //}



            palabras.Add(Convert.ToByte(counts.Count));
            palabras.Add(Convert.ToByte(4));

            foreach (byte value in counts.Keys)
            {
                palabras.Add(value);
                palabras.AddRange(BitConverter.GetBytes(counts[value]));

                var node = new HuffmanNode <T>((double)counts[value] / valueCount, value);
                coladeprioridad.Enqueue(node.Probability, node);
                _leafDictionary[value] = node;
            }


            while (coladeprioridad.contar > 1)
            {
                HuffmanNode <T> hijoizquieda = coladeprioridad.Dequeue();
                HuffmanNode <T> rightSon     = coladeprioridad.Dequeue();
                var             parent       = new HuffmanNode <T>(hijoizquieda, rightSon);
                coladeprioridad.Enqueue(parent.Probability, parent);
            }

            _root        = coladeprioridad.Dequeue();
            _root.IsZero = false;
            return(palabras.ToArray());
        }
예제 #2
0
        public void Encode(byte value, List <bool> encoding)
        {
            if (!_leafDictionary.ContainsKey(value))
            {
                throw new ArgumentException("Invalid value in Encode");
            }
            HuffmanNode <T> nodeCur         = _leafDictionary[value];
            var             reverseEncoding = new List <bool>();

            while (!nodeCur.IsRoot)
            {
                reverseEncoding.Add(nodeCur.Bit);
                nodeCur = nodeCur.Parent;
            }

            reverseEncoding.Reverse();
            encoding.AddRange(reverseEncoding);
        }
예제 #3
0
        public byte Decode(List <int> bitString, ref int position)
        {
            HuffmanNode <T> nodeCur = _root;

            while (!nodeCur.IsLeaf)
            {
                if (position > bitString.Count)
                {
                    //throw new ArgumentException("Invalid bitstring in Decode");
                    break;
                }
                if (position + 1 <= bitString.Count)
                {
                    nodeCur = bitString[position++] == 0 ? nodeCur.LeftSon : nodeCur.RightSon;
                }
                else
                {
                    break;
                }
            }
            return(nodeCur.Value);
        }