summaryrefslogtreecommitdiff
path: root/sources/scala/collection/immutable/TreeSet.scala
blob: bb3099e007f4fb88263ab813e2506da65c083bb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2005, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$

package scala.collection.immutable;


/** This class implements immutable sets using a tree.
 *
 *  @author  Matthias Zenger
 *  @author  Burak Emir
 *  @version 1.1, 03/05/2004
 */
//[serializable]
class TreeSet[A <% Ordered[A]]() extends Tree[A, A] with Set[A] with java.io.Serializable {

    override protected type This = TreeSet[A];
    override protected def getThis: This = this;

    protected def New(sz: Int, t: aNode): This = new TreeSet[A] {
        override def size = sz;
        override protected def tree: aNode = t;
    }

    /** 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;

    /** This method creates a new set with an additional element.
     */
    def +(elem: A): TreeSet[A] = updateOrAdd(elem, elem);

    /** <code>-</code> can be used to remove a single element from
     *  a set.
     */
    def -(elem: A): TreeSet[A] = deleteAny(elem);

    /** Creates a new iterator over all elements contained in this
     *  object.
     *
     *  @return the new iterator
     */
    def elements: Iterator[A] = entries;

    /** 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) map (._2);

    /** Compares two sets for equality.
     *  Two set are equal iff they contain the same elements.
     */
    override def equals(obj: Any): Boolean =
        obj.isInstanceOf[scala.collection.Set[A]] && {
          val that = obj.asInstanceOf[scala.collection.Set[A]];
          (size == that.size) && toList.forall(that.contains);
        }
}