本文整理汇总了C#中OrderedBag类的典型用法代码示例。如果您正苦于以下问题:C# OrderedBag类的具体用法?C# OrderedBag怎么用?C# OrderedBag使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OrderedBag类属于命名空间,在下文中一共展示了OrderedBag类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddProductsToBag
public static void AddProductsToBag(OrderedBag<Product> orderedBag, int numberOfItems)
{
for (int i = 1; i < numberOfItems; i++)
{
orderedBag.Add(new Product(i.ToString(), i));
}
}
示例2: DijkstraFindMinCableLengthBetweenHouses
static void DijkstraFindMinCableLengthBetweenHouses(Dictionary<HouseNode,
List<CableConnection>> graph, HouseNode houseToStartFrom)
{
OrderedBag<HouseNode> houseQueue = new OrderedBag<HouseNode>();
foreach (var house in graph)
{
house.Key.MinCableLenth = int.MaxValue;
}
houseToStartFrom.MinCableLenth = 0;
houseQueue.Add(houseToStartFrom);
while (houseQueue.Count > 0)
{
var currentHouse = houseQueue.RemoveFirst();
if (currentHouse.MinCableLenth == int.MaxValue)
{
break;
}
foreach (var connection in graph[currentHouse])
{
var currentCableLength = currentHouse.MinCableLenth + connection.CableLenth;
if (connection.House.MinCableLenth > currentCableLength)
{
connection.House.MinCableLenth = currentCableLength;
houseQueue.Add(connection.House);
}
}
}
}
示例3: ReadArticles
private static OrderedBag<Article> ReadArticles(string path)
{
OrderedBag<Article> articles = new OrderedBag<Article>();
using (StreamReader reader = new StreamReader(path))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] data = line.Split('|');
double price = double.Parse(data[0]);
Article article = new Article(
price,
data[1].Trim(),
data[2].Trim(),
data[3].Trim());
articles.Add(article);
}
}
return articles;
}
示例4: Main
public static void Main()
{
OrderedBag<Product> catalog = new OrderedBag<Product>();
for (int i = 0; i < 500000; i++)
{
catalog.Add(new Product("Product" + i, GetRandomNumber(39,709)));
}
var prices = catalog.FindAll(x => x.Price > 200 && x.Price < 350);
int count = 20;
StringBuilder sb = new StringBuilder();
foreach (var product in prices)
{
sb.AppendFormat("name: {0} -> price {1}", product.Name, product.Price);
sb.AppendLine();
count -= 1;
if (count <= 0)
{
break;
}
}
Console.WriteLine("Print results: ");
Console.WriteLine(sb.ToString());
}
示例5: 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());
}
}
示例6: Main
static void Main()
{
OrderedBag<Product> bag = new OrderedBag<Product>();
Random rand = new Random();
for (int i = 0; i < CountProducts; i++)
{
bag.Add(GetRandomProduct(rand));
}
decimal minPrice = 0M;
for (int i = 0; i < PriceSearches; i++)
{
Console.ForegroundColor = ConsoleColor.DarkMagenta;
decimal maxPrice = minPrice + 100M;
var result = GetProductsInPriceRange(bag, minPrice, maxPrice);
Console.WriteLine(new string('$', 79));
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("First 20 Products in Price Range {0:C} - {1:C}: ", minPrice, maxPrice);
Console.ForegroundColor = ConsoleColor.DarkYellow;
if (result.Count() < 1) { Console.Write("None\n"); }
Console.WriteLine(string.Join(" | ", result));
minPrice += 100;
Thread.Sleep(500);
}
}
示例7: 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);
}
示例8: Main
public static void Main()
{
var collectionOfProducts = new OrderedBag<Product>();
for (int i = 0; i < NumberOfProducts; i++)
{
var product = new Product(RandomGenerator.GetRandomStringWithRandomLength(3, 7),
RandomGenerator.RandomDecimalBetween(1, 100));
collectionOfProducts.Add(product);
}
Console.WriteLine("{0} products have been generated!", NumberOfProducts);
var testSearch = new List<Product>();
Console.WriteLine("Running {0} searches:", NumberOfSearches);
for (int i = 0; i < NumberOfSearches; i++)
{
testSearch = SearchProductsByRange(collectionOfProducts, RandomGenerator.RandomDecimalBetween(1, 10),
RandomGenerator.RandomDecimalBetween(11, 100));
if (i%100 == 0)
{
Console.Write("=");
}
}
Console.WriteLine("\r\nTotal products matching the last search criteria: {0}", testSearch.Count);
Console.WriteLine("First 20 products:");
foreach (var product in testSearch.Take(20))
{
Console.WriteLine(product);
}
}
示例9: Main
public static void Main()
{
OrderedBag<Product> test = new OrderedBag<Product>();
string originalName = "Product";
decimal originalPrice = 1;
Product productToAdd;
for (int i = 0; i < 500000; i++)
{
productToAdd = new Product(originalName + i, originalPrice + i);
test.Add(productToAdd);
}
int numberOfRangeChecks = 0;
List<Product> topTwentyProductsInRange = new List<Product>();
for (int i = 0, j = 10000; i < 10000; i++, j += 10)
{
var productsInRange = test.Range(new Product("", i), true, new Product("", j), true);
for (int k = 0; k < 20; k++)
{
topTwentyProductsInRange.Add(productsInRange[k]);
}
numberOfRangeChecks++;
}
}
示例10: DijkstraAlgorithm
static void DijkstraAlgorithm(Node[] graph, Node source)
{
var priority = new OrderedBag<Node>();
for (int i = 1; i < graph.Length; i++)
{
graph[i].DijkstraDistance = int.MaxValue;
}
source.DijkstraDistance = 0;
priority.Add(source);
while (priority.Count != 0)
{
Node currentNode = priority.RemoveFirst();
if (currentNode.DijkstraDistance == int.MaxValue)
{
break;
}
for (int i = 0; i < currentNode.Edges.Count; i++)
{
int potDistance = currentNode.DijkstraDistance + currentNode.Edges[i].Weight;
if (potDistance < graph[currentNode.Edges[i].NodeId].DijkstraDistance)
{
graph[currentNode.Edges[i].NodeId].DijkstraDistance = potDistance;
priority.Add(graph[currentNode.Edges[i].NodeId]);
}
}
}
}
示例11: Main
static void Main()
{
StringBuilder allProductsToString = new StringBuilder();
Stopwatch stopWatch = new Stopwatch();
OrderedBag<Product> siabongaOrderedBag = new OrderedBag<Product>();
int ranges = 10000;
//stopWatch.Start();
AddProductsToBag(siabongaOrderedBag, 500001);
List<Product> twentyProductsInSomeRange=FindFirstTwenty(siabongaOrderedBag,55,452);
for (int i = 0; i<twentyProductsInSomeRange.Count; i++)
{
Console.WriteLine("Name: "+twentyProductsInSomeRange[i].Name+" Price: "+twentyProductsInSomeRange[i].Price);
}
stopWatch.Start();
for (int i = 0; i < ranges; i++)
{
//stopWatch.Start();
List<Product> found = FindFirstTwenty(siabongaOrderedBag, 100000, 100000 + i);
//time for adding to stringbuilder not included
//stopWatch.Stop();
allProductsToString.Append(found.GetProductsToString());
}
// uncomment to see all ranges found
// Console.WriteLine(allProductsToString.ToString());
Console.WriteLine("Range searching of first 20 elm. in 500 000 elm. 10 000 times done for:\n "
+ stopWatch.Elapsed);
}
开发者ID:stoyanovalexander,项目名称:TheRepositoryOfAlexanderStoyanov,代码行数:32,代码来源:LargeColectionOfProducts.cs
示例12: Main
public static void Main()
{
string[] stringValues = new string[500000];
for (int i = 0; i < 500000; i++)
{
stringValues[i] = RandomString(5);
}
decimal[] numberValues = new decimal[500000];
for (int i = 0; i < 500000; i++)
{
numberValues[i] = GetRandomNumber(1, 1000);
}
OrderedBag<Product> products = new OrderedBag<Product>();
for (int i = 0; i < 500000; i++)
{
products.Add(new Product
{
Name = stringValues[i],
Price = numberValues[i]
});
}
Console.WriteLine("Products added!");
TestOrderBagSearchSpeed(products);
}
示例13: 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);
}
}
示例14: 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);
}
}
}
示例15: FillTheKnapsack
private static void FillTheKnapsack(OrderedBag<PriceWeigth> priceWeigth, int maxCapacity)
{
double totalPrice = 0;
int lastItemIndex = 0;
var priceWeigthList = priceWeigth.Reversed().ToList();
int currentLoad = 0;
while (currentLoad < maxCapacity)
{
int crntItemWeight = priceWeigthList[lastItemIndex].Weight;
if (currentLoad + crntItemWeight <= maxCapacity)
{
totalPrice += priceWeigthList[lastItemIndex].Price;
lastItemIndex++;
currentLoad += crntItemWeight;
}
else
{
double pricePortion = (maxCapacity - currentLoad) / (double)crntItemWeight;
totalPrice += (priceWeigthList[lastItemIndex].Price * pricePortion);
currentLoad += crntItemWeight;
}
}
Console.WriteLine("Total price = {0:f2}", totalPrice);
}