Exemplo n.º 1
0
        public (string, HuffmanChain) Condense(string w)
        {
            var chain = new HuffmanChain(w);

            if (w.Length == 0)
            {
                return("", chain);
            }


            var b         = new BitInputStream();
            var charArray = w.ToCharArray();

            char first = charArray[0], pre = first;

            b.AddFromStringSource(chain[first]);

            for (var i = 1; i < charArray.Length; i++)
            {
                var current = charArray[i];

                var relPath = chain[current, pre];
                b.AddFromStringSource(relPath);

                pre = current;
            }

            var output = new StringBuilder();

            b.Flush();
            output.Append(b.ToChars());

            return(output.ToString(), chain);
        }
Exemplo n.º 2
0
        public bool Equals(HuffmanChain other)
        {
            if (!other.overall.Equals(overall))
            {
                return(false);
            }
            if (other.trees.Keys.Except(trees.Keys).Any())
            {
                return(false);
            }

            foreach (var treesKey in trees.Keys)
            {
                if (!trees[treesKey].Equals(other.trees[treesKey]))
                {
                    return(false);
                }
            }

            return(true);
        }