summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/Set.scala
diff options
context:
space:
mode:
authorSean McDirmid <sean.mcdirmid@gmail.com>2007-08-22 16:01:57 +0000
committerSean McDirmid <sean.mcdirmid@gmail.com>2007-08-22 16:01:57 +0000
commit1ce5ecc912833a4452e1de23c6bb2fd859d66bd4 (patch)
tree89a3f359f1feb915b3654005238e0f1fbdf5d0da /src/library/scala/collection/Set.scala
parente731089080b55957551288915fd76d20f1e70d1d (diff)
downloadscala-1ce5ecc912833a4452e1de23c6bb2fd859d66bd4.tar.gz
scala-1ce5ecc912833a4452e1de23c6bb2fd859d66bd4.tar.bz2
scala-1ce5ecc912833a4452e1de23c6bb2fd859d66bd4.zip
Fixed Array.slice to use impementation in Rando...
Fixed Array.slice to use impementation in RandomAccessSeq, updated iterators, a bunch of stuff that I forgot to check in!
Diffstat (limited to 'src/library/scala/collection/Set.scala')
-rw-r--r--src/library/scala/collection/Set.scala25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/library/scala/collection/Set.scala b/src/library/scala/collection/Set.scala
index cdad6e4638..6cc99a0509 100644
--- a/src/library/scala/collection/Set.scala
+++ b/src/library/scala/collection/Set.scala
@@ -72,11 +72,34 @@ trait Set[A] extends (A => Boolean) with Collection[A] {
*/
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.
*/
@@ -87,11 +110,9 @@ trait Set[A] extends (A => Boolean) with Collection[A] {
false
}
- /** hashcode for this set */
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)