Skip to content

Instantly share code, notes, and snippets.

@e-sarkis
Last active March 13, 2024 05:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-sarkis/716c4415254a22c2b2f9eb8d9df777f5 to your computer and use it in GitHub Desktop.
Save e-sarkis/716c4415254a22c2b2f9eb8d9df777f5 to your computer and use it in GitHub Desktop.
PriorityQueue for Unity3D
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
// ----------------------------------------------------------------------------
// PriorityQueue class for use in Unity3D.
//
// Based on PriorityQueue implementation found in Red Blob Games' A*
// Implementation compantion:
// http://www.redblobgames.com/pathfinding/a-star/implementation.html#csharp
//
// Note: Requires EppyGames "Tuple implementation for Unity3D" by
// Michael Bartnett: https://gist.github.com/michaelbartnett/5652076
//
// Alternative implementations for Tuple usage in Unity3D should also work.
// ----------------------------------------------------------------------------
using System.Collections.Generic;
using Eppy;
namespace ESarkis
{
/// <summary>
/// A Queue class in which each item is associated with a Double value
/// representing the item's priority.
/// Dequeue and Peek functions return item with the best priority value.
/// </summary>
public class PriorityQueue<T>
{
List<Tuple<T, double>> elements = new List<Tuple<T, double>>();
/// <summary>
/// Return the total number of elements currently in the Queue.
/// </summary>
/// <returns>Total number of elements currently in Queue</returns>
public int Count
{
get { return elements.Count; }
}
/// <summary>
/// Add given item to Queue and assign item the given priority value.
/// </summary>
/// <param name="item">Item to be added.</param>
/// <param name="priorityValue">Item priority value as Double.</param>
public void Enqueue(T item, double priorityValue)
{
elements.Add(Tuple.Create(item, priorityValue));
}
/// <summary>
/// Return lowest priority value item and remove item from Queue.
/// </summary>
/// <returns>Queue item with lowest priority value.</returns>
public T Dequeue()
{
int bestPriorityIndex = 0;
for (int i = 0; i < elements.Count; i++)
{
if (elements[i].Item2 < elements[bestPriorityIndex].Item2)
{
bestPriorityIndex = i;
}
}
T bestItem = elements[bestPriorityIndex].Item1;
elements.RemoveAt(bestPriorityIndex);
return bestItem;
}
/// <summary>
/// Return lowest priority value item without removing item from Queue.
/// </summary>
/// <returns>Queue item with lowest priority value.</returns>
public T Peek()
{
int bestPriorityIndex = 0;
for (int i = 0; i < elements.Count; i++)
{
if (elements[i].Item2 < elements[bestPriorityIndex].Item2)
{
bestPriorityIndex = i;
}
}
T bestItem = elements[bestPriorityIndex].Item1;
return bestItem;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment