public void ClearShouldReduceCountToZero() { IntMinHeap.Add(10); IntMinHeap.Add(5); IntMinHeap.Clear(); Assert.AreEqual(0, IntMinHeap.Count); }
/// <summary> /// <para>Credit for the original code to Roy T. - http://roy-t.nl/2011/09/24/another-faster-version-of-a-2d3d-in-c.html</para> /// <para>This was modified and adapted to suit the needs of this mod, which changes the specific end vector to an "outside of grid" target.</para> /// <para>Also made into a separate thread.</para> /// </summary> private void ThreadRun() { // generate crumbs and path cost crumb = PathfindGenerateCrumbs(selectedGrid, ref startPosition); // cleanup after PathfindGenerateCrumbs(), not critical but helps memory a tiny bit scanned.Clear(); openList.Clear(); if (crumb != null) { while (crumb.Next != null) { if (ShouldCancelTask) { return; } lines.Add(new Line() { Start = crumb.Next.Position, End = crumb.Position, }); crumb = crumb.Next; } } }
void UpdatePossibleBridges(SimpleClosedPath exterior, SimpleClosedPath mainHole) { possibleBridges.Clear(); foreach (Vector2 from in mainHole.Points()) { foreach (SimpleClosedPath hole in exterior.Holes()) { if (hole == mainHole) { continue; } foreach (Vector2 to in hole.Points()) { possibleBridges.Add(new ExtendedEdge(from, to)); } } foreach (Vector2 to in exterior.Points()) { possibleBridges.Add(new ExtendedEdge(from, to)); } } // possibleBridges.Sort(bridgeComparer); }
public void TestAddAllClear() { MinHeap <int> mh = new MinHeap <int>(intComparer, new int[] { 1, 3, 4, 0, -1, 2, 5 }); Assert.AreEqual(mh.Count, 7); Assert.IsFalse(mh.IsEmpty); Assert.AreEqual(mh.RemoveFirst(), -1); Assert.AreEqual(mh.RemoveFirst(), 0); Assert.AreEqual(mh.RemoveFirst(), 1); Assert.AreEqual(mh.RemoveFirst(), 2); Assert.AreEqual(mh.RemoveFirst(), 3); Assert.AreEqual(mh.RemoveFirst(), 4); Assert.AreEqual(mh.RemoveFirst(), 5); Assert.IsTrue(mh.IsEmpty); mh.AddAll(new int[] { 4, 3, 2, 1, 0 }); Assert.IsFalse(mh.IsEmpty); Assert.AreEqual(mh.Count, 5); Assert.AreEqual(mh.First, 0); mh.Clear(); Assert.IsTrue(mh.IsEmpty); Assert.AreEqual(mh.Count, 0); Assert.AreEqual(mh.Capacity, 1); mh.AddAll(1, 2, 5, 10); Assert.AreEqual(mh.Count, 4); Assert.IsTrue(mh.Capacity >= 4); Assert.AreEqual(mh.RemoveFirst(), 1); Assert.AreEqual(mh.RemoveFirst(), 2); Assert.AreEqual(mh.RemoveFirst(), 5); Assert.AreEqual(mh.RemoveFirst(), 10); Assert.IsTrue(mh.IsEmpty); }
public void ClearMinHeap_LeavesCountOfZero(List <int> data) { // Arrange var heap = new MinHeap <int>(data); // Act heap.Clear(); // Assert Assert.Empty(heap); }
public void AssertThat_Clear_EmptiesHeap() { var heap = new MinHeap <int>(); heap.Add(1); heap.Add(3); heap.Add(2); Assert.AreEqual(3, heap.Count); heap.Clear(); Assert.AreEqual(0, heap.Count); }
public void TestClear() { MinHeap <int> minHeap = new MinHeap <int>(); minHeap.Add(1); minHeap.Add(2); minHeap.Add(3); minHeap.Clear(); Assert.AreEqual(0, minHeap.Count); minHeap.Peek(); }
public void ClearingHeapsSetsSizeToZero() { MinHeap <int> minHeap = new MinHeap <int>(IntComparer); MaxHeap <int> maxHeap = new MaxHeap <int>(IntComparer); foreach (int element in elements) { maxHeap.Add(element); minHeap.Add(element); } maxHeap.Clear(); minHeap.Clear(); Assert.AreEqual(0, maxHeap.Size); Assert.AreEqual(0, minHeap.Size); }
public void TestReferenceType() { MinHeap <Node> mh = new MinHeap <Node>(Node.Comparer); mh.Add(new Node(3)); Assert.AreEqual(mh.First.n, 3); Assert.AreEqual(mh.Count, 1); mh.AddAll(new Node[] { new Node(1), new Node(4), new Node(0) }); Assert.AreEqual(mh.Count, 4); Assert.AreEqual(mh.RemoveFirst().n, 0); Assert.AreEqual(mh.RemoveFirst().n, 1); Assert.AreEqual(mh.First.n, 3); Assert.AreEqual(mh.Count, 2); mh.Clear(); Assert.AreEqual(mh.Count, 0); Assert.IsNull(mh.First); }
public void Clear() { testMinHeap.Insert(2); testMinHeap.Insert(7); testMinHeap.Insert(8); testMinHeap.Insert(9); testMinHeap.Clear(); Dictionary <Int32, DoublyLinkedTreeNode <Int32> > allNodes = GetAllNodes(testMinHeap); Assert.AreEqual(0, allNodes.Count); testMinHeap.Insert(10); allNodes = GetAllNodes(testMinHeap); Assert.AreEqual(1, allNodes.Count); Assert.IsNull(allNodes[10].ParentNode); Assert.IsNull(allNodes[10].LeftChildNode); Assert.IsNull(allNodes[10].RightChildNode); }
public void TestClear() { MinHeap <int> maxHeap = CreateMaxHeap(); for (int i = 0; i < 100; i++) { maxHeap.Push(i); } maxHeap.Clear(); Assert.AreEqual(0, maxHeap.Count); maxHeap.Peek(); maxHeap.Push(90); maxHeap.Push(5); maxHeap.Push(15); maxHeap.Push(89); Assert.AreEqual(4, maxHeap.Count); Assert.AreEqual(90, maxHeap.Peek()); }
public void TestClear() { MinHeap <int> minHeap = new MinHeap <int>(); for (int i = 0; i < 100; i++) { minHeap.Push(i); } minHeap.Clear(); Assert.AreEqual(0, minHeap.Count); minHeap.Peek(); minHeap.Push(90); minHeap.Push(5); minHeap.Push(15); minHeap.Push(89); Assert.AreEqual(4, minHeap.Count); Assert.AreEqual(5, minHeap.Peek()); }
public void Teardown() { _heap.Clear(); _heap = null; }
private void Update() { if (startX != -1 && !finishAStar) { if (mh.HasNext()) { Node n = mh.Pop(); int[] dx = { 1, 0, -1, 0 }; int[] dy = { 0, 1, 0, -1 }; //color current block red fm.ColorBlock(n.x, n.y, red); //if previous node was not start node, color it gradient if (lastX != -1 && (lastX != startX || lastY != startY)) { fm.ColorBlock(lastX, lastY, Color.Lerp(blue, green, (float)nodes[lastX, lastY].value / solution)); } //check if we arrived at destination if (n.x == endX && n.y == endY) { mh.Clear(); //travel through parent nodes to store path while (n.x != startX || n.y != startY) { n = nodes[n.x, n.y].parent; path.Add(n.GetCoord()); } // -2 instead of -1 because we dont want to overwrite starting node travel = path.Count - 2; finishAStar = true; return; } lastX = n.x; lastY = n.y; for (int i = 0; i < 4; i++) { int newX = n.x + dx[i]; int newY = n.y + dy[i]; if (newX >= 0 && newX < fm.floor.GetLength(0) && newY >= 0 && newY < fm.floor.GetLength(1)) { //if not a wall if (fm.floor[newX, newY].value == 0) { float tempH = nodes[n.x, n.y].value + 1 + Vector2.Distance(new Vector2(newX, newY), new Vector2(endX, endY)); if (tempH < nodes[newX, newY].heuristic) { nodes[newX, newY].heuristic = tempH; nodes[newX, newY].value = nodes[n.x, n.y].value + 1; nodes[newX, newY].parent = nodes[n.x, n.y]; mh.Insert(nodes[newX, newY]); } } } } } else { //if we run out of nodes to explore, finish coloring if (lastX != -1 && (lastX != startX || lastY != startY)) { fm.ColorBlock(lastX, lastY, Color.Lerp(blue, green, (float)nodes[lastX, lastY].value / solution)); } } } if (finishAStar && travel >= 0) { fm.ColorBlock(path[travel].x, path[travel].y, purple); travel--; } }
public Polygon Compute(OperationType ot) { connector.OnAddEdgeEvent += OnAddEdge; if (subject == null) { return(clipper); } else if (clipper == null) { return(subject); } Polygon result = new Polygon(); futureSweepLineEvents.Clear(); // Test 1 for trivial result case if (subject.GetNumPaths() * clipper.GetNumPaths() == 0) { // At least one of the polygons is empty if (ot == OperationType.DIFFERENCE) { result = subject; } if (ot == OperationType.UNION) { result = (subject.GetNumPaths() == 0) ? clipper : subject; } return(result); } // Test 2 for trivial result case bbSubject = subject.GetBoundingBox(); bbClipper = clipper.GetBoundingBox(); if (bbSubject.min.x > bbClipper.max.x || bbSubject.min.y > bbClipper.max.y || bbClipper.min.x > bbSubject.max.x || bbClipper.min.y > bbSubject.max.y) { // the bounding boxes do not overlap if (ot == OperationType.DIFFERENCE) { result = subject; } if (ot == OperationType.UNION) { result = subject; for (int i = 0; i < clipper.GetNumPaths(); ++i) { result.Add(clipper.GetPath(i)); } } return(result); } // Insert all the endpoints associated to the line segments into the event queue for (int currentPath = 0; currentPath < subject.GetNumPaths(); ++currentPath) { for (int currentEdge = 0; currentEdge < subject.GetPath(currentPath).GetNumPoints(); ++currentEdge) { ProcessEdge(subject.GetPath(currentPath).GetEdge(currentEdge), Operand.SUBJECT); } } for (int currentPath = 0; currentPath < clipper.GetNumPaths(); ++currentPath) { for (int currentEdge = 0; currentEdge < clipper.GetPath(currentPath).GetNumPoints(); ++currentEdge) { ProcessEdge(clipper.GetPath(currentPath).GetEdge(currentEdge), Operand.CLIPPER); } } minMaxX = Mathf.Min(bbSubject.max.x, bbClipper.max.x); while (!futureSweepLineEvents.IsEmpty()) { SweepEvent currentEvent = futureSweepLineEvents.ExtractFirst(); currentEvent.Number = ++seCounter; if (AlreadyFinished(ref result, ot, currentEvent)) { return(result); } if (currentEvent.Left) { HandleLeftEvent(currentEvent); } else { HandleRightEvent(ot, currentEvent); } } return(connector.CreatePolygon()); }
public virtual AStarResultCode FindPath(T start, T finish, ref List <T> path, int maxOverload = 0) { path.Clear(); _openedNodes.Clear(); _allNodes.Clear(); Start = start; Finish = finish; if (Arrived(Start, Finish)) { // 如果起点==终点 path.Add(Start); return(AStarResultCode.SUCCESS); } else if (InPathCache(Start, Finish)) { // 获得从neighbor到Finish的路径 GetPathFromCache(Start, Finish, ref path); return(AStarResultCode.SUCCESS); } List <T> neighbors = new List <T>(); T curNode = Start; _openedNodes.Add(curNode); OnOpenedNodeAdded(curNode, curNode); _allNodes.Add(curNode); while (true) { // 获得curNode邻接的所有“可达”节点 Neighbors(curNode, ref neighbors); // 遍历cur节点的周边节点 foreach (T neighbor in neighbors) { if (neighbor.Status == AStarNodeStatus.CLOSED) { continue; } else if (neighbor.Status == AStarNodeStatus.OPENED) { // 如果neighbor已访问过 if (neighbor.G > GetCost(neighbor) + curNode.G) { // 如果neighbor之前的G值比当前新路径更大, 重置其Parent和G(即将其归入新路径) neighbor.Parent = curNode; neighbor.G = GetCost(neighbor) + curNode.G; OnOpenedNodeChanged(neighbor, curNode); // 调整二叉堆 _openedNodes.OnChanged(neighbor); } } else { // 如果neighbor尚未访问(即NONE状态) neighbor.Status = AStarNodeStatus.OPENED; neighbor.Parent = curNode; neighbor.G = GetCost(neighbor) + curNode.G; // 加入open列表 _openedNodes.Add(neighbor); OnOpenedNodeAdded(neighbor, curNode); _allNodes.Add(neighbor); if (Arrived(neighbor, Finish)) { // 如果到达终点 FillPath(neighbor, ref path); ResetAll(); return(AStarResultCode.SUCCESS); } else if (maxOverload > 0 && _openedNodes.Count >= maxOverload) { // 如果寻路过载 ResetAll(); return(AStarResultCode.FAIL_OVERLOAD); } } } // 遍历周边各节点之后,当前节点可以close curNode.Status = AStarNodeStatus.CLOSED; // 移出open列表 //_openedNodes.Pop(); _openedNodes.Remove(curNode); if (_openedNodes.Empty) { // 无可到达路径 ResetAll(); return(AStarResultCode.FAIL_NO_WAY); } else { // 取最小F值node作为下一个cur节点 _openedNodes.Peek(ref curNode); OnNodePeek(curNode); // -----------如果有路径缓存-------------- if (InPathCache(curNode, Finish)) { // 获得从curNode(不含)到Finish(含)的路径 GetPathFromCache(curNode, Finish, ref path); int countFromCache = path.Count; // 填入从Start(不含)到curNode(含)的路径 FillPath(curNode, ref path); ResetAll(); // 为path中find到的部分节点修改缓存 path.Insert(0, Start);// 把起点添加进来作添加缓存处理 int countToAddInCache = path.Count - countFromCache - 1 /*即curNode,因为curNode到Finish已经记录缓存了*/; for (int i = 0; i < countToAddInCache; i++) { if (i + 1 < countFromCache) { _pathCache[path[i].PathCacheIdx, path[i + 1].PathCacheIdx] = path[i + 1]; _pathCache[path[i + 1].PathCacheIdx, path[i].PathCacheIdx] = path[i]; } for (int j = i + 2; j < path.Count; j++) { _pathCache[path[i].PathCacheIdx, path[j].PathCacheIdx] = path[j - 1]; _pathCache[path[j].PathCacheIdx, path[i].PathCacheIdx] = path[j - 1]; } } return(AStarResultCode.SUCCESS); } } } }
public void constructCellOverlay(int level, int c) { var queue = new MinHeap <int, float>(); var cell = overlayVerticesCellMapping[level][c]; if (cell == null) { return; } var round = 0; var open = new Dictionary <int, float>(); for (var i = 0; i < cell.Count; i++) { queue.Clear(); open.Clear(); var start = overlayVertices[cell[i]]; start.OverlayEdges[level - 1] = new List <OverlayEdge>(); start.OverlayEdges[level - 1].Add(new OverlayEdge() { NeighborOverlayVertex = start.NeighborOverlayVertex, Cost = edges[start.OriginalEdge].Cost }); queue.Add(cell[i], 0); open[cell[i]] = round; while (queue.Count != 0) { float currentCost = queue.PeekCost(); int currentOverlayVertex = queue.Remove(); var neighbors = overlayVertices[currentOverlayVertex].OverlayEdges[level - 2]; if (neighbors == null || neighbors.Count == 0) { continue; } for (var j = 0; j < neighbors.Count; j++) { var target = neighbors[j].NeighborOverlayVertex; if (target == -1 || (int)OverlayGraphUtilities.GetCellNumberOnLevel(level, vertices[overlayVertices[target].OriginalVertex].CellNumber, overlayGraph.offset) != c) { bool wasFound = false; for (var x = 0; x < start.OverlayEdges[level - 1].Count; x++) { if (start.OverlayEdges[level - 1][x].NeighborOverlayVertex == currentOverlayVertex) { if (currentCost < start.OverlayEdges[0][x].Cost) { start.OverlayEdges[0][x] = new OverlayEdge { NeighborOverlayVertex = currentOverlayVertex, Cost = currentCost }; } wasFound = true; break; } } if (!wasFound) { start.OverlayEdges[level - 1].Add(new OverlayEdge() { NeighborOverlayVertex = currentOverlayVertex, Cost = currentCost }); } continue; } var newCost = currentCost + neighbors[j].Cost; var found = open.TryGetValue(target, out float oldCost); if (!found) { queue.Add(target, newCost); open[target] = newCost; } else if (newCost < oldCost) { queue.Update(target, newCost); open[target] = newCost; } } } } }
private void constructCellBase(int c) { var queue = new MinHeap <int, float>(); var open = new Dictionary <int, float>(); var cell = overlayVerticesCellMapping[1][c]; if (cell == null) { return; } for (var i = 0; i < cell.Count; i++) { queue.Clear(); open.Clear(); var start = overlayVertices[cell[i]]; start.OverlayEdges[0] = new List <OverlayEdge>(); start.OverlayEdges[0].Add(new OverlayEdge() { NeighborOverlayVertex = start.NeighborOverlayVertex, Cost = edges[start.OriginalEdge].Cost }); queue.Add(start.OriginalVertex, 0); open[start.OriginalVertex] = 0; while (queue.Count != 0) { var currentCost = queue.PeekCost(); var current = queue.Remove(); var edgeEnd = (current + 1 == vertices.Count) ? vertexEdgeMapping.Count : vertices[current + 1].EdgeOffset; for (var j = vertices[current].EdgeOffset; j < edgeEnd; j++) { var edge = edges[vertexEdgeMapping[j]]; var target = edge.FromVertex == current ? edge.ToVertex : edge.FromVertex; if (target == -1 || (int)OverlayGraphUtilities.GetCellNumberOnLevel(1, vertices[target].CellNumber, overlayGraph.offset) != c) { var targetGridPosition = target == -1 ? edge.ToVertexGridPosition : vertices[target].GridPosition; if (edgeToOverlayVertex.TryGetValue(((ulong)(uint)vertices[current].GridPosition << 32) | (uint)targetGridPosition, out int value)) { bool wasFound = false; for (var x = 0; x < start.OverlayEdges[0].Count; x++) { if (start.OverlayEdges[0][x].NeighborOverlayVertex == value) { if (currentCost < start.OverlayEdges[0][x].Cost) { start.OverlayEdges[0][x] = new OverlayEdge { NeighborOverlayVertex = value, Cost = currentCost }; } wasFound = true; break; } } if (!wasFound) { start.OverlayEdges[0].Add(new OverlayEdge() { NeighborOverlayVertex = value, Cost = currentCost }); } } continue; } var newCost = currentCost + edge.Cost; var found = open.TryGetValue(target, out float oldCost); if (!found) { queue.Add(target, newCost); open[target] = newCost; } else if (newCost < oldCost) { queue.Update(target, newCost); open[target] = newCost; } } } } }