public void Initialize(string csvFilePath)
        {
            CsvReader reader = new CsvReader(csvFilePath);

            this.AllCountries = reader.ReadAllCountries().OrderBy(x => x.Name).ToList();
            var dict = AllCountries.ToDictionary(x => x.Code);

            this.AllCountriesByKey = dict;
        }
        public void Initialise()
        {
            CsvReader reader = new CsvReader();

            AllCountries               = reader.ReadAllCountries();
            AllCountriesByKey          = AllCountries.ToDictionary(x => x.Code);
            AllCountriesSorted         = new SortedDictionary <CountryCode, Country>(new CountryCodeComparer());
            AllCountriesSortedWithList = new SortedList <CountryCode, Country>(new CountryCodeComparer());
        }
Exemple #3
0
        public void Initialize(string csvFilePath)
        {
            CsvReader reader = new CsvReader(csvFilePath);

            this.AllCountries = reader.ReadAllCountries().OrderBy(x => x.Name).ToList();

            this.AllCountriesByKey = AllCountries.ToDictionary(x => x.Code, StringComparer.OrdinalIgnoreCase); // add statndart comparator to ignore case

            this.AllCountriesCCByKey = AllCountries.ToDictionary(x => x.CountryCode);

            this.AllCountriesByKeySorted = new SortedDictionary <string, Country>(AllCountriesByKey);

            this.AllCountriesByKeySortedList = new SortedList <string, Country>(AllCountriesByKey);
        }
Exemple #4
0
        static CountryDatabase()
        {
            CountriesByAlpha2Code = AllCountries.ToDictionary(c => c.Alpha2Code, c => c);

            CountriesByAlpha3Code = AllCountries.ToDictionary(c => c.Alpha3Code, c => c);
        }
        public void Run()
        {
            CsvReader reader = new CsvReader(@".\Resources\PopByLargest.csv");

            this.AllCountries = reader.ReadAllCountries().OrderBy(x => x.Name).ToList();
            var dict = AllCountries.ToDictionary(x => x.Code);

            this.AllCountriesByKey = dict;

            Console.WriteLine("-----------------ADD-------------------------");
            Country selectedCountry  = AllCountries[4];
            Country selectedCountry1 = AllCountries[14];
            Country selectedCountry2 = AllCountries[24];
            Country selectedCountry3 = AllCountries[34];

            //Add at the end of LL
            this.ItineraryBuilder.AddLast(selectedCountry);
            var change = new ItineraryChange(ChangeType.Append, this.ItineraryBuilder.Count, selectedCountry);

            this.ChangeLog.Push(change);

            this.ItineraryBuilder.AddLast(selectedCountry1);
            var change1 = new ItineraryChange(ChangeType.Append, this.ItineraryBuilder.Count, selectedCountry1);

            this.ChangeLog.Push(change1);

            this.ItineraryBuilder.AddLast(selectedCountry2);
            var change2 = new ItineraryChange(ChangeType.Append, this.ItineraryBuilder.Count, selectedCountry2);

            this.ChangeLog.Push(change2);

            this.ItineraryBuilder.AddLast(selectedCountry3);
            var change3 = new ItineraryChange(ChangeType.Append, this.ItineraryBuilder.Count, selectedCountry3);

            this.ChangeLog.Push(change3);

            foreach (var item in this.ItineraryBuilder)
            {
                Console.WriteLine($"{item.Code}:{item.Name}");
            }
            Console.WriteLine("---stack----");
            foreach (var item in ChangeLog)
            {
                Console.WriteLine(item);
            }


            Console.WriteLine("-----------------REMOVE-------------------------");
            int selectedIndex = 1;
            var nodeToRemove  = this.ItineraryBuilder.GetNthNode(selectedIndex);

            this.ItineraryBuilder.Remove(nodeToRemove);
            var change4 = new ItineraryChange(ChangeType.Remove, selectedIndex, nodeToRemove.Value);

            this.ChangeLog.Push(change4);
            foreach (var item in this.ItineraryBuilder)
            {
                Console.WriteLine($"{item.Code}:{item.Name}");
            }
            Console.WriteLine("---stack----");
            foreach (var item in ChangeLog)
            {
                Console.WriteLine(item);
            }


            Console.WriteLine("----------------------INSERT------------------------------");
            int selectedIndex1 = 1;

            this.AllCountriesByKey.TryGetValue(new CountryCode("USA"), out Country result);
            var insertBeforeNode = this.ItineraryBuilder.GetNthNode(selectedIndex1);

            this.ItineraryBuilder.AddBefore(insertBeforeNode, result);
            var change5 = new ItineraryChange(ChangeType.Insert, selectedIndex1, insertBeforeNode.Value);

            this.ChangeLog.Push(change5);
            foreach (var item in this.ItineraryBuilder)
            {
                Console.WriteLine($"{item.Code}:{item.Name}");
            }
            Console.WriteLine("---stack----");
            foreach (var item in ChangeLog)
            {
                Console.WriteLine(item);
            }


            Console.WriteLine("---------------------UNDO-------------------------------");
            ItineraryChange lastChange = this.ChangeLog.Pop();

            ChangeUndoer.Undo(this.ItineraryBuilder, lastChange);
            ItineraryChange lastChange2 = this.ChangeLog.Pop();

            ChangeUndoer.Undo(this.ItineraryBuilder, lastChange2);
            ItineraryChange lastChange3 = this.ChangeLog.Pop();

            ChangeUndoer.Undo(this.ItineraryBuilder, lastChange3);
            ChangeUndoer.Undo(this.ItineraryBuilder, this.ChangeLog.Pop());

            foreach (var item in this.ItineraryBuilder)
            {
                Console.WriteLine($"{item.Code}:{item.Name}");
            }
            Console.WriteLine("---stack----");
            foreach (var item in ChangeLog)
            {
                Console.WriteLine(item);
            }
        }
        public void Run()
        {
            CsvReader reader = new CsvReader(@".\Resources\PopByLargest.csv");

            this.AllCountries      = reader.ReadAllCountries().OrderBy(x => x.Name).ToList();
            this.AllCountriesByKey = AllCountries.ToDictionary(x => x.Code);//List to Dictionary

            //SEARCH COUNTRY
            Console.WriteLine("-------------SEARCH COUNTRY (USA)------------------");
            //Country result1 = AllCountries.Find(x => x.Code == "USA");//O(N)
            this.AllCountriesByKey.TryGetValue("USA", out Country result);//O(1) //prefer   //case sensitive
            Console.WriteLine(result);

            //the search is case sensitive
            //Country result1 = AllCountries.Find(x => x.Code.ToUpper() == "usa".ToUpper());
            //like above, for list it is possible to use to upper
            //for DICTIONARY : if you want the dictionary to compare its KEYS in a way that its different from normal, you have to tell upfront at initialization time.
            //Reason is, inorder to give you the super efficient lookup, the dictionary needs to take into account how you want to lookup the keys when it's decides how to store its values internally.
            //it is done by passing the dectionary an "EQUALITY COMPARER"
            //EQUALITY COMPARER >Object that knows how to test for equality.
            //EQUALITY COMPARER implements IEqualityComparer<T>

            this.AllCountriesByKey = AllCountries.ToDictionary(x => x.Code, StringComparer.OrdinalIgnoreCase);
            Console.WriteLine("-------------SEARCH COUNTRY (usa) case insensitive------------------");
            this.AllCountriesByKey.TryGetValue("usa", out Country result2);//case insensitive
            Console.WriteLine(result);

            Console.WriteLine("------------------------------------------");
            this.AllCountries      = reader.ReadAllCountries().OrderBy(x => x.Name).ToList();
            this.AllCountriesByKey = AllCountries.ToDictionary(x => x.Code);
            //The order of the values in the dictionary is unspecified
            //you can't rely on the dictionary enumeration order
            //order works here since the list of sorted.so dictionary showed same order.
            //But there is no guarantee that it(order) is always be true with dictionary.since dictionary doesn't have any intrinsic order.
            //Relying on Dictionary order while enumerating is "not recommended".
            //Remedy: use SORTED DICTIONARY

            //-------------------------------------------------------------------------------------------------------
            //SortedDictionary<TKey,TValue>
            //Keyed access to items
            //automatically sorts items as it added to dictionary
            //Guaranteed sorted order when enumerating
            this.AllCountries = reader.ReadAllCountries().OrderBy(x => x.Name).ToList();
            var dict = AllCountries.ToDictionary(x => x.Code, StringComparer.OrdinalIgnoreCase); //List to Dictionary

            this.AllCountriesByKey1 = new SortedDictionary <string, Country>(dict);              //Dictionary to Sorted Dictionary
            //SortedDictionary sorts dictionary by KEY
            foreach (var item in AllCountriesByKey1)
            {//This enumeration is sorted by KEY
                Console.WriteLine($"{item.Key} : {item.Value}");
            }


            //----------------------------------------------------------------------------
            //this.AllCountriesByKey1=new SortedDictionary<string, Country>(dict);
            //OR
            //this.AllCountriesByKey1 = new SortedList<string, Country>(dict);

            //SORTEDLIST  is functionally exactly same as SORTEDdICTIONARY
            //difference in performance characteristics
            //SORTEDLIST uses less memory but scales worse for inserting and removing items
            //SORTEDdICTIONARY modifications scales better. O(logN) vs O(N)

            //always prefer SORTEDdICTIONARY

            //---------------------CUSTOM TYPE-----------CountryCode-------------
            Console.WriteLine("---------------------CUSTOM TYPE-----------CountryCode-------------");
            CsvReader1 reader1 = new CsvReader1(@".\Resources\PopByLargest.csv");

            this.AllCountries1 = reader1.ReadAllCountries().OrderBy(x => x.Name).ToList();
            var dict1 = AllCountries1.ToDictionary(x => x.Code);//StringComparer can't be used as Code is not string.it is CountryCode custom type

            this.AllCountriesByKey3 = dict1;
            this.AllCountriesByKey3.TryGetValue(new CountryCode("usa"), out Country1 country1);
            Console.WriteLine(country1); //empty
                                         //since it doesn't know how to compare "CountryCodes"  //since CountryCode is ref type is compare references.  //string is also a ref type but microsofts overridden that comparition to compare value
                                         //country1 works only if Equals,GetHashcode is implemented for "CountryCode"

            //Dictionary key can be any type. //generally string is used
            //to use customtype as key, you must override Equals & GetHashcode
        }
        public void Run()
        {
            CsvReader1 reader = new CsvReader1(@".\Resources\PopByLargest.csv");

            this.AllCountries = reader.ReadAllCountries().OrderBy(x => x.Name).ToList();
            var dict = AllCountries.ToDictionary(x => x.Code);

            this.AllCountriesByKey = dict;

            //--------ADD----------
            Country1 selectedCountry = AllCountries[4];

            //Add at the end of LL
            this.ItineraryBuilder.AddLast(selectedCountry);  //O(1)
            this.ItineraryBuilder.AddLast(AllCountries[14]); //O(1)
            this.ItineraryBuilder.AddLast(AllCountries[24]); //O(1)
            this.ItineraryBuilder.AddLast(AllCountries[34]); //O(1)
            foreach (var item in this.ItineraryBuilder)
            {
                Console.WriteLine($"{item.Code}:{item.Name}");
            }

            Console.WriteLine("----------------------REMOVE------------------------------");
            var nodeToRemove = this.ItineraryBuilder.GetNthNode(1);

            this.ItineraryBuilder.Remove(nodeToRemove);
            foreach (var item in this.ItineraryBuilder)
            {
                Console.WriteLine($"{item.Code}:{item.Name}");
            }

            Console.WriteLine("----------------------INSERT------------------------------");
            this.AllCountriesByKey.TryGetValue(new CountryCode("USA"), out Country1 result);
            var insertBeforeNode = this.ItineraryBuilder.GetNthNode(1);

            this.ItineraryBuilder.AddBefore(insertBeforeNode, result);
            foreach (var item in this.ItineraryBuilder)
            {
                Console.WriteLine($"{item.Code}:{item.Name}");
            }

            Console.WriteLine("----------------------INSERT likedlist to array------------------------------");
            string tourName = "MyTour";

            Country1[] itinerary = this.ItineraryBuilder.ToArray();
            try
            {
                Tour tour = new Tour(tourName, itinerary);
                this.AllTours.Add(tourName, tour);//sorteddictionary throws exception if tourName already exists.(enforce unique names)
            }
            catch (Exception)
            {
                Console.WriteLine("cannot save tour");
            }
            this.ItineraryBuilder.Clear();//clear linked list

            foreach (var item in this.AllTours)
            {
                Console.WriteLine($"{item.Key}:");
                foreach (var value in this.AllTours[item.Key].Itinerary)
                {
                    Console.WriteLine(value);
                }
            }
        }