当前位置: 首页>>代码示例>>C#>>正文


C# RedBlackTree.ContainsKey方法代码示例

本文整理汇总了C#中RedBlackTree.ContainsKey方法的典型用法代码示例。如果您正苦于以下问题:C# RedBlackTree.ContainsKey方法的具体用法?C# RedBlackTree.ContainsKey怎么用?C# RedBlackTree.ContainsKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RedBlackTree的用法示例。


在下文中一共展示了RedBlackTree.ContainsKey方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Simple

        public void Simple()
        {
            var redBlackTree = new RedBlackTree<int, string>();

            for (var i = 0; i < 100; i++)
            {
                redBlackTree.Add(i, i.ToString());

                Assert.IsTrue(redBlackTree.ContainsKey(i));
                Assert.AreEqual(redBlackTree.Count, i + 1);
            }

            for (var i = 300; i > 200; i--)
            {
                redBlackTree.Add(i, i.ToString());

                Assert.IsTrue(redBlackTree.ContainsKey(i));
                Assert.AreEqual(redBlackTree.Count, 100 + (300 - i) + 1);
            }

            for (var i = 100; i < 200; i++)
            {
                redBlackTree.Add(i, i.ToString());

                Assert.IsTrue(redBlackTree.ContainsKey(i));
                Assert.AreEqual(redBlackTree.Count, 100 + i + 1);
            }

            for (var i = 301; i < 400; i++)
            {
                redBlackTree.Add(new KeyValuePair<int, string>(i, i.ToString()));

                Assert.IsTrue(redBlackTree.ContainsKey(i));
                Assert.AreEqual(redBlackTree[i], i.ToString());
                Assert.IsTrue(redBlackTree.Contains(new KeyValuePair<int, string>(i, i.ToString())));
            }

            Assert.IsFalse(redBlackTree.Contains(new KeyValuePair<int, string>(500, "500")));
            Assert.IsFalse(redBlackTree.Contains(new KeyValuePair<int, string>(300, "301")));
            Assert.IsTrue(redBlackTree.Contains(new KeyValuePair<int, string>(300, "300")));
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:41,代码来源:Add.cs

示例2: AddKeyValuePairExample

        public void AddKeyValuePairExample()
        {
            // Create a simple tree
            var tree = new RedBlackTree<string, int>
                                                 {
                                                     new KeyValuePair<string, int>("cat", 1),
                                                     new KeyValuePair<string, int>("dog", 2),
                                                     new KeyValuePair<string, int>("canary", 3)
                                                 };

            // There should be 3 items in the tree.
            Assert.AreEqual(3, tree.Count);

            // The tree should contain all three keys
            Assert.IsTrue(tree.ContainsKey("cat"));
            Assert.IsTrue(tree.ContainsKey("dog"));
            Assert.IsTrue(tree.ContainsKey("canary"));

            // The value of the item with key "dog" must be 2.
            Assert.AreEqual(2, tree["dog"]);
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:21,代码来源:RedBlackTreeExamples.cs

示例3: TestUsage1

        public void TestUsage1()
        {
            RedBlackTree<Guid, string> tree = new RedBlackTree<Guid, string>();
            Guid item1 = Guid.NewGuid();
            Guid item2 = Guid.NewGuid();
            for (int i = 0; i < 100; i++)
            {
                tree.Add(Guid.NewGuid(), DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));
            }
            tree.Add(item1, DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));
            tree.Add(item2, DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));

            Assert.AreEqual(102, tree.Count, "Should be 102 items in the tree");
            Assert.IsTrue(tree.ContainsKey(item1), "Should contain item 1");
            Assert.IsTrue(tree.ContainsKey(item2), "Should contain item 2");
            tree.Remove(item1);
            Assert.AreEqual(101, tree.Count, "Should be 101 items in the tree");
            Assert.IsFalse(tree.ContainsKey(item1), "Should not contain item 1");
            tree.Remove(item2);
            Assert.AreEqual(100, tree.Count, "Should be 100 items in the tree");
            Assert.IsFalse(tree.ContainsKey(item2), "Should not contain item 2");
        }
开发者ID:stemarie,项目名称:redblacktree,代码行数:22,代码来源:UsageTests.cs

示例4: Main

        static void Main(string[] args)
        {
            var stack = new Stack<string>();
            var tree = new RedBlackTree<string, string>();
            var dict = new Dictionary<string, string>();

            for (int i = 0; i < 1000000; i++)
            {
                stack.Push(Guid.NewGuid().ToString());
            }

            var sw = new Stopwatch();
            sw.Start();
            foreach (string item in stack)
            {
                tree.Add(item, item);
            }
            sw.Stop();
            System.Console.WriteLine("{0}", sw.Elapsed);

            sw.Reset();
            sw.Start();
            foreach (string item in stack)
            {
                dict.Add(item, item);
            }
            sw.Stop();
            System.Console.WriteLine("{0}", sw.Elapsed);

            sw.Reset();
            sw.Start();
            foreach (string item in stack)
            {
                tree.ContainsKey(item);
            }
            sw.Stop();
            System.Console.WriteLine("{0}", sw.Elapsed);

            sw.Reset();
            sw.Start();
            foreach (string item in stack)
            {
                dict.ContainsKey(item);
            }
            sw.Stop();
            System.Console.WriteLine("{0}", sw.Elapsed);

            System.Console.ReadKey();
        }
开发者ID:stemarie,项目名称:performance,代码行数:49,代码来源:Program.cs

示例5: StressTestRandomData

        public void StressTestRandomData()
        {
            var data = new List<int>();
            var redBlackTree = new RedBlackTree<int, string>();

            var rand = new Random(Convert.ToInt32(DateTime.Now.Ticks % Int32.MaxValue));

            for (var i = 0; i < 2000; i++)
            {
                var randomNumber = rand.Next(100000);

                while (data.Contains(randomNumber))
                {
                    randomNumber = rand.Next(100000);
                }

                data.Add(randomNumber);
                redBlackTree.Add(randomNumber, randomNumber.ToString());

                Assert.AreEqual(redBlackTree.Count, i + 1);

                foreach (var t in data)
                {
                    Assert.IsTrue(redBlackTree.ContainsKey(t));
                }
            }

            while (data.Count != 0)
            {
                Assert.IsTrue(redBlackTree.Remove(data[0]));

                Assert.IsFalse(redBlackTree.ContainsKey(data[0]));

                data.RemoveAt(0);

                Assert.AreEqual(redBlackTree.Count, data.Count);

                foreach (var t in data)
                {
                    Assert.IsTrue(redBlackTree.ContainsKey(t));
                }
            }
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:43,代码来源:Add.cs

示例6: ClearExample

        public void ClearExample()
        {
            // Create a simple tree
            var tree = new RedBlackTree<string, int>
                                                 {
                                                     new KeyValuePair<string, int>("cat", 1),
                                                     new KeyValuePair<string, int>("dog", 2),
                                                     new KeyValuePair<string, int>("canary", 3)
                                                 };

            // There should be 3 items in the tree.
            Assert.AreEqual(3, tree.Count);

            // Clear the tree
            tree.Clear();

            // The tree should be empty.
            Assert.AreEqual(0, tree.Count);

            // No cat here..
            Assert.IsFalse(tree.ContainsKey("cat"));
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:22,代码来源:RedBlackTreeExamples.cs

示例7: RemoveExample

        public void RemoveExample()
        {
            // Build a simple tree.
            var tree = new RedBlackTree<string, int>
                                                 {
                                                     new KeyValuePair<string, int>("cat", 1),
                                                     new KeyValuePair<string, int>("dog", 2),
                                                     new KeyValuePair<string, int>("canary", 3)
                                                 };

            // There are three items in the tree
            Assert.AreEqual(3, tree.Count);

            // Let's remove the dog
            tree.Remove("dog");

            // Now the tree contains only two items, and dog isn't
            // in the tree.
            Assert.AreEqual(2, tree.Count);
            Assert.IsFalse(tree.ContainsKey("dog"));
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:21,代码来源:RedBlackTreeExamples.cs

示例8: ContainsKeyExample

        public void ContainsKeyExample()
        {
            // Create a simple tree
            var tree = new RedBlackTree<string, int>
                                                 {
                                                     new KeyValuePair<string, int>("cat", 1),
                                                     new KeyValuePair<string, int>("dog", 2),
                                                     new KeyValuePair<string, int>("canary", 3)
                                                 };

            // The tree should contain a cat and a dog...
            Assert.IsTrue(tree.ContainsKey("cat"));
            Assert.IsTrue(tree.ContainsKey("dog"));

            // But definitely not an ostrich.
            Assert.IsFalse(tree.ContainsKey("ostrich"));
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:17,代码来源:RedBlackTreeExamples.cs

示例9: StructureIntegrity

        public void StructureIntegrity()
        {
            RedBlackTree<int, int> tree = new RedBlackTree<int, int>();

            int range = 10;
            int[] order = new[] { 1, 3, 2, 5, 6 };

            var q = order
                .SelectMany(x => Enumerable.Range((x - 1) * range + 1, range));


            foreach (int i in q)
            {
                Assert.IsFalse(tree.ContainsKey(i));

                tree.Add(i, i);

                AssertStructure(tree.RootElement);
                Assert.IsTrue(tree.ContainsKey(i));
            }

            foreach (int i in q)
            {
                AssertStructure(tree.RootElement);
                Assert.IsTrue(tree.ContainsKey(i));

                tree.Remove(i);

                Assert.IsFalse(tree.ContainsKey(i));
            }

        }
开发者ID:CedricDumont,项目名称:NMemory.Next,代码行数:32,代码来源:RedBlackTreeFixture.cs


注:本文中的RedBlackTree.ContainsKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。