示例#1
0
        public void UnitRdkx_Indexer()
        {
            var rd = new RankedDictionary <string, int> {
                { "0zero", 0 }, { "1one", -1 }, { "2two", -2 }
            };

            Assert.AreEqual("0zero", rd.Keys[0]);
            Assert.AreEqual("1one", rd.Keys[1]);
            Assert.AreEqual("2two", rd.Keys[2]);
        }
示例#2
0
            /// <summary>Initializes a new collection that reflects the keys of a <see cref="RankedDictionary{TKey,TValue}"/>.</summary>
            /// <param name="dictionary">Dictionary containing these keys.</param>
            /// <remarks>This is a O(1) operation.</remarks>
            /// <exception cref="ArgumentNullException">When <em>dictionary</em> is <b>null</b>.</exception>
            public KeyCollection(RankedDictionary <TKey, TValue> dictionary)
            {
                if (dictionary == null)
#pragma warning disable IDE0016
                {
                    throw new ArgumentNullException(nameof(dictionary));
                }
#pragma warning restore IDE0016

                this.tree = dictionary;
            }
示例#3
0
        public void UnitRdv_Ctor()
        {
            Setup();
            dary1.Add(1, -1);
#if TEST_BCL
            var vals = new SortedDictionary <int, int> .ValueCollection(dary1);
#else
            var vals = new RankedDictionary <int, int> .ValueCollection(dary1);
#endif
            Assert.AreEqual(1, vals.Count);
        }
示例#4
0
        public void UnitRdk_Ctor()
        {
            Setup();
            dary1.Add(1, -1);
#if TEST_BCL
            var keys = new SortedDictionary <int, int> .KeyCollection(dary1);
#else
            var keys = new RankedDictionary <int, int> .KeyCollection(dary1);
#endif
            Assert.AreEqual(1, keys.Count);
        }
示例#5
0
        public void CrashRd_Ctor1NoComparer_InvalidOperation()
        {
            var comp0 = (System.Collections.Generic.Comparer <Person>)null;

#if TEST_BCL
            var d1 = new SortedDictionary <Person, int> (comp0);
#else
            var d1 = new RankedDictionary <Person, int> (comp0);
#endif
            d1.Add(new Person("Zed"), 1);
            d1.Add(new Person("Macron"), 2);
        }
示例#6
0
        public void UnitRdkx_IndexOf()
        {
            var rd = new RankedDictionary <string, int> {
                { "one", 1 }, { "two", 2 }
            };
            var pc = (System.Collections.Generic.ICollection <System.Collections.Generic.KeyValuePair <string, int> >)rd;

            pc.Add(new System.Collections.Generic.KeyValuePair <string, int> (null, -1));

            Assert.AreEqual(0, rd.Keys.IndexOf(null));
            Assert.AreEqual(2, rd.Keys.IndexOf("two"));
        }
示例#7
0
        public void UnitRdvx_Indexer()
        {
            var rd = new RankedDictionary <string, int> {
                Capacity = 4
            };

            foreach (var kv in greek)
            {
                rd.Add(kv.Key, kv.Value);
            }

            Assert.AreEqual(11, rd.Values[7]);
        }
示例#8
0
        public void UnitRdkx_ElementAtOrDefault()
        {
            var rd = new RankedDictionary <string, int> {
                { "one", 1 }, { "two", 2 }
            };

            string kn = rd.Keys.ElementAtOrDefault(-1);
            string k1 = rd.Keys.ElementAtOrDefault(1);
            string k2 = rd.Keys.ElementAtOrDefault(2);

            Assert.AreEqual(default(string), kn);
            Assert.AreEqual("two", k1);
            Assert.AreEqual(default(string), k2);
        }
示例#9
0
        public void UnitRd_Ctor2()
        {
            IDictionary <Person, int> empDary = new SortedDictionary <Person, int> (new PersonComparer());

            empDary.Add(new KeyValuePair <Person, int>(new Person("fay"), 1));
            empDary.Add(new KeyValuePair <Person, int>(new Person("ann"), 2));
            empDary.Add(new KeyValuePair <Person, int>(new Person("sam"), 3));

#if TEST_BCL
            var people = new SortedDictionary <Person, int> (empDary, new PersonComparer());
#else
            var people = new RankedDictionary <Person, int> (empDary, new PersonComparer());
#endif
            Assert.AreEqual(3, people.Count);
        }
示例#10
0
        static void Main(string[] args)
        {
            foreach (var reps in new int[] { 100, 1000, 10000, 100000, 1000000, 10000000, 20000000, 40000000 })
            {
                tree = new RankedDictionary <Guid, int>();

                for (int ii = 0; ii < reps; ++ii)
                {
                    tree.Add(Guid.NewGuid(), ii);
                }

                Console.WriteLine(reps);
#if DEBUG
                Console.WriteLine(tree.GetTreeStatsText());
#endif
            }
        }
示例#11
0
        public void UnitRdx_IndexOfValue()
        {
            var rd = new RankedDictionary <int, int>();

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

            var ix1 = rd.IndexOfValue(1400);

            Assert.AreEqual(400, ix1);

            var ix2 = rd.IndexOfValue(88888);

            Assert.AreEqual(-1, ix2);
        }
示例#12
0
        static void Main()
        {
            var rs = new RankedSet <int> {
                3, 5, 7
            };

            Console.WriteLine("Set items must be distinct:");
            foreach (var item in rs)
            {
                Console.WriteLine(item);
            }

            var rd = new RankedDictionary <int, int> {
                { 3, 0 }, { 1, 11 }, { 5, 0 }, { 9, 99 }
            };

            Console.WriteLine("\nDictionary keys must be distinct:");
            foreach (var kv in rd)
            {
                Console.WriteLine(kv);
            }
            Console.WriteLine("Just the keys: { " + String.Join(",", rd.Keys) + " }");
            Console.WriteLine("Just the values: { " + String.Join(",", rd.Values) + " }");

            var rb = new RankedBag <int> {
                1, 2, 2, 3
            };

            Console.WriteLine("\nBag items may repeat:");
            foreach (var bx in rb)
            {
                Console.WriteLine(bx);
            }

            var rm = new RankedMap <int, int> {
                { 2, 0 }, { 4, 0 }, { 4, 44 }, { 6, 66 }
            };

            Console.WriteLine("\nMap keys may repeat:");
            foreach (var kv in rm)
            {
                Console.WriteLine(kv);
            }
            Console.WriteLine("Just the keys: { " + String.Join(",", rm.Keys) + " }");
            Console.WriteLine("Just the values: { " + String.Join(",", rm.Values) + " }");
        }
示例#13
0
        public void UnitRdx_RemoveWhereB()
        {
            int n  = 2000;
            var rd = new RankedDictionary <int, int> {
                Capacity = 7
            };

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

            int removed = rd.RemoveWhere(IsAlways);

            Assert.AreEqual(n, removed);
            Assert.AreEqual(0, rd.Count);
        }
示例#14
0
        static void Main()
        {
            int reps = 5000000;
            var sd   = new SortedDictionary <Guid, int>();

            Console.Write($"Loading SortedDictionary with {reps} elements:\n\nLoad time = ");

            Stopwatch watch1 = new Stopwatch();

            watch1.Reset();
            watch1.Start();

            for (int i = 0; i < reps; ++i)
            {
                var guid = Guid.NewGuid();
                sd.Add(guid, i);
            }

            var time11 = watch1.ElapsedMilliseconds;

            Console.WriteLine(time11 + "ms");

            for (int order = 32; order <= 256; order += 16)
            {
                var bt = new RankedDictionary <Guid, int>()
                {
                    Capacity = order
                };

                Console.Write($"\nLoading BtreeDictionary (order={order}) with {reps} elements:\n\nLoad time = ");

                Stopwatch watch2 = new Stopwatch();
                watch2.Reset();
                watch2.Start();

                for (int i = 0; i < reps; ++i)
                {
                    var guid = Guid.NewGuid();
                    bt.Add(guid, i);
                }

                var time21 = watch2.ElapsedMilliseconds;
                Console.WriteLine($"{time21}ms");
            }
        }
示例#15
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");
        }
示例#16
0
        public void UnitRdx_RemoveWhereA()
        {
            var rd = new RankedDictionary <int, int>();

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

            int c0      = rd.Count;
            int removed = rd.RemoveWhere(IsEven);

            Assert.AreEqual(500, removed);
            foreach (int key in rd.Keys)
            {
                Assert.IsTrue(key % 2 != 0);
            }
        }
示例#17
0
        public void UnitRdx_RemoveWhereElement()
        {
            var rd = new RankedDictionary <int, int>();

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

            int c0      = rd.Count;
            int removed = rd.RemoveWhereElement(IsPairEven);

            Assert.AreEqual(500, removed);
            foreach (int val in rd.Values)
            {
                Assert.IsTrue(val % 2 != 0);
            }
        }
示例#18
0
        public void UnitRd_Ctor1B()
        {
            Setup();

            IDictionary <string, int> sl = new SortedList <string, int>();

            sl.Add("Gremlin", 1);
            sl.Add("Pacer", 2);

#if TEST_BCL
            var dary = new SortedDictionary <string, int> (sl);
#else
            var dary = new RankedDictionary <string, int> (sl);
#endif

            Assert.AreEqual(1, dary["Gremlin"]);
            Assert.AreEqual(2, dary["Pacer"]);
        }
示例#19
0
        public void UnitRdx_ElementsBetweenA()
        {
            var rd = new RankedDictionary <string, int>();
            var pc = (ICollection <KeyValuePair <string, int> >)rd;

            rd.Add("Alpha", 1);
            rd.Add("Beta", 2);
            rd.Add("Omega", 24);
            pc.Add(new KeyValuePair <string, int> (null, 0));

            int actual = 0;

            foreach (var kv in rd.ElementsBetween(null, "C"))
            {
                ++actual;
            }

            Assert.AreEqual(3, actual);
        }
示例#20
0
        public void UnitRdvx_IndexOf()
        {
            var rd = new RankedDictionary <int, int> {
                Capacity = 5
            };

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

            var ix1 = rd.Values.IndexOf(1500);

            Assert.AreEqual(500, ix1);

            var ix2 = rd.Values.IndexOf(77777);

            Assert.AreEqual(-1, ix2);
        }
示例#21
0
        static void Main()
        {
            var dary = new RankedDictionary <int, int>()
            {
                [36] = 360, [12] = 120
            };

            Console.WriteLine("Keys:");
            foreach (var key in dary.Keys)
            {
                Console.WriteLine(key);
            }

            Console.WriteLine("\nValues:");
            foreach (var val in dary.Values)
            {
                Console.WriteLine(val);
            }
        }
示例#22
0
        public void UnitRdx_RemoveRange()
        {
            var rd = new RankedDictionary <int, int> {
                Capacity = 7
            };

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

            rd.RemoveRange(20, 0);
            Assert.AreEqual(20, rd.Count);

            rd.RemoveRange(12, 4);
            Assert.AreEqual(16, rd.Count);
#if DEBUG
            rd.SanityCheck();
#endif
        }
示例#23
0
        public void UnitRdx_Reverse()
        {
            var rd = new RankedDictionary <int, int> {
                Capacity = 5
            };
            int expected = 500;

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

            foreach (var actual in rd.Reverse())
            {
                Assert.AreEqual(expected, actual.Key);
                Assert.AreEqual(-expected, actual.Value);
                --expected;
            }
            Assert.AreEqual(0, expected);
        }
示例#24
0
        static void Main()
        {
            IFormatter formatter = new BinaryFormatter();
            var        set1      = new RankedDictionary <Person, string> (new PersonComparer());

            set1.Add(new Person("Hugh", "Mann"), "B+");
            set1.Add(new Person("Hammond", "Egger"), "C-");

            SerializePersons("Persons.bin", set1, formatter);
            Console.WriteLine($"Wrote {set1.Count} key/value pairs.");
            Console.WriteLine();

            RankedDictionary <Person, string> set2 = DeserializePersons("Persons.bin", formatter);

            Console.WriteLine("Read back:");

            foreach (var kv in set2)
            {
                Console.WriteLine(kv);
            }
        }
示例#25
0
        public void UnitRdx_ElementsBetweenPassedEnd()
        {
            var rd = new RankedDictionary <int, int>();

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

            int iterations = 0;
            int sumVals    = 0;

            foreach (KeyValuePair <int, int> e in rd.ElementsBetween(500, 1500))
            {
                ++iterations;
                sumVals += e.Value;
            }

            Assert.AreEqual(500, iterations);
            Assert.AreEqual(-374750, sumVals, "Sum of values not correct");
        }
示例#26
0
        public void UnitRd_Ctor1A2()
        {
#if TEST_BCL
            var tree = new SortedDictionary <string, int> (StringComparer.Ordinal);
#else
            var tree = new RankedDictionary <string, int> (StringComparer.Ordinal);
#endif
            tree.Add("AAA", 0);
            tree.Add("bbb", 2);
            tree.Add("CCC", 1);
            tree.Add("ddd", 3);

            int actualPosition = 0;
            foreach (KeyValuePair <string, int> pair in tree)
            {
                Assert.AreEqual(actualPosition, pair.Value);
                ++actualPosition;
            }

            Assert.AreEqual(4, actualPosition);
        }
示例#27
0
        public void UnitRdx_TryGet()
        {
            var rd = new RankedDictionary <string, int> (StringComparer.InvariantCultureIgnoreCase);

            rd.Add("AAA", 1);
            rd.Add("bbb", 2);
            rd.Add("ccc", 3);

            bool got1 = rd.Keys.TryGet("aaa", out string actual1);

            Assert.IsTrue(got1);
            Assert.AreEqual("AAA", actual1);

            bool got2 = rd.Keys.TryGet("bb", out string actual2);

            Assert.IsFalse(got2);

            bool got3 = rd.Keys.TryGet("CCC", out string actual3);

            Assert.IsTrue(got3);
            Assert.AreEqual("ccc", actual3);
        }
示例#28
0
        public void UnitRdx_ElementsBetweenB()
        {
            var rd = new RankedDictionary <int, int> {
                Capacity = 4
            };

            for (int i = 90; i >= 0; i -= 10)
            {
                rd.Add(i, -100 - i);
            }

            int iterations = 0;
            int sumVals    = 0;

            foreach (var kv in rd.ElementsBetween(35, 55))
            {
                ++iterations;
                sumVals += kv.Value;
            }

            Assert.AreEqual(2, iterations);
            Assert.AreEqual(-290, sumVals);
        }
示例#29
0
        public void UnitRdx_MinMax()
        {
            var rd = new RankedDictionary <int, int> {
                Capacity = 4
            };

            int min0 = rd.MinKey;
            int max0 = rd.MaxKey;

            Assert.AreEqual(default(int), min0);
            Assert.AreEqual(default(int), max0);

            for (int i1 = 1; i1 <= 99; ++i1)
            {
                rd.Add(i1, i1 + 100);
            }

            int min = rd.MinKey;
            int max = rd.MaxKey;

            Assert.AreEqual(1, min);
            Assert.AreEqual(99, max);
        }
示例#30
0
        public void UnitRdx_TryGetValueIndex()
        {
            var rd = new RankedDictionary <int, int>();

            rd.Capacity = 5;
            for (int ii = 0; ii < 500; ii += 2)
            {
                rd.Add(ii, ii + 1000);
            }

            for (int ii = 0; ii < 500; ii += 2)
            {
                bool isOk = rd.TryGetValueAndIndex(ii, out int v1, out int i1);

                Assert.IsTrue(isOk);
                Assert.AreEqual(ii / 2, i1);
                Assert.AreEqual(ii + 1000, v1);
            }

            bool isOkNot = rd.TryGetValueAndIndex(111, out int v2, out int i2);

            Assert.IsFalse(isOkNot);
        }