本文整理汇总了C#中PriorityQueue.Pop方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.Pop方法的具体用法?C# PriorityQueue.Pop怎么用?C# PriorityQueue.Pop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.Pop方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PriorityQueueGeneralTest
public void PriorityQueueGeneralTest()
{
var queue = new PriorityQueue<int>();
Assert.IsTrue(queue.Empty, "New queue should start out empty.");
Assert.Throws<InvalidOperationException>(() => queue.Peek(), "Peeking at an empty queue should throw.");
Assert.Throws<InvalidOperationException>(() => queue.Pop(), "Popping an empty queue should throw.");
foreach (var value in new[] { 4, 3, 5, 1, 2 })
{
queue.Add(value);
Assert.IsFalse(queue.Empty, "Queue should not be empty - items have been added.");
}
foreach (var value in new[] { 1, 2, 3, 4, 5 })
{
Assert.AreEqual(value, queue.Peek(), "Peek returned the wrong item - should be in order.");
Assert.IsFalse(queue.Empty, "Queue should not be empty yet.");
Assert.AreEqual(value, queue.Pop(), "Pop returned the wrong item - should be in order.");
}
Assert.IsTrue(queue.Empty, "Queue should now be empty.");
Assert.Throws<InvalidOperationException>(() => queue.Peek(), "Peeking at an empty queue should throw.");
Assert.Throws<InvalidOperationException>(() => queue.Pop(), "Popping an empty queue should throw.");
}
示例2: TestMethodSizeAndPushAndPop
public void TestMethodSizeAndPushAndPop()
{
PriorityQueue<double> testQueue = new PriorityQueue<double>();
testQueue.Push(1,2);
testQueue.Pop();
Assert.IsTrue(testQueue.Size() == 0);
}
示例3: TestMethodPushAndTopAndPop
public void TestMethodPushAndTopAndPop()
{
PriorityQueue<double> testQueue = new PriorityQueue<double>();
testQueue.Push(2, 2);
testQueue.Push(1, 2);
testQueue.Push(1, 3);
testQueue.Push(5, 2);
testQueue.Push(3, 1);
testQueue.Push(1, 1);
testQueue.Pop();
Assert.IsTrue(testQueue.Top() == 3);
}
示例4: Main
public static void Main()
{
var queue = new PriorityQueue<int>(new MyComperer());
queue.Push(5);
queue.Push(1);
queue.Push(2);
queue.Push(8);
while (queue.Size > 0)
{
Console.WriteLine(queue.Pop());
}
}
示例5: Main
static void Main()
{
var test = new PriorityQueue<int>();
test.Insert(1);
test.Insert(2);
test.Insert(3);
test.Insert(4);
test.Insert(0);
test.Insert(20);
test.Insert(7);
test.Insert(3000);
var count = test.Size;
for (int i = 0; i < count; i++)
{
Console.WriteLine(test.Pop());
}
}
示例6: Run
public static void Run(Vertex vertex)
{
vertex.Depth = 0;
PriorityQueue<Vertex> queue = new PriorityQueue<Vertex>((x, y) => y.Depth.CompareTo(x.Depth));
queue.Insert(vertex);
while(!queue.IsEmpty)
{
Vertex current = queue.Pop();
foreach(Edge edge in current.Edges)
{
if(edge.From.Depth + edge.Weight < edge.To.Depth)
{
edge.To.Depth = edge.From.Depth + edge.Weight;
edge.To.Parent = edge.From;
queue.Insert(edge.To);
}
}
}
}
示例7: AStar
public void AStar(Vector3 start, Vector3 end, OnPathComputed callback)
{
// Reset all scores and sets membership
Reset();
Node startNode = getClosestNode(start);
Node endNode = getClosestNode(end);
if (startNode == null || endNode == null) {
callback(new Path());
return;
}
PriorityQueue<Node> openSet = new PriorityQueue<Node>();
openSet.Push(startNode); startNode.InOpenSet = true;
Dictionary<Node, Node> parentPath = new Dictionary<Node, Node>();
startNode.GScore = 0;
startNode.FScore = startNode.GScore + heuristic(startNode, endNode);
Path path = new Path();
Node current = startNode;
while (openSet.Count() > 0) {
current = openSet.Pop(); // automatically do the remove part
current.InOpenSet = false;
if (current.Id == endNode.Id) {
path.Add(current.Position);
while (parentPath.ContainsKey(current)) {
current = parentPath[current];
path.Add(current.Position);
}
path.Reverse();
callback(path);
return;
}
current.InClosedSet = true;
List<Node> neighbors = getNeighbors(current);
for (int i = 0; i < neighbors.Count; ++i) {
Node neighbor = neighbors[i];
if (neighbor.InClosedSet) continue;
float gAttempt = current.GScore + euclidian(current, neighbor);
if (!neighbor.InOpenSet || gAttempt <= neighbor.GScore) {
parentPath[neighbor] = current;
neighbor.GScore = gAttempt;
neighbor.FScore = neighbor.GScore + heuristic(neighbor, endNode);
if (!neighbor.InOpenSet) {
openSet.Push(neighbor);
neighbor.InOpenSet = true;
}
}
} // end loop neighbors
}
}
示例8: CustomAStar
// This isn't really A* at all. But whatevs.
public List<Coordinate> CustomAStar(Coordinate start, Coordinate end, int iterationLimit = 200)
{
Dictionary<Coordinate, bool> closed = new Dictionary<Coordinate, bool>();
Dictionary<Coordinate, AStarEntry> queueReverseLookup = new Dictionary<Coordinate, AStarEntry>();
Coordinate? closestStart = this.FindClosestAStarCell(start, 10);
Coordinate? closestEnd = this.FindClosestFilledCell(end);
if (!closestStart.HasValue || !closestEnd.HasValue)
return null;
else
{
start = closestStart.Value;
end = closestEnd.Value;
}
Vector3 endPos = this.GetRelativePosition(end);
PriorityQueue<AStarEntry> queue = new PriorityQueue<AStarEntry>(new LambdaComparer<AStarEntry>((x, y) => x.ToGoal.CompareTo(y.ToGoal)));
AStarEntry firstEntry = new AStarEntry { Coordinate = start, SoFar = 0, ToGoal = (this.GetRelativePosition(start) - endPos).Length() };
queue.Push(firstEntry);
queueReverseLookup.Add(start, firstEntry);
int iteration = 0;
while (queue.Count > 0)
{
AStarEntry entry = queue.Pop();
if (iteration == iterationLimit
|| (Math.Abs(entry.Coordinate.X - end.X) <= 1
&& Math.Abs(entry.Coordinate.Y - end.Y) <= 1
&& Math.Abs(entry.Coordinate.Z - end.Z) <= 1))
return this.constructPath(entry);
queueReverseLookup.Remove(entry.Coordinate);
try
{
closed.Add(entry.Coordinate, true);
}
catch (ArgumentException)
{
continue;
}
foreach (Direction d in DirectionExtensions.Directions)
{
Coordinate next = entry.Coordinate.Move(d);
if ((entry.Parent == null || !next.Equivalent(entry.Parent.Coordinate)) && !closed.ContainsKey(next))
{
Map.CellState state = this[next];
if (state.ID == 0)
{
// This is an empty cell
// We can still use it if it's adjacent to a full cell
if (this[next.Move(0, 0, 1)].ID == 0
&& this[next.Move(0, 1, 0)].ID == 0
&& this[next.Move(0, 1, 1)].ID == 0
&& this[next.Move(1, 0, 0)].ID == 0
&& this[next.Move(1, 0, 1)].ID == 0
&& this[next.Move(1, 1, 0)].ID == 0
&& this[next.Move(1, 1, 1)].ID == 0
&& this[next.Move(0, 0, -1)].ID == 0
&& this[next.Move(0, -1, 0)].ID == 0
&& this[next.Move(0, -1, -1)].ID == 0
&& this[next.Move(-1, 0, 0)].ID == 0
&& this[next.Move(-1, 0, 1)].ID == 0
&& this[next.Move(-1, -1, 0)].ID == 0
&& this[next.Move(-1, -1, -1)].ID == 0)
continue;
}
else if (state.Permanent)
continue;
float tentativeGScore = entry.SoFar + 1;
AStarEntry newEntry;
if (queueReverseLookup.TryGetValue(next, out newEntry))
{
if (newEntry.SoFar < tentativeGScore)
continue;
}
if (newEntry == null)
{
newEntry = new AStarEntry { Coordinate = next, Parent = entry, SoFar = tentativeGScore, ToGoal = (this.GetRelativePosition(next) - endPos).Length() };
queue.Push(newEntry);
queueReverseLookup.Add(next, newEntry);
}
else
newEntry.SoFar = tentativeGScore;
}
}
iteration++;
}
return null;
}
示例9: FindPath
public bool FindPath()
{
//Check if indexes are valid
if (!IsIndexValid (First) || !IsIndexValid (Last))
return false;
//Check if starting and ending position are same
if (First == Last) {
ConstructPath (new Dictionary<Point, Point> (), Last);
return true;
}
var profiler = new Stopwatch ();
profiler.Start ();
//Create open/close sets and cameFrom
var closed = new HashSet<Point> ();
var open = new PriorityQueue<Point> (true);
var cameFrom = new Dictionary<Point, Point> ();
//Create f/g score dicts
var gScore = new Dictionary<Point, float> ();
var fScore = new Dictionary<Point, float> ();
//Prepare the first node
var current = First;
gScore [current] = 0f;
fScore [current] = gScore [current] + CostEstimate (current, Last);
open.Insert (current, fScore [current]);
//Main loop
while (!open.IsEmpty) {
//Take the first in the priority queue
current = open.Pop ();
closed.Add (current);
//If goal reached
if (current == Last) {
ConstructPath (cameFrom, current);
profiler.Stop ();
ComputationTime = profiler.Elapsed;
return true;
}
//Iterate neighbours
var neighbours = GetNeighbours (current);
foreach (var neighbourPair in neighbours) {
var neighbour = neighbourPair.Key;
var cost = neighbourPair.Value;
//Don't search closed nodes
if (closed.Contains (neighbour))
continue;
//Check if it's worth it
var tentativeGScore = gScore [current] + cost;
if (open.Contains (neighbour) && tentativeGScore >= gScore [neighbour])
continue;
//Set up neighbour
cameFrom [neighbour] = current;
gScore [neighbour] = tentativeGScore;
fScore [neighbour] = gScore [neighbour] + CostEstimate (neighbour, Last);
//Change priority or add it to the priority queue
if (open.ChangePriority (neighbour, fScore [neighbour]))
continue;
open.Insert (neighbour, fScore [neighbour]);
}
}
profiler.Stop ();
ComputationTime = profiler.Elapsed;
return false;
}
示例10: TestMethodPopOnException
public void TestMethodPopOnException()
{
PriorityQueue<double> testQueue = new PriorityQueue<double>();
testQueue.Pop();
}