1
0
forked from s425077/PotatoPlan
JoelForkTest/Game1/Sources/Pathing/A-Star/PathSaver/PriorityQueue.cs

84 lines
1.7 KiB
C#
Raw Normal View History

2020-05-03 13:05:05 +02:00
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
class PriorityQueue
{
2020-05-03 16:35:46 +02:00
public List<Nodes> list;
2020-05-03 13:05:05 +02:00
public int Count { get { return list.Count; } }
public PriorityQueue()
{
2020-05-03 16:35:46 +02:00
list = new List<Nodes>();
2020-05-03 13:05:05 +02:00
}
public PriorityQueue(int count)
{
2020-05-03 16:35:46 +02:00
list = new List<Nodes>(count);
2020-05-03 13:05:05 +02:00
}
2020-05-03 16:35:46 +02:00
public void Enqueue(Nodes x)
2020-05-03 13:05:05 +02:00
{
list.Add(x);
int i = Count - 1;
while (i > 0)
{
int p = (i - 1) / 2;
2020-05-03 16:35:46 +02:00
if (list[p].getF() <= x.getF()) break;
2020-05-03 13:05:05 +02:00
list[i] = list[p];
i = p;
}
if (Count > 0) list[i] = x;
}
public void Dequeue()
{
2020-05-03 16:35:46 +02:00
Nodes min = Peek();
Nodes root = list[Count - 1];
2020-05-03 13:05:05 +02:00
list.RemoveAt(Count - 1);
int i = 0;
while (i * 2 + 1 < Count)
{
int a = i * 2 + 1;
int b = i * 2 + 2;
2020-05-03 16:35:46 +02:00
int c = b < Count && list[b].getF() < list[a].getF() ? b : a;
2020-05-03 13:05:05 +02:00
2020-05-03 16:35:46 +02:00
if (list[c].getF() >= root.getF()) break;
2020-05-03 13:05:05 +02:00
list[i] = list[c];
i = c;
}
if (Count > 0) list[i] = root;
}
2020-05-03 16:35:46 +02:00
public Nodes Peek()
2020-05-03 13:05:05 +02:00
{
if (Count == 0) throw new InvalidOperationException("Queue is empty.");
return list[0];
}
2020-05-03 16:35:46 +02:00
public Boolean Exists(Vector2 coordinates)
{
if (Count == 0)
return false;
foreach(Nodes node in list)
{
if (node.getCords() == coordinates)
return true;
}
return false;
}
2020-05-03 13:05:05 +02:00
public void Clear()
{
list.Clear();
}
}