diff --git a/lib/PriorityQueue.n b/lib/PriorityQueue.n deleted file mode 100644 index c5ce19f18e..0000000000 --- a/lib/PriorityQueue.n +++ /dev/null @@ -1,110 +0,0 @@ -using System.Text; -using Nemerle; -using Nemerle.Imperative; -using Nemerle.Collections; -using Nemerle.Text; -using Nemerle.Utility; - -using System; -using System.Collections.Generic; - -namespace Nemerle.Collections -{ - public class PriorityQueue[T] - { - private data : List[T] = List(); - private cmp : T * T -> int; - - public this(cmp : T * T -> int) - { - this.cmp = cmp; - } - - public Enqueue(item : T) : void - { - data.Add(item); - mutable ci = data.Count - 1; // child index; start at end - while (ci > 0) - { - def pi = (ci - 1) / 2; // parent index - when (cmp(data[ci], data[pi]) >= 0) - break; // child item is larger than (or equal) parent so we're done - - def tmp = data[ci]; - data[ci] = data[pi]; - data[pi] = tmp; - ci = pi; - } - } - - public Dequeue() : T - { - // assumes pq is not empty; up to calling code - mutable li = data.Count - 1; // last index (before removal) - def frontItem = data[0]; // fetch the front - data[0] = data[li]; - data.RemoveAt(li); - - --li; // last index (after removal) - mutable pi = 0; // parent index. start at front of pq - while (true) - { - mutable ci = pi * 2 + 1; // left child index of parent - when (ci > li) break; // no children so done - def rc = ci + 1; // right child - when (rc <= li && cmp(data[rc], data[ci]) < 0) // if there is a rc (ci + 1), and it is smaller than left child, use the rc instead - ci = rc; - when (cmp(data[pi], data[ci]) <= 0) - break; // parent is smaller than (or equal to) smallest child so done - - def tmp = data[pi]; // swap parent and child - data[pi] = data[ci]; - data[ci] = tmp; - pi = ci; - } - frontItem; - } - - public FrontItem : T - { - get - { - def frontItem = data[0]; - frontItem; - } - } - - public Count : int - { - get - { - data.Count - } - } - - public override ToString() : string - { - def sb = StringBuilder(); - for (mutable i = 0; i < data.Count; ++i) - _ = sb.Append(data[i].ToString()).Append(" "); - _ = sb.Append("count = ").Append(data.Count); - sb.ToString() - } - - public IsConsistent() : bool - { - // is the heap property true for all data? - when (data.Count == 0) return true; - def li = data.Count - 1; // last index - for (mutable pi = 0; pi < data.Count; ++pi) // each parent index - { - def lci = 2 * pi + 1; // left child index - def rci = 2 * pi + 2; // right child index - - when (lci <= li && cmp(data[pi], data[lci]) > 0) return false; // if lc exists and it's greater than parent then bad. - when (rci <= li && cmp(data[pi], data[rci]) > 0) return false; // check the right child too. - } - true; // passed all checks - } - } -}