diff options
Diffstat (limited to 'src/library/scala/collection/mutable/LinkedListLike.scala')
-rw-r--r-- | src/library/scala/collection/mutable/LinkedListLike.scala | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/src/library/scala/collection/mutable/LinkedListLike.scala b/src/library/scala/collection/mutable/LinkedListLike.scala index e26735ed23..42f37766b2 100644 --- a/src/library/scala/collection/mutable/LinkedListLike.scala +++ b/src/library/scala/collection/mutable/LinkedListLike.scala @@ -24,44 +24,60 @@ import annotation.tailrec * @version 2.8 * @since 2.8 */ -trait LinkedListLike[A, This >: Null <: Seq[A] with LinkedListLike[A, This]] extends SeqLike[A, This] { self => +trait LinkedListLike[A, This <: Seq[A] with LinkedListLike[A, This]] extends SeqLike[A, This] { self => var elem: A = _ var next: This = _ - override def isEmpty = false + override def isEmpty = next eq this - override def length: Int = 1 + (if (next eq null) 0 else next.length) + override def length: Int = if (isEmpty) 0 else next.length override def head: A = elem - override def tail: This = next - def append(that: This): Unit = { + override def tail: This = { + require(nonEmpty, "tail of empty list") + next + } + + /** Append linked list `that` at current position of this linked list + * @return the list after append (this is the list itself if nonempty, + * or list `that` if list this is empty. ) + */ + def append(that: This): This = { @tailrec - def loop(x: This): Unit = { - if (x.next eq null) x.next = that + def loop(x: This) { + if (x.next.isEmpty) x.next = that else loop(x.next) } - loop(repr) + if (isEmpty) that + else { loop(repr); repr } } - def insert(that: This): Unit = if (that ne null) { - that.append(next) - next = that + /** Insert linked list `that` at current position of this linked list + * @pre this linked list is not empty + */ + def insert(that: This): Unit = { + require(nonEmpty, "insert into empty list") + if (that.nonEmpty) { + that.append(next) + next = that + } } override def drop(n: Int): This = { var i = 0 var these: This = repr - while (i < n && (these ne null)) { + while (i < n && !these.isEmpty) { these = these.next.asInstanceOf[This] // !!! concrete overrides abstract problem i += 1 } these } + private def atLocation[T](n: Int)(f: This => T) = { val loc = drop(n) - if (loc ne null) f(loc) + if (!loc.isEmpty) f(loc) else throw new IndexOutOfBoundsException(n.toString) } @@ -70,24 +86,24 @@ trait LinkedListLike[A, This >: Null <: Seq[A] with LinkedListLike[A, This]] ext def get(n: Int): Option[A] = { val loc = drop(n) - if (loc ne null) Some(loc.elem) + if (loc.nonEmpty) Some(loc.elem) else None } override def iterator: Iterator[A] = new Iterator[A] { var elems = self - def hasNext = (elems ne null) + def hasNext = elems.nonEmpty def next = { val res = elems.elem elems = elems.next - res; + res } } override def foreach[B](f: A => B) { var these = this - while (these ne null) { - f(these.elem); + while (these.nonEmpty) { + f(these.elem) these = these.next } } |