From e59b179be851f2dc9b7a5f62bcd6cacf3b8ef7de Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Sat, 7 Apr 2012 20:18:35 +0100 Subject: [PATCH] Move queue implementations into new file. --- src/ofc/util/DirectedGraph.scala | 40 ------------------------------ src/ofc/util/Queue.scala | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 40 deletions(-) create mode 100644 src/ofc/util/Queue.scala diff --git a/src/ofc/util/DirectedGraph.scala b/src/ofc/util/DirectedGraph.scala index 4c2e1af..56f8146 100644 --- a/src/ofc/util/DirectedGraph.scala +++ b/src/ofc/util/DirectedGraph.scala @@ -2,46 +2,6 @@ package ofc.util import ofc.LogicError object DirectedGraph { - import scala.collection.generic.Growable - - private trait Queue[A] extends Growable[A] { - def pop() : A - def nonEmpty : Boolean - } - - private class StackQueue[A] extends Queue[A] { - private var stack = List[A]() - - def nonEmpty = stack.nonEmpty - - def +=(e: A) = { - stack = (e :: stack) - this - } - - def pop() = { - val (head, tail) = (stack.head, stack.tail) - stack = tail - head - } - - def clear() { - stack = Nil - } - } - - private class PriorityQueue[A](ordering: Ordering[A]) extends Queue[A] { - val queue = new scala.collection.mutable.PriorityQueue[A]()(ordering) - - def nonEmpty = queue.nonEmpty - def pop() = queue.dequeue() - def clear() = queue.clear() - - def +=(e: A) = { - queue += e - this - } - } def topoSort(graph: DirectedGraph) : Seq[DirectedGraph#Vertex] = { type Vertex = DirectedGraph#Vertex diff --git a/src/ofc/util/Queue.scala b/src/ofc/util/Queue.scala new file mode 100644 index 0000000..fea4559 --- /dev/null +++ b/src/ofc/util/Queue.scala @@ -0,0 +1,42 @@ +package ofc.util +import scala.collection.generic.Growable + +private trait Queue[A] extends Growable[A] { + def pop() : A + def nonEmpty : Boolean +} + +private class StackQueue[A] extends Queue[A] { + private var stack = List[A]() + + def nonEmpty = stack.nonEmpty + + def +=(e: A) = { + stack = (e :: stack) + this + } + + def pop() = { + val (head, tail) = (stack.head, stack.tail) + stack = tail + head + } + + def clear() { + stack = Nil + } +} + +private class PriorityQueue[A](ordering: Ordering[A]) extends Queue[A] { + val queue = new scala.collection.mutable.PriorityQueue[A]()(ordering) + + def nonEmpty = queue.nonEmpty + def pop() = queue.dequeue() + def clear() = queue.clear() + + def +=(e: A) = { + queue += e + this + } +} + -- 2.47.3