Click here to Skip to main content
11,494,851 members (58,777 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads
Add your own
alternative version

Project Tool

, 23 Sep 2007 CPOL 37K 1.2K 70
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
projecttool.zip
ProjectTool.exe
projecttool_0908_.zip
bin
Release
ICSharpCode.SharpZipLib.dll
ProjectTool.exe
CodeLib
Controls
ProjectTool
Items
Project
Propertys
Solution
Icon
cs.png
default.ico
dir.png
diro.png
dll.png
projecttool_20070924.zip
ProjectTool.exe
Checksums
Compress
Deflate
Common
Decode
Encode
Formats
Zip
HuffmanCoding
LempelZivWelch
LempelZiv
Resources
Icon
cs.png
default.ico
dir.png
diro.png
dll.png
projecttool_src.zip
ProjectTool.exe
cs.png
default.ico
dir.png
diro.png
dll.png
release.zip
ICSharpCode.SharpZipLib.dll
ProjectTool.exe
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace QiHe.CodeLib.Compress
{
    public partial class HuffmanTree
    {
        public int[] CodeLengths;
        int[] Codes;
        BinaryTree Tree;
        public HuffmanTree(int[] codeLengths)
        {
            CodeLengths = codeLengths;
            Codes = DeflateHuffmanCoding.AssignCodes(codeLengths);
        }

        public static HuffmanTree FromCodeLengths(int[] codeLengths)
        {
            HuffmanTree tree = new HuffmanTree(codeLengths);
            tree.BuildTree();
            return tree;
        }

        public int ReadSymbol(BitStream input)
        {
            long pos = input.Position;
            int bits = input.bitsRead;
            BinaryTreeNode node = Tree.Root;
            while (node is BinaryTreeInternalNode)
            {
                int bit = input.ReadBit();
                if (bit == -1)
                {
                    throw new Exception("data corupted");
                }
                node = ((BinaryTreeInternalNode)node).ChildNodes[bit];
            }
            return ((BinaryTreeLeafNode)node).Symbol;
        }

        public void WriteSymbol(BitStream output, int symbol)
        {
            output.WriteBitsBigEndian(Codes[symbol], CodeLengths[symbol]);
        }

        public string ListSymbolCodes()
        {
            StringBuilder text = new StringBuilder();
            foreach (Pair<int, string> pair in Tree.GetSymbolCodePairs(true))
            {
                text.AppendFormat("{0}\t{1}\r\n", pair.Left, pair.Right);
            }
            return text.ToString();
        }

        private void BuildTree()
        {
            BinaryTreeInternalNode root = new BinaryTreeInternalNode();
            for (int symbol = 0; symbol < Codes.Length; symbol++)
            {
                int length = CodeLengths[symbol];
                if (length == 0) continue; //!important
                int code = Codes[symbol];
                BitArray bits = new BitArray(BitConverter.GetBytes(code));
                BinaryTreeInternalNode parent = root;
                for (int i = 0; i < length - 1; i++)
                {
                    parent = CreateInternalNode(parent, Convert.ToInt32(bits[length - 1 - i]));
                }
                BinaryTreeLeafNode leaf = new BinaryTreeLeafNode(symbol);
                parent.ChildNodes[Convert.ToInt32(bits[0])] = leaf;
            }
            Tree = new BinaryTree(root);
        }

        static BinaryTreeInternalNode CreateInternalNode(BinaryTreeInternalNode parent, int index)
        {
            if (parent.ChildNodes[index] == null)
            {
                parent.ChildNodes[index] = new BinaryTreeInternalNode();
            }
            if (parent.ChildNodes[index] is BinaryTreeInternalNode)
            {
                return parent.ChildNodes[index] as BinaryTreeInternalNode;
            }
            else
            {
                throw new Exception("Inconsistent Huffman tree structure.");
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Liu Junfeng
Software Developer (Senior) Beyondsoft SH
China China
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 23 Sep 2007
Article Copyright 2006 by Liu Junfeng
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid