diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2009-11-11 00:51:20 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2009-11-11 00:51:20 +0000 |
commit | 0c17a1a7d66fe78d75aff864b9997837c5c16e76 (patch) | |
tree | 15085b2c89ec9669395343fb12469b4d82640127 | |
parent | 6aaab9a6df63e7a2d302e6654e7aac34090a58a7 (diff) | |
download | scala-0c17a1a7d66fe78d75aff864b9997837c5c16e76.tar.gz scala-0c17a1a7d66fe78d75aff864b9997837c5c16e76.tar.bz2 scala-0c17a1a7d66fe78d75aff864b9997837c5c16e76.zip |
Queue - several bugs fixed, particular one not ...
Queue - several bugs fixed, particular one not updating the last0 field of MutableList.
-rw-r--r-- | src/library/scala/collection/mutable/Queue.scala | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/src/library/scala/collection/mutable/Queue.scala b/src/library/scala/collection/mutable/Queue.scala index 4daba3c537..57bf6cd6a4 100644 --- a/src/library/scala/collection/mutable/Queue.scala +++ b/src/library/scala/collection/mutable/Queue.scala @@ -61,11 +61,25 @@ class Queue[A] extends MutableList[A] with Cloneable[Queue[A]] { first0 = first0.next len -= 1 res - } else - extractFirst(first0, p) match { - case None => None - case Some(cell) => Some(cell.elem) - } + } else { + val optElem = removeFromList(p) + if (optElem != None) len -= 1 + optElem + } + + private def removeFromList(p: A => Boolean): Option[A] = { + var leftlst = first0 + var res: Option[A] = None + while (leftlst.next.nonEmpty && !p(leftlst.next.elem)) { + leftlst = leftlst.next + } + if (leftlst.next.nonEmpty) { + res = Some(leftlst.next.elem) + if (leftlst.next eq last0) last0 = leftlst + leftlst.next = leftlst.next.next + } + res + } /** Returns all elements in the queue which satisfy the * given predicate, and removes those elements from the queue. @@ -84,13 +98,22 @@ class Queue[A] extends MutableList[A] with Cloneable[Queue[A]] { first0 = first0.next len -= 1 } - var cell: Option[LinkedList[A]] = extractFirst(first0, p) - while (!cell.isEmpty) { - res += cell.get.elem - cell = extractFirst(cell.get, p) - } - res + if (first0.isEmpty) res + else removeAllFromList(p, res) + } + } + + private def removeAllFromList(p: A => Boolean, res: ArrayBuffer[A]): ArrayBuffer[A] = { + var leftlst = first0 + while (leftlst.next.nonEmpty) { + if (p(leftlst.next.elem)) { + res += leftlst.next.elem + if (leftlst.next eq last0) last0 = leftlst + leftlst.next = leftlst.next.next + len -= 1 + } else leftlst = leftlst.next } + res } /** Return the proper suffix of this list which starts with the first element that satisfies `p`. |