summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/GenSetLike.scala
diff options
context:
space:
mode:
authorAleksandar Pokopec <aleksandar.prokopec@epfl.ch>2011-04-13 16:31:42 +0000
committerAleksandar Pokopec <aleksandar.prokopec@epfl.ch>2011-04-13 16:31:42 +0000
commit3de96153e5bfbde16dcc89bfbd71ff6e8cf1f6c6 (patch)
tree2794a7bd176b315a9f4bdc3f5ef5553254b7dd47 /src/library/scala/collection/GenSetLike.scala
parent9b5cb18dbd2d3e87def5da47ae76adb2e776487e (diff)
downloadscala-3de96153e5bfbde16dcc89bfbd71ff6e8cf1f6c6.tar.gz
scala-3de96153e5bfbde16dcc89bfbd71ff6e8cf1f6c6.tar.bz2
scala-3de96153e5bfbde16dcc89bfbd71ff6e8cf1f6c6.zip
Refactoring the collections api to support diff...
Refactoring the collections api to support differentiation between referring to a sequential collection and a parallel collection, and to support referring to both types of collections. New set of traits Gen* are now superclasses of both their * and Par* subclasses. For example, GenIterable is a superclass of both Iterable and ParIterable. Iterable and ParIterable are not in a subclassing relation. The new class hierarchy is illustrated below (simplified, not all relations and classes are shown): TraversableOnce --> GenTraversableOnce ^ ^ | | Traversable --> GenTraversable ^ ^ | | Iterable --> GenIterable <-- ParIterable ^ ^ ^ | | | Seq --> GenSeq <-- ParSeq (the *Like, *View and *ViewLike traits have a similar hierarchy) General views extract common view functionality from parallel and sequential collections. This design also allows for more flexible extensions to the collections framework. It also allows slowly factoring out common functionality up into Gen* traits. From now on, it is possible to write this: import collection._ val p = parallel.ParSeq(1, 2, 3) val g: GenSeq[Int] = p // meaning a General Sequence val s = g.seq // type of s is Seq[Int] for (elem <- g) { // do something without guarantees on sequentiality of foreach // this foreach may be executed in parallel } for (elem <- s) { // do something with a guarantee that foreach is executed in order, sequentially } for (elem <- p) { // do something concurrently, in parallel } This also means that some signatures had to be changed. For example, method `flatMap` now takes `A => GenTraversableOnce[B]`, and `zip` takes a `GenIterable[B]`. Also, there are mutable & immutable Gen* trait variants. They have generic companion functionality.
Diffstat (limited to 'src/library/scala/collection/GenSetLike.scala')
-rw-r--r--src/library/scala/collection/GenSetLike.scala123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/library/scala/collection/GenSetLike.scala b/src/library/scala/collection/GenSetLike.scala
new file mode 100644
index 0000000000..91e90c1499
--- /dev/null
+++ b/src/library/scala/collection/GenSetLike.scala
@@ -0,0 +1,123 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+
+package scala.collection
+
+
+
+
+/** A template trait for sets which may possibly
+ * have their operations implemented in parallel.
+ *
+ * @author Martin Odersky
+ * @author Aleksandar Prokopec
+ * @since 2.9
+ */
+trait GenSetLike[A, +Repr] extends GenIterableLike[A, Repr] with (A => Boolean) with Equals with Parallelizable[A, parallel.ParSet[A]] {
+ def seq: Set[A]
+ def contains(elem: A): Boolean
+ def +(elem: A): Repr
+ def -(elem: A): Repr
+
+ /** Tests if some element is contained in this set.
+ *
+ * This method is equivalent to `contains`. It allows sets to be interpreted as predicates.
+ * @param elem the element to test for membership.
+ * @return `true` if `elem` is contained in this set, `false` otherwise.
+ */
+ def apply(elem: A): Boolean = this contains elem
+
+ /** Computes the intersection between this set and another set.
+ *
+ * @param that the set to intersect with.
+ * @return a new set consisting of all elements that are both in this
+ * set and in the given set `that`.
+ */
+ def intersect(that: GenSet[A]): Repr = this filter that
+
+ /** Computes the intersection between this set and another set.
+ *
+ * '''Note:''' Same as `intersect`.
+ * @param that the set to intersect with.
+ * @return a new set consisting of all elements that are both in this
+ * set and in the given set `that`.
+ */
+ def &(that: GenSet[A]): Repr = this intersect that
+
+ /** Computes the union between of set and another set.
+ *
+ * @param that the set to form the union with.
+ * @return a new set consisting of all elements that are in this
+ * set or in the given set `that`.
+ */
+ def union(that: GenSet[A]): Repr
+
+ /** Computes the union between this set and another set.
+ *
+ * '''Note:''' Same as `union`.
+ * @param that the set to form the union with.
+ * @return a new set consisting of all elements that are in this
+ * set or in the given set `that`.
+ */
+ def | (that: GenSet[A]): Repr = this union that
+
+ /** Computes the difference of this set and another set.
+ *
+ * @param that the set of elements to exclude.
+ * @return a set containing those elements of this
+ * set that are not also contained in the given set `that`.
+ */
+ def diff(that: GenSet[A]): Repr
+
+ /** The difference of this set and another set.
+ *
+ * '''Note:''' Same as `diff`.
+ * @param that the set of elements to exclude.
+ * @return a set containing those elements of this
+ * set that are not also contained in the given set `that`.
+ */
+ def &~(that: GenSet[A]): Repr = this diff that
+
+ /** Tests whether this set is a subset of another set.
+ *
+ * @param that the set to test.
+ * @return `true` if this set is a subset of `that`, i.e. if
+ * every element of this set is also an element of `that`.
+ */
+ def subsetOf(that: GenSet[A]): Boolean = this forall that
+
+ /** Compares this set with another object for equality.
+ *
+ * '''Note:''' This operation contains an unchecked cast: if `that`
+ * is a set, it will assume with an unchecked cast
+ * that it has the same element type as this set.
+ * Any subsequent ClassCastException is treated as a `false` result.
+ * @param that the other object
+ * @return `true` if `that` is a set which contains the same elements
+ * as this set.
+ */
+ override def equals(that: Any): Boolean = that match {
+ case that: GenSet[_] =>
+ (this eq that) ||
+ (that canEqual this) &&
+ (this.size == that.size) &&
+ (try this subsetOf that.asInstanceOf[GenSet[A]]
+ catch { case ex: ClassCastException => false })
+ case _ =>
+ false
+ }
+
+ // Careful! Don't write a Set's hashCode like:
+ // override def hashCode() = this map (_.hashCode) sum
+ // Calling map on a set drops duplicates: any hashcode collisions would
+ // then be dropped before they can be added.
+ // Hash should be symmetric in set entries, but without trivial collisions.
+ override def hashCode() = util.MurmurHash.symmetricHash(seq, Set.hashSeed)
+
+}