Added priority queue with changable priority.

This commit is contained in:
glax 2023-04-23 14:49:09 +02:00
parent 97a8c2ea6f
commit 5f6cccd17d
2 changed files with 38 additions and 2 deletions

View File

@ -49,7 +49,7 @@ public class Pathfinder
return this;
}
PriorityQueue<OsmNode, double> openSetfScore = new();
RPriorityQueue<OsmNode, double> openSetfScore = new();
openSetfScore.Enqueue(startNode, 0);
gScore = new() { { startNode, 0 } };
_cameFromDict = new();
@ -78,7 +78,7 @@ public class Pathfinder
_cameFromDict[neighbor] = currentNode;
gScore[neighbor] = tentativeGScore;
double h = Heuristic(currentNode, neighbor, goalNode, edge);
openSetfScore.Enqueue(neighbor, tentativeGScore + h); //Currently set includes a lot of duplicates
openSetfScore.Enqueue(neighbor, tentativeGScore + h);
}
}
}

View File

@ -0,0 +1,36 @@
namespace Pathfinding;
public class RPriorityQueue<TKey, TPriority> where TKey : notnull
{
public Dictionary<TKey, TPriority> queue;
public int Count { get; private set; }
public RPriorityQueue()
{
queue = new();
}
public void Enqueue(TKey key, TPriority priority)
{
if (!queue.TryAdd(key, priority))
queue[key] = priority;
Count = queue.Count;
}
public TKey? Dequeue()
{
TKey? retKey = queue.MinBy(item => item.Value).Key;
queue.Remove(retKey);
Count = queue.Count;
return retKey;
}
public int Remove(IEnumerable<TKey> elements)
{
int before = Count;
queue = queue.Where(queueitem => !elements.Contains(queueitem.Key))
.ToDictionary(item => item.Key, item => item.Value);
Count = queue.Count;
return before - Count;
}
}