summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/Set.scala
blob: 99d79685677f6e7dd44d701fc172f8a86c4221e2 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2006, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.collection;


/** This class defines the interface of collections that do not contain
 *  duplicate elements. Class <code>Set</code> may only be used for
 *  accessing elements from set implementations. Two different extensions
 *  of class <code>Set</code> in the package <code>scala.collections.mutable</code>
 *  and  <code>scala.collections.immutable</code> provide functionality for
 *  adding new elements to a set. The class in the first package is implemented
 *  by sets the are modified destructively, whereas the class in the second
 *  package is used by functional set implementations that rely on immutable
 *  data structures.
 *
 *  @author  Matthias Zenger
 *  @version 1.0, 08/07/2003
 */
mixin class Set[A] extends AnyRef with Function1[A, Boolean] with Iterable[A] {

    /** Returns the number of elements in this set.
    *
    *  @return number of set elements.
    */
    def size: Int;

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

    /** This method allows sets to be interpreted as predicates.
     *  It returns true, iff 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 apply(elem: A): Boolean = contains(elem);

    /** Checks if this set is empty.
     *
     *  @return true, iff there is no element in the set.
     */
    def isEmpty: Boolean = (size == 0);

    /** Checks if this set is a subset of set <code>that</code>.
     *
     *  @param  that another set.
     *  @return true, iff the other set is a superset of this set.
     */
    def subsetOf(that: Set[A]): Boolean = forall(that.contains);

    /** Compares this set with another object and returns true, iff the
     *  other object is also a set which contains the same elements as
     *  this set.
     *
     *  @param  that  the other object
     *  @return true, iff this set and the other set contain the same
     *          elements.
     */
    override def equals(that: Any): Boolean = that match {
      case other: Set[A] =>
        this.size == other.size && this.elements.forall(other.contains)
      case _ =>
        false
    }

    /** Returns the elements of this set as a list.
     *
     *  @return    a list containing all set elements.
     */
    def toList: List[A] = elements.toList;

    /** Returns a string representation of this set.
     *
     *  @return a string showing all elements of this set.
     */
    override def toString(): String =
      if (isEmpty) "{}" else toList.mkString("{", ", ", "}");
}