public void TestContains()
        {
            int[]         heapArr = { };
            MaxHeap <int> pq      = new MaxHeap <int>(heapArr);

            for (int i = 1; i <= 100; ++i)
            {
                Assert.IsFalse(pq.Contains(i));
            }

            for (int i = 1; i <= 100; ++i)
            {
                pq.Insert(i);
            }

            for (int i = 1; i <= 100; ++i)
            {
                Assert.IsTrue(pq.Contains(i));
            }

            for (int i = 101; i <= 200; ++i)
            {
                Assert.IsFalse(pq.Contains(i));
            }

            Assert.IsTrue(pq.Contains(1));
            Assert.AreEqual(100, pq.Peek());

            pq.Pop();

            Assert.IsFalse(pq.Contains(100));
            Assert.AreEqual(99, pq.Peek());
        }
Beispiel #2
0
        public bool FindPath(DijkstraNode[] pathfindingNetwork, DijkstraNode targetNode, DijkstraNode startNode, IPathRequest pathRequest)
        {
            if (targetNode.Clearance < pathRequest.AgentSize)
            {
                return(false);
            }
            ResetNetwork(pathfindingNetwork);
            var openSet   = new MaxHeap <DijkstraNode>(pathfindingNetwork.Length);
            var closedSet = new HashSet <DijkstraNode>();

            openSet.Add(targetNode);
            targetNode.GCost = 0f;
            while (openSet.Count > 0)
            {
                var currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);

                foreach (var connection in currentNode.DefinitionNode.Connections)
                {
                    var toNode = NodePointer.Dereference(connection.To, pathfindingNetwork);
                    if ((connection.CollisionCategory & pathRequest.CollisionCategory) != 0 || closedSet.Contains(toNode))
                    {
                        continue;
                    }

                    if (toNode.Clearance < pathRequest.AgentSize)
                    {
                        toNode.GCost = float.NaN;
                    }
                    else
                    {
                        var newMovementCostToNeighbour = currentNode.GCost + GetDistance(currentNode.DefinitionNode, toNode.DefinitionNode) * currentNode.DefinitionNode.MovementCostModifier;
                        if (newMovementCostToNeighbour < toNode.GCost || !openSet.Contains(toNode))
                        {
                            toNode.GCost = newMovementCostToNeighbour;
                            if (!openSet.Contains(toNode))
                            {
                                openSet.Add(toNode);
                            }
                        }
                    }
                }
            }
            return(true);
        }
Beispiel #3
0
        private List <Tuple <int, int> > GenerateSkyline(int[][] input, List <Tuple <int, int, int> > horizontalList)
        {
            var result = new List <Tuple <int, int> >();

            horizontalList.Sort();
            var hasStarted = new bool[input.Length];
            var maxHeap    = new MaxHeap <Tuple <int, int, bool> >();

            for (int i = 0; i < horizontalList.Count; i++)
            {
                var coord = horizontalList[i];
                if (hasStarted[coord.Item3])
                {
                    var target = new Tuple <int, int, bool>(coord.Item2, coord.Item3, true);
                    if (maxHeap.Contains(target))
                    {
                        // remove this from heap and log start //
                        maxHeap.Remove(target);
                        result.Add(new Tuple <int, int>(input[coord.Item3][0], input[coord.Item3][2]));
                    }
                    else
                    {
                        maxHeap.Remove(new Tuple <int, int, bool>(coord.Item2, coord.Item3, false));
                    }

                    // log end
                    if (maxHeap.Count == 0)
                    {
                        result.Add(new Tuple <int, int>(coord.Item1, 0));
                    }
                    else if (coord.Item2 > maxHeap.Peek().Item1)
                    {
                        result.Add(new Tuple <int, int>(coord.Item1, maxHeap.Peek().Item1));
                    }
                }
                else
                {
                    if (maxHeap.Count == 0 || maxHeap.Peek().Item1 < coord.Item2)
                    {
                        maxHeap.Add(new Tuple <int, int, bool>(coord.Item2, coord.Item3, true));
                    }
                    else
                    {
                        maxHeap.Add(new Tuple <int, int, bool>(coord.Item2, coord.Item3, false));
                    }

                    hasStarted[coord.Item3] = true;
                }
            }

            return(result);
        }
Beispiel #4
0
        public void MaxHeap_Contains_True(HeapItem[] items)
        {
            //Create the heap and add the items.
            var heap = new MaxHeap <HeapItem>(items.Length);

            for (var i = 0; i < items.Length; i++)
            {
                heap.Add(items[i]);
            }

            //Check if all added items are contained in the heap.
            for (var i = 0; i < items.Length; i++)
            {
                Assert.IsTrue(heap.Contains(items[i]), $"Contains returned false for item {items[i]}");
            }
        }
Beispiel #5
0
        public void MaxHeap_Contains_False(HeapItem[] items)
        {
            //Create the heap and add the items.
            var heap = new MaxHeap <HeapItem>(items.Length);

            for (var i = 0; i < items.Length; i++)
            {
                heap.Add(items[i]);
            }

            //Create new items and check if contains returns false
            for (var i = 0; i < items.Length; i++)
            {
                var item = new HeapItem(items[i].Value);
                Assert.IsFalse(heap.Contains(item), $"Contains returned true for item {item}");
            }
        }
Beispiel #6
0
        public void ContainsTest()
        {
            ICollection<int> heap = new MaxHeap<int>(this.testInts, true);

            for (int i = this.count - 1; 0 <= i; i--)
            {
                Assert.True(heap.Contains(this.testInts[i]));
            }

            Assert.False(heap.Contains(-1));
        }
Beispiel #7
0
        private static List <DefinitionNode> FindPath(AstarNode[] pathfindingNetwork, AstarNode startNode, AstarNode targetNode, float neededClearance, PathfindaxCollisionCategory collisionCategory)
        {
            try
            {
                var sw = new Stopwatch();
                sw.Start();

                var pathSucces = false;
                if (startNode == targetNode)
                {
                    return(new List <DefinitionNode> {
                        targetNode.DefinitionNode
                    });
                }
                if (startNode.Clearance >= neededClearance && targetNode.Clearance >= neededClearance)
                {
                    var openSet          = new MaxHeap <AstarNode>(pathfindingNetwork.Length);
                    var closedSet        = new HashSet <AstarNode>();
                    var itterations      = 0;
                    var neighbourUpdates = 0;
                    openSet.Add(startNode);
                    while (openSet.Count > 0)
                    {
                        itterations++;
                        var currentNode = openSet.RemoveFirst();
                        closedSet.Add(currentNode);

                        if (currentNode == targetNode)
                        {
                            sw.Stop();
                            Debug.WriteLine($"NodePath found in {sw.ElapsedMilliseconds} ms. Itterations: {itterations} Neighbourupdates: {neighbourUpdates}");
                            pathSucces = true;
                            break;
                        }

                        foreach (var connection in currentNode.DefinitionNode.Connections)
                        {
                            var toNode = NodePointer.Dereference(connection.To, pathfindingNetwork);
                            if ((connection.CollisionCategory & collisionCategory) != 0 || closedSet.Contains(toNode))
                            {
                                continue;
                            }

                            if (toNode.Clearance >= neededClearance)
                            {
                                var newMovementCostToNeighbour = currentNode.GCost + GetDistance(currentNode.DefinitionNode, toNode.DefinitionNode) * currentNode.DefinitionNode.MovementCostModifier;
                                if (newMovementCostToNeighbour < toNode.GCost || !openSet.Contains(toNode))
                                {
                                    toNode.GCost  = newMovementCostToNeighbour;
                                    toNode.HCost  = GetDistance(toNode.DefinitionNode, targetNode.DefinitionNode);
                                    toNode.Parent = currentNode.DefinitionNode.Index;
                                    neighbourUpdates++;
                                    if (!openSet.Contains(toNode))
                                    {
                                        openSet.Add(toNode);
                                    }
                                }
                            }
                        }
                    }
                }
                if (pathSucces)
                {
                    return(RetracePath(pathfindingNetwork, startNode, targetNode));
                }
                Debug.WriteLine("Did not find a path :(");
                return(null);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                Debugger.Break();
                return(null);
            }
        }
 public void Contains()
 {
     ContainsMaxHeap.Contains(SomeContainedValue);
 }