本文整理汇总了C#中OrderedBag.Range方法的典型用法代码示例。如果您正苦于以下问题:C# OrderedBag.Range方法的具体用法?C# OrderedBag.Range怎么用?C# OrderedBag.Range使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OrderedBag
的用法示例。
在下文中一共展示了OrderedBag.Range方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
OrderedBag<Product> products = new OrderedBag<Product>();
Random priceGenerator = new Random();
int numberofProducts = 500000;
for (int i = 0; i < numberofProducts; i++)
{
Product testProduct = new Product();
testProduct.Price = priceGenerator.Next(1, int.MaxValue);
testProduct.Name = "product" + i;
products.Add(testProduct);
}
Console.WriteLine("{0} products added in bag.", numberofProducts);
Product firstProduct = new Product();
firstProduct.Price = 10;
Product secondProduct = new Product();
secondProduct.Price = int.MaxValue / 10;
var productsInRange = products.Range(firstProduct, true, secondProduct, true);
Console.WriteLine("First 20 products in range {0} - {1}", firstProduct.Price, secondProduct.Price);
for (int i = 0; i < 20; i++)
{
Console.WriteLine(productsInRange[i]);
}
Console.WriteLine("\nProducts in range {0} - {1}", firstProduct.Price, secondProduct.Price);
var productsInBigRange = products.Range(firstProduct, true, secondProduct, true);
Console.WriteLine("{0} products are found.", productsInBigRange.Count);
}
示例2: Main
public static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
OrderedBag<Product> catalog = new OrderedBag<Product>(new Comparison<Product>((x, y) => x.Price.CompareTo(y.Price)));
for (int i = 0; i < TotalProducts; i++)
{
decimal productPrice = randomGenerator.Next((int)MinProductPrice, (int)MaxProductPrice);
string productName = "Product " + i;
Product currentProduct = new Product(productPrice, productName);
catalog.Add(currentProduct);
}
OrderedBag<Product>.View productsInRange = default(OrderedBag<Product>.View);
for (int i = 0; i < SearchCount; i++)
{
productsInRange = catalog.Range(
new Product(MinProductRange + i * 0.001m, string.Empty), true,
new Product(MaxProductRange + i * 0.001m, string.Empty), true);
}
var firstNResults = productsInRange.Take(ResultCount);
Console.WriteLine(string.Join(Environment.NewLine, firstNResults));
sw.Stop();
Console.WriteLine(sw.Elapsed);
}
示例3: Main
static void Main(string[] args)
{
int items = 500010;
int ranges = 10000;
int priceRange = 200000;
OrderedBag<Product> orderedBag = new OrderedBag<Product>();
for (int i = 1; i < items; i++)
{
orderedBag.Add(new Product("P", i));
}
for (int i = 0; i < ranges; i++)
{
var result = orderedBag.Range(new Product("P", priceRange), true,
new Product("P", priceRange + 20000), true);
if (result.Count == 0)
{
Console.WriteLine("No products found.");
}
List<Product> collection = new List<Product>();
for (int j = 0; j < 20 && i < result.Count; j++)
{
collection.Add(result[j]);
}
Console.WriteLine("Collection contains {0} items. ", collection.Count);
}
}
示例4: Main
static void Main()
{
OrderedBag<Product> products = new OrderedBag<Product>();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 1; i < 500000; i++)
{
Product p = new Product();
p.Name = "Prodcut" + i;
p.Price = GetRandomNumber(35, 599) * i * GetRandomNumber(3, 5) / GetRandomNumber(2, 4);
products.Add(p);
}
stopwatch.Stop();
Console.WriteLine("Create and Add 500k products: {0}", stopwatch.Elapsed);
List<Product> prodRange = new List<Product>();
stopwatch.Reset();
stopwatch.Restart();
for (int i = 1; i <= 10000; i++)
{
int min = GetRandomNumber(35, 599) * i * GetRandomNumber(3, 5) / GetRandomNumber(2, 4);
int max = GetRandomNumber(35, 599) * i * 13 * GetRandomNumber(3, 5);
prodRange.AddRange(products.Range(new Product() { Price = min }, true, new Product() { Price = max }, true).Take(20));
}
stopwatch.Stop();
Console.WriteLine("Search for 10k random price ranges: {0}", stopwatch.Elapsed);
}
示例5: Main
/* 02. Write a program to read a large collection of products (name + price) 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.
*/
/// <summary>
/// Mains this instance.
/// </summary>
public static void Main()
{
OrderedBag<Product> products = new OrderedBag<Product>();
Random random = new Random();
// Test creation and filling.
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 500000; i++)
{
products.Add(new Product("Product " + i, random.Next(100, 1000)));
}
watch.Stop();
Console.WriteLine("The creation of 500 000 products takes: {0}", watch.Elapsed);
watch.Reset();
// Test searching.
List<Product> twentyElementsInRange = new List<Product>();
watch.Restart();
for (int i = 0; i < 10000; i++)
{
Product minSumProduct = new Product("Bulshit", random.Next(100, 500));
Product maxSumProduct = new Product("CoolStuff", random.Next(500, 1000));
twentyElementsInRange.AddRange(products.Range(minSumProduct, true, maxSumProduct, true).Take(20));
}
watch.Stop();
Console.WriteLine("10 000 price searches takes: {0}", watch.Elapsed);
}
示例6: Main
static void Main()
{
var collection = new OrderedBag<Product>();
int numberOfProducts = 500000;
int numberOfProductsToTake = 10000;
Console.WriteLine("Products generation started");
for (var i = 0; i < numberOfProducts; i++)
{
var product = new Product("Product #" + i, (decimal)i);
collection.Add(product);
}
Console.WriteLine("Products generated");
var stringBuilder = new StringBuilder();
Console.WriteLine("Collecting products...");
for (var i = 0; i < numberOfProductsToTake; i++)
{
var rangeOfProducts =
collection.Range(new Product("StartSearchProduct", 154000m), true, new Product("EndSearchProduct", 155000m), true)
.Take(20);
stringBuilder.AppendLine(string.Join(", ", rangeOfProducts.Select(p => p.ToString())));
}
Console.WriteLine(stringBuilder.ToString());
}
示例7: GetProductsInPriceRange
private static IEnumerable<Product> GetProductsInPriceRange(OrderedBag<Product> bag, decimal min, decimal max)
{
Product minProduct = bag.FirstOrDefault(p => p.Price >= min);
Product maxProduct = bag.FirstOrDefault(p => p.Price >= max);
var result = bag.Range(minProduct, true, maxProduct, false);
return result.Take(20);
}
示例8: Main
public static void Main()
{
myWatch.Start();
Random randomGen = new Random();
OrderedBag<Product> myBag = new OrderedBag<Product>();
for (int i = 0; i < 500000; i++)
{
myBag.Add(new Product("Product" + i, randomGen.Next(1, 1000000)));
}
int start = 0;
int end = 1000;
for (int i = 0; i < 10000; i++)
{
var subBag = myBag.Range(new Product(string.Empty, start), true, new Product(string.Empty, end), true);
IList<Product> firstTwenty = GetFirstResults(subBag);
////Console.WriteLine("Results");
////foreach (Product product in firstTwenty)
////{
//// Console.WriteLine(product);
////}
start += 10;
end += 10;
}
myWatch.Stop();
Console.WriteLine("END");
Console.WriteLine("Time: {0}", myWatch.Elapsed);
}
示例9: SolveWithBag
private static void SolveWithBag()
{
Console.WriteLine("---Bag---");
OrderedBag<Product> bag = new OrderedBag<Product>();
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < itemsCount; i++)
{
var price = rnd.Next(1, itemsCount);
bag.Add(new Product(GetRandomString(), GetRandomString(), GetRandomString(), price));
}
Console.WriteLine("Added {0} items in {1} time", itemsCount, sw.Elapsed);
sw.Restart();
var secondWatch = new Stopwatch();
for (int i = 0; i < searchesCount; i++)
{
var lowerProduct = new Product(GetRandomString(), GetRandomString(), GetRandomString(), rnd.Next(1, itemsCount / 2));
var upperProduct = new Product(GetRandomString(), GetRandomString(), GetRandomString(), rnd.Next(1, itemsCount / 2));
secondWatch.Start();
bag.Range(lowerProduct, true, upperProduct, true);
secondWatch.Stop();
}
Console.WriteLine("Found Range {0} items in {1} time", searchesCount, sw.Elapsed);
Console.WriteLine("Actual time spent getting the Range : {0}", secondWatch.Elapsed);
}
示例10: First20ProductsInInterval
private static Product[] First20ProductsInInterval(decimal minPrice, decimal maxPrice, OrderedBag<Product> products)
{
if (minPrice > maxPrice)
{
throw new ArgumentException("Maximal price must be greater or equal than minimal price!");
}
GenerateExceptionIfValueLessThanZero(minPrice, "minPrice");
GenerateExceptionIfValueLessThanZero(maxPrice, "maxPrice");
OrderedBag<Product>.View productsInRange =
products.Range(
new Product("testerMin", minPrice),
true,
new Product("testerMin", maxPrice),
true);
if (productsInRange.Count == 0)
{
return new Product[0];
}
int selectedItemsLen = (20 > productsInRange.Count) ? productsInRange.Count : 20;
Product[] selectedItems = new Product[selectedItemsLen];
for (int i = 0; i < selectedItemsLen; i++)
{
selectedItems[i] = productsInRange[i];
}
return selectedItems;
}
示例11: PrintFirst20ProductsInRange
private static void PrintFirst20ProductsInRange(OrderedBag<Product> products)
{
// find first 20 elements in range
var lowAndHighBound = GenerateBoundToSearchTo();
var productsInRange = products.Range(new Product(lowAndHighBound[0], "low"), true, new Product(lowAndHighBound[1], "high"), true);
Console.WriteLine("Start searching from: {0}", lowAndHighBound[0]);
Console.WriteLine("End searching to: {0}", lowAndHighBound[1]);
if (productsInRange.Count >= 20)
{
for (int i = 0; i < 20; i++)
{
Console.WriteLine(productsInRange[i]);
}
}
else
{
foreach (var product in productsInRange)
{
Console.WriteLine(product);
}
}
}
示例12: Main
public static void Main()
{
OrderedBag<Product> products = new OrderedBag<Product>();
for (int i = 0; i < 500000; i++)
{
var newProduct = new Product()
{
Name = string.Format("Product #{0}", i + 1),
Price = Math.Round((decimal)(random.NextDouble() * 10000), 2)
};
products.Add(newProduct);
}
var priceFrom = 999;
var PriceTo = 1000;
Console.WriteLine("Sub-range [{0}...{1}]: ", priceFrom, PriceTo);
var result = products.Range(new Product() { Price = priceFrom }, true, new Product() { Price = PriceTo}, true);
foreach(var product in result)
{
Console.WriteLine(product);
}
}
示例13: Main
public static void Main(string[] args)
{
var data = new OrderedBag<Product>();
Console.WriteLine("Creating random products");
for (int i = 0; i < ProductCount; i++)
{
data.Add(
new Product(
RandomGenerator.RandomString(RandomGenerator.RandomInt(1, 100)),
(double)(RandomGenerator.RandomInt(1, 100000)) / 100));
}
Console.WriteLine("Searching products");
for (int i = 0; i < SearchCount; i++)
{
double minValue = RandomGenerator.RandomInt(1, 100);
double maxValue = RandomGenerator.RandomInt((int)minValue, (int)minValue + 900);
var range = data.Range(new Product("", minValue), true, new Product("", maxValue), true);
Console.WriteLine("Search #" + i);
for (int j = 0; j < 20; j++)
{
Console.WriteLine("Item# " + j + " = " + range[j].Name);
}
}
}
示例14: Main
public static void Main()
{
var products = new OrderedBag<Product>();
var builder = new StringBuilder();
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
var productPriceTokens = Console.ReadLine().Split();
var name = productPriceTokens[0];
var price = float.Parse(productPriceTokens[1]);
products.Add(new Product(name, price));
}
var pricesTokens = Console.ReadLine().Split();
var lower = float.Parse(pricesTokens[0]);
var upper = float.Parse(pricesTokens[1]);
var subrangeProducts = products.Range(new Product(string.Empty, lower), true, new Product(string.Empty, upper), true);
foreach (var product in subrangeProducts)
{
Console.WriteLine(product.ToString());
}
}
示例15: Main
static void Main(string[] args)
{
OrderedBag<ComparableKeyValuePair<string, int>> catalog =
new OrderedBag<ComparableKeyValuePair<string, int>>();
for (int i = 0; i < 25; i++)
{
ComparableKeyValuePair<string, int> item = new ComparableKeyValuePair<string, int>(i.ToString(), i);
catalog.Add(item);
}
ComparableKeyValuePair<string, int> downRange = new ComparableKeyValuePair<string, int>("3", 3);
ComparableKeyValuePair<string, int> upRange = new ComparableKeyValuePair<string, int>("23", 23);
OrderedBag<ComparableKeyValuePair<string, int>>.View range = catalog.Range(downRange, true, upRange, true);
Console.WriteLine("from 3");
foreach (ComparableKeyValuePair<string, int> item in range)
{
Console.WriteLine(item.Value);
}
Console.WriteLine("to 23");
// Test for 500 000 products and 10 000 price searches.
catalog =
new OrderedBag<ComparableKeyValuePair<string, int>>();
Stopwatch timer = new Stopwatch();
timer.Start();
for (int i = 0; i <= 500000; i++)
{
ComparableKeyValuePair<string, int> item = new ComparableKeyValuePair<string, int>(i.ToString(), i);
catalog.Add(item);
}
Console.WriteLine("Adding 500 000 elements: " + timer.Elapsed.TotalSeconds + " sec");
OrderedBag<ComparableKeyValuePair<string, int>>.View bigRange = null;
ComparableKeyValuePair<string, int> from = new ComparableKeyValuePair<string, int>("400000", 400000);
ComparableKeyValuePair<string, int> to = new ComparableKeyValuePair<string, int>("410000", 410000);
timer.Reset();
timer.Start();
for (int i = 10000; i <= 400000; i = i + 20)
{
bigRange = catalog.Range(from, true, to, false);
from = new ComparableKeyValuePair<string, int>((i - 20).ToString(), (i - 20));
to = new ComparableKeyValuePair<string, int>(i.ToString(), i);
}
Console.WriteLine("10 000 searches (200 000 to 400 000):" + timer.Elapsed.TotalMilliseconds + " ms");
timer.Stop();
}