Example #1
0
            private AdaptiveHuffmanTreeNode HighestWithSameCountFGK(AdaptiveHuffmanTreeNode current)
            {
                AdaptiveHuffmanTreeNode highest = current;

                if (current.Parent != null)
                {
                    AdaptiveHuffmanTreeNode parent = current.Parent;

                    if (parent.LeftChild == current && parent.RightChild.Count == current.Count)
                    {
                        highest = parent.RightChild;
                    }

                    if (parent.Parent != null)
                    {
                        AdaptiveHuffmanTreeNode grandparent = parent.Parent;

                        if (grandparent.LeftChild == parent && grandparent.RightChild.Count == current.Count)
                        {
                            highest = grandparent.RightChild;
                        }
                        else if (grandparent.RightChild == parent && grandparent.LeftChild.Count == current.Count)
                        {
                            highest = grandparent.LeftChild;
                        }
                    }
                }

                return(highest);
            }
Example #2
0
            private void SwapNodes(AdaptiveHuffmanTreeNode node1, AdaptiveHuffmanTreeNode node2)
            {
                int num = node1.Number;

                node1.Number = node2.Number;
                node2.Number = num;

                AdaptiveHuffmanTreeNode oldLeft1 = node1.Parent.LeftChild, oldLeft2 = node2.Parent.LeftChild;

                if (oldLeft1 == node1)
                {
                    node1.Parent.LeftChild = node2;
                }
                else
                {
                    node1.Parent.RightChild = node2;
                }
                if (oldLeft2 == node2)
                {
                    node2.Parent.LeftChild = node1;
                }
                else
                {
                    node2.Parent.RightChild = node1;
                }

                AdaptiveHuffmanTreeNode parent = node1.Parent;

                node1.Parent = node2.Parent;
                node2.Parent = parent;
            }
Example #3
0
            public int[] GetBitEncoding(AdaptiveHuffmanTreeNode node)
            {
                ArrayList list = new ArrayList();

                while (node.Parent != null)
                {
                    list.Add(node.Parent.LeftChild == node ? 0 : 1);
                    node = node.Parent;
                }

                list.Reverse();
                return((int[])list.ToArray(typeof(int)));
            }
Example #4
0
            public AdaptiveHuffmanTree()
            {
                this.root = new AdaptiveHuffmanTreeNode(AdaptiveHuffmanTreeNodeType.Internal, 0, 1, maxnodes);
                this.root.LeftChild = new AdaptiveHuffmanTreeNode(AdaptiveHuffmanTreeNodeType.NYT, 0, 0, maxnodes - 2);
                this.root.RightChild = new AdaptiveHuffmanTreeNode(AdaptiveHuffmanTreeNodeType.EndOfStream, 0, 1, maxnodes - 1);

                this.root.LeftChild.Parent = this.root;
                this.root.RightChild.Parent = this.root;

                this.nodetable = new Hashtable(maxnodes, .5f);

                this.nodetable[nytkey] = this.root.LeftChild;
                this.nodetable[eoskey] = this.root.RightChild;
            }
Example #5
0
            public AdaptiveHuffmanTree()
            {
                this.root            = new AdaptiveHuffmanTreeNode(AdaptiveHuffmanTreeNodeType.Internal, 0, 1, maxnodes);
                this.root.LeftChild  = new AdaptiveHuffmanTreeNode(AdaptiveHuffmanTreeNodeType.NYT, 0, 0, maxnodes - 2);
                this.root.RightChild = new AdaptiveHuffmanTreeNode(AdaptiveHuffmanTreeNodeType.EndOfStream, 0, 1, maxnodes - 1);

                this.root.LeftChild.Parent  = this.root;
                this.root.RightChild.Parent = this.root;

                this.nodetable = new Hashtable(maxnodes, .5f);

                this.nodetable[nytkey] = this.root.LeftChild;
                this.nodetable[eoskey] = this.root.RightChild;
            }
Example #6
0
            private AdaptiveHuffmanTreeNode AddValueNode(byte value, int initialCount)
            {
                AdaptiveHuffmanTreeNode oldNYT = GetNYTNode();

                oldNYT.Type = AdaptiveHuffmanTreeNodeType.Internal;

                oldNYT.LeftChild =
                    new AdaptiveHuffmanTreeNode(AdaptiveHuffmanTreeNodeType.NYT, 0, 0, oldNYT.Number - 2);
                oldNYT.LeftChild.Parent = oldNYT;

                oldNYT.RightChild =
                    new AdaptiveHuffmanTreeNode(AdaptiveHuffmanTreeNodeType.Value, value, initialCount, oldNYT.Number - 1);
                oldNYT.RightChild.Parent = oldNYT;

                this.nodetable[nytkey] = oldNYT.LeftChild;
                this.nodetable[value]  = oldNYT.RightChild;

                return(oldNYT.RightChild);
            }
Example #7
0
        private void WriteByte(byte value)
        {
            AdaptiveHuffmanTreeNode node = tree.GetValueNode(value);

            if (node == null)
            {
                node = tree.GetNYTNode();
            }

            int[] bits = tree.GetBitEncoding(node);
            WriteBits(bits, 0, bits.Length);

            if (node.Type == AdaptiveHuffmanTreeNodeType.NYT)
            {
                WriteByteBits(value);
            }

            tree.UpdateTree(value);
        }
Example #8
0
            private void SwapNodes(AdaptiveHuffmanTreeNode node1, AdaptiveHuffmanTreeNode node2)
            {
                int num = node1.Number;
                node1.Number = node2.Number;
                node2.Number = num;

                AdaptiveHuffmanTreeNode oldLeft1 = node1.Parent.LeftChild, oldLeft2 = node2.Parent.LeftChild;
                if (oldLeft1 == node1)
                    node1.Parent.LeftChild = node2;
                else
                    node1.Parent.RightChild = node2;
                if (oldLeft2 == node2)
                    node2.Parent.LeftChild = node1;
                else
                    node2.Parent.RightChild = node1;

                AdaptiveHuffmanTreeNode parent = node1.Parent;
                node1.Parent = node2.Parent;
                node2.Parent = parent;
            }
Example #9
0
        private int ReadByte()
        {
            AdaptiveHuffmanTreeNode node = this.tree.RootNode;

            while (node.LeftChild != null)
            {
                int bit = ReadBit();

                if (bit < 0)
                {
                    return(-1);
                }

                if (bit == 0)
                {
                    node = node.LeftChild;
                }
                else if (bit == 1)
                {
                    node = node.RightChild;
                }
            }

            if (node.Type == AdaptiveHuffmanTreeNodeType.EndOfStream)
            {
                return(-1);
            }

            int readvalue = (node.Type == AdaptiveHuffmanTreeNodeType.NYT) ? ReadByteBits() : node.Value;

            if (readvalue < 0)
            {
                return(readvalue);
            }

            this.tree.UpdateTree((byte)readvalue);

            return(readvalue);
        }
Example #10
0
            public void UpdateTree(byte value)
            {
                AdaptiveHuffmanTreeNode current = GetValueNode(value);

                if (current == null)
                {
                    current = AddValueNode(value, 0);
                }

                do
                {
                    AdaptiveHuffmanTreeNode highest = HighestWithSameCountFGK(current);

                    if (current != highest && current.Parent != highest)
                    {
                        SwapNodes(current, highest);
                    }

                    current.Count++;
                    current = current.Parent;
                } while (current != null);
            }
Example #11
0
            private AdaptiveHuffmanTreeNode HighestWithSameCountFGK(AdaptiveHuffmanTreeNode current)
            {
                AdaptiveHuffmanTreeNode highest = current;

                if (current.Parent != null)
                {
                    AdaptiveHuffmanTreeNode parent = current.Parent;
                    
                    if (parent.LeftChild == current && parent.RightChild.Count == current.Count)
                        highest = parent.RightChild;

                    if (parent.Parent != null)
                    {
                        AdaptiveHuffmanTreeNode grandparent = parent.Parent;

                        if (grandparent.LeftChild == parent && grandparent.RightChild.Count == current.Count)
                            highest = grandparent.RightChild;
                        else if (grandparent.RightChild == parent && grandparent.LeftChild.Count == current.Count)
                            highest = grandparent.LeftChild;
                    }
                }

                return highest;
            }
Example #12
0
            public int[] GetBitEncoding(AdaptiveHuffmanTreeNode node)
            {
                ArrayList list = new ArrayList();

                while (node.Parent != null)
                {
                    list.Add(node.Parent.LeftChild == node ? 0 : 1);
                    node = node.Parent;
                }

                list.Reverse();
                return (int[])list.ToArray(typeof(int));
            }