blob: f14ad566b0fa66e385a85e7284146b42cdfdf1ed (
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
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.collection
/** <p>
* A set is a collection that includes at most one of any object.
* </p>
* <p>
* This trait provides a limited interface, only allowing reading of elements.
* There are two extensions of this trait, in packages
* <code><a href="mutable$content.html" target="contentFrame">
* scala.collection.mutable</a></code>
* and <code><a href="immutable$content.html" target="contentFrame">
* scala.collection.immutable</a></code>, which provide functionality for
* adding and removing objects from the set. The trait in the first package is
* for sets that are modified destructively, whereas the trait in
* the second package is for immutable sets which create a new set
* when something is added or removed to them.
*
* @author Matthias Zenger
* @author Martin Odersky
* @version 2.0, 01/01/2007
*/
trait Set[A] extends (A => Boolean) with Collection[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 <code>true</code> 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 <code>true</code>, iff this set contains element
* <code>elem</code>.
*
* @param elem the element to check for membership.
* @return <code>true</code> iff <code>elem</code> is contained in
* this set.
*/
def apply(elem: A): Boolean = contains(elem)
/** Checks if this set is empty.
*
* @return <code>true</code> iff there is no element in the set.
*/
override def isEmpty: Boolean = size == 0
/** Checks if this set is a subset of set <code>that</code>.
*
* @param that another set.
* @return <code>true</code> iff the other set is a superset of
* this set.
* todo: rename to isSubsetOf
*/
def subsetOf(that: Set[A]): Boolean = forall(that.contains)
/** set intersection */
def *(that : Set[A]) : Set[A] = {
val min = Math.min(size, that.size)
val buf = new Array[A](min)
var count = 0
val i = elements
while (i.hasNext) {
val a = i.next
if (that.contains(a)) {
buf(count) = a
count += 1
}
}
if (count == size) this
else if (count == that.size) that
else {
import scala.collection.jcl.LinkedHashSet
val ret = new LinkedHashSet[A]
ret ++= buf.projection.take(count)
ret.readOnly
}
}
/** 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
* @note not necessarily run-time type safe.
* @return <code>true</code> iff this set and the other set
* contain the same elements.
*/
override def equals(that: Any): Boolean = that match {
case other: Set[_] =>
this.size == other.size && subsetOf(other.asInstanceOf[Set[A]])
case _ =>
false
}
override def hashCode() =
(0 /: this)((hash, e) => hash + e.hashCode())
override def toArray[B >: A]: Array[B] = {
val result = new Array[B](size)
copyToArray(result, 0)
result
}
}
|