summaryrefslogtreecommitdiff
path: root/sources/scala/collection/immutable/TreeSet.scala
diff options
context:
space:
mode:
authorburaq <buraq@epfl.ch>2004-04-22 12:29:27 +0000
committerburaq <buraq@epfl.ch>2004-04-22 12:29:27 +0000
commit13000c076c61a749ff9cae4669861b5772198c85 (patch)
tree44149703bbf939efbc23f34ac8d6b25d57c2c626 /sources/scala/collection/immutable/TreeSet.scala
parentd8631cf668faf783bb24e48cfcaeb35a05abbfdd (diff)
downloadscala-13000c076c61a749ff9cae4669861b5772198c85.tar.gz
scala-13000c076c61a749ff9cae4669861b5772198c85.tar.bz2
scala-13000c076c61a749ff9cae4669861b5772198c85.zip
initial
Diffstat (limited to 'sources/scala/collection/immutable/TreeSet.scala')
-rw-r--r--sources/scala/collection/immutable/TreeSet.scala66
1 files changed, 66 insertions, 0 deletions
diff --git a/sources/scala/collection/immutable/TreeSet.scala b/sources/scala/collection/immutable/TreeSet.scala
new file mode 100644
index 0000000000..bf085990c2
--- /dev/null
+++ b/sources/scala/collection/immutable/TreeSet.scala
@@ -0,0 +1,66 @@
+package scala.collection.immutable;
+
+/** A set that uses TreeMap.
+*/
+
+class TreeSet[A](order: Order[A]) with Set[A] {
+
+ protected val map = new TreeMap[ A, boolean ]( order );
+
+ /** Returns the number of elements in this set.
+ *
+ * @return number of set elements.
+ */
+ def size: Int = map.size;
+
+ /** 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;
+ }
+
+ /** 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 );
+ }
+
+ /** <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 ;
+ }
+
+ /** 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
+ };
+
+ /** 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;
+
+ /** 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;
+
+ override def hashCode(): Int = map.hashCode();
+
+}