Exemplo n.º 1
0
        static void Main()
        {
            var towns = new RankedDictionary <string, int>();

            // Load sample data.
            towns.Add("Albany", 43600);
            towns.Add("Bandon", 2960);
            towns.Add("Corvallis", 54462);
            towns.Add("Damascus", 10539);
            towns.Add("Elkton", 195);
            towns.Add("Florence", 8466);
            towns.Add("Glide", 1795);
            towns.Add("Jacksonville", 2235);
            towns.Add("Lebanon", 13140);
            towns.Add("Lookingglass", 855);
            towns.Add("Medford", 75180);
            towns.Add("Powers", 689);
            towns.Add("Riddle", 1020);
            towns.Add("Roseburg", 20480);
            towns.Add("Scio", 710);
            towns.Add("Talent", 6066);
            towns.Add("Umatilla", 6906);
            towns.Add("Winston", 5379);
            towns.Add("Yamhill", 820);

            // Here's a typical LINQ-To-Objects operation.
            double avg = towns.Average(x => x.Value);

            Console.WriteLine($"Average population of all towns = {avg:f0}");

            // Lambda expression
            IEnumerable <KeyValuePair <string, int> > r1 = towns.Where(t => t.Key.CompareTo("E") < 0);

            Console.WriteLine("\nTowns A-D:");
            foreach (KeyValuePair <string, int> e in r1)
            {
                Console.WriteLine(e.Key);
            }

            // LINQ range: O(n)
            IEnumerable <KeyValuePair <string, int> > r2 = towns.SkipWhile(t => t.Key.CompareTo("E") < 0).TakeWhile(t => t.Key.CompareTo("J") < 0);

            Console.WriteLine("\nTowns E-G:");
            foreach (KeyValuePair <string, int> e in r2)
            {
                Console.WriteLine(e.Key);
            }

            //
            // Use the ElementsBetween iterator to query range.
            // Unlike LINQ SkipWhile and TakeWhile, this will perform an optimized (partial scan) lookup.
            //

            // BtreeDictionary range operator: O(log n)
            IEnumerable <KeyValuePair <string, int> > r3 = towns.ElementsBetween("K", "M");

            Console.WriteLine("\nTowns K-L:");
            foreach (KeyValuePair <string, int> town in r3)
            {
                Console.WriteLine(town.Key);
            }

            // Range operator without upper limit: O(log n)
            IEnumerable <KeyValuePair <string, int> > r4 = towns.ElementsFrom("M");

            Console.WriteLine("\nTowns M-R:");
            foreach (KeyValuePair <string, int> town in r4)
            {
                // This avoids the issue in the last example where a town named "M" would be included.
                if (town.Key.CompareTo("S") >= 0)
                {
                    break;
                }
                else
                {
                    Console.WriteLine(town.Key);
                }
            }

            // Range operator without upper limit: O(log n)
            IEnumerable <KeyValuePair <string, int> > r5 = towns.ElementsFrom("T");

            Console.WriteLine("\nTowns T-Z:");
            foreach (KeyValuePair <string, int> town in r5)
            {
                Console.WriteLine(town.Key);
            }
        }