summaryrefslogtreecommitdiff
path: root/src/library/scalax/collection/mutable/Set.scala
blob: 78f3234336a19562b50b75f5e38cdbff0f194c85 (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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id: Set.scala 16893 2009-01-13 13:09:22Z cunei $


package scalax.collection.mutable

import collection.generic._

/** The canonical factory methods for <a href="Set.html">mutable sets</a>.
 *  Currently these return <a href="HashSet.html">HashSet's</a>.
 */
object Set extends generic.SetFactory[Set] {
  /** The empty map of this type; this is implemented as a hashtable */
  def empty[A]: Set[A] = new HashSet[A]
}

/** 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
 *  @author Martin Odersky
 *  @version 2.8
 */
trait Set[A] extends collection.Set[A]
                with SetTemplate[Set, A]
                with Addable[Set[A], A]
                with Subtractable[Set[A], A]
                with Growable[A]
                with Shrinkable[A]
                with Cloneable[Set[A]] {
self =>

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

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

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

  /** Adds a new element to the set and returns the set itself.
   *
   *  @param elem the element to be added
   */
  def +(elem: A): this.type = { +=(elem); this }

  /** Removed a new element from the set and returns the set itself.
   *
   *  @param elem the element to be added
   */
  def -(elem: A): this.type = { -=(elem); this }

  /** 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 = foreach (elem => if (!p(elem)) -=(elem))

  /** Removes all elements from the set. After this operation is completed,
   *  the set will be empty.
   */
  def clear() { foreach(-=) }

  override def clone(): Set[A] = { val b = newBuilder[A]; b ++= this; b.result }

  /** 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 read-only projection of this set !!! just us an (immutable) setProxy? */
  def readOnly : collection.Set[A] = new collection.Set[A] {
    def contains(item : A) = Set.this.contains(item)
    override def size = self.size
    override def +(elem: A) = self + elem
    override def -(elem: A) = self - elem
    override def elements = self.elements
    override def foreach(f: A => Unit) = self.foreach(f)
    override def newBuilder[B]: Builder[collection.Set, B] = self.newBuilder[B]
  }
}