Esempio n. 1
0
        private static void HuffmanTree(string path)
        {
            using (var File = new FileStream(path, FileMode.Open))
            {
                var Buffer = new byte[BufferLenght];
                using var Reader = new BinaryReader(File);
                TotalData        = Reader.BaseStream.Length;

                while (Reader.BaseStream.Position != Reader.BaseStream.Length)
                {
                    Buffer = Reader.ReadBytes(BufferLenght);

                    foreach (var item in Buffer)
                    {
                        if (FrequencyTable.Keys.Contains(item))
                        {
                            FrequencyTable[(item)]++;
                        }
                        else
                        {
                            FrequencyTable.Add(item, 1);
                        }
                    }
                }
            }

            List <HuffmanNode> FrequencyList = new List <HuffmanNode>();

            foreach (KeyValuePair <byte, int> Nodes in FrequencyTable)
            {
                FrequencyList.Add(new HuffmanNode(Nodes.Key, Convert.ToDecimal(Nodes.Value) / TotalData));
            }

            while (FrequencyList.Count > 1)
            {
                if (FrequencyList.Count == 1)
                {
                    break;
                }
                else
                {
                    FrequencyList = FrequencyList.OrderBy(x => x.Probability).ToList();
                    HuffmanNode Union = LinkNodes(FrequencyList[1], FrequencyList[0]);
                    FrequencyList.RemoveRange(0, 2);
                    FrequencyList.Add(Union);
                }
            }

            Root = FrequencyList[0];
        }
        /// <summary>
        /// checks all possible sets of cards from array
        /// in which some cards is already known and others aren't.
        /// Card is unknown if it is equal to "default(Card)".
        /// This method is recursive
        /// </summary>
        private void CheckPossibleSets(Card[] userCards, Card[] restCards, int unknownCardsCount)
        {
            #region BaseCase
            if (unknownCardsCount == 0)
            {
                CaseCount++;
                CardSet set = new CardSet(userCards);

                if (!FrequencyTable.ContainsKey(set.HighestCombination.Type))
                {
                    FrequencyTable.Add(set.HighestCombination.Type, 1);
                }
                else
                {
                    FrequencyTable[set.HighestCombination.Type]++;
                }

                return;
            }
            #endregion

            int index = 0;
            while (userCards[index] != default(Card))
            {
                index++;         // location first unknown cards poristion
            }
            unknownCardsCount--; // temponary decrease unknown cards count

            for (int c = 0; c < restCards.Length; c++)
            {
                if (restCards[c] == default(Card))
                {
                    continue;
                }

                Card temp = restCards[c];

                userCards[index] = restCards[c];
                restCards[c]     = default(Card);

                CheckPossibleSets(userCards, restCards, unknownCardsCount); // recursive step

                restCards[c] = temp;                                        //backtracking
            }


            userCards[index] = default(Card); //backtracking
        }
Esempio n. 3
0
        static void HuffmanTree(string path)
        {
            using var nameJumper = new StreamReader(path);
            var position = nameJumper.ReadLine().Length;

            nameJumper.Close();

            using (var File = new FileStream(path, FileMode.Open))
            {
                File.Position = position + 1;
                int    separator1   = 0;
                var    buffer       = new byte[BufferLenght];
                string Data_Lenght1 = "";
                string frequency    = "";
                string Datamount    = "";
                int    final        = 0;
                byte   bit          = new byte();
                using (var reader = new BinaryReader(File))
                {
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        buffer = reader.ReadBytes(BufferLenght);
                        foreach (var item in buffer)
                        {
                            if (separator1 == 0)
                            {
                                if (Convert.ToChar(item) == '|' || Convert.ToChar(item) == 'ÿ' || Convert.ToChar(item) == 'ß')
                                {
                                    separator1 = 1;
                                    if (Convert.ToChar(item) == '|')
                                    {
                                        Separator = '|';
                                    }
                                    else if (Convert.ToChar(item) == 'ÿ')
                                    {
                                        Separator = 'ÿ';
                                    }
                                    else
                                    {
                                        Separator = 'ß';
                                    }
                                }
                                else
                                {
                                    Data_Lenght1 += Convert.ToChar(item).ToString();
                                }
                            }
                            else if (separator1 == 2)
                            {
                                break;
                            }
                            else
                            {
                                if (final == 1 && Convert.ToChar(item) == Separator)
                                {
                                    final      = 2;
                                    separator1 = 2;
                                }
                                else
                                {
                                    final = 0;
                                }

                                if (Datamount == "")
                                {
                                    Datamount = Convert.ToChar(item).ToString(); bit = item;
                                }
                                else if (Convert.ToChar(item) == Separator && final == 0)
                                {
                                    FrequencyTable.Add(bit, Convert.ToInt32(frequency));
                                    Datamount = "";
                                    frequency = "";
                                    final     = 1;
                                }
                                else
                                {
                                    frequency += Convert.ToChar(item).ToString();
                                }
                            }
                        }
                    }
                }

                DataLenght = Convert.ToDecimal(Data_Lenght1);
            }

            List <HuffmanNode> FrequencyList = new List <HuffmanNode>();

            foreach (KeyValuePair <byte, int> Nodes in FrequencyTable)
            {
                FrequencyList.Add(new HuffmanNode(Nodes.Key, Convert.ToDecimal(Nodes.Value) / DataLenght));
            }

            FrequencyList = FrequencyList.OrderBy(x => x.Probability).ToList();

            while (FrequencyList.Count > 1)
            {
                FrequencyList = FrequencyList.OrderBy(x => x.Probability).ToList();
                HuffmanNode Link = LinkNodes(FrequencyList[1], FrequencyList[0]);
                FrequencyList.RemoveRange(0, 2);
                FrequencyList.Add(Link);
            }

            Root = FrequencyList[0];
        }
Esempio n. 4
0
        public void FrequencyTableTest() {
            /* Test the Add command and the GetTableAsArray command with strings */
            var table = new FrequencyTable<string>();

            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("B");
            table.Add("A");
            table.Add("A");
            table.Add("A");
            table.Add("A");
            table.Add("B");
            table.Add("B");
            table.Add("A");
            table.Add("C");
            table.Add("C");
            table.Add("C");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("r");
            table.Add("t");
            table.Add("t");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("r");
            table.Add("Z");
            table.Add("Z");
            table.Add("C");
            table.Add("C");
            table.Add("C");
            table.Add("t");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("Z");
            table.Add("t");
            table.Add("C");
            table.Add("d");
            table.Add("Z");
            table.Add("Z");
            table.Add("C");
            table.Add("C");
            table.Add("C");
            table.Add("e");
            table.Add("e");
            table.Add("e");
            table.Add("C");
            table.Add("C");
            table.Add("C");
            table.Add("C");
            table.Add("C");
            table.Add("C");
            table.Add("e");
            table.Add("C");
            table.Add("C");
            var r = table.GetTableAsArray(FrequencyTableSortOrder.None);
            var r1 = table.GetTableAsArray(FrequencyTableSortOrder.Value_Ascending);
            var r2 = table.GetTableAsArray(FrequencyTableSortOrder.Value_Descending);
            var r3 = table.GetTableAsArray(FrequencyTableSortOrder.Frequency_Ascending);
            var r4 = table.GetTableAsArray(FrequencyTableSortOrder.Frequency_Descending);

            Console.WriteLine("Table unsorted:");
            Console.WriteLine("***************************************************");
            foreach(FrequencyTableEntry<string> f in r) {
                log.Debug("{0}  {1}   {2}   {3}",
                          f.Value, f.AbsoluteFrequency, f.RelativeFrequency, Math.Round(f.Percentage, 2));
            }
            Console.WriteLine("***************************************************");
            Console.WriteLine("");
            Console.WriteLine("Table sorted by value - ascending:");
            Console.WriteLine("***************************************************");
            foreach(FrequencyTableEntry<string> f in r1) {
                log.Debug("{0}  {1}   {2}   {3}", f.Value, f.AbsoluteFrequency, f.RelativeFrequency,
                          Math.Round(f.Percentage, 2));
            }
            Console.WriteLine("***************************************************");
            Console.WriteLine("");
            Console.WriteLine("Table sorted by value - descending:");
            Console.WriteLine("***************************************************");
            foreach(FrequencyTableEntry<string> f in r2) {
                log.Debug("{0}  {1}   {2}   {3}", f.Value, f.AbsoluteFrequency, f.RelativeFrequency,
                          Math.Round(f.Percentage, 2));
            }
            Console.WriteLine("***************************************************");
            Console.WriteLine("");
            Console.WriteLine("Table sorted by frequency - ascending:");
            Console.WriteLine("***************************************************");
            foreach(FrequencyTableEntry<string> f in r3) {
                log.Debug("{0}  {1}   {2}   {3}", f.Value, f.AbsoluteFrequency, f.RelativeFrequency,
                          Math.Round(f.Percentage, 2));
            }
            Console.WriteLine("***************************************************");
            log.Debug("Scarcest Value:\t{0}\tFrequency: {1}", table.ScarcestValue, table.SmallestFrequency);
            log.Debug("Mode:\t\t{0}\tFrequency: {1}", table.Mode, table.HighestFrequency);
            Console.WriteLine("");
            Console.WriteLine("Table sorted by frequency - descending:");
            Console.WriteLine("***************************************************");
            foreach(FrequencyTableEntry<string> f in r4) {
                log.Debug("{0}  {1}   {2}   {3}", f.Value, f.AbsoluteFrequency, f.RelativeFrequency,
                          Math.Round(f.Percentage, 2));
            }
            Console.WriteLine("***************************************************");
            Console.WriteLine("");
            /* now test the class with integers
			 * initialize a new frequency table using an integer array*/
            var test = new[] { 1, 1, 1, 2, 3, 3, 2, 2, 1, 1, 1, 2, 3, 4, 23, 1, 1, 1 };
            var table1 = new FrequencyTable<int>(test);
            Console.WriteLine("");
            Console.WriteLine("Integer table unsorted:");
            Console.WriteLine("***************************************************");
            foreach(FrequencyTableEntry<int> f in table1) {
                log.Debug("{0}  {1}   {2}   {3}", f.Value, f.AbsoluteFrequency, f.RelativeFrequency,
                          Math.Round(f.Percentage, 2));
            }

            /* now test the class using a string */
            var testString =
                new FrequencyTable<string>("NON NOBIS DOMINE, NON NOBIS, SED NOMINI TUO DA GLORIAM", TextAnalyzeMode.LettersOnly);
            var stringArray = testString.GetTableAsArray(FrequencyTableSortOrder.Frequency_Descending);
            Console.WriteLine("");
            Console.WriteLine("Character table sorted by frequency - descending:");
            Console.WriteLine("***************************************************");
            foreach(FrequencyTableEntry<string> f in stringArray) {
                log.Debug("{0}  {1}   {2}   {3}", f.Value, f.AbsoluteFrequency, f.RelativeFrequency,
                          Math.Round(f.Percentage, 2));
            }
            Console.Write("Press any key to exit");
        }