Ejemplo n.º 1
0
        static void Main()
        {
            var products = new OrderedMultiDictionary<decimal, Product>(true);

            Console.Write("Creating and adding products");
            for (int i = 0; i < 1000000; i++)
            {
                products.Add(
                    (i + 1) % 50 + (decimal)i / 100,
                    new Product(
                    "Product #" + (i + 1),
                    ((i + 1) % 50 + (decimal)i / 100),
                    "Vendor#" + i % 50000,
                    null));
                if (i % 1000 == 0)
                {
                    Console.Write(".");
                }
            }

            Console.WriteLine();

            var productsInPricerangeFrom10To11 = products.Range(10M, true, 11M, true);
            var productsInPricerangeFrom15To25 = products.Range(15M, true, 25M, true);
            var productsInPricerangeFrom30To35 = products.Range(30M, true, 35M, true);

            Console.WriteLine("Products with price between 10 and 11: " + productsInPricerangeFrom10To11.Count);
            Console.WriteLine("Products with price between 15 and 25: " + productsInPricerangeFrom15To25.Count);
            Console.WriteLine("Products with price between 30 and 35: " + productsInPricerangeFrom30To35.Count);
        }
Ejemplo n.º 2
0
        public static void RunTest()
        {
            Console.Clear();
            OrderedMultiDictionary <double, string> articles = new OrderedMultiDictionary <double, string>(true);

            int max = 4000;
            int min = 1000;
            var r   = new Random();

            for (int i = 1; i <= 10; i++)
            {
                var article = new Article("Barcode: b" + i, "Vendor: v" + i, "Title: t" + i, r.Next(min, max));
                articles.Add(article.Price, article.Title);
                articles.Add(article.Price, article.Vendor);
                articles.Add(article.Price, article.Barcode);
            }
            var GivenRange = articles.Range(min, true, max, true);

            foreach (var article in GivenRange)
            {
                Console.WriteLine($"Price: {article.Key}");
                foreach (var item in article.Value)
                {
                    Console.WriteLine($"{item}");
                }
                Console.WriteLine();
            }
            Console.WriteLine("------------------------------");
            Console.WriteLine("Enter price range: ");
            Console.Write("min: ");
            int from = int.Parse(Console.ReadLine());

            Console.Write("max: ");
            int to = int.Parse(Console.ReadLine());

            Console.Clear();

            Console.WriteLine($"Articles in the diapason {from} - {to}: \n");
            var articlesInGivenRange = articles.Range(from, true, to, true);

            foreach (var article in articlesInGivenRange)
            {
                Console.WriteLine($"Price: {article.Key}");
                foreach (var item in article.Value)
                {
                    Console.WriteLine($"{item}");
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            Random randGen = new Random();
            OrderedMultiDictionary<Article, int> articles = new OrderedMultiDictionary<Article, int>(false);

            for (int i = 0; i < 100; i++)
            {
                Article myArticle = new Article(randGen.Next(10000, 99999), "Vendor" + i, "Title" + i, randGen.Next(1000, 9999));

                if (!articles.ContainsKey(myArticle))
                {
                    articles.Add(myArticle, 1);
                }
            }

            Console.WriteLine("Enter bottom price:");
            int startPrice = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter top price:");
            int endPrice = int.Parse(Console.ReadLine());

            OrderedMultiDictionary<Article, int>.View neededArticles = articles.Range(
                new Article(0, string.Empty, string.Empty, 2000),
                true,
                new Article(0, string.Empty, string.Empty, 4000),
                true);

            foreach (var item in neededArticles)
            {
                Console.WriteLine(item.Key);
            }
        }
Ejemplo n.º 4
0
    static void Main()
    {
        StreamReader fileName = new StreamReader("../../products.txt");

        OrderedMultiDictionary<decimal, string> productsByPrice = new OrderedMultiDictionary<decimal, string>(true);

        using (fileName)
        {
            string line;

            while ((line = fileName.ReadLine()) != null)
            {
                var tokens = line.Split(';');
                decimal price = decimal.Parse(tokens[0]);
                string product = tokens[1];

                productsByPrice.Add(price, product);
            }
        }

        Console.WriteLine("Search products in price range: ");
        Console.Write("From price:");
        decimal fromPrice = decimal.Parse(Console.ReadLine());
        Console.Write("To price: ");
        decimal toPrice = decimal.Parse(Console.ReadLine());

        var productsInPriceRange = productsByPrice.Range(fromPrice, true, toPrice, true);

        Console.Write("Number of products to be printed: ");
        int numberOfProductsToPrint = int.Parse(Console.ReadLine());

        PrintGivenNumberOfProducts(numberOfProductsToPrint, productsInPriceRange);
    }
Ejemplo n.º 5
0
 public static void Main(string[] args)
 {
     var products = new OrderedMultiDictionary<float, string> (true);
     Console.WriteLine("Add 'product price'. 'q' for end. ");
     while(true) {
         var input = Console.ReadLine ();
         if ("q" == input) {
             break;
         }
         var pair = input.Split (' ');
         try {
             products.Add (float.Parse (pair [1]), pair [0]);
         } catch (FormatException e) {
             Console.WriteLine (e.Message);
         }
     }
     Console.WriteLine("Add range 'price1 price2'. 'q' for end. ");
     while (true) {
         var input = Console.ReadLine ();
         if ("q" == input) {
             break;
         }
         var range = input.Split (' ');
         try {
             Console.WriteLine(products.Range (float.Parse(range [0]), true, float.Parse(range [1]), true));
         } catch (FormatException e) {
             Console.WriteLine (e.Message);
         }
     }
 }
Ejemplo n.º 6
0
    /* 2. A large trade company has millions of articles, each described
       by barcode, vendor, title and price. Implement a data structure to
       store them that allows fast retrieval of all articles in given price
       range [x...y].

       Hint: use OrderedMultiDictionary<K,T> from Wintellect's Power
       Collections for .NET.
     * */
    static void Main(string[] args)
    {
        // What's there to "implement"?
        // That's exactly what OrderedMultiDictionary was made for.

        // input generator in bin\debug\generator.cs

        var products = new OrderedMultiDictionary<decimal, string>(true);

        foreach (var line in File.ReadLines("products.txt").Skip(1))
        {
            var split = line.Split('|');
            products.Add(decimal.Parse(split[0]), split[1]);
        }

        foreach (var line in File.ReadLines("commands.txt"))
        {
            Console.WriteLine("Command: " + line);
            var split = line.Split('|');

            var first = decimal.Parse(split[0]);
            var second = decimal.Parse(split[1]);

            var matching = products.Range(Math.Min(first, second), true,
                                          Math.Max(first, second), true);

            Console.WriteLine(string.Join(", ", matching));
            Console.WriteLine();
            Console.WriteLine("Press Ctrl+C to quit, or any other key to continue...");
            Console.ReadLine();
        }
    }
Ejemplo n.º 7
0
        public static void Main(string[] args)
        {
            OrderedMultiDictionary<double, Article> articles = new OrderedMultiDictionary<double, Article>(true);
            Random randomNumberGenerator = new Random();
            double randomNumber;
            for (int i = 0; i < 2000000; i++)
            {
                randomNumber = randomNumberGenerator.NextDouble() * MaxValue;
                Article article = new Article("barcode" + i, "vendor" + i, "article" + i, randomNumber);
                articles.Add(article.Price, article);
            }

            Console.Write("from = ");
            double from = double.Parse(Console.ReadLine());
            Console.Write("to = ");
            double to = double.Parse(Console.ReadLine());
            var articlesInRange = articles.Range(from, true, to, true);
            foreach (var pair in articlesInRange)
            {
                foreach (var article in pair.Value)
                {
                    Console.WriteLine("{0} => {1}", Math.Round(article.Price, 2), article);
                }
            }
        }
Ejemplo n.º 8
0
    static void Main()
    {
        var productsPrices = new OrderedMultiDictionary <decimal, string>(false);
        int productsCount  = int.Parse(Console.ReadLine());

        for (int i = 0; i < productsCount; i++)
        {
            string[] productParams = Console.ReadLine().Split();
            string   productType   = productParams[0];
            decimal  productPrice  = decimal.Parse(productParams[1]);
            productsPrices.Add(productPrice, productType);
        }

        decimal[] priceRangeParams = Console.ReadLine().Split().Select(decimal.Parse).ToArray();
        decimal   mintPrice        = priceRangeParams[0];
        decimal   maxPrice         = priceRangeParams[1];

        foreach (var priceProductsPair in productsPrices.Range(mintPrice, true, maxPrice, true))
        {
            foreach (string product in priceProductsPair.Value)
            {
                Console.WriteLine("{0} {1}", priceProductsPair.Key, product);
            }
        }
    }
Ejemplo n.º 9
0
 private static void PrintAllArticlesFromGivenRange(OrderedMultiDictionary<int, Article> articles, int from, int to)
 {
     foreach (var article in articles.Range(from, true, to, true))
     {
         Console.WriteLine(article.Value.ToString());
     }
 }
        static void Main()
        {
            StreamReader reader = new StreamReader(ProductsFile);
            OrderedMultiDictionary<double, string> multiDict = new OrderedMultiDictionary<double, string>(true);

            string currentLine = reader.ReadLine();

            while (currentLine != null)
            {
                string[] productParameters = currentLine.Split(' ');
                string productName = productParameters[0];
                double price = double.Parse(productParameters[1]);

                multiDict[price].Add(productName);

                currentLine = reader.ReadLine();
            }

            Console.Write("Insert price range: ");
            string[] priceRangeParameters = Console.ReadLine().Split(' ');

            double start = double.Parse(priceRangeParameters[0]);
            double end = double.Parse(priceRangeParameters[1]);

            var range = multiDict.Range(start, true, end, true);

            foreach (var price in range)
            {
                foreach (var product in price.Value)
                {
                    Console.WriteLine(product);
                }
            }
        }
Ejemplo n.º 11
0
        public static void Main()
        {
            OrderedMultiDictionary <int, Article> dict = new OrderedMultiDictionary <int, Article>(true);

            Console.WriteLine("Filling data in the dictionary...");

            var sw = new Stopwatch();

            sw.Start();

            for (int i = 0; i < 1000000; i++)
            {
                dict.Add(i % 50, new Article("barcode", "vendor", "title", i % 50));
            }

            Console.WriteLine(sw.Elapsed);

            sw.Restart();

            Console.WriteLine("Retrieving articles in given range:");
            for (int i = 0; i < 1000000; i++)
            {
                dict.Range(i % 100, true, i % 100, true);
            }

            Console.WriteLine(sw.Elapsed);
        }
Ejemplo n.º 12
0
        private static void ReadInput(OrderedMultiDictionary <double, string> products)
        {
            int elements = int.Parse(Console.ReadLine());

            for (int i = 0; i < elements; i++)
            {
                string[] parameters = Console.ReadLine().Split();
                string   name       = parameters[0];
                double   price      = double.Parse(parameters[1]);
                products.Add(price, name);
            }

            string line = Console.ReadLine();

            double[] ranges     = line.Split().Select(double.Parse).ToArray();
            double   lowerBound = ranges[0];
            double   upperBound = ranges[1];

            var range = products.Range(lowerBound, true, upperBound, true).Take(20);

            foreach (var pair in range)
            {
                Console.WriteLine("{0} {1}", pair.Key, string.Join(", ", pair.Value));
            }
        }
Ejemplo n.º 13
0
        public static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            var eventCount = int.Parse(Console.ReadLine());
            var datesAndEvents = new OrderedMultiDictionary<DateTime, string>(true);
            for (int i = 0; i < eventCount; i++)
            {
                var currentEvent = Console.ReadLine();
                var token = currentEvent.Split('|');
                var date = DateTime.Parse(token[1].Trim());
                var name = token[0].Trim();
                datesAndEvents.Add(date, name);
            }

            var queryNumber = int.Parse(Console.ReadLine());
            for (int i = 0; i < queryNumber; i++)
            {
                var query = Console.ReadLine().Split('|');
                var startDate = DateTime.Parse(query[0].Trim());
                var endDate = DateTime.Parse(query[1].Trim());
                var queryData = datesAndEvents.Range(startDate, true, endDate, true);
                Console.WriteLine(queryData.KeyValuePairs.Count);
                foreach (var e in queryData)
                {
                    foreach (var name in e.Value)
                    {
                        Console.WriteLine("{0} | {1:dd-MMM-yyyy}", name, e.Key);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        static void Main()
        {
            var mDict = new OrderedMultiDictionary<decimal, Product>(true);
            string[] productNames =
            {
                "Toy", "Food", "Car", "TV", "Computer", "Refrigerator", 
                "Bicylce", "Skateboard", "Bag", "EReader", "Phone", "Guitar", 
            };
            var rand = new Random();
            for (int i = 0; i < 500000; i++)
            {
                decimal price = rand.Next(1, 10000);
                string name = productNames[rand.Next(0, productNames.Length)];
                mDict.Add(price, new Product(name, price));
            }

            var productsInRange = mDict.Range(25, true, 30, true);
            Console.WriteLine("Total products in price range: {0}", productsInRange.KeyValuePairs.Count);
            Console.WriteLine("==================================");
            foreach (var products in productsInRange)
            {
                Console.WriteLine("Price: {0}, Count: {1}", products.Key, productsInRange[products.Key].Count);
                Console.WriteLine("Products: {0}", string.Join(", ", products.Value));
                Console.WriteLine("==================================");
            }
        }
Ejemplo n.º 15
0
    static void Main()
    {
        int    productNameMaxLenghth = 30;
        double productMinPrice       = 1;
        double productMaxPrice       = 100;
        int    count         = 500000;
        int    priceSearches = 10000;

        Console.WriteLine("Generating random products with random prices...");
        OrderedMultiDictionary <double, string> products = GenerateRandomProducts(
            count, productNameMaxLenghth, productMinPrice, productMaxPrice);

        DateTime startTime = DateTime.Now;

        Console.WriteLine("\nStart finding: {0}", DateTime.Now);
        for (int i = 0; i < priceSearches; i++)
        {
            double minRangePrice   = GetRandomBetween(productMinPrice, productMaxPrice - 1);
            double maxRangePrice   = GetRandomBetween(minRangePrice, productMaxPrice);
            var    productsInRange = products.Range(minRangePrice, true, maxRangePrice, true).Take(20);
            // Warning!!! Printing is very slow operation.
            //Console.WriteLine("Produucts in range [{0}-{1}]: {2}", minRangePrice, maxRangePrice, string.Join("\n", productsInRange));
            //Console.WriteLine(new string('-',80));
        }
        Console.WriteLine("End finding: {0}", DateTime.Now);
        Console.WriteLine("Total finding time {0}", DateTime.Now - startTime);
    }
Ejemplo n.º 16
0
        public static void Main()
        {
            StringBuilder consoleResult = new StringBuilder();

            Console.WriteLine("Adding products...");
            for (int i = 0; i < 500000; i++)
            {
                products.Add(RandomDataGenerator.GetRandomNumberInRange(0, 100000), RandomDataGenerator.GetRandomString(1, 5));
            }

            Console.WriteLine("Starting the search test...");
            for (int i = 0; i < 10000; i++)
            {
                int minValue = RandomDataGenerator.GetRandomNumberInRange(0 + i, 5 + i);
                int maxValue = RandomDataGenerator.GetRandomNumberInRange(minValue, minValue + 5 + i);

                consoleResult.AppendLine(string.Format("Results for price between: {0} and {1}", minValue, maxValue));

                products.Range(minValue, true, maxValue, false)
                .Take(20)
                .ToList()
                .ForEach(pair =>
                {
                    foreach (var item in pair.Value)
                    {
                        consoleResult.AppendLine(string.Format("{0} -> {1}", item, pair.Key));
                    }
                });
            }

            Console.WriteLine(consoleResult.ToString());
        }
Ejemplo n.º 17
0
        public static void FindByPriceRange(string command)
        {
            var tokens    = command.Split(new char[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var fromPrice = double.Parse(tokens[0]);
            var toPrice   = double.Parse(tokens[1]);

            var productsToShow = productsByPriceRange.Range(fromPrice, true, toPrice, true);

            if (productsToShow.Count == 0)
            {
                answer.AppendLine("No products found");
                return;
            }
            List <Product> temp = new List <Product>();

            foreach (var pr in productsToShow)
            {
                foreach (var p in pr.Value)
                {
                    temp.Add(p);
                }
            }
            var ordered = temp.OrderBy(x => x.Name).ThenBy(x => x.Producer).ThenBy(x => x.Price);

            foreach (var item in ordered)
            {
                answer.AppendLine(item.ToString());
            }
        }
Ejemplo n.º 18
0
        static void Main(string[] args)
        {
            OrderedMultiDictionary <decimal, Article> articles =
                new OrderedMultiDictionary <decimal, Article>(true);

            articles.Add(12.43m, new Article("Choco", "123124234", "Milka", 12.43m));
            articles.Add(14.43m, new Article("Natural", "123124234", "Milka", 14.43m));
            articles.Add(15.43m, new Article("Raffy", "123124234", "Milka", 15.43m));
            articles.Add(17.43m, new Article("Milk", "123124234", "Milka", 17.43m));
            articles.Add(18.43m, new Article("Sugar", "123124234", "Milka", 18.43m));
            articles.Add(19.43m, new Article("Bread", "123124234", "Milka", 19.43m));
            articles.Add(20.43m, new Article("Susam", "123124234", "Milka", 20.43m));
            articles.Add(23.43m, new Article("Paper", "123124234", "Milka", 23.43m));
            articles.Add(25.43m, new Article("Grape", "123124234", "Milka", 25.43m));
            articles.Add(54.43m, new Article("Banana", "123124234", "Milka", 54.43m));
            articles.Add(43.43m, new Article("Orange", "123124234", "Milka", 43.43m));
            articles.Add(32.43m, new Article("Melon", "123124234", "Milka", 32.43m));
            articles.Add(24.43m, new Article("WaterMelon", "123124234", "Milka", 24.43m));
            articles.Add(76.43m, new Article("Junk", "123124234", "Milka", 76.43m));

            var rangeArticles = articles.Range(20m, true, 35m, true);

            foreach (var article in rangeArticles)
            {
                foreach (var item in article.Value)
                {
                    Console.WriteLine(item);
                }
            }
        }
Ejemplo n.º 19
0
        static void Main(string[] args)
        {
            OrderedMultiDictionary <decimal, Product> products = new OrderedMultiDictionary <decimal, Product>(true);

            string[] priceRange    = Console.ReadLine().Split();
            decimal  startPrice    = decimal.Parse(priceRange[0]);
            decimal  endPrice      = decimal.Parse(priceRange[1]);
            int      productsCount = int.Parse(Console.ReadLine());

            for (int i = 0; i < productsCount; i++)
            {
                string[] productInfo = Console.ReadLine().Split(" | ");
                decimal  price       = decimal.Parse(productInfo[3]);
                Product  product     = new Product(productInfo[0], productInfo[1], productInfo[2], price);
                products.Add(price, product);
            }

            OrderedMultiDictionary <decimal, Product> .View searchedProducts = products.Range(startPrice, true, endPrice, true);
            if (searchedProducts.Count == 0)
            {
                Console.WriteLine("There are no products in this range!");
                return;
            }

            foreach (KeyValuePair <decimal, ICollection <Product> > productsWithPrice in searchedProducts)
            {
                foreach (Product product in productsWithPrice.Value)
                {
                    Console.WriteLine(product);
                }
            }
        }
Ejemplo n.º 20
0
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        OrderedMultiDictionary <decimal, Product> allProducts = new OrderedMultiDictionary <decimal, Product>(true);

        for (int i = 0; i < n; i++)
        {
            string[] input = Console.ReadLine().Split(' ');

            string  name       = input[0];
            decimal price      = decimal.Parse(input[1]);
            Product newProduct = new Product()
            {
                Name = name, Price = price
            };

            allProducts.Add(price, newProduct);
        }

        string[] priceRanges = Console.ReadLine().Split(' ');
        decimal  start       = decimal.Parse(priceRanges[0]);
        decimal  end         = decimal.Parse(priceRanges[1]);

        var productsInRange = allProducts.Range(start, true, end, true).Take(20);

        Console.WriteLine();
        foreach (var price in productsInRange)
        {
            foreach (var product in price.Value)
            {
                Console.WriteLine(product);
            }
        }
    }
Ejemplo n.º 21
0
        public static void Main()
        {
            var productByPrice = new OrderedMultiDictionary<double, string>(true);
            int linesCount = int.Parse(Console.ReadLine());

            for (int i = 0; i < linesCount; i++)
            {
                string input = Console.ReadLine();
                string[] inputArgs = input.Split();
                double price = double.Parse(inputArgs[1]);
                string product = inputArgs[0];

                productByPrice.Add(price, product);
            }

            double[] range = Console.ReadLine().Split().Select(double.Parse).ToArray();

            var productsInRange = productByPrice.Range(range[0], true, range[1], true);

            int count = 0;
            foreach (var product in productsInRange)
            {
                if (count == 20)
                {
                    break;
                }

                Console.WriteLine(product.Key + " " + product.Value.First());
                count++;
            }
        }
Ejemplo n.º 22
0
    static void Main()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

        var events      = new OrderedMultiDictionary <DateTime, string>(true);
        int eventsCount = int.Parse(Console.ReadLine());

        for (int i = 0; i < eventsCount; i++)
        {
            string[] eventParams = Console.ReadLine().Split('|');
            string   eventName   = eventParams[0].Trim();
            var      eventTime   = DateTime.Parse(eventParams[1].Trim());
            events.Add(eventTime, eventName);
        }

        int rangesCount = int.Parse(Console.ReadLine());

        for (int i = 0; i < rangesCount; i++)
        {
            string[] timeParams = Console.ReadLine().Split('|');
            var      startTime  = DateTime.Parse(timeParams[0].Trim());
            var      endTime    = DateTime.Parse(timeParams[1].Trim());

            var filteredEvents = events.Range(startTime, true, endTime, true);
            Console.WriteLine(filteredEvents.Values.Count);
            foreach (var timeEventsPair in filteredEvents)
            {
                foreach (string eventName in timeEventsPair.Value)
                {
                    Console.WriteLine("{0} | {1}", eventName, timeEventsPair.Key);
                }
            }
        }
    }
        static void Main()
        {
            const bool areDublicationOfValuesAllawed = true;

            OrderedMultiDictionary<decimal, Articule> articules =
                new OrderedMultiDictionary<decimal, Articule>(areDublicationOfValuesAllawed);

            PopulateArticules(articules);
            
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var result = articules.Range(200m, true, 220m, true);
            sw.Stop();
            

            foreach (var record in result)
            {
                Console.BufferWidth = 160;
                foreach (var value in record.Value) 
                {
                    Console.WriteLine(value);
                }
            }

            Console.WriteLine("Elapsed time to find: {0}", sw.Elapsed);
        }
Ejemplo n.º 24
0
        public static void Main()
        {
            var articles = new OrderedMultiDictionary <decimal, Article>(false);

            DateTime start_at = DateTime.Now;

            for (int i = 0; i < NumberOfProducts; i++)
            {
                var tempArticle = new Article(
                    RandomGenerator.GetRandomBarcode(13),
                    RandomGenerator.GetRandomStringWithRandomLength(5, 10),
                    RandomGenerator.GetRandomStringWithRandomLength(10, 15),
                    RandomGenerator.GetRandomDecimalBetween(5, 15));

                articles.Add(tempArticle.Price, tempArticle);
            }

            DateTime stop_at = DateTime.Now;

            Console.WriteLine("{0} articles generated in {1} secs", NumberOfProducts, new TimeSpan(stop_at.Ticks - start_at.Ticks).TotalSeconds);

            var retrievedArticles = articles.Range(10, true, 10.5M, true);

            Console.WriteLine("Articles in range: {0}", retrievedArticles.Count);
            Console.WriteLine("Top {0} articles: ", NumberOfArticlesToPrint);

            foreach (var article in retrievedArticles.ToList().Take(NumberOfArticlesToPrint))
            {
                Console.WriteLine(article);
            }
        }
Ejemplo n.º 25
0
        public static void Main(string[] args)
        {
            OrderedMultiDictionary <double, Article> articles = new OrderedMultiDictionary <double, Article>(true);
            Random randomNumberGenerator = new Random();
            double randomNumber;

            for (int i = 0; i < 2000000; i++)
            {
                randomNumber = randomNumberGenerator.NextDouble() * MaxValue;
                Article article = new Article("barcode" + i, "vendor" + i, "article" + i, randomNumber);
                articles.Add(article.Price, article);
            }

            Console.Write("from = ");
            double from = double.Parse(Console.ReadLine());

            Console.Write("to = ");
            double to = double.Parse(Console.ReadLine());
            var    articlesInRange = articles.Range(from, true, to, true);

            foreach (var pair in articlesInRange)
            {
                foreach (var article in pair.Value)
                {
                    Console.WriteLine("{0} => {1}", Math.Round(article.Price, 2), article);
                }
            }
        }
Ejemplo n.º 26
0
        static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            var products = new OrderedMultiDictionary<double, string>(true);
            int numberOfLines = int.Parse(Console.ReadLine());
            string line = string.Empty;
            string productName = string.Empty;
            double productPrice = 0;

            for (int i = 0; i < numberOfLines; i++)
            {
                line = Console.ReadLine();

                var parts = line.Split(' ');

                productName = parts[0].Trim();
                productPrice = double.Parse(parts[1].Trim());

                products.Add(productPrice, productName);
            }

            var limits = Console.ReadLine().Split(' ');
            double lowerLimit = double.Parse(limits[0].Trim());
            double upperLimit = double.Parse(limits[1].Trim());
            var productsInRange = products.Range(lowerLimit, true, upperLimit, true);

            foreach (var productInRange in productsInRange)
            {
                Console.WriteLine("{0} {1}", productInRange.Key, productInRange.Value);
            }
        }
Ejemplo n.º 27
0
        static void Main(string[] args)
        {
            var courses = new OrderedMultiDictionary<DateTime, string>(true);
            int coursesTotal = int.Parse(Console.ReadLine());
            for (int i = 0; i < coursesTotal; i++)
            {
                string[] courseData = Console.ReadLine().Split('|');
                string courseName = courseData[0].Trim();
                DateTime courseTime = DateTime.Parse(courseData[1].Trim());
                courses[courseTime].Add(courseName);
            }

            int intervalsTotal = int.Parse(Console.ReadLine());
            for (int i = 0; i < intervalsTotal; i++)
            {
                string[] intervData = Console.ReadLine().Split('|');
                DateTime start = DateTime.Parse(intervData[0].Trim());
                DateTime end = DateTime.Parse(intervData[1].Trim());
                var result = courses.Range(start, true, end, true);
                Console.WriteLine(new string('-', 10));
                Console.WriteLine(result.Values.Count);
                foreach (var coursesBe in result)
                {
                    foreach (var course in coursesBe.Value)
                    {
                        Console.WriteLine("{1} | {0}", coursesBe.Key, course);
                    }
                }

                Console.WriteLine(new string('-', 10));
            }
        }
Ejemplo n.º 28
0
        public static void Main(string[] args)
        {
            Random randomGenerator  = new Random();
            var    articles         = new OrderedMultiDictionary <double, Article>(false);
            int    numberOfArticles = 50;

            for (int i = 0; i < numberOfArticles; i++)
            {
                double randomPrice = randomGenerator.NextDouble() * 100;
                articles.Add(randomPrice, new Article("A", "B", "C", randomPrice));
            }

            Console.WriteLine("Select a range for price of the articles:");
            Console.Write("From: ");
            double from = double.Parse(Console.ReadLine());

            Console.Write("To: ");
            double to = double.Parse(Console.ReadLine());

            // Find the articles in the given range
            var articlesInTheGivenRange = articles.Range(from, true, to, true);

            // Print the articles in the given range
            foreach (var articleCollection in articlesInTheGivenRange)
            {
                foreach (var article in articleCollection.Value)
                {
                    Console.WriteLine(article);
                }
            }
        }
Ejemplo n.º 29
0
        public static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            int productsNumber         = int.Parse(Console.ReadLine());
            var productsOrderedByPrice = new OrderedMultiDictionary <float, string>(true);

            for (int i = 0; i < productsNumber; i++)
            {
                string name  = Console.ReadLine();
                float  price = float.Parse(Console.ReadLine());

                if (!productsOrderedByPrice.ContainsKey(price))
                {
                    productsOrderedByPrice.Add(price, name);
                }
                else
                {
                    productsOrderedByPrice[price].Add(name);
                }
            }

            var startPrice = float.Parse(Console.ReadLine());
            var endPrice   = float.Parse(Console.ReadLine());

            var range = productsOrderedByPrice.Range(startPrice, true, endPrice, true).Take(20);

            foreach (var keyValuePair in range)
            {
                Console.WriteLine("{0} -> {1}", keyValuePair.Key, keyValuePair.Value);
            }
        }
Ejemplo n.º 30
0
        static void Main(string[] args)
        {
            OrderedMultiDictionary<decimal, Article> articles = 
                new OrderedMultiDictionary<decimal, Article>(true);

            articles.Add(12.43m, new Article("Choco", "123124234", "Milka", 12.43m));
            articles.Add(14.43m, new Article("Natural", "123124234", "Milka", 14.43m));
            articles.Add(15.43m, new Article("Raffy", "123124234", "Milka", 15.43m));
            articles.Add(17.43m, new Article("Milk", "123124234", "Milka", 17.43m));
            articles.Add(18.43m, new Article("Sugar", "123124234", "Milka", 18.43m));
            articles.Add(19.43m, new Article("Bread", "123124234", "Milka", 19.43m));
            articles.Add(20.43m, new Article("Susam", "123124234", "Milka", 20.43m));
            articles.Add(23.43m, new Article("Paper", "123124234", "Milka", 23.43m));
            articles.Add(25.43m, new Article("Grape", "123124234", "Milka", 25.43m));
            articles.Add(54.43m, new Article("Banana", "123124234", "Milka", 54.43m));
            articles.Add(43.43m, new Article("Orange", "123124234", "Milka", 43.43m));
            articles.Add(32.43m, new Article("Melon", "123124234", "Milka", 32.43m));
            articles.Add(24.43m, new Article("WaterMelon", "123124234", "Milka", 24.43m));
            articles.Add(76.43m, new Article("Junk", "123124234", "Milka", 76.43m));

            var rangeArticles = articles.Range(20m, true, 35m, true);
            foreach (var article in rangeArticles)
            {
                foreach (var item in article.Value)
                {
                    Console.WriteLine(item);
                }
            }
        }
Ejemplo n.º 31
0
        public static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            var eventCount     = int.Parse(Console.ReadLine());
            var datesAndEvents = new OrderedMultiDictionary <DateTime, string>(true);

            for (int i = 0; i < eventCount; i++)
            {
                var currentEvent = Console.ReadLine();
                var token        = currentEvent.Split('|');
                var date         = DateTime.Parse(token[1].Trim());
                var name         = token[0].Trim();
                datesAndEvents.Add(date, name);
            }

            var queryNumber = int.Parse(Console.ReadLine());

            for (int i = 0; i < queryNumber; i++)
            {
                var query     = Console.ReadLine().Split('|');
                var startDate = DateTime.Parse(query[0].Trim());
                var endDate   = DateTime.Parse(query[1].Trim());
                var queryData = datesAndEvents.Range(startDate, true, endDate, true);
                Console.WriteLine(queryData.KeyValuePairs.Count);
                foreach (var e in queryData)
                {
                    foreach (var name in e.Value)
                    {
                        Console.WriteLine("{0} | {1:dd-MMM-yyyy}", name, e.Key);
                    }
                }
            }
        }
Ejemplo n.º 32
0
        public static void Main(string[] args)
        {
            var productsAndPrices = new OrderedMultiDictionary<int, string>(true);
            var rng = new Random();
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 500000; i++)
            {
                var product = GenerateProduct(rng, i);
                var tokens = product.Split('|');
                var name = tokens[0].Trim();
                var price = int.Parse(tokens[1].Trim());
                productsAndPrices.Add(price, name);
            }

            Console.WriteLine("Seed time: " + stopwatch.Elapsed);
            stopwatch.Restart();
            Console.WriteLine("Query Start");
            var printed = new Set<int>();
            for (int i = 0; i < 10000; i++)
            {
                var startPrice = rng.Next(0, 100001);
                var endPrice = rng.Next(startPrice, 100001);
                var productsInRange = productsAndPrices.Range(startPrice, true, endPrice, true).Take(20);
                var count = productsInRange.Count();
                if (!printed.Contains(count))
                {
                    Console.WriteLine("{0} to {1} -> {2}", startPrice, endPrice, count);
                    printed.Add(count);
                }
            }

            Console.WriteLine("Query time: " + stopwatch.Elapsed);
            Console.WriteLine("All Done");
        }
Ejemplo n.º 33
0
        public static void Main()
        {
            var products = new OrderedMultiDictionary <double, string>(false);

            int numberOfProducts = int.Parse(Console.ReadLine());

            for (int i = 0; i < numberOfProducts; i++)
            {
                string[] productArgs = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                string   product     = productArgs[0];
                double   price       = double.Parse(productArgs[1]);
                if (!products.ContainsKey(price))
                {
                    products.Add(price, product);
                }
                else
                {
                    products[price].Add(product);
                }
            }

            string[] range = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            double   start = double.Parse(range[0]);
            double   end   = double.Parse(range[1]);

            var priceRange = products.Range(start, true, end, true);

            foreach (var price in priceRange.Keys)
            {
                foreach (var product in products[price])
                {
                    Console.WriteLine("{0:F2} {1}", price, product);
                }
            }
        }
Ejemplo n.º 34
0
        public static void Main()
        {
            Console.WriteLine("Adding products");

            var products = new OrderedMultiDictionary<decimal, string>(true);

            for (int i = 0; i < 500000; i++)
            {
                var currentPrice = (i * Math.Abs(Math.Sin(i)));
                products.Add((decimal)currentPrice, "Product " + i);

                if (i % 5000 == 0)
                {
                    Console.Write(".");
                }
            }

            Console.WriteLine();

            decimal minPrice = 100000M;
            decimal maxPrice = 110000M;

            Console.WriteLine("Top 20 product in price range [{0}, {1}]", minPrice, maxPrice);

            var result = products.Range(minPrice, true, maxPrice, true).ToList().Take(20);

            foreach (var product in result)
            {
                Console.WriteLine("Name: {0}, Price: {1}", product.Value, product.Key);
            }
        }
Ejemplo n.º 35
0
        public static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            int productsNumber = int.Parse(Console.ReadLine());
            var productsOrderedByPrice = new OrderedMultiDictionary<float, string>(true);

            for (int i = 0; i < productsNumber; i++)
            {
                string name = Console.ReadLine();
                float price = float.Parse(Console.ReadLine());

                if (!productsOrderedByPrice.ContainsKey(price))
                {
                    productsOrderedByPrice.Add(price, name);
                }
                else
                {
                    productsOrderedByPrice[price].Add(name);
                }
            }

            var startPrice = float.Parse(Console.ReadLine());
            var endPrice = float.Parse(Console.ReadLine());

            var range = productsOrderedByPrice.Range(startPrice, true, endPrice, true).Take(20);

            foreach (var keyValuePair in range)
            {
                Console.WriteLine("{0} -> {1}", keyValuePair.Key, keyValuePair.Value);
            }
        }
    static void Main()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

        var events = new OrderedMultiDictionary<DateTime, string>(true);
        int eventsCount = int.Parse(Console.ReadLine());
        for (int i = 0; i < eventsCount; i++)
        {
            string[] eventParams = Console.ReadLine().Split('|');
            string eventName = eventParams[0].Trim();
            var eventTime = DateTime.Parse(eventParams[1].Trim());
            events.Add(eventTime, eventName);
        }

        int rangesCount = int.Parse(Console.ReadLine());
        for (int i = 0; i < rangesCount; i++)
        {
            string[] timeParams = Console.ReadLine().Split('|');
            var startTime = DateTime.Parse(timeParams[0].Trim());
            var endTime = DateTime.Parse(timeParams[1].Trim());

            var filteredEvents = events.Range(startTime, true, endTime, true);
            Console.WriteLine(filteredEvents.Values.Count);
            foreach (var timeEventsPair in filteredEvents)
            {
                foreach (string eventName in timeEventsPair.Value)
                {
                    Console.WriteLine("{0} | {1}", eventName, timeEventsPair.Key);
                }
            }
        }
    }
Ejemplo n.º 37
0
        private static void Main()
        {
            var articles = new OrderedMultiDictionary <decimal, Article>(false);
            var rand     = new RandomGenerator();

            Console.WriteLine($"Populating {ArticlesCount} articles");
            for (int i = 0; i < ArticlesCount; i++)
            {
                string  title   = rand.GetRandomString(5, 10);
                string  vendor  = rand.GetRandomString(5, 10);
                decimal price   = rand.GetRandomInteger(10, 2500);
                string  barcode = rand.GetRandomString(10, 15);

                var article = new Article(title, barcode, vendor, price);
                articles.Add(price, article);
            }

            Console.WriteLine($"Searching for articles in price range {MinPrice} - {MaxPrice}");
            var extractedArticles = articles.Range(MinPrice, true, MaxPrice, true);

            Console.WriteLine("Finished search. Man that was fast!\nPress a key to start printing to console...");
            Console.ReadLine();

            foreach (var priceArticles in extractedArticles)
            {
                foreach (var article in priceArticles.Value)
                {
                    Console.WriteLine($"Title: {article.Title}, price: {article.Price}");
                }
            }
            ;

            Console.WriteLine($"\n\nThats all articles in price range {MinPrice} - {MaxPrice} for {ArticlesCount} articles.\n\n");
        }
    static void Main()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

        var events = new OrderedMultiDictionary<DateTime, string>(true);
        int eventsCount = int.Parse(Console.ReadLine());
        for (int i = 0; i < eventsCount; i++)
        {
            string eventEntry = Console.ReadLine();
            var eventTokens = eventEntry.Split('|');
            string eventName = eventTokens[0].Trim();
            DateTime eventDate = DateTime.Parse(eventTokens[1].Trim());
            events.Add(eventDate, eventName);
        }

        int seratchCount = int.Parse(Console.ReadLine());
        for (int i = 0; i < seratchCount; i++)
        {
            string dateEntry = Console.ReadLine();
            var dateTokens = dateEntry.Split('|');
            DateTime startDate = DateTime.Parse(dateTokens[0].Trim());
            DateTime endDate = DateTime.Parse(dateTokens[1].Trim());
            var eventsInRange = events.Range(startDate, true, endDate, true);

            Console.WriteLine("\n\rResult:" + eventsInRange);
            PrintEvents(eventsInRange);
            Console.WriteLine();
        }
    }
Ejemplo n.º 39
0
        /// <summary>
        /// OrderedMultiDictionary<TKey,TValue>              (WITH 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 TestOrderedMultiDictionary()
        {
            OrderedMultiDictionary <int, Student> students = new OrderedMultiDictionary <int, Student>(true);
            var student1 = new Student("First DUPLICATE", 21);
            var student2 = new Student("Second", 21);

            students.Add(5, student1);
            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 >= 4 && <= 5 ============= ");
            var inRangeStudents = students.Range(4, true, 5, true);

            foreach (var item in inRangeStudents)
            {
                Console.WriteLine(item);
            }
        }
Ejemplo n.º 40
0
        public static void Main()
        {
            OrderedMultiDictionary <decimal, Article> articlesByPrice = new OrderedMultiDictionary <decimal, Article>(true);
            Random random = new Random();

            for (int i = 0; i < 1000000; i++)
            {
                var     barcode      = random.Next(111111, 999999);
                var     lengthTitle  = random.Next(2, 20);
                string  title        = GetRandomString(lengthTitle, random);
                var     lengthVendor = random.Next(2, 20);
                string  vendor       = GetRandomString(lengthVendor, random);
                var     price        = random.Next(1, 10000);
                Article article      = new Article(barcode, vendor, title, price);
                articlesByPrice.Add(article.Price, article);
            }

            for (int i = 0; i < 20; i++)
            {
                var lowerBound      = random.Next(1, 9950);
                var upperBound      = lowerBound + 50;
                var articlesInRange = articlesByPrice.Range(lowerBound, true, upperBound, true);
                Console.WriteLine("Articles in price range {0}- {1}: {2} articles", lowerBound, upperBound, articlesInRange.Values.Count);
            }
        }
Ejemplo n.º 41
0
        public static void Main()
        {
            OrderedMultiDictionary<double, Article> articles = new OrderedMultiDictionary<double, Article>(true);

            int range = 10000;
            for (int i = 0; i < range; i++)
            {
                var article = new Article("barcode" + i, "vendor" + i, "title" + i, i);
                articles.Add(article.Price, article);
            }

            int from = 5000;
            int to = 5040;

            //Make some duplications
            for (int i = from; i < to - 30; i++)
            {
                var article = new Article("newBarcode" + i, "newVendor" + i, "newTitle" + i, i);
                articles.Add(article.Price, article);
            }

            var articlesInGivenRange = articles.Range(from, true, to, true);
            foreach (var article in articlesInGivenRange)
            {
                foreach (var item in article.Value)
                {
                    Console.WriteLine("Title: {0}, Vendor: {1}, Barcode: {2}, Price: {3}",
                    item.Title, item.Vendor, item.Barcode, item.Price);
                }
                Console.WriteLine();
            }
        }
Ejemplo n.º 42
0
        public static void Main()
        {
            Console.WriteLine("Adding products");

            var products = new OrderedMultiDictionary <decimal, string>(true);

            for (int i = 0; i < 500000; i++)
            {
                var currentPrice = (i * Math.Abs(Math.Sin(i)));
                products.Add((decimal)currentPrice, "Product " + i);

                if (i % 5000 == 0)
                {
                    Console.Write(".");
                }
            }

            Console.WriteLine();

            decimal minPrice = 100000M;
            decimal maxPrice = 110000M;

            Console.WriteLine("Top 20 product in price range [{0}, {1}]", minPrice, maxPrice);

            var result = products.Range(minPrice, true, maxPrice, true).ToList().Take(20);

            foreach (var product in result)
            {
                Console.WriteLine("Name: {0}, Price: {1}", product.Value, product.Key);
            }
        }
 public static void PrintArticlesInRange(OrderedMultiDictionary<decimal, Article> orderedByPrice, decimal minRange, decimal maxRange)
 {
     var articles = orderedByPrice.Range(minRange, true, maxRange, true);
     foreach (var pair in articles)
     {
         Console.WriteLine(pair.Value);
     }
 }
Ejemplo n.º 44
0
        private static void FindProductsByPriceRange(string[] commandArgs)
        {
            double from          = double.Parse(commandArgs[0]);
            double to            = double.Parse(commandArgs[1]);
            var    foundProducts = productsByPrice.Range(from, true, to, true).Values;

            AddProductsToOutput(foundProducts);
        }
Ejemplo n.º 45
0
    private string FindProductsByPriceRange(string from, string to)
    {
        decimal rangeStart    = decimal.Parse(from);
        decimal rangeEnd      = decimal.Parse(to);
        var     productsFound = productsByPrice.Range(rangeStart, true, rangeEnd, true).Values;

        return(SortAndPrintProducts(productsFound));
    }
Ejemplo n.º 46
0
        static void Main()
        {
            OrderedMultiDictionary <double, string> productsByPrice = new OrderedMultiDictionary <double, string>(true);

            string[] products = new[]
            {
                "milk", "banana", "beer", "lukanka", "banski starec", "tarator", "mastika", "djonga", "koker shpaniol"
            };

            double[] prices = new[]
            {
                0.1, 3.4, 13.37, 0.20, 1.2, 2.50, 15, 50, 100
            };
            int maxIndexProducts = products.Length;
            int maxIndexPrices   = prices.Length;

            Random rng = new Random();

            int product = rng.Next(0, maxIndexProducts);
            int price   = rng.Next(0, maxIndexPrices);

            for (int i = 0; i < 50; i++)
            {
                productsByPrice[prices[price]].Add(products[product]);
                rng     = new Random(i * i);
                product = rng.Next(0, maxIndexProducts);
                price   = rng.Next(0, maxIndexPrices);
            }
            for (int i = 0; i < 10; i++)
            {
                rng = new Random(i * i);
                int secondPrice = rng.Next(0, maxIndexPrices);
                price = rng.Next(0, maxIndexPrices);
                var range = productsByPrice.Range(prices[price], true, prices[secondPrice], true);


                int index = 0;
                foreach (var item in range)
                {
                    Console.WriteLine("Price: {0} БЪЛГАРСКИ ЛЕВА", item.Key);
                    foreach (var innerItem in range[item.Key])
                    {
                        if (index >= 20)
                        {
                            break;
                        }
                        Console.WriteLine("--" + innerItem);
                        index++;
                    }
                    if (index >= 20)
                    {
                        break;
                    }
                }
                Console.WriteLine();
            }
            //ako ti vurje da nabarash djonga za 15, imam telefoni da znaesh
        }
Ejemplo n.º 47
0
        static void Main()
        {
            OrderedMultiDictionary<double, string> products = new OrderedMultiDictionary<double, string>(false);

            // Test 1: Input from console

            int numberOfProducts = Int32.Parse(Console.ReadLine());

            for (int i = 0; i < numberOfProducts; i++)
            {
                string inputLine = Console.ReadLine();
                string[] tokens = inputLine.Split(' ');
                string productName = tokens[0];
                double price = Double.Parse(tokens[1]);

                products.Add(price, productName);
            }

            string priceRange = Console.ReadLine();
            string[] priceRangeTokens = priceRange.Split(' ');
            double minPrice = Double.Parse(priceRangeTokens[0]);
            double maxPrice = Double.Parse(priceRangeTokens[1]);

            var productsWithinRange = products.Range(minPrice, true, maxPrice, true);

            foreach (var product in productsWithinRange)
            {
                foreach (var productName in product.Value)
                {
                    Console.WriteLine("{0} {1}", product.Key, productName);
                }
            }

            // Test 2: Performance
            products = new OrderedMultiDictionary<double, string>(false);
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            for (int i = 0; i < 500000; i++)
            {
                products.Add(i, "product" + i);
            }

            Random rnd = new Random();

            for (int i = 0; i < 10000; i++)
            {
                double min = rnd.NextDouble() * 500000;
                double max = rnd.NextDouble() * 500000;

                var range = products.Range(min, true, max, true);
            }

            stopwatch.Stop();
            Console.WriteLine();
            Console.WriteLine(stopwatch.Elapsed);
        }
Ejemplo n.º 48
0
        private static void FindArticlesInPriceRange(double lowBound, double highBound)
        {
            var result = articles.Range(lowBound, true, highBound, true);

            // foreach (var item in result)
            // {
            //     Console.WriteLine(item);
            // }
        }
Ejemplo n.º 49
0
        public static void Main(string[] args)
        {
            ReadInputFile();
            var articlesInPriceRange = storage.Range(MinPrice, true, MaxPrice, true);

            foreach (var entry in articlesInPriceRange)
            {
                Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
            }
        }
        public IEnumerable <Product> GetProductsByPriceRange(float fromPrice, float toPrice)
        {
            var extract = productsOrderedByPrice.Range(fromPrice, true, toPrice, true).Values;

            var extractSorted =
                from product in extract
                orderby product ascending
                select product;

            return(extractSorted);
        }
Ejemplo n.º 51
0
    static string FindProductsByPriceRange(decimal min, decimal max)
    {
        var result = byPrice.Range(min, true, max, true).Values.OrderBy(p => p.Price);

        if (!result.Any())
        {
            return("No products found");
        }

        return(string.Join(Environment.NewLine, result));
    }
Ejemplo n.º 52
0
    /* 2 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.
     * */
    static void Main(string[] args)
    {
        // input generator in bin\debug\generator.cs

        var products = new OrderedMultiDictionary<decimal, string>(true);

        foreach (var line in File.ReadLines("products.txt"))
        {
            var split = line.Split('|');
            products.Add(decimal.Parse(split[1]), split[0]);
        }

        int totalCount = 0;

        // show one line of results and exit OR benchmark all 10,000 commands
        // without printing anything
        const bool JUST_ONE = true;

        var sw = new Stopwatch();
        sw.Start();

        int queries = 0;
        foreach (var line in File.ReadLines("commands.txt"))
        {
            queries += 1;
            var split = line.Split('|');

            var first = decimal.Parse(split[0]);
            var second = decimal.Parse(split[1]);

            var first20 = products.Range(Math.Min(first, second), true,
                                         Math.Max(first, second), true)
                                  .Take(20);

            totalCount += first20.Count();

            if (JUST_ONE)
            {
                Console.WriteLine(line);
                Console.WriteLine(String.Join(",", first20.Select(kvp => "{" + kvp.Key + ":" + string.Join(",", kvp.Value) + "}")));
                Console.WriteLine();
                break;
            }
        }

        if (JUST_ONE)
            return;

        sw.Stop();

        Debug.Assert(totalCount <= queries * 20 && queries * 20 - totalCount < 100);
        Console.WriteLine("Total time: " + sw.Elapsed.TotalMilliseconds);
    }
Ejemplo n.º 53
0
        private static void FindArticlesByPriceRange(OrderedMultiDictionary<decimal, Article> dictionary)
        {
            decimal fromPrice = 250;
            decimal toPrice = 275;
            var articles = dictionary.Range(fromPrice, true, toPrice, true);

            Console.WriteLine("Price range: [{0}, {1}]", fromPrice, toPrice);
            foreach (var article in articles)
            {
                Console.WriteLine(article);
            }
        }
        private static void GetArticlesInRange(OrderedMultiDictionary <double, Article> articles, double priceFrom, double priceTo)
        {
            var articlesRange = articles.Range(priceFrom, true, priceTo, true);

            foreach (var article in articlesRange)
            {
                foreach (var item in article.Value)
                {
                    Console.WriteLine(item);
                }
            }
        }
Ejemplo n.º 55
0
        private static void FindArticlesByPriceRange(OrderedMultiDictionary <decimal, Article> dictionary)
        {
            decimal fromPrice = 250;
            decimal toPrice   = 275;
            var     articles  = dictionary.Range(fromPrice, true, toPrice, true);

            Console.WriteLine("Price range: [{0}, {1}]", fromPrice, toPrice);
            foreach (var article in articles)
            {
                Console.WriteLine(article);
            }
        }
Ejemplo n.º 56
0
 public static void GetArticlesInPriceRange(OrderedMultiDictionary<double, Article> articles, double min, double max)
 {
     articles
         .Range(min, true, max, true)
         .ForEach(x =>
         {
             foreach (var item in x.Value)
             {
                 Console.WriteLine(item);
             }
         });
 }
Ejemplo n.º 57
0
        public static void Main()
        {
            OrderedMultiDictionary<double, Tuple<string, string, string>> articles =
                new OrderedMultiDictionary<double, Tuple<string, string, string>>(false);

            FillDictionary(articles);
            var articlesInRange = articles.Range(10, true, 100, true);

            foreach (var article in articlesInRange)
            {
                Console.WriteLine(article);
            }
        }
 // Prints all articles found in articlesByPrice in the range [fromPrice; toPrice]
 private static void PrintArticlesInRange(
     OrderedMultiDictionary<decimal, Article> articlesByPrice,
     decimal fromPrice,
     decimal toPrice)
 {
     Console.WriteLine("Articles in range [{0}; {1}]", fromPrice, toPrice);
     Console.WriteLine("==============================");
     var articlesInRange = articlesByPrice.Range(fromPrice, true, toPrice, true);
     foreach (var article in articlesInRange)
     {
         Console.WriteLine(article.Value);
     }
 }
Ejemplo n.º 59
0
 public void TestAdd500KSearch10K()
 {
     var products = new OrderedMultiDictionary<float, string>(true);
     for( var i = 0; i<500000; i++)
     {
         products.Add(i/5000f, "Product "+i);
     }
     for (var i = 0; i < 10000; i++)
     {
         var productResults = products.Range(0, true, 1, true);
         Assert.AreEqual(productResults.Count, 5001);
     }
 }
Ejemplo n.º 60
0
        public static void Main()
        {
            OrderedMultiDictionary <decimal, long> tradeCompanyData = new OrderedMultiDictionary<decimal, long>(true);

            for (long i = 0; i < 2000000; i++)
            {
                tradeCompanyData.Add((decimal)i + (0.30m * i), i);
            }

            System.Console.WriteLine("Articles Loaded!");

            var firstRange = tradeCompanyData.Range(1.0m, true, 158.0m, true);

            var secondRange = tradeCompanyData.Range(30m, true, 20000m, true);

            var thirdRange = tradeCompanyData.Range(3000m, true, 800000m, true);

            var forthRange = tradeCompanyData.Range(567m, true, 100000.2m, true);

            var fifthRange = tradeCompanyData.RangeFrom(2m, true);

            System.Console.WriteLine("Ranges loaded");
        }