diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-11-17 17:25:25 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-11-17 17:25:25 +0000 |
commit | d41da608a37fa74ae17042a837a77c01d1e1f9ea (patch) | |
tree | 7f250b0210e53b46d5bd0cae28fe16ccc982e1d1 /src/library | |
parent | 4c1cae0ef2772574a85d49f939b12c0e515aa6bd (diff) | |
download | scala-d41da608a37fa74ae17042a837a77c01d1e1f9ea.tar.gz scala-d41da608a37fa74ae17042a837a77c01d1e1f9ea.tar.bz2 scala-d41da608a37fa74ae17042a837a77c01d1e1f9ea.zip |
Added another fix for #3970.
No review.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/collection/mutable/DoubleLinkedListLike.scala | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/src/library/scala/collection/mutable/DoubleLinkedListLike.scala b/src/library/scala/collection/mutable/DoubleLinkedListLike.scala index 6236afa89b..77c00bc1d7 100644 --- a/src/library/scala/collection/mutable/DoubleLinkedListLike.scala +++ b/src/library/scala/collection/mutable/DoubleLinkedListLike.scala @@ -97,32 +97,25 @@ trait DoubleLinkedListLike[A, This <: Seq[A] with DoubleLinkedListLike[A, This]] if (prev ne null) prev.next = next // because this could be the first node } - private def getAtLocation(n: Int): This = if (this.isEmpty) null.asInstanceOf[This] else { + private def atLocation[T](n: Int)(f: This => T)(onOutOfBounds: => T) = if (isEmpty) onOutOfBounds else { var loc = repr var left = n while (left > 0) { loc = loc.next left -= 1 - if (loc.isEmpty) return null.asInstanceOf[This] + if (loc.isEmpty) onOutOfBounds } - loc + f(loc) } - private def atLocation[T](n: Int)(f: This => T) = { - val node = getAtLocation(n) - if (node eq null) f(node) - else throw new IndexOutOfBoundsException(n.toString) - } + private def outofbounds(n: Int) = throw new IndexOutOfBoundsException(n.toString) override def drop(n: Int): This = super[SeqLike].drop(n) - override def apply(n: Int): A = atLocation(n)(_.elem) - override def update(n: Int, x: A): Unit = atLocation(n)(_.elem = x) + override def tail = drop(1) - override def get(n: Int): Option[A] = { - val loc = getAtLocation(n) - if (loc ne null) Some(loc.elem) - else None - } + override def apply(n: Int): A = atLocation(n)(_.elem)(outofbounds(n)) + override def update(n: Int, x: A): Unit = atLocation(n)(_.elem = x)(outofbounds(n)) + override def get(n: Int): Option[A] = atLocation[Option[A]](n)(x => Some(x.elem))(None) } |