Click here to Skip to main content
12,251,686 members (59,592 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads

Stats

38.9K views
1.3K downloads
71 bookmarked
Posted

Project Tool

, 23 Sep 2007 CPOL
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
ProjectTool.exe
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.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.exe
cs.png
default.ico
dir.png
diro.png
dll.png
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

You may also be interested in...

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