コード例 #1
0
ファイル: Add.cs プロジェクト: GTuritto/ngenerics
        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")));
        }
コード例 #2
0
        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"]);
        }
コード例 #3
0
        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"]);
        }
コード例 #4
0
ファイル: UsageTests.cs プロジェクト: stemarie/redblacktree
        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");
        }
コード例 #5
0
ファイル: UsageTests.cs プロジェクト: taotao111/redblacktree
        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");
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: ilnestorova/performance
        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();
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: stemarie/performance
        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();
        }
コード例 #8
0
        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"));
        }
コード例 #9
0
        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"));
        }
コード例 #10
0
        public void BuildSercherIndexToSQLDB(Action <double, string> IndexesProgress = null)
        {
            //hashLoadBalance.RemoveAllDBData();
            //hashLoadBalance = new ConsistentHashLoadBalance();
            SetServerDBCount();
            RedBlackTree <string, string> documentIndices_cachList = new RedBlackTree <string, string>();
            var DocumentToatalList = documentDB.GetNotIndexDocument();
            int remainder          = DocumentToatalList.Count;
            var remotewords        = SercherIndexesDB.GetWords(hashLoadBalance.GetServerNodes());
            var localwords         = new HashSet <string>();
            Dictionary <string, TextComponent> textComponent = new Dictionary <string, TextComponent>();//使用到的时候进行缓存
            int curWordCachNum = 0;

            for (int i = 0, j = 0; i < DocumentToatalList.Count; i++)
            {
                var doc = DocumentToatalList[i];
                documentDB.UpdateDocumentStateIndexStatus(doc._id, "pro_" + Config.CurrentConfig.IndexesServiceName);

                IEnumerable <SegmenterToken>       textSplit       = Pretreatment(doc);
                Dictionary <string, DocumentIndex> documentIndices = new Dictionary <string, DocumentIndex>();
                int wordTotal = textSplit.Count();

                foreach (var token in textSplit)
                {
                    string word = token.Word.Trim().ToLower();
                    if (!remotewords.Contains(word))
                    {
                        if (!localwords.Contains(word))
                        {
                            localwords.Add(word);
                            remotewords.Add(word);
                        }
                    }
                    //记录一个文档的所有相同词汇
                    if (documentIndices.TryGetValue(word, out DocumentIndex documentIndex))
                    {
                        documentIndex.WordFrequency++;
                        if (documentIndex.WordFrequency <= Config.CurrentConfig.MaxIndexWordStartLocation)
                        {
                            documentIndex.BeginIndex += ',' + token.StartIndex.ToString();
                        }
                        documentIndex.DocumentWordTotal = wordTotal;
                    }
                    else
                    {
                        documentIndices[word] = new DocumentIndex
                        {
                            IndexTime         = DateTime.Now.Ticks,
                            DocId             = doc._id,
                            WordFrequency     = 1,
                            BeginIndex        = token.StartIndex.ToString(),
                            DocumentWordTotal = wordTotal,
                            Permission        = doc.Permission == 0 ? Config.CurrentConfig.DefaultPermission : doc.Permission
                        }
                    };
                }

                //转换为脚本并加入全局缓存等待上传
                documentIndices.AsParallel().ForAll(kvp =>
                {
                    //UpdateIndex(kvp.Key, kvp.Value);
                    if (documentIndices_cachList.ContainsKey(kvp.Key.ToString()))
                    {
                        string sql = InsetValueIntoMemory(kvp.Key, new DocumentIndex[1] {
                            kvp.Value
                        }, false);
                        lock (lockobj1)//因为此循环内Key唯一,所以只锁了添加代码
                        {
                            documentIndices_cachList[kvp.Key] += "," + sql;
                        }
                    }
                    else
                    {
                        string sql = InsetValueIntoMemory(kvp.Key, new DocumentIndex[1] {
                            kvp.Value
                        }, true);
                        lock (lockobj1)
                        {
                            documentIndices_cachList.Add(kvp.Key, sql);
                        }
                    }
                });


                remainder--;

                IndexesProgress?.Invoke(i / (double)DocumentToatalList.Count, "文档:" + doc.Name + " 缓存完成");
                curWordCachNum += documentIndices.Count;
                documentIndices.Clear();
                if (Config.CurrentConfig.MaxIndexCachWordNum < curWordCachNum || i == DocumentToatalList.Count - 1)
                {
                    IndexesProgress?.Invoke(i / (double)DocumentToatalList.Count, "以达缓存上限,开始创建表");
                    //对每一个同数据库的词汇的脚本进行组合,创建表
                    var group1 = localwords.GroupBy(w => hashLoadBalance.FindCloseServerDBsByTableName(w).DbName).ToArray();
                    System.Diagnostics.Stopwatch watch = new Stopwatch();
                    watch.Start();

                    Parallel.ForEach(group1, g =>
                    {
                        var wordgroup = g.ToArray();
                        hashLoadBalance.GetServerNodes().First(n => n.DbName == g.Key) //!##GroupKey欠妥,不过数据库比较少的时候影响不大
                        .CreateIndexTable(wordgroup);
                        IndexesProgress?.Invoke(i / (double)DocumentToatalList.Count, g.Key + ":一组表创建完成");
                    });
                    watch.Stop();
                    IndexesProgress?.Invoke(i / (double)DocumentToatalList.Count, "表创建完成,用时(s):" + watch.ElapsedMilliseconds / 1000);
                    localwords.Clear();
                    IndexesProgress?.Invoke(i / (double)DocumentToatalList.Count, "开始上传索引");
                    //对每一个同数据库的词汇的脚本进行组合,上传
                    var group2 = documentIndices_cachList.AsQueryable().GroupBy(kv => hashLoadBalance.FindCloseServerDBsByTableName(kv.Key).DbName).ToArray();

                    watch.Restart();
                    Parallel.ForEach(group2, new ParallelOptions()
                    {
                        MaxDegreeOfParallelism = Config.CurrentConfig.UploadThreadNum
                    }, g =>
                    {
                        //上传此db的inser脚本
                        hashLoadBalance.FindCloseServerDBsByTableName(g.First().Key)
                        .UploadDocumentIndex(g.Select(s => s.Value + ";").ToArray());
                        IndexesProgress?.Invoke(i / (double)DocumentToatalList.Count, g.Key + ":一组索引创建完成");
                    });
                    watch.Stop();
                    IndexesProgress?.Invoke(i / (double)DocumentToatalList.Count, "上传索引完成,用时(s):" + watch.ElapsedMilliseconds / 1000);

                    documentIndices_cachList.Clear();
                    while (j <= i)
                    {
                        documentDB.UpdateDocumentStateIndexStatus(DocumentToatalList[j]._id, "yes");
                        j++;
                    }
                    curWordCachNum = 0;
                    IndexesProgress?.Invoke(i / (double)DocumentToatalList.Count, "一批上传完成,刷新缓存");
                }
            }
        }
コード例 #11
0
 /// <summary>
 /// Determines whether a T object exists for the given from type.
 /// </summary>
 /// <param name="fromType">From type to search for.</param>
 /// <returns>If a T object exists for the given from type, true; otherwise, false.</returns>
 public bool ContainsKey(Type fromType)
 {
     return(m_FromTypeTree.ContainsKey(GetKeyFromType(fromType)));
 }
コード例 #12
0
        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));
            }

        }
コード例 #13
0
        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"));
        }
コード例 #14
0
 /// <summary>
 /// Determines whether a from type tree exists for the given from type.
 /// </summary>
 /// <param name="toType">To type to search for.</param>
 /// <returns>If a from type tree exists for the given to type, true; otherwise, false.</returns>
 public bool ContainsKey(Type toType)
 {
     return(m_ToTypeTree.ContainsKey(GetKeyFromType(toType)));
 }
コード例 #15
0
ファイル: Add.cs プロジェクト: GTuritto/ngenerics
        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));
                }
            }
        }
コード例 #16
0
        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"));
        }
コード例 #17
0
        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"));
        }