本文整理汇总了C#中PriorityQueue.IsEmpty方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.IsEmpty方法的具体用法?C# PriorityQueue.IsEmpty怎么用?C# PriorityQueue.IsEmpty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.IsEmpty方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PriorityQueue_Dequeue_NonRepeatedValues_Success
public void PriorityQueue_Dequeue_NonRepeatedValues_Success()
{
// Arrange
var priorityQueue = new PriorityQueue<int>();
var values = new int[] { 10, 7, 6, 1, 2, 3, 5, 4, 9, 8 };
var expected = "1,2,3,4,5,6,7,8,9,10";
// Act
foreach (var value in values)
{
priorityQueue.Enqueue(value);
}
var result = "";
var index = 0;
var lastIndex = priorityQueue.Count() - 1;
while (!priorityQueue.IsEmpty())
{
var element = priorityQueue.Dequeue();
result += index++ == lastIndex ?
string.Format("{0}", element.ToString()) : string.Format("{0},", element.ToString());
}
// Assert
Assert.AreEqual(result, expected);
}
示例2: FindRoute
public static Vector3[] FindRoute(Vector3 s, Vector3 e)
{
PriorityQueue q = new PriorityQueue();
Dictionary<Square, bool> dic = new Dictionary<Square, bool>();
PathElement start = new PathElement(PathGrid.FromReal(s.x), PathGrid.FromReal(s.z), null);
PathElement end = new PathElement(PathGrid.FromReal(e.x), PathGrid.FromReal(e.z), null);
q.Enqueue(new PriorityElement(DistFromEnd(start, end), start));
//Debug.Log("Calculating for " + start.x + ", " + start.y + " :: " + end.x + ", " + end.y);
int tick = 0 ;
while(!q.IsEmpty() && tick < 100000) {
tick ++;
PathElement p = (PathElement)q.Dequeue().stuff;
float dist = DistFromEnd(p, end);
if(dist < 1.0f) {
PathElement cur = p;
int size = 1;
while(cur != start) {
//Debug.Log(cur.x + " " + cur.y);
cur = cur.prev;
size ++;
}
cur = p;
Vector3[] route = new Vector3[size - 1];
while(cur != start) {
route[size - 2] = PathGrid.ToReal(cur.x, cur.y);
cur = cur.prev;
size --;
}
//SDebug.Log("START");
return route;
}
else {
AddOptions(q, p, end, dic);
}
}
Debug.Log("Cannot find route!");
Vector3[] route2 = new Vector3[1];
route2[0] = Vector3.zero;
return route2;
}
示例3: AllPathDijkstra
public double AllPathDijkstra(Stopwatch s)
{
int n_nodes = points.Count();
PriorityQueue q = new PriorityQueue(n_nodes);
double[] src_dist = new double[n_nodes];
int[] prev = new int[n_nodes];
int i;
s.Start();
for (i = 0; i < n_nodes; i++)
{
src_dist[i] = double.MaxValue;
prev[i] = -1;
q.Insert(i, src_dist[i]);
}
src_dist[src] = 0;
q.ReduceVal(src, src_dist[src]);
while (!q.IsEmpty())
{
int id = q.PopMin();
HashSet<int> adj_nodes = adjacencyList[id];
for (i = 0; i < adj_nodes.Count(); i++)
{
int temp_id = adj_nodes.ElementAt(i);
double temp_dist = src_dist[id] + GetDist(points[id], points[temp_id]);
//Console.WriteLine("Distance from " + id + " to " + temp_id + " is " + temp_dist);
if (temp_dist < src_dist[temp_id]) // If the node HAS been visited and the temp distance is less than the previous distance
{
src_dist[temp_id] = temp_dist;
prev[temp_id] = id;
if (!q.ReduceVal(temp_id, temp_dist))
{
Console.WriteLine("ERROR reducing id " + temp_id + " connected to id " + id);
return 1;
}
//Console.WriteLine("queue:\n" + q.ToString());
}
}
}
s.Stop();
return src_dist[dst];
}
示例4: DequeueWhenSingleEntry_ReturnsValueAndIsEmpty
public void DequeueWhenSingleEntry_ReturnsValueAndIsEmpty()
{
/* Arrange */
var msg = new Mock<IMessage>();
var pq = new PriorityQueue<string, IMessage>();
pq.Enqueue("", msg.Object);
/* Act */
var result = pq.Dequeue();
/* Assert */
Assert.AreSame(result, msg.Object);
Assert.AreEqual(true, pq.IsEmpty());
}
示例5: Run
public static IEnumerable<INode> Run(INode start, INode finish, IEnumerable<INode> nodes)
{
if (!nodes.Contains(start) || !nodes.Contains(finish))
throw new System.ArgumentException("Both `start` and `finish` must be in `nodes`.");
var distances = nodes.ToDictionary(node => node, _ => int.MaxValue);
var notVisitedQueue = new PriorityQueue(nodes, distances);
notVisitedQueue.DecreasePriority(start, 0);
var previous = new Dictionary<INode, INode>(notVisitedQueue.Count);
bool ifPathFound = false;
while (!notVisitedQueue.IsEmpty())
{
var nearest = notVisitedQueue.PullLowest();
if (distances[nearest] == int.MaxValue)
break;
if (nearest.Equals(finish))
{
ifPathFound = true;
break;
}
var links = nearest.GetLinks();
foreach (var link in links)
{
var alt = distances[nearest] + link.Weight;
if (alt < distances[link.Neighbour])
{
notVisitedQueue.DecreasePriority(link.Neighbour, alt);
previous[link.Neighbour] = nearest;
}
}
}
List<INode> result = new List<INode>();
if (ifPathFound)
{
INode current = finish;
while (current != start)
{
result.Add(current);
current = previous[current];
}
result.Add(current);
result.Reverse();
}
return result.AsEnumerable();
}
示例6: OnePathDijkstra
public double OnePathDijkstra(Stopwatch s)
{
int n_nodes = points.Count();
PriorityQueue q = new PriorityQueue(n_nodes);
double[] src_dist = new double[n_nodes];
int[] prev = new int[n_nodes];
int i;
s.Start();
for (i = 0; i < n_nodes; i++)
{
src_dist[i] = double.MaxValue;
prev[i] = -1;
}
src_dist[src] = 0;
prev[src] = -1;
q.Insert(src, src_dist[src]);
while (!q.IsEmpty())
{
int id = q.PopMin();
if (id == dst) { break; }
HashSet<int> adj_nodes = adjacencyList[id];
for (i = 0; i < adj_nodes.Count(); i++)
{
int temp_id = adj_nodes.ElementAt(i);
double temp_dist = src_dist[id] + GetDist(points[id], points[temp_id]);
if (src_dist[temp_id] == double.MaxValue) // If the node has NOT been visited
{
src_dist[temp_id] = temp_dist;
prev[temp_id] = id;
if (!q.Insert(temp_id, src_dist[temp_id]))
{
Console.WriteLine("ERROR inserting id " + temp_id + " connected to id " + id);
return 1;
}
}
else if (temp_dist < src_dist[temp_id]) // If the node HAS been visited and the temp distance is less than the previous distance
{
src_dist[temp_id] = temp_dist;
prev[temp_id] = id;
if (!q.ReduceVal(temp_id, temp_dist))
{
Console.WriteLine("ERROR reducing id " + temp_id + " connected to id " + id);
return 1;
}
}
}
}
s.Stop();
if (src_dist[dst] != double.MaxValue)
{
Pen pen = new Pen(Color.Black);
Font font = new Font("Arial", 8);
SolidBrush brush = new SolidBrush(Color.Black);
int temp = dst;
while (prev[temp] != -1)
{
PointF p1 = points[temp];
PointF p2 = points[prev[temp]];
graphics.DrawLine(pen, p1, p2);
graphics.DrawString(GetDist(p1, p2).ToString("#.##"), font, brush, GetMidPoint(p1, p2));
temp = prev[temp];
}
pictureBox.Refresh();
}
else
{
Console.WriteLine("Destination is unreachable");
}
return src_dist[dst];
}
示例7: CreatePath
private void CreatePath(Vertex2 start)
{
PriorityQueue frontier = new PriorityQueue ();
frontier.Add (0, start);
Dictionary<Vertex2, Vertex2> from = new Dictionary<Vertex2, Vertex2> ();
Dictionary<Vertex2, double> cost = new Dictionary<Vertex2, double> ();
cost [start] = 0;
from [start] = null;
Vertex2 end = null;
while (!frontier.IsEmpty()) {
var current = frontier.GetMin ();
if(IsGoal(current)) {
end = current;
break;
}
foreach(PathNode neighbor in GetNeighbors(current)) {
Vertex2 vNeighbor = neighbor.location;
double newCost = cost[current] + neighbor.cost;
if(!cost.ContainsKey(vNeighbor) || newCost < cost[vNeighbor]) {
cost[vNeighbor] = newCost;
frontier.Add(newCost, vNeighbor);
from[vNeighbor] = current;
}
}
}
if (end != null) {
Vertex2 current = end;
while(!current.Equals(start)) {
EnablePoint(current);
current = from[current];
}
}
}