diff options
Diffstat (limited to 'src')
4 files changed, 23 insertions, 26 deletions
diff --git a/src/library/scala/collection/mutable/DoubleLinkedList.scala b/src/library/scala/collection/mutable/DoubleLinkedList.scala index f2a732ffd1..4465ebef93 100644 --- a/src/library/scala/collection/mutable/DoubleLinkedList.scala +++ b/src/library/scala/collection/mutable/DoubleLinkedList.scala @@ -68,24 +68,22 @@ class DoubleLinkedList[A]() extends LinearSeq[A] object DoubleLinkedList extends SeqFactory[DoubleLinkedList] { /** $genericCanBuildFrom */ implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, DoubleLinkedList[A]] = new GenericCanBuildFrom[A] + def newBuilder[A]: Builder[A, DoubleLinkedList[A]] = new Builder[A, DoubleLinkedList[A]] { - var current: DoubleLinkedList[A] = _ - val emptyList = new DoubleLinkedList[A]() - if(null == current) - current = emptyList + def emptyList() = new DoubleLinkedList[A]() + var current = emptyList() def +=(elem: A): this.type = { - if (current.nonEmpty) - current.insert(new DoubleLinkedList(elem, emptyList)) + if (current.isEmpty) + current = new DoubleLinkedList(elem, emptyList()) else - current = new DoubleLinkedList(elem, emptyList) + current append new DoubleLinkedList(elem, emptyList()) + this } - def clear() { - current = emptyList - } + def clear(): Unit = current = emptyList() def result() = current } } diff --git a/src/library/scala/collection/mutable/DoubleLinkedListLike.scala b/src/library/scala/collection/mutable/DoubleLinkedListLike.scala index 18a0e164a2..7ad2f9558f 100644 --- a/src/library/scala/collection/mutable/DoubleLinkedListLike.scala +++ b/src/library/scala/collection/mutable/DoubleLinkedListLike.scala @@ -110,12 +110,9 @@ trait DoubleLinkedListLike[A, This <: Seq[A] with DoubleLinkedListLike[A, This]] private def outofbounds(n: Int) = throw new IndexOutOfBoundsException(n.toString) - override def drop(n: Int): This = super[SeqLike].drop(n) - - override def tail = drop(1) - - 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) - + override def drop(n: Int): This = super[SeqLike].drop(n) + override def tail = drop(1) + 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) } diff --git a/src/library/scala/collection/mutable/LinkedList.scala b/src/library/scala/collection/mutable/LinkedList.scala index beee4857b0..6c5bf9ae49 100644 --- a/src/library/scala/collection/mutable/LinkedList.scala +++ b/src/library/scala/collection/mutable/LinkedList.scala @@ -60,10 +60,9 @@ class LinkedList[A]() extends LinearSeq[A] * @define coll linked list */ object LinkedList extends SeqFactory[LinkedList] { - override def empty[A]: LinkedList[A] = new LinkedList[A] - implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, LinkedList[A]] = new GenericCanBuildFrom[A] + def newBuilder[A]: Builder[A, LinkedList[A]] = (new MutableList) mapResult ((l: MutableList[A]) => l.toLinkedList) } diff --git a/src/library/scala/collection/mutable/LinkedListLike.scala b/src/library/scala/collection/mutable/LinkedListLike.scala index b24cda946e..a3fd786c2a 100644 --- a/src/library/scala/collection/mutable/LinkedListLike.scala +++ b/src/library/scala/collection/mutable/LinkedListLike.scala @@ -60,12 +60,14 @@ trait LinkedListLike[A, This <: Seq[A] with LinkedListLike[A, This]] extends Seq var next: This = _ override def isEmpty = next eq this - override def length: Int = length0(repr, 0) - @tailrec private def length0(elem: This, acc: Int): Int = if (elem.isEmpty) acc else length0(elem.next, acc + 1) + @tailrec private def length0(elem: This, acc: Int): Int = + if (elem.isEmpty) acc else length0(elem.next, acc + 1) - override def head: A = elem + override def head: A = + if (isEmpty) throw new NoSuchElementException + else elem override def tail: This = { require(nonEmpty, "tail of empty list") @@ -92,7 +94,8 @@ trait LinkedListLike[A, This <: Seq[A] with LinkedListLike[A, This]] extends Seq def insert(that: This): Unit = { require(nonEmpty, "insert into empty list") if (that.nonEmpty) { - next = next.append(that) + that append next + next = that } } @@ -100,7 +103,7 @@ trait LinkedListLike[A, This <: Seq[A] with LinkedListLike[A, This]] extends Seq var i = 0 var these: This = repr while (i < n && !these.isEmpty) { - these = these.next.asInstanceOf[This] // !!! concrete overrides abstract problem + these = these.next i += 1 } these @@ -108,7 +111,7 @@ trait LinkedListLike[A, This <: Seq[A] with LinkedListLike[A, This]] extends Seq private def atLocation[T](n: Int)(f: This => T) = { val loc = drop(n) - if (!loc.isEmpty) f(loc) + if (loc.nonEmpty) f(loc) else throw new IndexOutOfBoundsException(n.toString) } |