コード例 #1
0
        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);
        }
コード例 #2
0
 public void DestroyHighestPriorityTargets(int count)
 {
     foreach (var node in invadersByPriority.Take(count).ToList())
     {
         invadersByInsertion.Remove(node);
         this.invadersByPriority.Remove(node);
     }
 }
コード例 #3
0
 public void DestroyHighestPriorityTargets(int count)
 {
     foreach (var node in byPriority.Take(count).ToArray())
     {
         byInsertion.Remove(node);
         byPriority.Remove(node);
     }
 }
コード例 #4
0
 public IEnumerable <Product> FindFirstMostExpensiveProducts(int count)
 {
     if (count > productsByIndex.Count)
     {
         throw new ArgumentException();
     }
     return(productsByPrice.Take(count));
 }
コード例 #5
0
    public IEnumerable <T> Min(int count)
    {
        if (count > byInsertion.Count)
        {
            throw new ArgumentOutOfRangeException();
        }

        return(byAscending.Take(count).Select(x => x.Value));
    }
コード例 #6
0
    public IEnumerable <T> Min(int count)
    {
        ValidateCount(count);

        foreach (var node in byOrder.Take(count))
        {
            yield return(node.Value);
        }
    }
コード例 #7
0
    public IEnumerable <T> Min(int count)
    {
        if (this.Count < count)
        {
            throw new ArgumentOutOfRangeException();
        }

        return(byValue.Take(count).Select(node => node.Value));
    }
コード例 #8
0
    public IEnumerable <T> Min(int count)
    {
        if (this.ThereIsNotEnoughElements(count))
        {
            ThrowNotEnoughElementsException();
        }

        return(acscendingOrder.Take(count).Select(x => x.Value));
    }
コード例 #9
0
    public IEnumerable <T> Max(int count)
    {
        if (this.ThereIsNotEnoughElements(count))
        {
            ThrowNotEnoughElementsException();
        }

        return(descendingOrder.Take(count));
    }
コード例 #10
0
    public IEnumerable <T> Min(int count)
    {
        if (!CountIsInBounds(count))
        {
            throw new ArgumentOutOfRangeException();
        }
        var minElements = byAscending.Take(count);

        return(minElements.Select(x => x.Value));
    }
コード例 #11
0
ファイル: Program.cs プロジェクト: bstaykov/Telerik-DSA
 private static void PrintUnitsByAttack(int numberOfUnits)
 {
     if (!unitsByAttack.Any())
     {
         output.AppendLine(string.Format("RESULT: "));
         return;
     }
     else
     {
         var units        = unitsByAttack.Take(numberOfUnits);
         var orderedUnits = units.OrderByDescending(x => x.Attack).ThenBy(x => x.Name);
         output.AppendLine(string.Format("RESULT: {0}", string.Join(", ", orderedUnits)));
     }
 }
コード例 #12
0
    public void DestroyHighestPriorityTargets(int count)
    {
        if (count < orderedList.Count)
        {
            OrderedBag <Invader> arr = new OrderedBag <Invader>();

            var toRemove = orderedList.Take(count);

            foreach (var invader in toRemove)
            {
                byAppearance.Remove(invader);
            }

            for (int i = 0; i <= count; i++)
            {
                orderedList.RemoveFirst();
            }
        }
        else
        {
            orderedList  = new OrderedBag <Invader>();
            byAppearance = new List <Invader>();
        }


        /*if (count < orderedList.Count)
         * {
         *  for (int i = count; i < orderedList.Count; i++)
         *  {
         *      arr.Add(orderedList[i]);
         *  }
         *
         *
         *  orderedList = arr;
         * }*/
    }
コード例 #13
0
        public static void Main(string[] args)
        {
            Dictionary <string, Unit> units = new Dictionary <string, Unit>();

            OrderedBag <Unit> bagOfUnits = new OrderedBag <Unit>();

            Dictionary <string, OrderedBag <Unit> > unitsByUnittype = new Dictionary <string, OrderedBag <Unit> >();

            string command;

            while ((command = Console.ReadLine()) != "end")
            {
                string[] subcommand = command.Split();

                switch (subcommand[0])
                {
                case "add":
                {
                    string name   = subcommand[1];
                    string type   = subcommand[2];
                    int    attack = int.Parse(subcommand[3]);

                    Unit unit = new Unit();
                    unit.Name   = name;
                    unit.Type   = type;
                    unit.Attack = attack;

                    if (units.ContainsKey(name))
                    {
                        Console.WriteLine("FAIL: {0} already exists!", name);
                    }
                    else
                    {
                        units.Add(name, unit);
                        bagOfUnits.Add(unit);

                        if (unitsByUnittype.ContainsKey(type))
                        {
                            unitsByUnittype[type].Add(unit);
                        }
                        else
                        {
                            unitsByUnittype.Add(type, new OrderedBag <Unit>()
                                {
                                    unit
                                });
                        }

                        Console.WriteLine("SUCCESS: {0} added!", name);
                    }
                    break;
                }

                case "remove":
                {
                    string name = subcommand[1];
                    if (units.ContainsKey(name))
                    {
                        var unitToDelete = units[name];
                        units.Remove(name);
                        bagOfUnits.Remove(unitToDelete);
                        unitsByUnittype[unitToDelete.Type].Remove(unitsByUnittype[unitToDelete.Type].First(x => x.Name == name));

                        Console.WriteLine("SUCCESS: {0} removed!", name);
                    }
                    else
                    {
                        Console.WriteLine("FAIL: {0} could not be found!", name);
                    }
                    break;
                }

                case "find":
                {
                    string        type    = subcommand[1];
                    StringBuilder builder = new StringBuilder();
                    builder.Append("RESULT: ");

                    if (unitsByUnittype.ContainsKey(type))
                    {
                        builder.Append(string.Join("", unitsByUnittype[type].Take(10)));
                    }

                    if (builder[builder.Length - 2] == ',')
                    {
                        builder.Remove(builder.Length - 2, 2);
                    }
                    Console.WriteLine(builder.ToString());
                    break;
                }

                case "power":
                {
                    int           numberOfUnits = int.Parse(subcommand[1]);
                    StringBuilder builder       = new StringBuilder();
                    builder.Append("RESULT: ");

                    builder.Append(string.Join("", bagOfUnits.Take(numberOfUnits)));

                    if (builder[builder.Length - 2] == ',')
                    {
                        builder.Remove(builder.Length - 2, 2);
                    }
                    Console.WriteLine(builder.ToString());
                    break;
                }
                }
            }
        }
コード例 #14
0
 public IEnumerable <T> Min(int count)
 {
     IsCountEnought(count);
     return(bag.Take(count).Select(x => x.Value));
 }