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


C# SortedMap.TryFind方法代码示例

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


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

示例1: ContinuousZipIsCorrectByRandomCheck

        public void ContinuousZipIsCorrectByRandomCheck() {
            var sw = new Stopwatch();

            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();


            var rng = new System.Random(31415926); //31415926

            var prev1 = 0;
            var prev2 = 0;

            for (int i = 0; i < 100000; i = i + 1) {
                prev1 = prev1 + rng.Next(1, 11);
                sm1.Add(prev1, prev1);
                prev2 = prev2 + rng.Next(1, 11);
                sm2.Add(prev2, prev2);
            }
            sm1.IsMutable = false;
            sm2.IsMutable = false;

            //Console.WriteLine("First map:");
            //foreach (var kvp in sm1)
            //{
            //    Console.WriteLine(kvp.Key);
            //}

            //Console.WriteLine("Second map:");
            //foreach (var kvp in sm2) {
            //    Console.WriteLine(kvp.Key);
            //}

            var series = new[] { sm1.Repeat(), sm2.Repeat(), };

            sw.Start();
            var allKeys = sm1.keys.Union(sm2.keys).OrderBy(x => x).ToArray();
            int[] expectedKeys = new int[allKeys.Length];
            int[] expectedValues = new int[allKeys.Length];
            var size = 0;
            for (int i = 0; i < allKeys.Length; i++) {
                var val = 0;
                KeyValuePair<int, int> temp;
                var hasFirst = sm1.TryFind(allKeys[i], Lookup.LE, out temp);
                if (hasFirst) {
                    val += temp.Value;
                    var hasSecond = sm2.TryFind(allKeys[i], Lookup.LE, out temp);
                    if (hasSecond) {
                        val += temp.Value;
                        expectedKeys[size] = allKeys[i];
                        expectedValues[size] = val;
                        size++;
                    }
                }
            }

            var expectedMap = SortedMap<int, int>.OfSortedKeysAndValues(expectedKeys, expectedValues, size);

            sw.Stop();

            //Console.WriteLine("Expected map:");
            //foreach (var kvp in expectedMap) {
            //    Console.WriteLine(kvp.Key + " ; " + kvp.Value);
            //}

            Console.WriteLine("Manual join, elapsed msec: {0}", sw.ElapsedMilliseconds);


            SortedMap<int, int> sum = new SortedMap<int, int>();

            for (int round = 0; round < 1; round++) {
                sw.Restart();
                var ser = series.Zip((k, varr) => varr.Sum());

                var cur = ser.GetCursor();
                while (cur.MoveNext()) {
                    sum.AddLast(cur.CurrentKey, cur.CurrentValue);
                }

                sw.Stop();
                Console.WriteLine("Zip join, elapsed msec: {0}", sw.ElapsedMilliseconds);
                //Console.WriteLine("StateCreation: {0}", RepeatCursor<int, int>.StateCreation);
                //Console.WriteLine("StateHit: {0}", RepeatCursor<int, int>.StateHit);
                //Console.WriteLine("StateMiss: {0}", RepeatCursor<int, int>.StateMiss);
            }



            //Console.WriteLine("Sync zip map:");
            //foreach (var kvp in sum) {
            //    Console.WriteLine(kvp.Key + " ; " + kvp.Value);
            //}
            Assert.AreEqual(expectedMap.Count, sum.Count, "Results of sync and expected must be equal");

            foreach (var kvp in expectedMap) {
                Assert.AreEqual(kvp.Value, sum[kvp.Key]);
            }

            for (int round = 0; round < 1; round++) {
                sw.Restart();
                var ser = series.Zip((k, varr) => varr.Sum());
//.........这里部分代码省略.........
开发者ID:b-e-n-j,项目名称:Spreads,代码行数:101,代码来源:ZipNTests.cs


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