Exemplo n.º 1
0
        public void UnitRdx_ElementsFromA()
        {
            var rd = new RankedDictionary <string, int>()
            {
                { "A", -1 }, { "B", -2 }
            };
            var pc = (ICollection <KeyValuePair <string, int> >)rd;

            int actual1 = 0;

            foreach (var pair in rd.ElementsFrom(null))
            {
                ++actual1;
            }

            pc.Add(new KeyValuePair <string, int> (null, 0));

            int actual2 = 0;

            foreach (var pair in rd.ElementsFrom(null))
            {
                ++actual2;
            }

            Assert.AreEqual(2, actual1);
            Assert.AreEqual(3, actual2);
        }
Exemplo n.º 2
0
        public void UnitRdx_ElementsFromMissingVal()
        {
            var rd = new RankedDictionary <int, int>()
            {
                Capacity = 8
            };

#if STRESS
            int n = 1000;
#else
            int n = 10;
#endif
            for (int i = 0; i < n; i += 2)
            {
                rd.Add(i, -i);
            }

            for (int i = 1; i < n - 1; i += 2)
            {
                foreach (var x in rd.ElementsFrom(i))
                {
                    Assert.AreEqual(i + 1, x.Key, "Incorrect key value");
                    break;
                }
            }
        }
Exemplo n.º 3
0
        public void UnitRdx_ElementsFromPassedEnd()
        {
            var rd = new RankedDictionary <int, int>();

            for (int i = 0; i < 1000; ++i)
            {
                rd.Add(i, -i);
            }

            int iterations = 0;

            foreach (var x in rd.ElementsFrom(2000))
            {
                ++iterations;
            }

            Assert.AreEqual(0, iterations, "SkipUntilKey shouldn't find anything");
        }
Exemplo n.º 4
0
        public void UnitRdx_ElementsFromB()
        {
            var rd = new RankedDictionary <int, int>();

            for (int i = 1; i <= 1000; ++i)
            {
                rd.Add(i, -i);
            }

            int firstKey   = -1;
            int iterations = 0;

            foreach (var e in rd.ElementsFrom(501))
            {
                if (iterations == 0)
                {
                    firstKey = e.Key;
                }
                ++iterations;
            }

            Assert.AreEqual(501, firstKey);
            Assert.AreEqual(500, iterations);
        }
Exemplo n.º 5
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);
            }
        }