summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/mutable/Set.scala
blob: 0bb13bb7aa0125dc52f12282435c4cfc84d90b7f (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2006, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.collection.mutable;


/** This class represents mutable sets. Concrete set implementations
 *  just have to provide functionality for the abstract methods in
 *  <code>scala.collection.Set</code> as well as for <code>add</code>,
 *  <code>remove</code>, and <code>clear</code>.
 *
 *  @author  Matthias Zenger
 *  @version 1.1, 09/05/2004
 */
[cloneable]
trait Set[A] extends AnyRef with collection.Set[A]
      with Scriptable[Message[A]]
{

    /** This method allows one to add or remove an element <code>elem</code>
     *  from this set depending on the value of parameter <code>included</code>.
     *  Typically, one would use the following syntax:
     *  <pre>set(elem) = true</pre>
     */
    def update(elem: A, included: Boolean): Unit =
        if (included) +=(elem) else -=(elem);

    /** This method adds a new element to the set.
     */
    def +=(elem: A): Unit;

    /** This method will add all the elements provided by an iterator
     *  of the iterable object <code>that</code> to the set.
     */
    def ++=(that: Iterable[A]): Unit = ++=(that.elements);

    /** This method will add all the elements provided by an iterator
     *  of the iterable object <code>that</code> to the set.
     */
    def ++=(it: Iterator[A]): Unit = it foreach +=;

    /** <code>incl</code> can be used to add many elements to the set
     *  at the same time.
     */
    def incl(elems: A*): Unit = ++=(elems.elements);

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

    /** This method removes all the elements provided by the
     *  the iterable object <code>that</code> from the set.
     */
    def --=(that: Iterable[A]): Unit = --=(that.elements);

    /** This method removes all the elements provided by an iterator
     *  <code>it</code> from the set.
     */
    def --=(it: Iterator[A]): Unit = it foreach -=;

    /** <code>excl</code> removes many elements from the set.
     */
    def excl(elems: A*): Unit = --=(elems.elements);

    /** This method computes an intersection with set <code>that</code>.
     *  It removes all the elements that are not present in <code>that</code>.
     */
    def intersect(that: Set[A]): Unit = filter(that.contains);

    /** Method <code>filter</code> removes all elements from the set for
     *  which the predicate <code>p</code> yields the value <code>false</code>.
     */
    def filter(p: A => Boolean): Unit = toList.foreach {
        elem => if (!p(elem)) -=(elem);
    }

    /** Removes all elements from the set. After this operation is completed,
     *  the set will be empty.
     */
    def clear: Unit;

    /** Send a message to this scriptable object.
     *
     *  @param cmd  the message to send.
     */
    def <<(cmd: Message[A]): Unit = cmd match {
        case Include(elem) => this += elem;
        case Remove(elem) => this -= elem;
        case Reset() => clear;
        case s: Script[A] => s.elements foreach <<;
        case _ => error("message " + cmd + " not understood");
    }

    /** Return a clone of this set.
     *
     *  @return  a set with the same elements.
     */
    override def clone(): Set[A] = super.clone().asInstanceOf[Set[A]];

    /** The hashCode method always yields an error, since it is not
     *  safe to use mutable stacks as keys in hash tables.
     *
     *  @return never.
     */
    override def hashCode(): Int = error("unsuitable as hash key");
}