From 90714e7d4a03a33aa26f757217972e306b96c9d0 Mon Sep 17 00:00:00 2001 From: Ruediger Klaehn Date: Thu, 9 Aug 2012 03:41:08 +0200 Subject: Renamed elem to head and next to tail both head and tail are now O(1) with very low overhead like you would expect for a list-like data structure --- src/library/scala/collection/immutable/ListSet.scala | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/library/scala/collection/immutable/ListSet.scala b/src/library/scala/collection/immutable/ListSet.scala index ce3abaacb7..4dd0d62fc0 100644 --- a/src/library/scala/collection/immutable/ListSet.scala +++ b/src/library/scala/collection/immutable/ListSet.scala @@ -116,8 +116,8 @@ class ListSet[A] extends AbstractSet[A] def hasNext = that.nonEmpty def next: A = if (hasNext) { - val res = that.elem - that = that.next + val res = that.head + that = that.tail res } else Iterator.empty.next @@ -126,18 +126,18 @@ class ListSet[A] extends AbstractSet[A] /** * @throws Predef.NoSuchElementException */ - protected def elem: A = throw new NoSuchElementException("Set has no elements"); + override def head: A = throw new NoSuchElementException("Set has no elements"); /** * @throws Predef.NoSuchElementException */ - protected def next: ListSet[A] = throw new NoSuchElementException("Next of an empty set"); + override def tail: ListSet[A] = throw new NoSuchElementException("Next of an empty set"); override def stringPrefix = "ListSet" /** Represents an entry in the `ListSet`. */ - protected class Node(override protected val elem: A) extends ListSet[A] with Serializable { + protected class Node(override val head: A) extends ListSet[A] with Serializable { override private[ListSet] def unchecked_outer = self /** Returns the number of elements in this set. @@ -162,7 +162,7 @@ class ListSet[A] extends AbstractSet[A] */ override def contains(e: A) = containsInternal(this, e) @tailrec private def containsInternal(n: ListSet[A], e: A): Boolean = - !n.isEmpty && (n.elem == e || containsInternal(n.unchecked_outer, e)) + !n.isEmpty && (n.head == e || containsInternal(n.unchecked_outer, e)) /** This method creates a new set with an additional element. */ @@ -170,10 +170,10 @@ class ListSet[A] extends AbstractSet[A] /** `-` can be used to remove a single element from a set. */ - override def -(e: A): ListSet[A] = if (e == elem) self else { - val tail = self - e; new tail.Node(elem) + override def -(e: A): ListSet[A] = if (e == head) self else { + val tail = self - e; new tail.Node(head) } - override protected def next: ListSet[A] = self + override def tail: ListSet[A] = self } } -- cgit v1.2.3