本文整理汇总了C#中OrderedDictionary.Range方法的典型用法代码示例。如果您正苦于以下问题:C# OrderedDictionary.Range方法的具体用法?C# OrderedDictionary.Range怎么用?C# OrderedDictionary.Range使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OrderedDictionary
的用法示例。
在下文中一共展示了OrderedDictionary.Range方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
/* Task 2:
* Write a program to read a large collection of products (name + price) I love
* and efficiently find the first 20 products in the price range [a…b]. Test for
* 500 000 products and 10 000 price searches.
* Hint: you may use OrderedBag<T> and sub-ranges.
*/
static void Main(string[] args)
{
Random randomGenerator = new Random();
Stopwatch stopWatch = new Stopwatch();
string[] product = { "bread", "butter", "meat", "eggs", "flower", "oil", "soda", "candy" };
var productLenght = product.Length;
var orderedDictionary = new OrderedDictionary<int, string>();
stopWatch.Start();
while (orderedDictionary.Count < 500000)
{
var key = randomGenerator.Next(1, 1000000);
var value = product[randomGenerator.Next(0, productLenght)];
if (!orderedDictionary.ContainsKey(key))
{
orderedDictionary.Add(key, value);
}
}
stopWatch.Stop();
Console.WriteLine("Time for creating the elements is: {0}", stopWatch.Elapsed);
stopWatch.Reset();
stopWatch.Start();
for (int i = 0; i < 10000; i++)
{
var min = randomGenerator.Next(0, 500000);
var max = randomGenerator.Next(500000, 1000000);
var products = orderedDictionary.Range(min, true, max, true);
}
stopWatch.Stop();
Console.WriteLine("Time for performing 10k range searches: {0}", stopWatch.Elapsed);
var range = orderedDictionary.Range(1000, true, 100000, true);
for (int i = 0; i < 20; i++)
{
Console.WriteLine(range.ElementAt(i));
}
}
示例2: Main
public static void Main()
{
OrderedDictionary<double, Product> products = new OrderedDictionary<double, Product>();
for (int i = 0; i < 500000; i++)
{
var currPrice = 100 + i;
products.Add(currPrice, new Product("Pesho" + i, currPrice));
}
// Wait 2-3 seconds.
Console.WriteLine(products.Range(120, true, 1040, true));
}
示例3: TestOrderedDictionary
/// <summary>
/// OrderedDictionary<TKey,TValue> (NO DUPLICATES)
/// A dictionary based on balanced search tree
/// Add / Find / Remove work in time O(log(N))
/// Provides fast .Range(from,to) operation
/// </summary>
private static void TestOrderedDictionary()
{
OrderedDictionary<int, Student> students = new OrderedDictionary<int, Student>();
var student1 = new Student("First", 21);
var student2 = new Student("Second", 21);
students.Add(5, student1);
students.Add(2, student2);
var student3 = new Student("Third", 22);
var student4 = new Student("Forth", 23);
var student5 = new Student("Fifth", 24);
students.Add(3, student3);
students.Add(4, student4);
students.Add(1, student5);
foreach (var item in students)
{
Console.WriteLine(item);
}
Console.WriteLine("========== Range Key >= 2 && <= 4 ============= ");
var inRangeStudents = students.Range(2, true, 4, true);
foreach (var item in inRangeStudents)
{
Console.WriteLine(item);
}
Console.WriteLine("==========ForEach(x => { x.Value.Age += 1; Console.WriteLine(x); })============= ");
students.ForEach(x => { x.Value.Age += 1; Console.WriteLine(x); });
}