summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/immutable/HashSet.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2009-05-08 16:33:15 +0000
committerMartin Odersky <odersky@gmail.com>2009-05-08 16:33:15 +0000
commit14a631a5fec42d04d0723355a0b93e482b5e4662 (patch)
treef639c2a22e89e193b9abea391993ecfd4d5326ee /src/library/scala/collection/immutable/HashSet.scala
parent2379eb4ebbd28c8892b50a1d9fa8a687099eea4d (diff)
downloadscala-14a631a5fec42d04d0723355a0b93e482b5e4662.tar.gz
scala-14a631a5fec42d04d0723355a0b93e482b5e4662.tar.bz2
scala-14a631a5fec42d04d0723355a0b93e482b5e4662.zip
massive new collections checkin.
Diffstat (limited to 'src/library/scala/collection/immutable/HashSet.scala')
-rw-r--r--src/library/scala/collection/immutable/HashSet.scala64
1 files changed, 33 insertions, 31 deletions
diff --git a/src/library/scala/collection/immutable/HashSet.scala b/src/library/scala/collection/immutable/HashSet.scala
index 11573e17ea..d5ee878c76 100644
--- a/src/library/scala/collection/immutable/HashSet.scala
+++ b/src/library/scala/collection/immutable/HashSet.scala
@@ -10,39 +10,30 @@
package scala.collection.immutable
-/** The canonical factory methods for <a href="HashSet.html">immutable HashSet's<la>.
- *
- * @author Martin Odersky
- * @version 2.0, 19/01/2007
- */
-object HashSet {
-
- /** The empty set of this type.
- */
- def empty[A] = new HashSet[A]
+import generic._
+
+/** This class implements immutable sets using a hash table.
+ * It is optimized for sequential accesses where the last updated table is accessed most often.
+ * It supports with reasonable efficiency accesses to previous versions of the table by keeping
+ * a change log that's regularly compacted.
+ * It needs to synchronize most methods, so it is less suitable for highly concurrent accesses.
+ *
+ * @note the builder of a hash set returns specialized representations EmptySet,Set1,..., Set4
+ * for sets of size <= 4.
+ *
+ * @author Martin Odersky
+ * @version 2.8
+ */
+@serializable
+class HashSet[A] extends Set[A] with SetTemplate[A, HashSet[A]] with mutable.FlatHashTable[A] {
- /** The canonical factory for this type
- */
- def apply[A](elems: A*) = empty[A] ++ elems
-}
+ override def empty = HashSet.empty
+ override def traversibleBuilder[B]: Builder[B, HashSet[B], Any] = HashSet.newBuilder[B]
-/** This class implements immutable maps/sets using a hash table.
- * It is optimized for sequential accesses where the last updated table is accessed most often.
- * It supports with reasonable efficiency accesses to previous versions of the table by keeping
- * a change log that's regularly compacted.
- * It needs to synchronize most methods, so it is less suitable for highly concurrent accesses.
- *
- * @author Martin Odersky
- * @version 2.0, 19/01/2007
- */
-@serializable
-class HashSet[A] extends Set[A] with mutable.FlatHashTable[A] {
protected var later: HashSet[A] = null
protected var changedElem: A = _
protected var deleted: Boolean = _
- def empty[C]: Set[C] = new EmptySet[C]
-
def contains(elem: A): Boolean = synchronized {
var m = this
var cnt = 0
@@ -55,7 +46,7 @@ class HashSet[A] extends Set[A] with mutable.FlatHashTable[A] {
m.containsEntry(elem)
}
- def + (elem: A): Set[A] = synchronized {
+ def + (elem: A): HashSet[A] = synchronized {
makeCopyIfUpdated()
if (containsEntry(elem)) this
else {
@@ -65,7 +56,7 @@ class HashSet[A] extends Set[A] with mutable.FlatHashTable[A] {
}
}
- def - (elem: A): Set[A] = synchronized {
+ def - (elem: A): HashSet[A] = synchronized {
makeCopyIfUpdated()
if (!containsEntry(elem)) this
else {
@@ -119,8 +110,8 @@ class HashSet[A] extends Set[A] with mutable.FlatHashTable[A] {
else removeEntry(m.changedElem)
}
}
- table = new Array[AnyRef](last.table.length)
- Array.copy(last.table, 0, table, 0, table.length)
+ table = new scala.Array[AnyRef](last.table.length)
+ scala.Array.copy(last.table, 0, table, 0, table.length)
tableSize = last.tableSize
threshold = last.threshold
undo(this)
@@ -134,3 +125,14 @@ class HashSet[A] extends Set[A] with mutable.FlatHashTable[A] {
}
}
+/** A factory object for immutable HashSets
+ *
+ * @author Martin Odersky
+ * @version 2.8
+ */
+object HashSet extends SetFactory[HashSet] {
+ type Coll = HashSet[_]
+ implicit def builderFactory[A]: BuilderFactory[A, HashSet[A], Coll] = new BuilderFactory[A, HashSet[A], Coll] { def apply(from: Coll) = from.traversibleBuilder[A] }
+ def empty[A]: HashSet[A] = new HashSet
+}
+