コード例 #1
0
        private static CharEntryNode GetMin(List <CharEntryNode> charEntryNodes)
        {
            CharEntryNode minCharEntry = charEntryNodes
                                         .First(ce => ce.RepetitionsSum == charEntryNodes.Min(cen => cen.RepetitionsSum));

            charEntryNodes.Remove(minCharEntry);

            return(minCharEntry);
        }
コード例 #2
0
        private static Dictionary <char, string> FormeEcryptingTable(CharEntryNode rootCharEntry,
                                                                     List <CharEntry> charEntries)
        {
            Dictionary <char, string> encryptingTable = charEntries
                                                        .ToDictionary(ce => ce.Character, ce => (string)null);

            FormEncodingTableInternal(encryptingTable, rootCharEntry, "");

            return(encryptingTable);
        }
コード例 #3
0
        public static string Encrypt(string source)
        {
            char[] sourceCharacters = source.ToCharArray();

            List <CharEntry>          charEntries     = Parse(sourceCharacters);
            CharEntryNode             rootCharEntry   = FormTree(charEntries);
            Dictionary <char, string> encryptingTable = FormeEcryptingTable(rootCharEntry, charEntries);

            return(EncryptInternal(sourceCharacters, encryptingTable));
        }
コード例 #4
0
        private static CharEntryNode FormTreeInternal(List <CharEntryNode> charEntryNodes)
        {
            while (charEntryNodes.Count() != 1)
            {
                CharEntryNode oneCharEntry = GetMin(charEntryNodes);
                CharEntryNode twoCharEntry = GetMin(charEntryNodes);

                CharEntryNode charEntryNode = new CharEntryNode(oneCharEntry, twoCharEntry,
                                                                oneCharEntry.Value + twoCharEntry.Value,
                                                                oneCharEntry.RepetitionsSum + twoCharEntry.RepetitionsSum);

                charEntryNodes.Add(charEntryNode);
            }

            return(charEntryNodes.First());
        }
コード例 #5
0
        private static void FormEncodingTableInternal(Dictionary <char, string> encryptingTable,
                                                      CharEntryNode charEntry, string encoding)
        {
            if (charEntry.LeftNode != null)
            {
                FormEncodingTableInternal(encryptingTable, charEntry.LeftNode, encoding + "0");
            }

            if (charEntry.RightNode != null)
            {
                FormEncodingTableInternal(encryptingTable, charEntry.RightNode, encoding + "1");
            }

            if (charEntry.LeftNode == null && charEntry.RightNode == null)
            {
                encryptingTable[Convert.ToChar(charEntry.Value)] = encoding;
            }
        }