Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.6KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. // By James McCaffrey11/02/2012
  5. sealed public class PriorityQueue<T>:IEnumerable<T> where T : IComparable<T>
  6. {
  7. private List<T> data;
  8. public PriorityQueue ()
  9. {
  10. this.data = new List<T> ();
  11. }
  12. IEnumerator System.Collections.IEnumerable.GetEnumerator ()
  13. {
  14. // Lets call the generic version here
  15. return this.GetEnumerator ();
  16. }
  17. public IEnumerator<T> GetEnumerator ()
  18. {
  19. return data.GetEnumerator () as IEnumerator<T>;
  20. }
  21. public void Enqueue (T item)
  22. {
  23. data.Add (item);
  24. int ci = data.Count - 1; // child index; start at end
  25. while (ci > 0) {
  26. int pi = (ci - 1) / 2; // parent index
  27. if (data [ci].CompareTo (data [pi]) >= 0)
  28. break; // child item is larger than (or equal) parent so we're done
  29. T tmp = data [ci];
  30. data [ci] = data [pi];
  31. data [pi] = tmp;
  32. ci = pi;
  33. }
  34. }
  35. public T Dequeue()
  36. {
  37. // assumes pq is not empty; up to calling code
  38. int li = data.Count - 1; // last index (before removal)
  39. T frontItem = data [0]; // fetch the front
  40. data [0] = data [li];
  41. data.RemoveAt (li);
  42. --li; // last index (after removal)
  43. int pi = 0; // parent index. start at front of pq
  44. while (true)
  45. {
  46. int ci = pi * 2 + 1; // left child index of parent
  47. if (ci > li)
  48. break; // no children so done
  49. int rc = ci + 1; // right child
  50. if (rc <= li && data [rc].CompareTo (data [ci]) < 0) // if there is a rc (ci + 1), and it is smaller than left child, use the rc instead
  51. ci = rc;
  52. if (data [pi].CompareTo (data [ci]) <= 0)
  53. break; // parent is smaller than (or equal to) smallest child so done
  54. T tmp = data [pi];
  55. data [pi] = data [ci];
  56. data [ci] = tmp; // swap parent and child
  57. pi = ci;
  58. }
  59. return frontItem;
  60. }
  61. public T Peek ()
  62. {
  63. T frontItem = data [0];
  64. return frontItem;
  65. }
  66. public int Count ()
  67. {
  68. return data.Count;
  69. }
  70. public override string ToString ()
  71. {
  72. string s = "";
  73. for (int i = 0; i < data.Count; ++i)
  74. s += data [i].ToString () + " ";
  75. s += "count = " + data.Count;
  76. return s;
  77. }
  78. public bool IsConsistent ()
  79. {
  80. // is the heap property true for all data?
  81. if (data.Count == 0)
  82. return true;
  83. int li = data.Count - 1; // last index
  84. for (int pi = 0; pi < data.Count; ++pi) { // each parent index
  85. int lci = 2 * pi + 1; // left child index
  86. int rci = 2 * pi + 2; // right child index
  87. if (lci <= li && data [pi].CompareTo (data [lci]) > 0)
  88. return false; // if lc exists and it's greater than parent then bad.
  89. if (rci <= li && data [pi].CompareTo (data [rci]) > 0)
  90. return false; // check the right child too.
  91. }
  92. return true; // passed all checks
  93. } // IsConsistent
  94. } // PriorityQueue