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
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.collection
package immutable
import generic._
/** A generic trait for immutable sets.
*
* $setnote
*
* @author Matthias Zenger
* @author Martin Odersky
* @version 2.8
* @since 1
* @define Coll immutable.Set
* @define coll immutable set
*/
trait Set[A] extends Iterable[A]
with scala.collection.Set[A]
with GenericSetTemplate[A, Set]
with SetLike[A, Set[A]] {
override def companion: GenericCompanion[Set] = Set
}
/** $factoryInfo
* @define Coll immutable.Set
* @define coll immutable set
*/
object Set extends ImmutableSetFactory[Set] {
/** $setCanBuildFromInfo */
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, Set[A]] = setCanBuildFrom[A]
override def empty[A]: Set[A] = EmptySet.asInstanceOf[Set[A]]
private val hashSeed = "Set".hashCode
/** An optimized representation for immutable empty sets */
@serializable
private object EmptySet extends Set[Any] {
override def size: Int = 0
def contains(elem: Any): Boolean = false
def + (elem: Any): Set[Any] = new Set1(elem)
def - (elem: Any): Set[Any] = this
def iterator: Iterator[Any] = Iterator.empty
override def foreach[U](f: Any => U): Unit = {}
}
@serializable @deprecated("use `Set.empty' instead")
class EmptySet[A] extends Set[A] {
override def size: Int = 0
def contains(elem: A): Boolean = false
def + (elem: A): Set[A] = new Set1(elem)
def - (elem: A): Set[A] = this
def iterator: Iterator[A] = Iterator.empty
override def foreach[U](f: A => U): Unit = {}
}
/** An optimized representation for immutable sets of size 1 */
@serializable @SerialVersionUID(1233385750652442003L)
class Set1[A](elem1: A) extends Set[A] {
override def size: Int = 1
def contains(elem: A): Boolean =
elem == elem1
def + (elem: A): Set[A] =
if (contains(elem)) this
else new Set2(elem1, elem)
def - (elem: A): Set[A] =
if (elem == elem1) Set.empty
else this
def iterator: Iterator[A] =
Iterator(elem1)
override def foreach[U](f: A => U): Unit = {
f(elem1)
}
}
/** An optimized representation for immutable sets of size 2 */
@serializable @SerialVersionUID(-6443011234944830092L)
class Set2[A](elem1: A, elem2: A) extends Set[A] {
override def size: Int = 2
def contains(elem: A): Boolean =
elem == elem1 || elem == elem2
def + (elem: A): Set[A] =
if (contains(elem)) this
else new Set3(elem1, elem2, elem)
def - (elem: A): Set[A] =
if (elem == elem1) new Set1(elem2)
else if (elem == elem2) new Set1(elem1)
else this
def iterator: Iterator[A] =
Iterator(elem1, elem2)
override def foreach[U](f: A => U): Unit = {
f(elem1); f(elem2)
}
}
/** An optimized representation for immutable sets of size 3 */
@serializable @SerialVersionUID(-3590273538119220064L)
class Set3[A](elem1: A, elem2: A, elem3: A) extends Set[A] {
override def size: Int = 3
def contains(elem: A): Boolean =
elem == elem1 || elem == elem2 || elem == elem3
def + (elem: A): Set[A] =
if (contains(elem)) this
else new Set4(elem1, elem2, elem3, elem)
def - (elem: A): Set[A] =
if (elem == elem1) new Set2(elem2, elem3)
else if (elem == elem2) new Set2(elem1, elem3)
else if (elem == elem3) new Set2(elem1, elem2)
else this
def iterator: Iterator[A] =
Iterator(elem1, elem2, elem3)
override def foreach[U](f: A => U): Unit = {
f(elem1); f(elem2); f(elem3)
}
}
/** An optimized representation for immutable sets of size 4 */
@serializable @SerialVersionUID(-3622399588156184395L)
class Set4[A](elem1: A, elem2: A, elem3: A, elem4: A) extends Set[A] {
override def size: Int = 4
def contains(elem: A): Boolean =
elem == elem1 || elem == elem2 || elem == elem3 || elem == elem4
def + (elem: A): Set[A] =
if (contains(elem)) this
else new HashSet[A] + (elem1, elem2, elem3, elem4, elem)
def - (elem: A): Set[A] =
if (elem == elem1) new Set3(elem2, elem3, elem4)
else if (elem == elem2) new Set3(elem1, elem3, elem4)
else if (elem == elem3) new Set3(elem1, elem2, elem4)
else if (elem == elem4) new Set3(elem1, elem2, elem3)
else this
def iterator: Iterator[A] =
Iterator(elem1, elem2, elem3, elem4)
override def foreach[U](f: A => U): Unit = {
f(elem1); f(elem2); f(elem3); f(elem4)
}
}
}
|