diff options
author | Matthias Zenger <mzenger@gmail.com> | 2004-05-03 20:17:00 +0000 |
---|---|---|
committer | Matthias Zenger <mzenger@gmail.com> | 2004-05-03 20:17:00 +0000 |
commit | 0c42b4a80bdd112f14925ef5d2e202575536e307 (patch) | |
tree | d3c40d6b0a35605c7fb38a706efc9570284478bb /sources/scala/collection/immutable/TreeSet.scala | |
parent | c7ce40c3c7041bdc018e4054e634f061537b15e2 (diff) | |
download | scala-0c42b4a80bdd112f14925ef5d2e202575536e307.tar.gz scala-0c42b4a80bdd112f14925ef5d2e202575536e307.tar.bz2 scala-0c42b4a80bdd112f14925ef5d2e202575536e307.zip |
Refactored library.
Diffstat (limited to 'sources/scala/collection/immutable/TreeSet.scala')
-rw-r--r-- | sources/scala/collection/immutable/TreeSet.scala | 108 |
1 files changed, 55 insertions, 53 deletions
diff --git a/sources/scala/collection/immutable/TreeSet.scala b/sources/scala/collection/immutable/TreeSet.scala index bf085990c2..ddcc7b27c2 100644 --- a/sources/scala/collection/immutable/TreeSet.scala +++ b/sources/scala/collection/immutable/TreeSet.scala @@ -1,66 +1,68 @@ -package scala.collection.immutable; +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ -/** A set that uses TreeMap. -*/ +package scala.collection.immutable; -class TreeSet[A](order: Order[A]) with Set[A] { - protected val map = new TreeMap[ A, boolean ]( order ); +/** This class implements immutable sets using a tree. + * + * @author Matthias Zenger + * @author Burak Emir + * @version 1.1, 03/05/2004 + */ +class TreeSet[A <% Ordered[A]] extends Tree[A, A] with Set[A] { - /** Returns the number of elements in this set. - * - * @return number of set elements. - */ - def size: Int = map.size; + type This = TreeSet[A]; - /** Checks if this set contains element <code>elem</code>. - * - * @param elem the element to check for membership. - * @return true, iff <code>elem</code> is contained in this set. - */ - def contains(elem: A): Boolean = map.get(elem) match { - case Some(_) => true; - case _ => false; - } + def entryKey(entry: A) = entry; - /** This method creates a new set with an additional element. - */ - def +(elem: A): TreeSet[A] = new TreeSet(order) { - override val map = TreeSet.this.map.update( elem, true ); - } + protected def New(sz: Int, t: aNode): This = new TreeSet[A] { + override def size = sz; + override protected val tree: aNode = t; + } - /** <code>-</code> can be used to remove a single element from - * a set. - */ - def -(elem: A): TreeSet[A] = new TreeSet(order) { - override val map = TreeSet.this.map - elem ; - } + /** Checks if this set contains element <code>elem</code>. + * + * @param elem the element to check for membership. + * @return true, iff <code>elem</code> is contained in this set. + */ + def contains(elem: A): Boolean = !findValue(elem).isEmpty; - /** Creates a new iterator over all elements contained in this - * object. - * - * @return the new iterator - */ - def elements: Iterator[A] = map.elements.map { - x:Pair[A,boolean] => x._1 - }; + /** This method creates a new set with an additional element. + */ + def +(elem: A): TreeSet[A] = update_or_add(elem, elem); - /** Transform this set into a list of all elements. - * - * @return a list which enumerates all elements of this set. - */ - override def toList: List[A] = elements.toList; + /** <code>-</code> can be used to remove a single element from + * a set. + */ + def -(elem: A): TreeSet[A] = delete_any(elem); - /** Compares two sets for equality. - * Two set are equal iff they contain the same elements. - */ - override def equals(obj: Any): Boolean = - if (obj.isInstanceOf[scala.collection.Set[A]]) { - val that = obj.asInstanceOf[scala.collection.Set[A]]; - if (size != that.size) false else toList.forall(that.contains); - } else - false; + /** Creates a new iterator over all elements contained in this + * object. + * + * @return the new iterator + */ + def elements: Iterator[A] = entries; - override def hashCode(): Int = map.hashCode(); + /** Transform this set into a list of all elements. + * + * @return a list which enumerates all elements of this set. + */ + override def toList: List[A] = tree.toList(scala.Nil); + /** Compares two sets for equality. + * Two set are equal iff they contain the same elements. + */ + override def equals(obj: Any): Boolean = + if (obj.isInstanceOf[scala.collection.Set[A]]) { + val that = obj.asInstanceOf[scala.collection.Set[A]]; + if (size != that.size) false else toList.forall(that.contains); + } else + false; } |