summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/mutable/Set.scala
blob: 7bc6be929250483834bd3adb49455be510b41808 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.collection.mutable

import Predef._


/** The canonical factory methods for <a href="Set.html">mutable sets</a>.
 *  Currently these return <a href="HashSet.html">HashSet's</a>.
 */
object Set {

  /** The empty map of this type; this is implemented as a hashtable */
  def empty[A]: Set[A] = new HashSet[A]

  /** The canonical factory for this type
   */
  def apply[A](elems: A*) = empty[A] ++ elems
}

/** This class represents mutable sets. Concrete set implementations
 *  just have to provide functionality for the abstract methods in
 *  <a href="../Set.html" target="contentFrame">
 *  <code>scala.collection.Set</code></a> as well as for <code>+=</code>,
 *  <code>-= and <code>clear</code>.
 *
 *  @author  Matthias Zenger
 *  @version 1.1, 09/05/2004
 */
@cloneable
trait Set[A] extends collection.Set[A] with Scriptable[Message[A]] with CloneableCollection {

  /** 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) {
    if (included) this += elem else this -= elem
  }

  /** Add a new element to the set.
   *
   *  @param elem the element to be added
   */
  def +=(elem: A)

  /** Add two or more elements to this set.
   *  @param    elem1 the first element.
   *  @param    elem2 the second element.
   *  @param    elems the remaining elements.
   */
  def += (elem1: A, elem2: A, elems: A*) { this += elem1; this += elem2; this ++= elems }

  /** Add all the elements provided by an iterator
   *  of the iterable object <code>that</code> to the set.
   *  @param elems  the iterable object containing the elements to be added
   */
  def ++=(elems: Iterable[A]): Unit = ++=(elems.elements)

  /** Add all the elements provided by an iterator
   *  <code>elems</code> to the set.
   *  @param elems  the iterator containing the elements to be added
   */
  def ++=(elems: Iterator[A]) { elems foreach += }

  /** Add a new element to the set.
   *  @return the set itself with the element added.
   *
   *  @param elem the element to be added
   */
  def + (elem: A): Set[A] = { +=(elem); this }

  /** Add two or more elements to this set.
   *  @param    elem1 the first element.
   *  @param    kv2 the second element.
   *  @param    kvs the remaining elements.
   *  @return the set itself with the elements added.
   */
  def + (elem1: A, elem2: A, elems: A*): Set[A] = { this.+=(elem1, elem2, elems: _*); this }

  /** Add all the elements provided by an iterator
   *  of the iterable object <code>elems</code> to the set.
   *
   *  @param elems  the iterable object containing the elements to be added
   *  @return the set itself with the elements added.
   */
  def ++ (elems: Iterable[A]): Set[A] = { this ++= elems; this }

  /** Add all the elements provided by an iterator
   *  <code>elems</code> to the set.
   *  @param elems  the iterator containing the elements to be added
   */
  def ++ (elems: Iterator[A]): Set[A] = { this ++= elems; this }

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

  /** Removes a single element from a set.
   *  @param elem  The element to be removed.
   */
  def -=(elem: A)

  /** Remove two or more elements from this set.
   *  @param    elem1 the first element.
   *  @param    elem2 the second element.
   *  @param    elems the remaining elements.
   */
  def -= (elem1: A, elem2: A, elems: A*) { this -= elem1; this -= elem2; this --= elems }

  /** Remove all the elements provided by an iterator
   *  of the iterable object <code>elems</code> from the set.
   */
  def --=(elems: Iterable[A]): Unit = --=(elems.elements)

  /** Remove all the elements provided by an iterator
   *  <code>elems</code> from the set.
   */
  def --=(elems: Iterator[A]) { elems foreach -= }

  /** Remove a new element from the set.
   *  @return the set itself with the element removed.
   *
   *  @param elem the element to be removed
   */
  def - (elem: A): Set[A] = { -=(elem); this }

  /** Remove two or more elements from this set.
   *  @param    elem1 the first element.
   *  @param    elem2 the second element.
   *  @param    elems the remaining elements.
   *  @return the set itself with the elements removed.
   */
  def - (elem1: A, elem2: A, elems: A*): Set[A] = { this.-=(elem1, elem2, elems: _*); this }

  /** Remove all the elements provided by an iterator
   *  of the iterable object <code>elems</code> from the set.
   *
   *  @param elems An iterable object containing the elements to remove from the set.
   *  @return the set itself with the elements removed.
   */
  def -- (elems: Iterable[A]): Set[A] = { this --= elems; this }

  /** Remove all the elements provided by an iterator
   *  <code>elems</code> from the set.
   *  @param elems An iterator containing the elements to remove from the set.
   *  @return the set itself with the elements removed.
   */
  def -- (elems: Iterator[A]): Set[A] = { this --= elems; this }

  /** <code>excl</code> removes many elements from the set.
   */
  @deprecated
  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>.
   *
   *  @param that the set to intersect with.
   */
  def intersect(that: Set[A]) { retain(that.contains) }

  /** Method <code>retain removes all elements from the set for
   *  which the predicate <code>p</code> yields the value <code>false</code>.
   */
  def retain(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() { elements foreach -= }

  /** Send a message to this scriptable object.
   *
   *  @param cmd  the message to send.
   *  @throws <code>Predef.UnsupportedOperationException</code>
   *  if the message was not understood.
   */
  def <<(cmd: Message[A]): Unit = cmd match {
    case Include(elem) => this += elem
    case Remove(elem) => this -= elem
    case Reset() => clear
    case s: Script[_] => s.elements foreach <<
    case _ => throw new UnsupportedOperationException("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]]

  /** Return a read-only projection of this set */
  def readOnly : scala.collection.Set[A] = new scala.collection.Set[A] {
    def contains(item : A) = Set.this.contains(item)
    override def toString = "ro-" + Set.this.toString
    override def size = Set.this.size
    override def elements = Set.this.elements
  }
}