示例#1
0
        public static void Main(string[] args)
        {
            OrderedBag <Product> bag       = new OrderedBag <Product>();
            Stopwatch            stopwatch = new Stopwatch();

            Console.WriteLine("---Performance test (Adding)---");
            stopwatch.Start();
            int productsCount = 500000;

            for (int i = 0; i < productsCount; i++)
            {
                bag.Add(new Product("asd", i));
            }

            stopwatch.Stop();
            Console.WriteLine("{0} products added for {1}", productsCount, stopwatch.Elapsed);

            Console.WriteLine("Choose subrange of product's price:");
            Console.Write("From: ");
            double from = double.Parse(Console.ReadLine());

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

            var subrangeProducts = bag.Range(
                new Product("asd", from), true,
                new Product("asd", to), true);

            if (subrangeProducts.Count >= 20)
            {
                Console.WriteLine("The first 20 products in this range are:");
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine("Name: {0}, Price: {1}", subrangeProducts[i].Name, subrangeProducts[i].Price);
                }
            }
            else
            {
                Console.WriteLine("The products with price in this range are:");
                for (int i = 0; i < subrangeProducts.Count; i++)
                {
                    Console.WriteLine("Name: {0}, Price: {1}", subrangeProducts[i].Name, subrangeProducts[i].Price);
                }
            }

            Console.WriteLine("---Performance test (Searching)---");
            int     numberOfSearches = 10000;
            Product searchedProduct  = new Product("asd", 100);

            stopwatch.Reset();
            stopwatch.Start();
            for (int i = 0; i < numberOfSearches; i++)
            {
                bag.Contains(searchedProduct);
            }

            stopwatch.Stop();
            Console.WriteLine("{0} searches for {1}", numberOfSearches, stopwatch.Elapsed);
        }
        public IEnumerable <Point> FindPath(Point start, Point goal)
        {
            var startNode  = Map[start.Row][start.Column];
            var targetNode = Map[goal.Row][goal.Column];

            var openNodes   = new OrderedBag <Node>(PathComparison);
            var closedNodes = new HashSet <Node>();

            openNodes.Add(startNode);

            while (openNodes.Count > 0)
            {
                var currentNode = openNodes.RemoveFirst();
                closedNodes.Add(currentNode);

                if (currentNode == targetNode)
                {
                    return(tracePath(targetNode));
                }

                var neighbours = getNeighbours(currentNode);
                foreach (var neighbour in neighbours)
                {
                    if (!neighbour.IsWalkable || closedNodes.Contains(neighbour))
                    {
                        continue;
                    }

                    var newCost = currentNode.GainedCost + getCost(currentNode, neighbour);
                    if (newCost < neighbour.GainedCost || !openNodes.Contains(neighbour))
                    {
                        neighbour.GainedCost    = newCost;
                        neighbour.HeuristicCost = getDistance(neighbour, targetNode);
                        neighbour.Parent        = currentNode;

                        if (!openNodes.Contains(neighbour))
                        {
                            openNodes.Add(neighbour);
                        }
                    }
                }
            }

            //a path doesn't exist
            return(new List <Point>());
        }
        public IEnumerable<Point> FindPath(Point start, Point goal)
        {
            var startNode = Map[start.Row][start.Column];
            var targetNode = Map[goal.Row][goal.Column];

            var openNodes = new OrderedBag<Node>(PathComparison);
            var closedNodes = new HashSet<Node>();
            openNodes.Add(startNode);

            while (openNodes.Count > 0)
            {
                var currentNode = openNodes.RemoveFirst();
                closedNodes.Add(currentNode);

                if (currentNode == targetNode)
                {
                    return tracePath(targetNode);
                }

                var neighbours = getNeighbours(currentNode);
                foreach (var neighbour in neighbours)
                {
                    if (!neighbour.IsWalkable || closedNodes.Contains(neighbour))
                    {
                        continue;
                    }

                    var newCost = currentNode.GainedCost + getCost(currentNode, neighbour);
                    if (newCost < neighbour.GainedCost || !openNodes.Contains(neighbour))
                    {
                        neighbour.GainedCost = newCost;
                        neighbour.HeuristicCost = getDistance(neighbour, targetNode);
                        neighbour.Parent = currentNode;

                        if (!openNodes.Contains(neighbour))
                        {
                            openNodes.Add(neighbour);
                        }
                    }
                }
            }

            //a path doesn't exist
            return new List<Point>();
        }
        static void OrderedBagExample()
        {
            Console.WriteLine("Wintellect.PowerCollections.OrderedBag<T> example:");
            Console.WriteLine("Elements will enter in the bag with duplicates, in ascending order.");
            OrderedBag <string> bag = new OrderedBag <string>();

            bag.Add("programming");
            bag.Add("C#");
            bag.Add("Visual Studio");
            bag.Add("dotnet");
            bag.Add("C#"); // Duplicates are allowed
            bag.Add("C#"); // Duplicates are allowed
            Console.WriteLine("Contains `dotnet`: {0}", bag.Contains("dotnet"));
            Console.WriteLine("Deleted the first `C#` occurence: " + bag.Remove("C#"));
            Console.WriteLine("Elements: {0}", bag);
            Console.WriteLine("Range[`d`...`q`]: {0}", bag.Range("d", true, "q", true));
        }
示例#5
0
 public bool Contains(IEnemy enemy)
 {
     return(enemies.Contains(enemy));
 }
示例#6
0
        /// <summary>
        /// AStar - Performs the A* algorithm search to find a
        ///         solution of a given BoardNode.
        /// </summary>
        /// <param name="root">BoardNode to find a solution for</param>
        /// <returns>BoardNode containing the solution. NULL if no solution found</returns>
        public static BoardNode AStar(BoardNode root)
        {
            BoardNode current = root;
            Comparison<BoardNode> compare = new Comparison<BoardNode>(compareFScore);
            OrderedBag<BoardNode> openList = new OrderedBag<BoardNode>(compare);
            OrderedBag<BoardNode> closedList = new OrderedBag<BoardNode>(compare);
            int curGScore;
            int heuristic;

            curGScore = 0;
            heuristic = current.board.numRemainingLights();
            current.gScore = curGScore;
            current.fScore = (curGScore + heuristic);

            openList.Add(current);

            while(openList.Count != 0)
            {
                current = openList.GetFirst();

                if(current.clicked.Capacity >= 100000)
                {
                    return null;
                }

                if(current.board.solved())
                {
                    return current;
                }

                openList.Remove(current);
                closedList.Add(current);

                addChildren(current);

                foreach (BoardNode child in current.children)
                {
                    if (closedList.Contains(child))
                    {
                        ;
                    }
                    else
                    {
                        curGScore = current.gScore + 1;

                        if (openList.Contains(child) == false || curGScore < child.gScore)
                        {
                            child.gScore = (curGScore);
                            heuristic = child.board.numRemainingLights();
                            child.fScore = (curGScore + heuristic);

                            if (openList.Contains(child) == false)
                            {
                                openList.Add(child);
                            }
                        }
                    }
                }
            }

            return null;
        }
示例#7
0
 public bool Contains(Lap item)
 {
     return(laps.Contains(item));
 }
示例#8
0
        static void Main()
        {
            OrderedBag<int> newBag = new OrderedBag<int>();
            OrderedBag<int> newBag2 = new OrderedBag<int>();
            OrderedBag<int> newBag3 = new OrderedBag<int>();

            OrderedBag<int> newOrderedBag = new OrderedBag<int>();
            OrderedBag<int> newOrderedBag2 = new OrderedBag<int>();
            OrderedBag<int> newOrderedBag3 = new OrderedBag<int>();

            for (int i = 4; i >= 0; i--)
            {
                newBag2.Add(numbers[i]);
                newOrderedBag2.Add(numbers[i]);
            }

            for (int i = 5; i < 10; i++)
            {
                newBag.Add(numbers[i]);
                newBag2.Add(numbers[i]);
                newOrderedBag.Add(numbers[i]);
                newOrderedBag2.Add(numbers[i]);
            }

            newBag3 = newBag2.Union(newBag);

            Console.WriteLine("Union of 6-10 & 1-10 :");
            Console.WriteLine("Unordered:");
            Console.WriteLine();

            foreach (int a in newBag3.list)
                Console.Write(a + "  ");

            Console.WriteLine();
            Console.WriteLine("Ordered:");
            Console.WriteLine();

            foreach (int a in newOrderedBag3.list)
                Console.Write(a + "  ");

            Console.WriteLine();
            Console.WriteLine("Adding 9-14 to ordered and unordered list.");

            newBag2.Add(12);
            newBag2.Add(10);
            newBag2.Add(11);
            newBag2.Add(14);
            newBag2.Add(13);
            newBag2.Add(9);
            newOrderedBag2.Add(12);
            newOrderedBag2.Add(10);
            newOrderedBag2.Add(11);
            newOrderedBag2.Add(14);
            newOrderedBag2.Add(13);
            newOrderedBag2.Add(9);

            Console.WriteLine();
            Console.WriteLine("Unordered Contains 23? : " + newBag3.Contains(23));
            Console.WriteLine("Ordered Contains 23? : " + newOrderedBag3.Contains(23));

            Console.WriteLine();

            newBag3 = newBag.Intersection(newBag2);
            newOrderedBag3 = newOrderedBag.Intersection(newOrderedBag2);

            Console.WriteLine("Intersection of 6-10 & 1-10 :");
            Console.WriteLine();
            Console.WriteLine("Unordsered:");
            Console.WriteLine();

            foreach (int a in newBag3.list)
                Console.Write(a + "  ");

            Console.WriteLine();
            Console.WriteLine("Ordered:");
            Console.WriteLine();

            foreach (int a in newOrderedBag3.list)
                Console.Write(a + "  ");

            Console.WriteLine();
            Console.WriteLine("Unordered Contains 6? : " + newBag3.Contains(6));
            Console.WriteLine();
            Console.WriteLine("Ordered Contains 6? : " + newOrderedBag3.Contains(6));
            Console.WriteLine();
            newBag3.Remove(6);
            newOrderedBag3.Remove(6);
            Console.WriteLine();
            Console.WriteLine("After removing 6, does unordered bag contain 6? : " + newBag3.Contains(6));
            Console.WriteLine();
            Console.WriteLine("After removing 6, does ordered bag contain 6? : " + newBag3.Contains(6));
        }