diff options
Diffstat (limited to 'src/library')
90 files changed, 1222 insertions, 906 deletions
diff --git a/src/library/scala/Either.scala b/src/library/scala/Either.scala index 3704219d31..0f0105b482 100644 --- a/src/library/scala/Either.scala +++ b/src/library/scala/Either.scala @@ -125,7 +125,7 @@ object Either { * * @param e The side-effect to execute. */ - def foreach(f: A => Unit) = e match { + def foreach[U](f: A => U) = e match { case Left(a) => f(a) case Right(_) => {} } @@ -226,7 +226,7 @@ object Either { * * @param e The side-effect to execute. */ - def foreach(f: B => Unit) = e match { + def foreach[U](f: B => U) = e match { case Left(_) => {} case Right(b) => f(b) } diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala index d88ac05789..e752fcd523 100644 --- a/src/library/scala/Enumeration.scala +++ b/src/library/scala/Enumeration.scala @@ -107,7 +107,7 @@ abstract class Enumeration(initial: Int, names: String*) { /** The value of this enumeration with given id `x` */ - final def withId(x: Int): Value = vmap(x) + final def apply(x: Int): Value = vmap(x) /** Returns a Value from this Enumeration whose name matches * the argument <var>s</var>. @@ -215,9 +215,9 @@ abstract class Enumeration(initial: Int, names: String*) { class ValueSet private[Enumeration] (val ids: BitSet) extends Set[Value] with SetTemplate[Value, ValueSet] { override def empty = ValueSet.empty def contains(v: Value) = ids contains (v.id) - def + (value: Value) = new ValueSet(ids + value.id) - def - (value: Value) = new ValueSet(ids - value.id) - def elements = ids.elements map withId + def plus (value: Value) = new ValueSet(ids + value.id) + def minus (value: Value) = new ValueSet(ids - value.id) + def elements = ids.elements map Enumeration.this.apply override def stringPrefix = Enumeration.this + ".ValueSet" } @@ -236,212 +236,4 @@ abstract class Enumeration(initial: Int, names: String*) { /** The implicit builder for value sets */ implicit def builderFactory: BuilderFactory[Value, ValueSet, ValueSet] = new BuilderFactory[Value, ValueSet, ValueSet] { def apply(from: ValueSet) = newBuilder } } - - /** A set that efficiently stores enumeration values as bits. - * - * @author Sean McDirmid - * - * @ex - * - * <pre> - * <b>object</b> flags <b>extends</b> Enumeration { - * <b>val</b> Public = Value(5, "public"); - * <b>val</b> Private = Value(4, "private"); - * <b>val</b> Protected = Value(6, "protected"); - * <b>val</b> Final = Value(7, "final"); - * } - * - * <b>class</b> Entity { - * <b>var</b> flags0 : Int = ...; - * <b>def</b> flags = flags.Set32(flags0); - * } - * - * <b>val</b> e : Entity = ...; - * - * <b>if</b> (e.flags.contains(flags.Private)) - * e.flags0 = (e.flags | flags.Final).underlying; - * </pre> - * @deprecated use ValueSet instead - */ - @deprecated abstract class SetXX extends collection.immutable.Set[Value] { - - /** either Int or Long */ - type Underlying <: AnyVal - type TSet <: SetXX - - /** The integer that bit-encodes a set of enumeration values. - */ - val underlying: Underlying - - /** returns the underlying integer representation of this set as a long. */ - protected def underlyingAsLong: Long - - /** Equivalent to <code>++</code> for bit sets. Returns a set - * that has all values in <code>this</code> and <code>set</code>. - */ - def |(set: TSet): TSet - - /** Equivalent to <code>+</code> for bit sets. Returns a set - * that has all values in <code>this</code> with the addition of <code>value</code>. - */ - def |(value: Value): TSet - - /** Equivalent to <code>**</code> for bit sets. - * Returns a bit set that has all values that are both in <code>this</code> and <code>set</code>. - */ - def &(set: TSet): TSet - - /** Equivalent to <code>-</code> for bit sets. - * Returns a bit set that has all values in <code>this</code> except for <code>value</code>. - */ - def &~(value: Value): TSet - def -(value: Value): TSet = this &~ value - def +(value: Value): TSet = this | value - def ++(set: TSet): TSet = this | set - def **(set: TSet): TSet = this & set - override def size = { - var x = underlyingAsLong - var sz = 0 - while (x != 0) { - if ((x & 1) != 0) sz += 1 - x = x >> 1 - } - sz - } - override def stringPrefix = Enumeration.this.name; - def elements = new Iterator[Value] { - var bit = 0 - var underlying = underlyingAsLong - def hasNext = underlying != 0 - private def shift = { - underlying = underlying >> 1 - bit += 1 - } - def next = { - if (underlying == 0) throw new NoSuchElementException - while ((underlying & 1) == 0) shift - val ret = vmap(bit) - shift - ret - } - } - } - - /** An enumeration bit set that can handle enumeration values with ids up - * to 31 in an <code>Int</code>. - * @deprecated use ValueSet instead - */ - @deprecated class Set32(val underlying: Int) extends SetXX { - def this() = this(0) - type Underlying = Int - type TSet = Set32 - def underlyingAsLong = { - if (underlying >= 0) underlying.toLong - else { - val underlying0 = (~(1 << 31)) & underlying - assert(underlying0 >= 0) - underlying0.toLong | (1L << 31) - } - } - def contains(value: Value) = (underlying & value.mask32) != 0 - def |(set: Set32) = new Set32(underlying | set.underlying) - def |(value: Value) = new Set32(underlying | value.mask32) - def &~(value: Value) = new Set32(underlying & (~value.mask32)) - def &(set: Set32) = new Set32(underlying & set.underlying) - } - - /** create an empty 32 bit enumeration set */ - protected def Set32 = new Set32 - - /** create a bit enumeration set according to underlying */ - protected def Set32(underlying: Int) = new Set32(underlying) - - /** An enumeration bit set that can handle enumeration values with ids up - * to 63 in a <code>Long</code>. - * @deprecated use ValueSet instead - */ - @deprecated class Set64(val underlying: Long) extends SetXX { - def this() = this(0) - type Underlying = Long - type TSet = Set64 - def underlyingAsLong = underlying - def contains(value: Value) = (underlying & value.mask64) != 0 - def |(set: Set64) = new Set64(underlying | set.underlying) - def |(value: Value) = new Set64(underlying | value.mask64) - def &~(value: Value) = new Set64(underlying & (~value.mask64)) - def &(set: Set64) = new Set64(underlying & set.underlying) - } - - /** create an empty 64 bit enumeration set */ - protected def Set64 = new Set64 - - /** create a bit enumeration set according to underlying */ - protected def Set64(underlying: Long) = new Set64(underlying) - - /** used to reverse engineer bit locations from pre-defined bit masks */ - protected def maskToBit(n: Long) = { - assert(n != 0) - var bit = 0 - var m = n - while ((m & 1) != 1) { - m = m >> 1 - bit += 1 - } - assert(m == 1) - bit - } - - /** The name of this enumeration. - * @deprecated use toString instead - */ - @deprecated def name = toString - - /** The value in this enumeration identified by integer <code>x</code>. - * @deprecated use withId instead. - */ - @deprecated final def apply(x: Int): Value = withId(x) - - /** @deprecated use withName instead - */ - @deprecated def valueOf(s: String) = values.find(_.toString == s) - - /** A new iterator over all values of this enumeration. - * @deprecated use values.elements instead - */ - @deprecated final def elements: Iterator[Value] = values.elements - - /** Apply a function f to all values of this enumeration. - * @deprecated use values.foreach instead - */ - @deprecated def foreach(f: Value => Unit): Unit = elements foreach f - - /** Apply a predicate p to all values of this enumeration and return - * true, iff the predicate yields true for all values. - * @deprecated use values.forall instead - */ - @deprecated def forall(p: Value => Boolean): Boolean = elements forall p - - /** Apply a predicate p to all values of this enumeration and return - * true, iff there is at least one value for which p yields true. - * @deprecated use values.exists instead - */ - @deprecated def exists(p: Value => Boolean): Boolean = elements exists p - - /** Returns an iterator resulting from applying the given function f to each - * value of this enumeration. - * @deprecated use values.map instead - */ - @deprecated def map[B](f: Value => B): Iterator[B] = elements map f - - /** Applies the given function f to each value of this enumeration, then - * concatenates the results. - * @deprecated use values.flatMap instead - */ - @deprecated def flatMap[B](f: Value => Iterator[B]): Iterator[B] = elements flatMap f - - /** Returns all values of this enumeration that satisfy the predicate p. - * The order of values is preserved. - * @deprecated use values.filter instead - */ - @deprecated def filter(p: Value => Boolean): Iterator[Value] = elements filter p } diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala index 8191325c06..a1fe76c074 100644 --- a/src/library/scala/Option.scala +++ b/src/library/scala/Option.scala @@ -90,7 +90,7 @@ sealed abstract class Option[+A] extends Product { * * @param f the procedure to apply. */ - def foreach(f: A => Unit) { + def foreach[U](f: A => U) { if (!isEmpty) f(this.get) } diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index 93c9e29040..600ea56dd3 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -336,7 +336,7 @@ object Predef { /** any array projection can be automatically converted into an array */ //implicit def forceArrayProjection[A](x: Array.Projection[A]): Array[A] = x.force !!! re-enable? /** any random access character seq (including rich string can be converted into a string */ - //implicit def forceRandomAccessCharSeq(x: runtime.RichString): String = x.mkString + implicit def richString2String(x: runtime.RichString): String = x.toString //implicit def lazyStreamToConsable[A](xs: => Stream[A]) = new runtime.StreamCons(xs) implicit def seqToCharSequence(xs: collection.Vector[Char]): CharSequence = new CharSequence { diff --git a/src/library/scala/Range.scala b/src/library/scala/Range.scala index c78f0718f3..016ea5fc03 100644 --- a/src/library/scala/Range.scala +++ b/src/library/scala/Range.scala @@ -38,7 +38,7 @@ class Range(val start: Int, val end: Int, val step: Int) extends VectorView[Int, */ def by(step: Int): Range = new Range(start, end, step) - override def foreach(f: Int => Unit) { + override def foreach[U](f: Int => U) { var i = start if (step > 0) { while (i < end) { diff --git a/src/library/scala/Tuple2.scala b/src/library/scala/Tuple2.scala index ccf8f189d5..4d8d64702d 100644 --- a/src/library/scala/Tuple2.scala +++ b/src/library/scala/Tuple2.scala @@ -45,7 +45,7 @@ object Tuple2 { b ++= f(elems1.next, elems2.next) b.result } - def foreach(f: (A1, A2) => Unit) { + def foreach[U](f: (A1, A2) => U) { val elems1 = tuple._1.elements val elems2 = tuple._2.elements while (elems1.hasNext && elems2.hasNext) diff --git a/src/library/scala/collection/DefaultMap.scala b/src/library/scala/collection/DefaultMap.scala index c188c45a45..ec83382660 100644 --- a/src/library/scala/collection/DefaultMap.scala +++ b/src/library/scala/collection/DefaultMap.scala @@ -13,7 +13,7 @@ package scala.collection import generic._ -/* A default map which implements the `add` and `-` methods of maps. +/* A default map which implements the `updated` and `minus` methods of maps. * Instances that inherit from DefaultMap[A, B] still have to define: * * def get(key: A): Option[B] @@ -28,10 +28,10 @@ trait DefaultMap[A, +B] extends Map[A, B] { self => /** A default implementation which creates a new immutable map. */ - override def add[B1 >: B](key: A, value: B1): Map[A, B1] = + override def updated[B1 >: B](key: A, value: B1): Map[A, B1] = Map[A, B1]() ++ this + ((key, value)) /** A default implementation which creates a new immutable map. */ - override def - (key: A): Map[A, B] = Map[A, B]() ++ this - key + override def minus (key: A): Map[A, B] = Map[A, B]() ++ this - key } diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index bf893b6acd..2e20595d76 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -60,7 +60,7 @@ object Iterator { * @param its the argument iterators that are to be concatenated * @return the concatenation of all the argument iterators */ - def concat[A](xss: Iterator[A]*): Iterator[A] = + @deprecated def concat[A](xss: Iterator[A]*): Iterator[A] = Iterable.fromOld(xss).elements.flatten /** An iterator that returns the results of some element computation a number of times. @@ -510,7 +510,7 @@ trait Iterator[+A] { self => * * @param f a function that is applied to every element. */ - def foreach(f: A => Unit) { while (hasNext) f(next()) } + def foreach[U](f: A => U) { while (hasNext) f(next()) } /** Apply a predicate <code>p</code> to all elements of this * iterable object and return <code>true</code> iff the predicate yields diff --git a/src/library/scala/collection/JavaConversions.scala b/src/library/scala/collection/JavaConversions.scala index 6c1e844c56..698d1579c5 100644 --- a/src/library/scala/collection/JavaConversions.scala +++ b/src/library/scala/collection/JavaConversions.scala @@ -355,7 +355,7 @@ object JavaConversions { def apply(i : Int) = underlying.get(i) def update(i : Int, elem : A) = underlying.set(i, elem) def +:(elem : A) = { underlying.subList(0, 0).add(elem) ; this } - def +=(elem : A) = underlying.add(elem) + def +=(elem : A): this.type = { underlying.add(elem); this } def insertAll(i : Int, elems : Traversable[A]) = { val ins = underlying.subList(0, i) ; elems.foreach(ins.add(_)) } def remove(i : Int) = underlying.remove(i) def clear = underlying.clear @@ -386,16 +386,16 @@ object JavaConversions { } } - case class JSetWrapper[A](underlying : ju.Set[A]) extends mutable.Set[A] with generic.SetTemplate[A, JSetWrapper[A]] { + case class JSetWrapper[A](underlying : ju.Set[A]) extends mutable.Set[A] with generic.MutableSetTemplate[A, JSetWrapper[A]] { override def size = underlying.size def elements = underlying.iterator def contains(elem: A): Boolean = underlying.contains(elem) - def +=(elem: A) { underlying.add(elem) } + def put(elem: A): Boolean = underlying.add(elem) - def -=(elem: A) { underlying.remove(elem) } + def remove(elem: A): Boolean = underlying.remove(elem) override def clear = underlying.clear @@ -467,9 +467,19 @@ object JavaConversions { None } - def update(k : A, v : B) { underlying.put(k, v) } + override def put(k : A, v : B): Option[B] = { + val r = underlying.put(k, v) + if (r != null) Some(r) else None + } + + override def update(k : A, v : B) { underlying.put(k, v) } + + override def remove(k : A): Option[B] = { + val r = underlying.remove(k) + if (r != null) Some(r) else None + } - def -=(k : A) = { underlying.remove(k) ; this } + override def delete(k : A) { underlying.remove(k) } def elements = new Iterator[(A, B)] { val ui = underlying.entrySet.iterator diff --git a/src/library/scala/collection/Map.scala b/src/library/scala/collection/Map.scala index c395bd85e3..4028fe870d 100644 --- a/src/library/scala/collection/Map.scala +++ b/src/library/scala/collection/Map.scala @@ -19,8 +19,8 @@ import generic._ * * def get(key: A): Option[B] * def elements: Iterator[(A, B)] - * def add[B1 >: B](key: A, value: B1): This - * def -(key: A): This + * def updated[B1 >: B](key: A, value: B1): This + * def minus(key: A): This * * If you wish that methods like, take, drop, filter return the same kind of map, you should also * override: diff --git a/src/library/scala/collection/Traversable.scala b/src/library/scala/collection/Traversable.scala index 1b29f9efd1..1150a88728 100644 --- a/src/library/scala/collection/Traversable.scala +++ b/src/library/scala/collection/Traversable.scala @@ -42,7 +42,7 @@ trait Traversable[+A] extends TraversableTemplate[A, Traversable[A]] { override def remove(p: A => Boolean): Traversable[A] override def partition(p: A => Boolean): (Traversable[A], Traversable[A]) override def groupBy[K](f: A => K): Map[K, Traversable[A]] - override def foreach(f: A => Unit): Unit + override def foreach[U](f: A => U): Unit override def forall(p: A => Boolean): Boolean override def exists(p: A => Boolean): Boolean override def count(p: A => Boolean): Int @@ -94,7 +94,9 @@ trait Traversable[+A] extends TraversableTemplate[A, Traversable[A]] { object Traversable extends TraversableFactory[Traversable] { self => type Coll = Traversable[_] + implicit def builderFactory[A]: BuilderFactory[A, Traversable[A], Coll] = new BuilderFactory[A, Traversable[A], Coll] { def apply(from: Coll) = from.traversableBuilder[A] } + def newBuilder[A]: Builder[A, Traversable[A]] = immutable.Traversable.newBuilder[A] /** A wrapper class which adds `min` and `max` methods to iterables of an element type that has an Ordering. diff --git a/src/library/scala/collection/generic/Addable.scala b/src/library/scala/collection/generic/Addable.scala index 21215cefac..db580c3771 100644 --- a/src/library/scala/collection/generic/Addable.scala +++ b/src/library/scala/collection/generic/Addable.scala @@ -20,42 +20,75 @@ trait Addable[A, +This <: Addable[A, This]] { self => protected def thisCollection: This - /** Adds a single element to this collection and returns - * either the collection itself (if it is mutable), or a new collection - * with the added element. + /** Creates a new collection with an additional element, unless the element is already present. + * @param elem the element to be added + * @return a fresh collection + */ + def plus(elem: A): This + + /** Creates a new collection with an additional element, unless the element is already present. + * @param elem the element to be added + * @return a fresh collection * - * @param elem the element to add. + * @note same as `plus` + */ + def + (elem: A): This = plus(elem) + + /** Adds two or more elements to this collection and returns + * a new collection. + * + * @param elem1 the first element to add. + * @param elem2 the second element to add. + * @param elems the remaining elements to add. */ - def + (elem: A): This + def plus (elem1: A, elem2: A, elems: A*): This = + this plus elem1 plus elem2 plusAll Iterable.fromOld(elems) /** Adds two or more elements to this collection and returns - * either the collection itself (if it is mutable), or a new collection - * with the added elements. + * a new collection. * * @param elem1 the first element to add. * @param elem2 the second element to add. * @param elems the remaining elements to add. + * @note same as `plus` */ - def + (elem1: A, elem2: A, elems: A*): This = - this + elem1 + elem2 ++ Iterable.fromOld(elems) + def + (elem1: A, elem2: A, elems: A*): This = plus(elem1, elem2, elems: _*) - /** Adds a number of elements provided by an iterable object - * via its <code>elements</code> method and returns - * either the collection itself (if it is mutable), or a new collection - * with the added elements. + /** Adds a number of elements provided by a traversable object + * and returns a new collection with the added elements. * - * @param iter the iterable object. + * @param elems the traversable object. + */ + def plusAll(elems: Traversable[A]): This = (thisCollection /: elems) (_ plus _) + + /** Adds a number of elements provided by a traversable object + * and returns a new collection with the added elements. + * + * @param elems the traversable object. + * @note This is a more efficient version of Traversiable.++ which avoids + * copying of the collection's elements. However, it applies only if + * the type of the added elements is a subtype of the element type of the + * collection. + */ + def ++ (elems: Traversable[A]): This = plusAll(elems) + + /** Adds a number of elements provided by an iterator + * and returns a new collection with the added elements. + * + * @param iter the iterator */ - def ++ (iter: Traversable[A]): This = (thisCollection /: iter) (_ + _) + def plusAll (iter: Iterator[A]): This = (thisCollection /: iter) (_ plus _) /** Adds a number of elements provided by an iterator - * via its <code>elements</code> method and returns - * either the collection itself (if it is mutable), or a new collection - * with the added elements. + * and returns a new collection with the added elements. * * @param iter the iterator + * @note This is a more efficient version of Traversiable.++ which avoids + * copying of the collection's elements. However, it applies only if + * the type of the added elements is a subtype of the element type of the + * collection. */ - def ++ (iter: Iterator[A]): This = (thisCollection /: iter) (_ + _) + def ++ (iter: Iterator[A]): This = plusAll(iter) } diff --git a/src/library/scala/collection/generic/AddingBuilder.scala b/src/library/scala/collection/generic/AddingBuilder.scala index 40e8fb2862..da35889432 100644 --- a/src/library/scala/collection/generic/AddingBuilder.scala +++ b/src/library/scala/collection/generic/AddingBuilder.scala @@ -20,7 +20,7 @@ package scala.collection.generic class AddingBuilder[A, Coll <: Addable[A, Coll] with Iterable[A] with IterableTemplate[A, Coll]](empty: Coll) extends Builder[A, Coll] { protected var elems: Coll = empty - def +=(x: A) { elems = elems + x } + def +=(x: A): this.type = { elems = elems + x; this } def clear() { elems = empty } def result: Coll = elems } diff --git a/src/library/scala/collection/generic/BitSetTemplate.scala b/src/library/scala/collection/generic/BitSetTemplate.scala index 406ba1d431..83c51d9aed 100644 --- a/src/library/scala/collection/generic/BitSetTemplate.scala +++ b/src/library/scala/collection/generic/BitSetTemplate.scala @@ -15,35 +15,10 @@ trait BitSetTemplate[+This <: BitSetTemplate[This] with Set[Int]] extends SetTem */ protected def word(idx: Int): Long - /** Update word at index `idx`; enlarge set if `idx` outside range of set - */ - protected def updateWord(idx: Int, w: Long): This - /** Create a new set of this kind from an array of longs */ protected def fromArray(elems: Array[Long]): This - /** Adds element to bitset, reusing set if its mutable. - */ - override def + (elem: Int): This = { - require(elem >= 0) - if (contains(elem)) thisCollection - else { - val idx = elem >> LogWL - updateWord(idx, word(idx) | (1L << elem)) - } - } - - /** Removes element to bitset, reusing set if its mutable. - */ - override def - (elem: Int): This = { - require(elem >= 0) - if (contains(elem)) { - val idx = elem >> LogWL - updateWord(idx, word(idx) & ~(1L << elem)) - } else thisCollection - } - /** The number of elements in the bitset. */ override def size: Int = { @@ -68,7 +43,7 @@ trait BitSetTemplate[+This <: BitSetTemplate[This] with Set[Int]] extends SetTem else Iterator.empty.next } - override def foreach(f: Int => Unit) { + override def foreach[B](f: Int => B) { for (i <- 0 until nwords) { val w = word(i) for (j <- i * WordLength until (i + 1) * WordLength) { @@ -142,9 +117,21 @@ trait BitSetTemplate[+This <: BitSetTemplate[This] with Set[Int]] extends SetTem } object BitSetTemplate { - private val LogWL = 6 + private[collection] val LogWL = 6 private val WordLength = 64 + private[collection] def updateArray(elems: Array[Long], idx: Int, w: Long): Array[Long] = { + var len = elems.length + while (len > 0 && (elems(len - 1) == 0L || w == 0L && idx == len - 1)) len -= 1 + var newlen = len + if (idx >= newlen && w != 0L) newlen = idx + 1 + val newelems = new Array[Long](newlen) + Array.copy(elems, 0, newelems, 0, len) + if (idx < newlen) newelems(idx) = w + else assert(w == 0L) + newelems + } + private val pc1: Array[Int] = { def countBits(x: Int): Int = if (x == 0) 0 else x % 2 + countBits(x >>> 1) Array.fromFunction(countBits _)(256) diff --git a/src/library/scala/collection/generic/BufferTemplate.scala b/src/library/scala/collection/generic/BufferTemplate.scala index 139d5f7316..56ae754ec7 100644 --- a/src/library/scala/collection/generic/BufferTemplate.scala +++ b/src/library/scala/collection/generic/BufferTemplate.scala @@ -61,7 +61,7 @@ trait BufferTemplate[A, +This <: BufferTemplate[A, This] with Buffer[A]] * * @param elem the element to append. */ - def +=(elem: A) + def +=(elem: A): this.type /** Clears the buffer contents. */ @@ -75,10 +75,6 @@ trait BufferTemplate[A, +This <: BufferTemplate[A, This] with Buffer[A]] */ def +:(elem: A): This - /** Append a single element to this buffer and return the identity of the buffer - */ - def +(elem: A): This = { +=(elem); thisCollection } - /** Inserts new elements at the index <code>n</code>. Opposed to method * <code>update</code>, this method will not replace an element with a * one. Instead, it will insert a new element at index <code>n</code>. @@ -93,6 +89,7 @@ trait BufferTemplate[A, +This <: BufferTemplate[A, This] with Buffer[A]] /** Removes the element on a given index position. * * @param n the index which refers to the element to delete. + * @return the previous element */ def remove(n: Int): A @@ -111,17 +108,19 @@ trait BufferTemplate[A, +This <: BufferTemplate[A, This] with Buffer[A]] * * @param x the element to remove. */ - def -= (x: A) { + def -= (x: A): this.type = { val i = indexOf(x) if (i != -1) remove(i) + this } - /** Removes a single element from this buffer and returns the identity - * of the buffer. If the buffer does not contain that element, it is unchanged. - * - * @param x the element to remove. - */ - def - (elem: A): This = { -=(elem); thisCollection } + /** Returns a new buffer which contains the elements of this buffer, plus + * the given element appended at the end */ + def plus(elem: A): This = clone() += elem + + /** Returns a new buffer which contains the elements of this buffer, plus + * except that the given element is removed */ + def minus (elem: A): This = { -=(elem); thisCollection } /** Prepend two ore more elements to this buffer and return * the identity of the buffer. @@ -146,7 +145,7 @@ trait BufferTemplate[A, +This <: BufferTemplate[A, This] with Buffer[A]] */ def ++:(iter: Iterator[A]): This = { for (x <- iter) x +: this; thisCollection } - /** Appends a elements to this buffer. + /** Appends elements to this buffer. * * @param elems the elements to append. */ @@ -254,6 +253,100 @@ trait BufferTemplate[A, +This <: BufferTemplate[A, This] with Buffer[A]] i += 1 } } + + /** Adds a single element to this collection and returns + * the collection itself. + * + * @param elem the element to add. + * @deprecated use += instead if you inted to add by side effect to an existing collection. + * Use `plus` if you intend to create a new collection. + */ + @deprecated override def + (elem: A): This = { +=(elem); thisCollection } + + /** Adds two or more elements to this collection and returns + * the collection itself. + * + * @param elem1 the first element to add. + * @param elem2 the second element to add. + * @param elems the remaining elements to add. + * @deprecated use += instead if you inted to add by side effect to an existing collection. + * Use `plus` if you intend to create a new collection. + */ + @deprecated override def + (elem1: A, elem2: A, elems: A*): This = { + this += elem1 += elem2 ++= elems + thisCollection + } + + /** Adds a number of elements provided by a traversable object and returns + * either the collection itself. + * + * @param iter the iterable object. + * @deprecated use ++= instead if you inted to add by side effect to an existing collection. + * Use `plusAll` if you intend to create a new collection. + */ + @deprecated override def ++(iter: Traversable[A]): This = { + for (elem <- iter) +=(elem) + thisCollection + } + + /** Adds a number of elements provided by an iterator and returns + * the collection itself. + * + * @param iter the iterator + * @deprecated use ++= instead if you inted to add by side effect to an existing collection. + * Use `plusAll` if you intend to create a new collection. + */ + @deprecated override def ++ (iter: Iterator[A]): This = { + for (elem <- iter) +=(elem) + thisCollection + } + + /** Removes a single element from this collection and returns + * the collection itself. + * + * @param elem the element to remove. + * @deprecated use -= instead if you inted to remove by side effect from an existing collection. + * Use `minus` if you intend to create a new collection. + */ + @deprecated override def -(elem: A): This = { -=(elem); thisCollection } + + /** Removes two or more elements from this collection and returns + * the collection itself. + * + * @param elem1 the first element to remove. + * @param elem2 the second element to remove. + * @param elems the remaining elements to remove. + * @deprecated use -= instead if you inted to remove by side effect from an existing collection. + * Use `minus` if you intend to create a new collection. + */ + @deprecated override def -(elem1: A, elem2: A, elems: A*): This = { + this -= elem1 -= elem2 --= elems + thisCollection + } + + /** Removes a number of elements provided by a traversible object and returns + * the collection itself. + * @deprecated use --= instead if you inted to remove by side effect from an existing collection. + * Use `minusAll` if you intend to create a new collection. + * + * @param iter the iterable object. + */ + @deprecated override def --(iter: Traversable[A]): This = { + for (elem <- iter) -=(elem) + thisCollection + } + + /** Removes a number of elements provided by an iterator and returns + * the collection itself. + * + * @param iter the iterator + * @deprecated use --= instead if you inted to remove by side effect from an existing collection. + * Use `minusAll` if you intend to create a new collection. + */ + @deprecated override def --(iter: Iterator[A]): This = { + for (elem <- iter) -=(elem) + thisCollection + } } diff --git a/src/library/scala/collection/generic/Builder.scala b/src/library/scala/collection/generic/Builder.scala index 0c826c8976..d88e3dd755 100644 --- a/src/library/scala/collection/generic/Builder.scala +++ b/src/library/scala/collection/generic/Builder.scala @@ -20,7 +20,7 @@ trait Builder[-Elem, +To] extends Growable[Elem] { /** Adds a single element to the builder. * @param elem The element to be added */ - def +=(elem: Elem) + def +=(elem: Elem): this.type /** Clear the contents of this builder */ @@ -42,10 +42,10 @@ trait Builder[-Elem, +To] extends Growable[Elem] { def mapResult[NewTo](f: To => NewTo): Builder[Elem, NewTo] = new Builder[Elem, NewTo] with Proxy { val self = Builder.this - def +=(x: Elem) = self += x + def +=(x: Elem): this.type = { self += x; this } def clear() = self.clear() - override def ++=(xs: Iterator[Elem]) = self ++= xs - override def ++=(xs: Traversable[Elem]) = self ++= xs + override def ++=(xs: Iterator[Elem]): this.type = { self ++= xs; this } + override def ++=(xs: Traversable[Elem]): this.type = { self ++= xs; this } def result: NewTo = f(self.result) } } diff --git a/src/library/scala/collection/generic/Growable.scala b/src/library/scala/collection/generic/Growable.scala index d664a60c19..804f824422 100644 --- a/src/library/scala/collection/generic/Growable.scala +++ b/src/library/scala/collection/generic/Growable.scala @@ -23,7 +23,7 @@ trait Growable[-A] { * * @param elem the element to add. */ - def +=(elem: A): Unit + def +=(elem: A): this.type /** Adds two or more elements to this collection. * @@ -31,7 +31,7 @@ trait Growable[-A] { * @param elem2 the second element to add. * @param elems the remaining elements to add. */ - def +=(elem1: A, elem2: A, elems: A*) { + def +=(elem1: A, elem2: A, elems: A*): this.type = { this += elem1 this += elem2 this ++= Iterable.fromOld(elems) @@ -41,13 +41,13 @@ trait Growable[-A] { * * @param iter the iterator. */ - def ++=(iter: Iterator[A]) { iter foreach += } + def ++=(iter: Iterator[A]): this.type = { iter foreach += ; this} /** Adds a number of elements provided by an iterable object to this collection. * * @param iter the iterable object. */ - def ++=(iter: Traversable[A]) { iter foreach += } + def ++=(iter: Traversable[A]): this.type = { iter foreach +=; this } /** Clears the collection contents. */ diff --git a/src/library/scala/collection/generic/ImmutableMapBuilder.scala b/src/library/scala/collection/generic/ImmutableMapBuilder.scala index 337d7d14d1..060e71a2d8 100644 --- a/src/library/scala/collection/generic/ImmutableMapBuilder.scala +++ b/src/library/scala/collection/generic/ImmutableMapBuilder.scala @@ -20,7 +20,7 @@ package scala.collection.generic class ImmutableMapBuilder[A, B, Coll <: immutable.Map[A, B] with MapTemplate[A, B, Coll]](empty: Coll) extends Builder[(A, B), Coll] { protected var elems: Coll = empty - def +=(x: (A, B)) { elems = (elems + x).asInstanceOf[Coll] } + def +=(x: (A, B)): this.type = { elems = (elems + x).asInstanceOf[Coll]; this } def clear() { elems = empty } def result: Coll = elems } diff --git a/src/library/scala/collection/generic/ImmutableMapTemplate.scala b/src/library/scala/collection/generic/ImmutableMapTemplate.scala index cfb602fc21..165cc365f7 100644 --- a/src/library/scala/collection/generic/ImmutableMapTemplate.scala +++ b/src/library/scala/collection/generic/ImmutableMapTemplate.scala @@ -17,8 +17,8 @@ package scala.collection.generic * * def get(key: A): Option[B] * def elements: Iterator[(A, B)] - * def add(key: A, value: B): This - * def -(key: A): This + * def updated(key: A, value: B): This + * def minus(key: A): This * * If you wish that methods like, take, drop, filter return the same kind of map, you should also * override: @@ -33,6 +33,86 @@ self => override protected[this] def newBuilder: Builder[(A, B), This] = new ImmutableMapBuilder[A, B, This](empty) + /** A new immutable map containing updating this map with a given key/value mapping. + * @param key the key + * @param value the value + * @return A new map with the new key/value mapping + */ + def updated [B1 >: B](key: A, value: B1): immutable.Map[A, B1] + + /** Add a key/value pair to this map, returning a new map. + * @param kv the key/value pair + * @return A new map with the new binding added to this map + */ + override def plus [B1 >: B] (kv: (A, B1)): immutable.Map[A, B1] = updated(kv._1, kv._2) + + /** Add a key/value pair to this map, returning a new map. + * @param kv the key/value pair + * @return A new map with the new binding added to this map + * @note same as `plus` + */ + override def + [B1 >: B] (kv: (A, B1)): immutable.Map[A, B1] = updated(kv._1, kv._2) + + /** Adds two or more elements to this collection and returns + * a new collection. + * + * @param elem1 the first element to add. + * @param elem2 the second element to add. + * @param elems the remaining elements to add. + */ + override def plus [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): immutable.Map[A, B1] = + this plus elem1 plus elem2 plusAll elems + + /** Adds two or more elements to this collection and returns + * a new collection. + * + * @param elem1 the first element to add. + * @param elem2 the second element to add. + * @param elems the remaining elements to add. + */ + override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): immutable.Map[A, B1] = + plus(elem1, elem2, elems: _*) + + /** Adds a number of elements provided by a traversable object + * and returns a new collection with the added elements. + * + * @param elems the traversable object. + */ + override def plusAll[B1 >: B](elems: Traversable[(A, B1)]): immutable.Map[A, B1] = + ((thisCollection: immutable.Map[A, B1]) /: elems) (_ plus _) + + /** Adds a number of elements provided by a traversable object + * and returns a new collection with the added elements. + * + * @param elems the traversable object. + * @note same as `plusAll` + * @note This is a more efficient version of Traversable.++ which avoids + * copying of the collection's elements. However, it applies only if + * the type of the added elements is a subtype of the element type of the + * collection. + */ + override def ++ [B1 >: B](elems: Traversable[(A, B1)]): immutable.Map[A, B1] = plusAll(elems) + + /** Adds a number of elements provided by an iterator + * and returns a new collection with the added elements. + * + * @param iter the iterator + */ + override def plusAll[B1 >: B] (iter: Iterator[(A, B1)]): immutable.Map[A, B1] = + ((thisCollection: immutable.Map[A, B1]) /: iter) (_ plus _) + + /** Adds a number of elements provided by an iterator + * and returns a new collection with the added elements. + * + * @param iter the iterator + * @note same as `plusAll` + * @note This is a more efficient version of Traversable.++ which avoids + * copying of the collection's elements. However, it applies only if + * the type of the added elements is a subtype of the element type of the + * collection. + */ + override def ++ [B1 >: B](iter: Iterator[(A, B1)]): immutable.Map[A, B1] = plusAll(iter) + /** This function transforms all the values of mappings contained * in this map with function <code>f</code>. * @@ -45,6 +125,21 @@ self => b.result } - /** @deprecated use add instead */ - @deprecated def update[B1 >: B](key: A, value: B1): immutable.Map[A, B1] = add(key, value).asInstanceOf[immutable.Map[A, B1]] + /** Returns a new map with all key/value pairs for which the predicate + * <code>p</code> returns <code>true</code>. + * + * @param p A predicate over key-value pairs + * @note This method works by successively removing elements fro which the predicate is false from this set. + * If removal is slow, or you expect that most elements of the set will be removed, + * you might consider using `filter` with a negated predicate instead. + */ + override def filterNot(p: ((A, B)) => Boolean): This = { + var res: This = thisCollection + for (kv <- this) + if (p(kv)) res = (res minus kv._1).asInstanceOf[This] // !!! concrete overrides abstract problem + res + } + + /** @deprecated use updated instead */ + @deprecated def update[B1 >: B](key: A, value: B1): immutable.Map[A, B1] = updated(key, value).asInstanceOf[immutable.Map[A, B1]] } diff --git a/src/library/scala/collection/generic/IterableTemplate.scala b/src/library/scala/collection/generic/IterableTemplate.scala index 2df5b6a048..40f66e2d38 100644 --- a/src/library/scala/collection/generic/IterableTemplate.scala +++ b/src/library/scala/collection/generic/IterableTemplate.scala @@ -46,7 +46,7 @@ trait IterableTemplate[+A, +This <: IterableTemplate[A, This] with Iterable[A]] * @note This method underlies the implementation of most other bulk operations. * It should be overridden in concrete collection classes with efficient implementations. */ - def foreach(f: A => Unit): Unit = elements.foreach(f) + def foreach[B](f: A => B): Unit = elements.foreach(f) /** Is this collection empty? */ diff --git a/src/library/scala/collection/generic/LazyBuilder.scala b/src/library/scala/collection/generic/LazyBuilder.scala index 88d8f8d8c8..9262a19169 100644 --- a/src/library/scala/collection/generic/LazyBuilder.scala +++ b/src/library/scala/collection/generic/LazyBuilder.scala @@ -19,9 +19,9 @@ import mutable.ListBuffer abstract class LazyBuilder[Elem, +To] extends Builder[Elem, To] { /** The different segments of elements to be added to the builder, represented as iterators */ protected var parts = new ListBuffer[Traversable[Elem]] - def +=(x: Elem) = { parts += List(x) } - override def ++=(xs: Iterator[Elem]) { parts += xs.toStream } - override def ++=(xs: Traversable[Elem]) { parts += xs } + def +=(x: Elem): this.type = { parts += List(x); this } + override def ++=(xs: Iterator[Elem]): this.type = { parts += xs.toStream; this } + override def ++=(xs: Traversable[Elem]): this.type = { parts += xs; this } def result(): To def clear() { parts.clear() } } diff --git a/src/library/scala/collection/generic/LinearSequenceTemplate.scala b/src/library/scala/collection/generic/LinearSequenceTemplate.scala index 764f33651e..6c13e71beb 100644 --- a/src/library/scala/collection/generic/LinearSequenceTemplate.scala +++ b/src/library/scala/collection/generic/LinearSequenceTemplate.scala @@ -75,7 +75,7 @@ trait LinearSequenceTemplate[+A, +This <: LinearSequenceTemplate[A, This] with L * * @param f the treatment to apply to each element. */ - override def foreach(f: A => Unit) { + override def foreach[B](f: A => B) { var these = this while (!these.isEmpty) { f(these.head) diff --git a/src/library/scala/collection/generic/LinkedListTemplate.scala b/src/library/scala/collection/generic/LinkedListTemplate.scala index 4abb6ceffb..0be2562996 100644 --- a/src/library/scala/collection/generic/LinkedListTemplate.scala +++ b/src/library/scala/collection/generic/LinkedListTemplate.scala @@ -79,7 +79,7 @@ trait LinkedListTemplate[A, This >: Null <: Sequence[A] with LinkedListTemplate[ } } - override def foreach(f: A => Unit): Unit = { + override def foreach[B](f: A => B): Unit = { var these = this while (these ne null) { f(these.elem); diff --git a/src/library/scala/collection/generic/MapTemplate.scala b/src/library/scala/collection/generic/MapTemplate.scala index 4be5fb2fb9..90c5372230 100644 --- a/src/library/scala/collection/generic/MapTemplate.scala +++ b/src/library/scala/collection/generic/MapTemplate.scala @@ -17,8 +17,8 @@ package scala.collection.generic * * def get(key: A): Option[B] * def elements: Iterator[(A, B)] - * def add[B1 >: B](key: A, value: B1): This - * def -(key: A): This + * def updated[B1 >: B](key: A, value: B1): This + * def minus(key: A): This * * If you wish that methods like, take, drop, filter return the same kind of map, you should also * override: @@ -101,10 +101,10 @@ self => protected class DefaultKeySet extends Set[A] { def contains(key : A) = self.contains(key) def elements = self.elements.map(_._1) - def + (elem: A): Set[A] = (Set[A]() ++ this + elem).asInstanceOf[Set[A]] // !!! concrete overrides abstract problem - def - (elem: A): Set[A] = (Set[A]() ++ this - elem).asInstanceOf[Set[A]] // !!! concrete overrides abstract problem + def plus (elem: A): Set[A] = (Set[A]() ++ this + elem).asInstanceOf[Set[A]] // !!! concrete overrides abstract problem + def minus (elem: A): Set[A] = (Set[A]() ++ this - elem).asInstanceOf[Set[A]] // !!! concrete overrides abstract problem override def size = self.size - override def foreach(f: A => Unit) = for ((k, v) <- self) f(k) + override def foreach[B](f: A => B) = for ((k, v) <- self) f(k) } /** @return the keys of this map as a set. @@ -121,6 +121,10 @@ self => def next = iter.next._2 } + /** @return the values of this map as a set (can't do this since Set is covariant) + def valueSet: immutable.Set[B] = immutable.Set.empty[B] ++ (self map (_._2)) + */ + /** The default value for the map, returned when a key is not found * The method implemented here yields an error, * but it might be overridden in subclasses. @@ -135,7 +139,7 @@ self => * predicate `p`. */ def filterKeys(p: A => Boolean) = new DefaultMap[A, B] { - override def foreach(f: ((A, B)) => Unit): Unit = for (kv <- self) if (p(kv._1)) f(kv) + override def foreach[C](f: ((A, B)) => C): Unit = for (kv <- self) if (p(kv._1)) f(kv) def elements = self.elements.filter(kv => p(kv._1)) override def contains(key: A) = self.contains(key) && p(key) def get(key: A) = if (!p(key)) None else self.get(key) @@ -144,7 +148,7 @@ self => /** A map view resulting from applying a given function `f` to each value. */ def mapValues[C](f: B => C) = new DefaultMap[A, C] { - override def foreach(g: ((A, C)) => Unit): Unit = for ((k, v) <- self) g((k, f(v))) + override def foreach[D](g: ((A, C)) => D): Unit = for ((k, v) <- self) g((k, f(v))) def elements = for ((k, v) <- self.elements) yield (k, f(v)) override def size = self.size override def contains(key: A) = self.contains(key) @@ -155,50 +159,91 @@ self => */ @deprecated def mapElements[C](f: B => C) = mapValues(f) - /** Add a key/value pair to this map. + /** A new immutable map containing updating this map with a given key/value mapping. * @param key the key * @param value the value + * @return A new map with the new key/value mapping + */ + def updated [B1 >: B](key: A, value: B1): Map[A, B1] + + /** Add a key/value pair to this map, returning a new map. + * @param kv the key/value pair * @return A new map with the new binding added to this map */ - def add [B1 >: B](key: A, value: B1): Map[A, B1] + def plus [B1 >: B] (kv: (A, B1)): Map[A, B1] = updated(kv._1, kv._2) - /** Add a key/value pair to this map. + /** Add a key/value pair to this map, returning a new map. * @param kv the key/value pair * @return A new map with the new binding added to this map + * @note same as `plus` + */ + def + [B1 >: B] (kv: (A, B1)): Map[A, B1] = updated(kv._1, kv._2) + + /** Adds two or more elements to this collection and returns + * a new collection. + * + * @param elem1 the first element to add. + * @param elem2 the second element to add. + * @param elems the remaining elements to add. */ - def + [B1 >: B] (kv: (A, B1)): Map[A, B1] = add(kv._1, kv._2) + def plus [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): Map[A, B1] = + this plus elem1 plus elem2 plusAll elems /** Adds two or more elements to this collection and returns - * either the collection itself (if it is mutable), or a new collection - * with the added elements. + * a new collection. * * @param elem1 the first element to add. * @param elem2 the second element to add. * @param elems the remaining elements to add. */ def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): Map[A, B1] = - (this + elem1 + elem2).++(collection.Iterable.fromOld(elems))(Map.builderFactory) + plus(elem1, elem2, elems: _*) - /** Remove a key from this map - * @param key the key to be removed - * @return If the map does not contain a binding for <code>key</code> - * it is returned unchanged. Otherwise, return a new map - * without a binding for <code>key</code> + /** Adds a number of elements provided by a traversable object + * and returns a new collection with the added elements. + * + * @param elems the traversable object. */ - def - (key: A): This + def plusAll[B1 >: B](elems: Traversable[(A, B1)]): Map[A, B1] = + ((thisCollection: Map[A, B1]) /: elems) (_ plus _) - /** Removes all the mappings for which the predicate - * <code>p</code> returns <code>true</code>. + /** Adds a number of elements provided by a traversable object + * and returns a new collection with the added elements. * - * @param p A predicate over key-value pairs - * @return the updated map + * @param elems the traversable object. + * @note same as `plusAll` + * @note This is a more efficient version of Traversable.++ which avoids + * copying of the collection's elements. However, it applies only if + * the type of the added elements is a subtype of the element type of the + * collection. */ - override def remove(p: ((A, B)) => Boolean): This = { - var res: This = thisCollection - for (kv <- this) - if (p(kv)) res = (res - kv._1).asInstanceOf[This] // !!! concrete overrides abstract problem - res - } + def ++ [B1 >: B](elems: Traversable[(A, B1)]): Map[A, B1] = plusAll(elems) + + /** Adds a number of elements provided by an iterator + * and returns a new collection with the added elements. + * + * @param iter the iterator + */ + def plusAll[B1 >: B] (iter: Iterator[(A, B1)]): Map[A, B1] = + ((thisCollection: Map[A, B1]) /: iter) (_ plus _) + + /** Adds a number of elements provided by an iterator + * and returns a new collection with the added elements. + * + * @param iter the iterator + * @note same as `plusAll` + * @note This is a more efficient version of Traversable.++ which avoids + * copying of the collection's elements. However, it applies only if + * the type of the added elements is a subtype of the element type of the + * collection. + */ + def ++ [B1 >: B](iter: Iterator[(A, B1)]): Map[A, B1] = plusAll(iter) + + /** Removes a key from this map, returning a new map + * @param key the key to be removed + * @return A new map without a binding for <code>key</code> + */ + def minus (key: A): This /** Creates a string representation for this map. * diff --git a/src/library/scala/collection/generic/MutableMapBuilder.scala b/src/library/scala/collection/generic/MutableMapBuilder.scala index 7e64543214..419eaef0a4 100644 --- a/src/library/scala/collection/generic/MutableMapBuilder.scala +++ b/src/library/scala/collection/generic/MutableMapBuilder.scala @@ -20,7 +20,7 @@ package scala.collection.generic class MutableMapBuilder[A, B, Coll <: mutable.Map[A, B] with MapTemplate[A, B, Coll]](empty: Coll) extends Builder[(A, B), Coll] { protected var elems: Coll = empty - def +=(x: (A, B)) { elems += x } + def +=(x: (A, B)): this.type = { elems += x; this } def clear() { elems = empty } def result: Coll = elems } diff --git a/src/library/scala/collection/generic/MutableMapTemplate.scala b/src/library/scala/collection/generic/MutableMapTemplate.scala index fc622ab578..81ebe42997 100644 --- a/src/library/scala/collection/generic/MutableMapTemplate.scala +++ b/src/library/scala/collection/generic/MutableMapTemplate.scala @@ -11,21 +11,23 @@ package scala.collection.generic -/** A generic template for maps from keys of type A to values of type B. - * To implement a concrete map, you need to provide implementations of the following methods: - * (where This is the type of the map in question): +/** A generic template for mutable maps from keys of type A to values of type B. + * To implement a concrete mutable map, you need to provide implementations of the following methods: * * def get(key: A): Option[B] * def elements: Iterator[(A, B)] - * def update(key: A, value: B) - * def -(key: A): This + * def put(key: A, value: B): Option[B] + * def remove(key: A): Option[B] * * If you wish that methods like, take, drop, filter return the same kind of map, you should also * override: * * def empty: This * - * It is also good idea to override methods foreach and size for efficiency. + * If you to avoid the unncessary construction of an Option object, + * you could also override apply, update, and delete. + * It is also good idea to override methods `foreach` and `size` for efficiency. + * */ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with mutable.Map[A, B]] extends MapTemplate[A, B, This] @@ -37,95 +39,128 @@ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with muta override protected[this] def newBuilder: Builder[(A, B), This] = new MutableMapBuilder[A, B, This](empty.asInstanceOf[This]) // !!! concrete overrides abstract problem - /** This method allows one to add a new mapping from <code>key</code> + /** Adds a new mapping from <code>key</code> * to <code>value</code> to the map. If the map already contains a - * mapping for <code>key</code>, it will be overridden by this - * function. + * mapping for <code>key</code>, it will be overridden. * * @param key The key to update * @param value The new value */ - def update(key: A, value: B) + def put(key: A, value: B): Option[B] - /** Add a key/vaue pair to this map, and return the map itself + /** Adds a new mapping from <code>key</code> + * to <code>value</code> to the map. If the map already contains a + * mapping for <code>key</code>, it will be overridden. + * @param key The key to update + * @param value The new value + * @return An option consisting of value associated previously associated with `key` in the map, + * or None if `key` was not yet defined in the map. */ - def add(key: A, value: B): this.type = { update(key, value); this } + def update(key: A, elem: B) { put(key, elem) } - /** Map <code>key</code> to <code>elem</code> in this map and return the element - * that the key was previously mapped to (if any). + /** Add a new key/value mapping this map. + * @param kv the key/value pair. + * @return the map itself */ - def put(key: A, elem: B): Option[B] = { - val ret = get(key) - this(key) = elem - ret - } + def += (kv: (A, B)): this.type = { update(kv._1, kv._2); this } - /** Add a key/value pair to this map. - * @param kv the key/value pair. + /** Create a new map consisting of all elements of the current map + * plus the given mapping from `key` to `value`. + * @param key The key to add + * @param value The new value + * @return A fresh immutable map */ - def += (kv: (A, B)) { update(kv._1, kv._2) } + def updated[B1 >: B](key: A, value: B1): collection.Map[A, B1] = + (Map[A, B1]() plusAll thisCollection).updated[B1](key, value) - /** A default implementation of addition where the value is not of the same type as - * the current map value type. This will always create a new immutable Map. + /** Create a new map consisting of all elements of the current map + * except any mapping from `key`. + * @param key the key to be removed + * @return A new map without a binding for <code>key</code> */ - def add[B1 >: B](key: A, value: B1): collection.Map[A, B1] = - Map[A, B1]() ++ thisCollection + ((key, value)) + def minus (key: A): This = clone() minus key - /** Adds a single element to this collection and returns - * either the collection itself (if it is mutable), or a new collection - * with the added element. + /** Add a new key/value mapping and return the map itself. * - * @param elem the element to add. + * @param kv the key/value mapping to be added + * @deprecated This operation will create a new map in the future. To add + * an element as a side effect to an existing map and return + * that map itself, use +=. If you do want to create a fresh map, + * you can use `plus` to avoid a @deprecated warning. */ - //override - def +(kv: (A, B)): this.type = add(kv._1, kv._2) + @deprecated def +(kv: (A, B)): this.type = { update(kv._1, kv._2); this } - /** Adds two or more elements to this collection and returns - * either the collection itself (if it is mutable), or a new collection + /** Adds two or more key/value mappings and return the map itself. * with the added elements. * * @param elem1 the first element to add. * @param elem2 the second element to add. * @param elems the remaining elements to add. + * @deprecated This operation will create a new map in the future. To add + * an element as a side effect to an existing map and return + * that map itself, use +=. If you do want to create a fresh map, + * you can use `plus` to avoid a @deprecated warning. */ - //override - def +(elem1: (A, B), elem2: (A, B), elems: (A, B)*): this.type = - this + elem1 + elem2 ++ Iterable.fromOld(elems) + @deprecated def +(elem1: (A, B), elem2: (A, B), elems: (A, B)*): this.type = + this += elem1 += elem2 ++= elems - /** Adds a number of elements provided by an iterable object + /** Adds a number of elements provided by a traversable object * via its <code>elements</code> method and returns * either the collection itself (if it is mutable), or a new collection * with the added elements. - * - * @param iter the iterable object. + * @deprecated This operation will create a new map in the future. To add + * elements as a side effect to an existing map and return + * that map itself, use ++=. If you do want to create a fresh map, + * you can use `plusAll` to avoid a @deprecated warning. + * @param iter the traversable object. */ - //override - def ++(iter: Traversable[(A, B)]): this.type = { (this /: iter) (_ + _); this } + @deprecated def ++(iter: Traversable[(A, B)]): this.type = { for (elem <- iter) +=(elem); this } /** Adds a number of elements provided by an iterator * via its <code>elements</code> method and returns - * either the collection itself (if it is mutable), or a new collection - * with the added elements. + * the collection itself. + * @deprecated This operation will create a new map in the future. To add + * elements as a side effect to an existing map and return + * that map itself, use ++=. If you do want to create a fresh map, + * you can use `plus` to avoid a @deprecated warning. * * @param iter the iterator */ - //override - def ++(iter: Iterator[(A, B)]): this.type = { (this /: iter) (_ + _); this } + @deprecated def ++(iter: Iterator[(A, B)]): this.type = { for (elem <- iter) +=(elem); this } - /** Remove a key from this map, noop if key is not presentg. + /** If given key is defined in this map, remove it and return associated value as an Option. + * If key is not present return None. * @param key the key to be removed */ - def -= (key: A) + def remove(key: A): Option[B] - def -(key: A): This = { -=(key); thisCollection } + /** Delete a key from this map if it is present. + * @param key the key to be removed + */ + def delete (key: A) { remove(key) } - def removeKey(key: A): Option[B] = { - val ret = get(key) - this -= key - ret - } + /** Delete a key from this map if it is present. + * @param key the key to be removed + * @note same as `delete`. + */ + def -= (key: A): this.type = { delete(key); this } + + /** Delete a key from this map if it is present and return the map itself. + * @param key the key to be removed + * @deprecated This operation will create a new map in the future. To add + * elements as a side effect to an existing map and return + * that map itself, use -=. If you do want to create a fresh map, + * you can use `minus` to avoid a @deprecated warning. + */ + @deprecated override def -(key: A): This = { -=(key); thisCollection } + + /** If given key is defined in this map, remove it and return associated value as an Option. + * If key is not present return None. + * @param key the key to be removed + * @deprecated Use `remove` instead. + */ + @deprecated def removeKey(key: A): Option[B] = remove(key) - // todo: add other -'s /** Removes all elements from the set. After this operation is completed, * the set will be empty. @@ -154,11 +189,11 @@ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with muta this } - /** This method retains only those mappings for which the predicate + /** Retain only those mappings for which the predicate * <code>p</code> returns <code>true</code>. * * @param p The test predicate - * @deprecated cannot be type inferred because if retain in Iterable. + * @deprecated cannot be type inferred because of retain in Iterable. */ def retain(p: (A, B) => Boolean): this.type = { for ((k, v) <- this) if (!p(k, v)) -=(k) @@ -170,4 +205,43 @@ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with muta /** The result when this map is used as a builder */ def result: This = thisCollection + + /** Removes two or more elements from this collection and returns + * the collection itself. + * + * @param elem1 the first element to remove. + * @param elem2 the second element to remove. + * @param elems the remaining elements to remove. + * @deprecated use -= instead if you inted to remove by side effect from an existing collection. + * Use `minus` if you intend to create a new collection. + */ + @deprecated override def -(elem1: A, elem2: A, elems: A*): This = { + this -= elem1 -= elem2 --= elems + thisCollection + } + + /** Removes a number of elements provided by a traversible object and returns + * the collection itself. + * @deprecated use --= instead if you inted to remove by side effect from an existing collection. + * Use `minusAll` if you intend to create a new collection. + * + * @param iter the iterable object. + */ + @deprecated override def --(iter: Traversable[A]): This = { + for (elem <- iter) -=(elem) + thisCollection + } + + + /** Removes a number of elements provided by an iterator and returns + * the collection itself. + * + * @param iter the iterator + * @deprecated use --= instead if you inted to remove by side effect from an existing collection. + * Use `minusAll` if you intend to create a new collection. + */ + @deprecated override def --(iter: Iterator[A]): This = { + for (elem <- iter) -=(elem) + thisCollection + } } diff --git a/src/library/scala/collection/generic/MutableSetTemplate.scala b/src/library/scala/collection/generic/MutableSetTemplate.scala new file mode 100644 index 0000000000..f9f4b52dc8 --- /dev/null +++ b/src/library/scala/collection/generic/MutableSetTemplate.scala @@ -0,0 +1,206 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Map.scala 16884 2009-01-09 16:52:09Z cunei $ + + +package scala.collection.generic + +/** A generic template for mutable sets of elements of type A. + * To implement a concrete mutable set, you need to provide implementations of the following methods: + * + * def contains(elem: A): Boolean + * def put(elem: A): Boolean + * def remove(elem: A): Boolean + * def elements: Iterator[A] + * + * If you wish that methods like, take, drop, filter return the same kind of map, you should also + * override: + * + * def empty: This + * + * It is also good idea to override methods `foreach` and `size` for efficiency. + * + */ +trait MutableSetTemplate[A, +This <: MutableSetTemplate[A, This] with mutable.Set[A]] + extends SetTemplate[A, This] + with Builder[A, This] + with Growable[A] + with Shrinkable[A] + with Cloneable[This] +{ self => + + override protected[this] def newBuilder: Builder[A, This] = empty + + /** Adds a new element to the set. + * + * @param elem the element to be added + * @return true if the element was already present in the set. + */ + def put(elem: A): Boolean + + /** Removes a single element from a set. + * @param elem The element to be removed. + * @return true if the element was already present in the set. + */ + def remove(elem: A): Boolean + + /** This method allows one to add or remove an element <code>elem</code> + * from this set depending on the value of parameter <code>included</code>. + * Typically, one would use the following syntax: + * <pre>set(elem) = true</pre> + * + */ + def update(elem: A, included: Boolean) { + if (included) this put elem else this remove elem + } + + /** Adds a new element to the set. + * + * @param elem the element to be added + */ + def +=(elem: A): this.type = { put(elem); this } + + /** Removes a single element from a set. + * @param elem The element to be removed. + */ + def -=(elem: A): this.type = { remove(elem); this } + + /** Removes all elements from the set for which the predicate <code>p</code> + * yields the value <code>false</code>. + */ + def retain(p: A => Boolean): Unit = for (elem <- this.toList) if (!p(elem)) this -= elem + + /** Removes all elements from the set. After this operation is completed, + * the set will be empty. + */ + def clear() { foreach(-=) } + + override def clone(): This = empty ++= thisCollection + + def plus(elem: A): This = clone() += elem + + def minus(elem: A): This = clone() -= elem + + def result: This = thisCollection + + /** Adds a single element to this collection and returns + * the collection itself. + * + * @param elem the element to add. + * @deprecated use += instead if you inted to add by side effect to an existing collection. + * Use `plus` if you intend to create a new collection. + */ + @deprecated override def + (elem: A): This = { +=(elem); thisCollection } + + /** Adds two or more elements to this collection and returns + * the collection itself. + * + * @param elem1 the first element to add. + * @param elem2 the second element to add. + * @param elems the remaining elements to add. + * @deprecated use += instead if you inted to add by side effect to an existing collection. + * Use `plus` if you intend to create a new collection. + */ + @deprecated override def + (elem1: A, elem2: A, elems: A*): This = { + this += elem1 += elem2 ++= elems + thisCollection + } + + /** Adds a number of elements provided by a traversable object and returns + * either the collection itself. + * + * @param iter the iterable object. + * @deprecated use ++= instead if you inted to add by side effect to an existing collection. + * Use `plusAll` if you intend to create a new collection. + */ + @deprecated override def ++(iter: Traversable[A]): This = { + for (elem <- iter) +=(elem) + thisCollection + } + + + /** Adds a number of elements provided by an iterator and returns + * the collection itself. + * + * @param iter the iterator + * @deprecated use ++= instead if you inted to add by side effect to an existing collection. + * Use `plusAll` if you intend to create a new collection. + */ + @deprecated override def ++ (iter: Iterator[A]): This = { + for (elem <- iter) +=(elem) + thisCollection + } + + /** Removes a single element from this collection and returns + * the collection itself. + * + * @param elem the element to remove. + * @deprecated use -= instead if you inted to remove by side effect from an existing collection. + * Use `minus` if you intend to create a new collection. + */ + @deprecated override def -(elem: A): This = { -=(elem); thisCollection } + + /** Removes two or more elements from this collection and returns + * the collection itself. + * + * @param elem1 the first element to remove. + * @param elem2 the second element to remove. + * @param elems the remaining elements to remove. + * @deprecated use -= instead if you inted to remove by side effect from an existing collection. + * Use `minus` if you intend to create a new collection. + */ + @deprecated override def -(elem1: A, elem2: A, elems: A*): This = { + this -= elem1 -= elem2 --= elems + thisCollection + } + + /** Removes a number of elements provided by a traversible object and returns + * the collection itself. + * @deprecated use --= instead if you inted to remove by side effect from an existing collection. + * Use `minusAll` if you intend to create a new collection. + * + * @param iter the iterable object. + */ + @deprecated override def --(iter: Traversable[A]): This = { + for (elem <- iter) -=(elem) + thisCollection + } + + /** Removes a number of elements provided by an iterator and returns + * the collection itself. + * + * @param iter the iterator + * @deprecated use --= instead if you inted to remove by side effect from an existing collection. + * Use `minusAll` if you intend to create a new collection. + */ + @deprecated override def --(iter: Iterator[A]): This = { + for (elem <- iter) -=(elem) + thisCollection + } + + /** Send a message to this scriptable object. + * + * @param cmd the message to send. + * @throws <code>Predef.UnsupportedOperationException</code> + * if the message was not understood. + def <<(cmd: Message[A]): Unit = cmd match { + case Include(elem) => this += elem + case Remove(elem) => this -= elem + case Reset() => clear + case s: Script[_] => s.elements foreach << + case _ => throw new UnsupportedOperationException("message " + cmd + " not understood") + } + */ +} + + + + + + diff --git a/src/library/scala/collection/generic/SetBuilder.scala b/src/library/scala/collection/generic/SetBuilder.scala index 30456ff38d..73882781f4 100644 --- a/src/library/scala/collection/generic/SetBuilder.scala +++ b/src/library/scala/collection/generic/SetBuilder.scala @@ -20,7 +20,7 @@ package scala.collection.generic class SetBuilder[A, Coll <: Addable[A, Coll] with Iterable[A] with IterableTemplate[A, Coll]](empty: Coll) extends Builder[A, Coll] { protected var elems: Coll = empty - def +=(x: A) { elems = elems + x } + def +=(x: A): this.type = { elems = elems + x; this } def clear() { elems = empty } def result: Coll = elems } diff --git a/src/library/scala/collection/generic/SetTemplate.scala b/src/library/scala/collection/generic/SetTemplate.scala index ae339c9077..5d69abe389 100644 --- a/src/library/scala/collection/generic/SetTemplate.scala +++ b/src/library/scala/collection/generic/SetTemplate.scala @@ -16,8 +16,8 @@ package scala.collection.generic * * def contains(key: A): Boolean * def elements: Iterator[A] - * def +(elem: A): This - * def -(elem: A): This + * def plus(elem: A): This + * def minus(elem: A): This * * If you wish that methods like, take, drop, filter return the same kind of set, you should also * override: @@ -26,7 +26,8 @@ package scala.collection.generic * * It is also good idea to override methods foreach and size for efficiency. */ -trait SetTemplate[A, +This <: SetTemplate[A, This] with Set[A]] extends IterableTemplate[A, This] with Addable[A, This] with Subtractable[A, This] { self => +trait SetTemplate[A, +This <: SetTemplate[A, This] with Set[A]] extends IterableTemplate[A, This] with Addable[A, This] with Subtractable[A, This] { +self => def empty: This @@ -43,12 +44,12 @@ trait SetTemplate[A, +This <: SetTemplate[A, This] with Set[A]] extends Iterable /** Creates a new set with an additional element, unless the element is already present. * @param elem the element to be added */ - def + (elem: A): This + def plus (elem: A): This - /** Removes a single element from a set, unless the element is not present. + /** Creates a new set with given element removed from this set, unless the element is not present. * @param elem the element to be removed */ - def - (elem: A): This + def minus (elem: A): This /** Checks if this set is empty. * @@ -66,14 +67,22 @@ trait SetTemplate[A, +This <: SetTemplate[A, This] with Set[A]] extends Iterable */ def apply(elem: A): Boolean = contains(elem) - /** This method computes an intersection with set <code>that</code>. - * It removes all the elements that are not present in <code>that</code>. + /** Returns a new set consisting of all elements that are both in the current set + * and in the argument set. * * @param that the set to intersect with. */ def intersect(that: Set[A]): This = filter(that.contains) - /** This method is an alias for <code>intersect</code>. + /** Returns a new set consisting of all elements that are both in the current set + * and in the argument set. + * + * @param that the set to intersect with. + * @note same as `intersect` + */ + def &(that: Set[A]): This = intersect(that) + + /** This method is an alias for <code>intersect</code>. * It computes an intersection with set <code>that</code>. * It removes all the elements that are not present in <code>that</code>. * @@ -82,26 +91,39 @@ trait SetTemplate[A, +This <: SetTemplate[A, This] with Set[A]] extends Iterable */ @deprecated def ** (that: Set[A]): This = intersect(that) - /** This method is an alias for <code>intersect</code>. - * It computes an intersection with set <code>that</code>. - * It removes all the elements that are not present in <code>that</code>. + /** The union of this set and the given set <code>that</code>. * - * @param that the set to intersect with + * @param that the set of elements to add + * @return a set containing the elements of this + * set and those of the given set <code>that</code>. */ - def & (that: collection.Set[A]): This = intersect(that) + def union(that: Set[A]): This = plusAll(that) - /** Computes the union of this set and the given set <code>that</code>. + /** The union of this set and the given set <code>that</code>. * - * @param that the sequence of elements to add to the sequence. + * @param that the set of elements to add * @return a set containing the elements of this * set and those of the given set <code>that</code>. + * @note same as `union` + */ + def | (that: Set[A]): This = union(that) + + /** The difference of this set and the given set <code>that</code>. + * + * @param that the set of elements to remove + * @return a set containing those elements of this + * set that are not also contained in the given set <code>that</code>. */ - def union(that: Set[A]): This = (thisCollection /: that) (_ + _) + def diff(that: Set[A]): This = minusAll(that) - /** This method is an alias for <code>union</code>. - * It computes the union of this set and the given set <code>that</code>. + /** The difference of this set and the given set <code>that</code>. + * + * @param that the set of elements to remove + * @return a set containing those elements of this + * set that are not also contained in the given set <code>that</code>. + * @note same as `diff`. */ - def | (that: Set[A]): This = union(that) + def &~(that: Set[A]): This = diff(that) /** Checks if this set is a subset of set <code>that</code>. * diff --git a/src/library/scala/collection/generic/Shrinkable.scala b/src/library/scala/collection/generic/Shrinkable.scala index 937ec2097e..6eecbaac3b 100644 --- a/src/library/scala/collection/generic/Shrinkable.scala +++ b/src/library/scala/collection/generic/Shrinkable.scala @@ -22,7 +22,7 @@ trait Shrinkable[-A] { * * @param elem the element to remove. */ - def -=(elem: A): Unit + def -=(elem: A): this.type /** Removes two or more elements from this collection. * @@ -30,7 +30,7 @@ trait Shrinkable[-A] { * @param elem2 the second element to remove. * @param elems the remaining elements to remove. */ - def -=(elem1: A, elem2: A, elems: A*) { + def -=(elem1: A, elem2: A, elems: A*): this.type = { this -= elem1 this -= elem2 this --= Iterable.fromOld(elems) @@ -40,13 +40,13 @@ trait Shrinkable[-A] { * * @param iter the iterator. */ - def --=(iter: Iterator[A]) { iter foreach -= } + def --=(iter: Iterator[A]): this.type = { iter foreach -=; this } /** Removes a number of elements provided by an iterable object from this collection. * * @param iter the iterable object. */ - def --=(iter: Traversable[A]) { iter foreach -= } + def --=(iter: Traversable[A]): this.type = { iter foreach -=; this } } diff --git a/src/library/scala/collection/generic/SortedMapTemplate.scala b/src/library/scala/collection/generic/SortedMapTemplate.scala index 8c7e4eb54f..a7bed88567 100644 --- a/src/library/scala/collection/generic/SortedMapTemplate.scala +++ b/src/library/scala/collection/generic/SortedMapTemplate.scala @@ -30,8 +30,8 @@ self => def compare(k0: A, k1: A) = self.thisCollection.compare(k0, k1) /** We can't give an implementation of +/- here because we do not have a generic sorted set implementation */ - override def + (elem: A): SortedSet[A] = throw new UnsupportedOperationException("keySet.+") - override def - (elem: A): SortedSet[A] = throw new UnsupportedOperationException("keySet.-") + override def plus (elem: A): SortedSet[A] = throw new UnsupportedOperationException("keySet.+") + override def minus (elem: A): SortedSet[A] = throw new UnsupportedOperationException("keySet.-") override def rangeImpl(from : Option[A], until : Option[A]) : SortedSet[A] = { val map = self.rangeImpl(from, until) new map.DefaultKeySet @@ -45,13 +45,15 @@ self => * @param value the value * @return A new map with the new binding added to this map */ - def add [B1 >: B](key: A, value: B1): SortedMap[A, B1] + def updated[B1 >: B](key: A, value: B1): SortedMap[A, B1] /** Add a key/value pair to this map. * @param kv the key/value pair * @return A new map with the new binding added to this map */ - override def + [B1 >: B] (kv: (A, B1)): SortedMap[A, B1] = add(kv._1, kv._2) + override def plus [B1 >: B] (kv: (A, B1)): SortedMap[A, B1] = updated(kv._1, kv._2) + + // todo: Add generic +,-, and so on. /** Adds two or more elements to this collection and returns * either the collection itself (if it is mutable), or a new collection @@ -61,9 +63,9 @@ self => * @param elem2 the second element to add. * @param elems the remaining elements to add. */ - override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): SortedMap[A, B1] = { - var m = this + elem1 + elem2; - for (e <- elems) m = m + e + override def plus [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): SortedMap[A, B1] = { + var m = this plus elem1 plus elem2; + for (e <- elems) m = m plus e m } } diff --git a/src/library/scala/collection/generic/Subtractable.scala b/src/library/scala/collection/generic/Subtractable.scala index 928ef939ae..c83e70ca13 100644 --- a/src/library/scala/collection/generic/Subtractable.scala +++ b/src/library/scala/collection/generic/Subtractable.scala @@ -20,45 +20,69 @@ trait Subtractable[A, +This <: Subtractable[A, This]] { self => protected def thisCollection: This - /** Removes a single element from this collection and returns - * either the collection itself (if it is mutable), or a new collection - * with the removed element. + /** Returns a new collection that contains all elements of the current collection + * except a given element. * * @param elem the element to remove. */ - def -(elem: A): This + def minus(elem: A): This - /** Removes two or more elements from this collection and returns - * either the collection itself (if it is mutable), or a new collection - * with the removed elements. + /** Returns a new collection that contains all elements of the current collection + * except a given element. + * + * @param elem the element to remove. + * @note same as `minus` + */ + def -(elem: A): This = minus(elem) + + /** Returns a new collection that contains all elements of the current collection + * except a two or more given elements. * * @param elem1 the first element to remove. * @param elem2 the second element to remove. * @param elems the remaining elements to remove. */ - def -(elem1: A, elem2: A, elems: A*): This = - this - elem1 - elem2 -- Iterable.fromOld(elems) + def minus(elem1: A, elem2: A, elems: A*): This = + this minus elem1 minus elem2 minusAll Iterable.fromOld(elems) - /** Removes a number of elements provided by an iterable object - * via its <code>elements</code> method and returns - * either the collection itself (if it is mutable), or a new collection - * with the removed elements. + /** Returns a new collection that contains all elements of the current collection + * except two or more given elements. * - * @param iter the iterable object. + * @param elem1 the first element to remove. + * @param elem2 the second element to remove. + * @param elems the remaining elements to remove. + * @note same as minus */ - def --(iter: Traversable[A]): This = (thisCollection /: iter) (_ - _) + def - (elem1: A, elem2: A, elems: A*): This = minus(elem1, elem2, elems: _*) - /** Removes a number of elements provided by an iterator - * via its <code>elements</code> method and returns - * either the collection itself (if it is mutable), or a new collection - * with the removed elements. + /** Returns a new collection that contains all elements of the current collection + * except the elements provided by a traversable object * - * @param iter the iterator + * @param elems the traversable object containing the elements that do not form part of the new collection. */ - def --(iter: Iterator[A]): This = (thisCollection /: iter) (_ - _) - -} - + def minusAll(elems: Traversable[A]): This = (thisCollection /: elems) (_ minus _) + /** Returns a new collection that contains all elements of the current collection + * except the elements provided by a traversable object + * + * @param elems the traversable object containing the elements that do not form part of the new collection. + * @note same as minusAll + */ + def --(elems: Traversable[A]): This = minusAll(elems) + /** Returns a new collection that contains all elements of the current collection + * except the elements provided by an iterator + * + * @param elems the iterator containing the elements that do not form part of the new collection + * @note same as minusAll + */ + def minusAll(iter: Iterator[A]): This = (thisCollection /: iter) (_ minus _) + /** Returns a new collection that contains all elements of the current collection + * except the elements provided by an iterator + * + * @param elems the iterator containing the elements that do not form part of the new collection + * @note same as minusAll + */ + def --(iter: Iterator[A]): This = minusAll(iter) +} diff --git a/src/library/scala/collection/generic/TraversableBuildable.scala b/src/library/scala/collection/generic/TraversableBuildable.scala new file mode 100755 index 0000000000..d1137632fc --- /dev/null +++ b/src/library/scala/collection/generic/TraversableBuildable.scala @@ -0,0 +1,14 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ +// $Id: Traversable.scala 15188 2008-05-24 15:01:02Z stepancheg $ +package scala.collection.generic + +trait TraversableClass[CC[X] <: Traversable[X]] { + def factory: TraversableFactory[CC] + protected[this] def newBuilder = factory.newBuilder +} diff --git a/src/library/scala/collection/generic/TraversableForwarder.scala b/src/library/scala/collection/generic/TraversableForwarder.scala index 219021f8e6..f3ab981526 100644 --- a/src/library/scala/collection/generic/TraversableForwarder.scala +++ b/src/library/scala/collection/generic/TraversableForwarder.scala @@ -36,7 +36,7 @@ trait TraversableForwarder[+A] extends Traversable[A] { override def isEmpty = underlying.isEmpty override def hasDefiniteSize = underlying.hasDefiniteSize - override def foreach(f: A => Unit) = underlying.foreach(f) + override def foreach[B](f: A => B) = underlying.foreach(f) override def forall(p: A => Boolean): Boolean = underlying.forall(p) override def exists(p: A => Boolean): Boolean = underlying.exists(p) override def count(p: A => Boolean): Int = underlying.count(p) diff --git a/src/library/scala/collection/generic/TraversableProxyTemplate.scala b/src/library/scala/collection/generic/TraversableProxyTemplate.scala index 0cd955d50a..a7ec343827 100644 --- a/src/library/scala/collection/generic/TraversableProxyTemplate.scala +++ b/src/library/scala/collection/generic/TraversableProxyTemplate.scala @@ -36,7 +36,7 @@ trait TraversableProxyTemplate[+A, +This <: TraversableTemplate[A, This] with Tr override def remove(p: A => Boolean): This = self.remove(p) override def partition(p: A => Boolean): (This, This) = self.partition(p) override def groupBy[K](f: A => K): Map[K, This] = self.groupBy(f) - override def foreach(f: A => Unit): Unit = self.foreach(f) + override def foreach[B](f: A => B): Unit = self.foreach(f) override def forall(p: A => Boolean): Boolean = self.forall(p) override def exists(p: A => Boolean): Boolean = self.exists(p) override def count(p: A => Boolean): Int = self.count(p) diff --git a/src/library/scala/collection/generic/TraversableTemplate.scala b/src/library/scala/collection/generic/TraversableTemplate.scala index 95bc524bb4..735f883a2a 100644 --- a/src/library/scala/collection/generic/TraversableTemplate.scala +++ b/src/library/scala/collection/generic/TraversableTemplate.scala @@ -85,6 +85,11 @@ self => b.result } + /** Same as ++ + */ + def concat[B >: A, That](that: Traversable[B])(implicit bf: BuilderFactory[B, That, This]): That = ++(that)(bf) + def concat[B >: A, That](that: Iterator[B])(implicit bf: BuilderFactory[B, That, This]): That = ++(that)(bf) + /** Returns the traversable that results from applying the given function * <code>f</code> to each element of this traversable and collecing the results * in an traversable of type `That`. @@ -124,14 +129,22 @@ self => b.result } - /** Removes all elements of the traversable which satisfy the predicate - * <code>p</code>. This is like <code>filter</code> with the - * predicate inversed. + /** Returns a traversable with all elements of this traversable which do not satisfy the predicate + * <code>p</code>. + * + * @param p the predicate used to test elements + * @return the traversable without all elements that satisfy <code>p</code> + */ + def filterNot(p: A => Boolean): This = filter(!p(_)) + + /** Returns a traversable with all elements of this traversable which do not satisfy the predicate + * <code>p</code>. * * @param p the predicate used to test elements * @return the traversable without all elements that satisfy <code>p</code> + * @deprecated use `filterNot` instead */ - def remove(p: A => Boolean): This = filter(!p(_)) + @deprecated def remove(p: A => Boolean): This = filterNot(p) /** Partitions this traversable in two traversables according to a predicate. * @@ -160,7 +173,7 @@ self => for (elem <- this) { val key = f(elem) val bldr = m get key match { - case None => val b = newBuilder; m = m add (key, b); b + case None => val b = newBuilder; m = m updated (key, b); b case Some(b) => b } bldr += elem @@ -175,7 +188,7 @@ self => * @note This method underlies the implementation of most other bulk operations. * It should be overridden in concrete collection classes with efficient implementations. */ - def foreach(f: A => Unit): Unit + def foreach[B](f: A => B): Unit /** Return true iff the given predicate `p` yields true for all elements * of this traversable. @@ -722,7 +735,7 @@ self => */ def view = new TraversableView[A, This] { protected lazy val underlying = self.thisCollection - override def foreach(f: A => Unit) = self foreach f + override def foreach[B](f: A => B) = self foreach f } /** A sub-traversable starting at index `from` diff --git a/src/library/scala/collection/generic/TraversableView.scala b/src/library/scala/collection/generic/TraversableView.scala index 939dc5b032..c89639bab1 100644 --- a/src/library/scala/collection/generic/TraversableView.scala +++ b/src/library/scala/collection/generic/TraversableView.scala @@ -19,7 +19,7 @@ trait TraversableView[+A, +Coll <: Traversable[_]] extends TraversableViewTempla object TraversableView { class NoBuilder[A] extends Builder[A, Nothing] { - def +=(elem: A) {} + def +=(elem: A): this.type = this def elements: Iterator[A] = Iterator.empty def result() = throw new UnsupportedOperationException("TraversableView.Builder.result") def clear() {} diff --git a/src/library/scala/collection/generic/TraversableViewTemplate.scala b/src/library/scala/collection/generic/TraversableViewTemplate.scala index dd38fe98b8..29fa718ae5 100644 --- a/src/library/scala/collection/generic/TraversableViewTemplate.scala +++ b/src/library/scala/collection/generic/TraversableViewTemplate.scala @@ -41,7 +41,7 @@ self => trait Sliced extends Transformed[A] { protected[this] val from: Int protected[this] val until: Int - override def foreach(f: A => Unit) { + override def foreach[C](f: A => C) { var index = 0 for (x <- self) { if (from <= index) { @@ -58,7 +58,7 @@ self => trait Mapped[B] extends Transformed[B] { protected[this] val mapping: A => B - override def foreach(f: B => Unit) { + override def foreach[C](f: B => C) { for (x <- self) f(mapping(x)) } @@ -67,7 +67,7 @@ self => trait FlatMapped[B] extends Transformed[B] { protected[this] val mapping: A => Traversable[B] - override def foreach(f: B => Unit) { + override def foreach[C](f: B => C) { for (x <- self) for (y <- mapping(x)) f(y) @@ -77,7 +77,7 @@ self => trait Appended[B >: A] extends Transformed[B] { protected[this] val rest: Traversable[B] - override def foreach(f: B => Unit) { + override def foreach[C](f: B => C) { for (x <- self) f(x) for (x <- rest) f(x) } @@ -86,7 +86,7 @@ self => trait Filtered extends Transformed[A] { protected[this] val pred: A => Boolean - override def foreach(f: A => Unit) { + override def foreach[C](f: A => C) { for (x <- self) if (pred(x)) f(x) } @@ -95,7 +95,7 @@ self => trait TakenWhile extends Transformed[A] { protected[this] val pred: A => Boolean - override def foreach(f: A => Unit) { + override def foreach[C](f: A => C) { for (x <- self) { if (!pred(x)) return f(x) @@ -106,7 +106,7 @@ self => trait DroppedWhile extends Transformed[A] { protected[this] val pred: A => Boolean - override def foreach(f: A => Unit) { + override def foreach[C](f: A => C) { var go = false for (x <- self) { if (!go && !pred(x)) go = true diff --git a/src/library/scala/collection/generic/VectorTemplate.scala b/src/library/scala/collection/generic/VectorTemplate.scala index 96c9e125dc..2c1d8496d5 100644 --- a/src/library/scala/collection/generic/VectorTemplate.scala +++ b/src/library/scala/collection/generic/VectorTemplate.scala @@ -56,7 +56,7 @@ trait VectorTemplate[+A, +This <: VectorTemplate[A, This] with Vector[A]] extend override def isEmpty: Boolean = { length == 0 } - override def foreach(f: A => Unit): Unit = { + override def foreach[U](f: A => U): Unit = { var i = 0 val len = length while (i < len) { f(this(i)); i += 1 } diff --git a/src/library/scala/collection/generic/VectorViewTemplate.scala b/src/library/scala/collection/generic/VectorViewTemplate.scala index 0d21b98691..a7f4c5991f 100644 --- a/src/library/scala/collection/generic/VectorViewTemplate.scala +++ b/src/library/scala/collection/generic/VectorViewTemplate.scala @@ -28,39 +28,39 @@ trait VectorViewTemplate[+A, trait Sliced extends Transformed[A] with super.Sliced { /** Override to use Vector's foreach; todo: see whether this is really faster */ - override def foreach(f: A => Unit) = super[Transformed].foreach(f) + override def foreach[U](f: A => U) = super[Transformed].foreach(f) } trait Mapped[B] extends Transformed[B] with super.Mapped[B] { - override def foreach(f: B => Unit) = super[Transformed].foreach(f) + override def foreach[U](f: B => U) = super[Transformed].foreach(f) } trait FlatMapped[B] extends Transformed[B] with super.FlatMapped[B] { - override def foreach(f: B => Unit) = super[Transformed].foreach(f) + override def foreach[U](f: B => U) = super[Transformed].foreach(f) } trait Appended[B >: A] extends Transformed[B] with super.Appended[B] { - override def foreach(f: B => Unit) = super[Transformed].foreach(f) + override def foreach[U](f: B => U) = super[Transformed].foreach(f) } trait Filtered extends Transformed[A] with super.Filtered { - override def foreach(f: A => Unit) = super[Transformed].foreach(f) + override def foreach[U](f: A => U) = super[Transformed].foreach(f) } trait TakenWhile extends Transformed[A] with super.TakenWhile { - override def foreach(f: A => Unit) = super[Transformed].foreach(f) + override def foreach[U](f: A => U) = super[Transformed].foreach(f) } trait DroppedWhile extends Transformed[A] with super.DroppedWhile { - override def foreach(f: A => Unit) = super[Transformed].foreach(f) + override def foreach[U](f: A => U) = super[Transformed].foreach(f) } trait Reversed extends Transformed[A] with super.Reversed { - override def foreach(f: A => Unit) = super[Transformed].foreach(f) + override def foreach[U](f: A => U) = super[Transformed].foreach(f) } trait Patched[B >: A] extends Transformed[B] with super.Patched[B] { - override def foreach(f: B => Unit) = super[Transformed].foreach(f) + override def foreach[U](f: B => U) = super[Transformed].foreach(f) } /** Boilerplate method, to override in each subclass diff --git a/src/library/scala/collection/immutable/BitSet.scala b/src/library/scala/collection/immutable/BitSet.scala index 575d8dfd7d..ac6970a9f9 100644 --- a/src/library/scala/collection/immutable/BitSet.scala +++ b/src/library/scala/collection/immutable/BitSet.scala @@ -13,12 +13,38 @@ package scala.collection.immutable import BitSet._ import generic._ +import BitSetTemplate.{LogWL, updateArray} /** a base class for immutable bit sets */ abstract class BitSet extends Set[Int] with collection.BitSet with BitSetTemplate[BitSet] { override def empty = BitSet.empty def fromArray(elems: Array[Long]): BitSet = BitSet.fromArray(elems) + + /** Update word at index `idx`; enlarge set if `idx` outside range of set + */ + protected def updateWord(idx: Int, w: Long): BitSet + + /** Adds element to bitset, returning a new set. + */ + def plus (elem: Int): BitSet = { + require(elem >= 0) + if (contains(elem)) this + else { + val idx = elem >> LogWL + updateWord(idx, word(idx) | (1L << elem)) + } + } + + /** Removes element from bitset, returning a new set + */ + def minus (elem: Int): BitSet = { + require(elem >= 0) + if (contains(elem)) { + val idx = elem >> LogWL + updateWord(idx, word(idx) & ~(1L << elem)) + } else this + } } /** A factory object for bitsets */ @@ -41,25 +67,13 @@ object BitSet { private val hashSeed = "BitSet".hashCode - private def updateArray(elems: Array[Long], idx: Int, w: Long): BitSet = { - var len = elems.length - while (len > 0 && (elems(len - 1) == 0L || w == 0L && idx == len - 1)) len -= 1 - var newlen = len - if (idx >= newlen && w != 0L) newlen = idx + 1 - val newelems = new Array[Long](newlen) - Array.copy(elems, 0, newelems, 0, len) - if (idx < newlen) newelems(idx) = w - else assert(w == 0L) - fromArray(newelems) - } - class BitSet1(val elems: Long) extends BitSet { protected def nwords = 1 protected def word(idx: Int) = if (idx == 0) elems else 0L protected def updateWord(idx: Int, w: Long): BitSet = if (idx == 0) new BitSet1(w) else if (idx == 1) new BitSet2(elems, w) - else updateArray(Array(elems), idx, w) + else fromArray(updateArray(Array(elems), idx, w)) } class BitSet2(val elems0: Long, elems1: Long) extends BitSet { @@ -68,13 +82,13 @@ object BitSet { protected def updateWord(idx: Int, w: Long): BitSet = if (idx == 0) new BitSet2(w, elems1) else if (idx == 1) new BitSet2(elems0, w) - else updateArray(Array(elems0, elems1), idx, w) + else fromArray(updateArray(Array(elems0, elems1), idx, w)) } class BitSetN(val elems: Array[Long]) extends BitSet { protected def nwords = elems.length protected def word(idx: Int) = if (idx < nwords) elems(idx) else 0L - protected def updateWord(idx: Int, w: Long): BitSet = updateArray(elems, idx, w) + protected def updateWord(idx: Int, w: Long): BitSet = fromArray(updateArray(elems, idx, w)) } } diff --git a/src/library/scala/collection/immutable/FlexMap.scala b/src/library/scala/collection/immutable/FlexMap.scala index eb982734a3..7ea21b3c64 100644 --- a/src/library/scala/collection/immutable/FlexMap.scala +++ b/src/library/scala/collection/immutable/FlexMap.scala @@ -31,8 +31,8 @@ object FlexMap extends ImmutableMapFactory[Map] { override def size: Int = 0 def get(key: A): Option[B] = None def elements: Iterator[(A, B)] = Iterator.empty - def add [B1 >: B] (key: A, value: B1): Map[A, B1] = new Map1(key, value) - def - (key: A): Map[A, B] = this + def updated [B1 >: B] (key: A, value: B1): Map[A, B1] = new Map1(key, value) + def minus (key: A): Map[A, B] = this } @serializable @@ -41,12 +41,12 @@ object FlexMap extends ImmutableMapFactory[Map] { def get(key: A): Option[B] = if (key == key1) Some(value1) else None def elements = Iterator((key1, value1)) - def add [B1 >: B] (key: A, value: B1): Map[A, B1] = + def updated [B1 >: B] (key: A, value: B1): Map[A, B1] = if (key == key1) new Map1(key1, value) else new Map2(key1, value1, key, value) - def - (key: A): Map[A, B] = + def minus (key: A): Map[A, B] = if (key == key1) empty else this - override def foreach(f: ((A, B)) => Unit): Unit = { + override def foreach[U](f: ((A, B)) => U): Unit = { f((key1, value1)) } } @@ -59,15 +59,15 @@ object FlexMap extends ImmutableMapFactory[Map] { else if (key == key2) Some(value2) else None def elements = Iterator((key1, value1), (key2, value2)) - def add [B1 >: B] (key: A, value: B1): Map[A, B1] = + def updated [B1 >: B] (key: A, value: B1): Map[A, B1] = if (key == key1) new Map2(key1, value, key2, value2) else if (key == key2) new Map2(key1, value1, key2, value) else new Map3(key1, value1, key2, value2, key, value) - def - (key: A): Map[A, B] = + def minus (key: A): Map[A, B] = if (key == key1) new Map1(key2, value2) else if (key == key2) new Map1(key1, value1) else this - override def foreach(f: ((A, B)) => Unit): Unit = { + override def foreach[U](f: ((A, B)) => U): Unit = { f((key1, value1)); f((key2, value2)) } } @@ -81,17 +81,17 @@ object FlexMap extends ImmutableMapFactory[Map] { else if (key == key3) Some(value3) else None def elements = Iterator((key1, value1), (key2, value2), (key3, value3)) - def add [B1 >: B] (key: A, value: B1): Map[A, B1] = + def updated [B1 >: B] (key: A, value: B1): Map[A, B1] = if (key == key1) new Map3(key1, value, key2, value2, key3, value3) else if (key == key2) new Map3(key1, value1, key2, value, key3, value3) else if (key == key3) new Map3(key1, value1, key2, value2, key3, value) else new Map4(key1, value1, key2, value2, key3, value3, key, value) - def - (key: A): Map[A, B] = + def minus (key: A): Map[A, B] = if (key == key1) new Map2(key2, value2, key3, value3) else if (key == key2) new Map2(key1, value1, key3, value3) else if (key == key3) new Map2(key1, value1, key2, value2) else this - override def foreach(f: ((A, B)) => Unit): Unit = { + override def foreach[U](f: ((A, B)) => U): Unit = { f((key1, value1)); f((key2, value2)); f((key3, value3)) } } @@ -106,19 +106,19 @@ object FlexMap extends ImmutableMapFactory[Map] { else if (key == key4) Some(value4) else None def elements = Iterator((key1, value1), (key2, value2), (key3, value3), (key4, value4)) - def add [B1 >: B] (key: A, value: B1): Map[A, B1] = + def updated [B1 >: B] (key: A, value: B1): Map[A, B1] = if (key == key1) new Map4(key1, value, key2, value2, key3, value3, key4, value4) else if (key == key2) new Map4(key1, value1, key2, value, key3, value3, key4, value4) else if (key == key3) new Map4(key1, value1, key2, value2, key3, value, key4, value4) else if (key == key4) new Map4(key1, value1, key2, value2, key3, value3, key4, value) else new HashMap + ((key1, value1), (key2, value2), (key3, value3), (key4, value4), (key, value)) - def - (key: A): Map[A, B] = + def minus (key: A): Map[A, B] = if (key == key1) new Map3(key2, value2, key3, value3, key4, value4) else if (key == key2) new Map3(key1, value1, key3, value3, key4, value4) else if (key == key3) new Map3(key1, value1, key2, value2, key4, value4) else if (key == key4) new Map3(key1, value1, key2, value2, key3, value3) else this - override def foreach(f: ((A, B)) => Unit): Unit = { + override def foreach[U](f: ((A, B)) => U): Unit = { f((key1, value1)); f((key2, value2)); f((key3, value3)); f((key4, value4)) } } diff --git a/src/library/scala/collection/immutable/FlexSet.scala b/src/library/scala/collection/immutable/FlexSet.scala index 350ac96854..b1410961b2 100644 --- a/src/library/scala/collection/immutable/FlexSet.scala +++ b/src/library/scala/collection/immutable/FlexSet.scala @@ -30,10 +30,10 @@ object FlexSet extends SetFactory[Set] { class EmptySet[A] extends FlexSet[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 plus (elem: A): Set[A] = new Set1(elem) + def minus (elem: A): Set[A] = this def elements: Iterator[A] = Iterator.empty - override def foreach(f: A => Unit): Unit = {} + override def foreach[U](f: A => U): Unit = {} } /** An optimized representation for immutable sets of size 1 */ @@ -42,15 +42,15 @@ object FlexSet extends SetFactory[Set] { override def size: Int = 1 def contains(elem: A): Boolean = elem == elem1 - def + (elem: A): Set[A] = + def plus (elem: A): Set[A] = if (contains(elem)) this else new Set2(elem1, elem) - def - (elem: A): Set[A] = + def minus (elem: A): Set[A] = if (elem == elem1) new EmptySet[A] else this def elements: Iterator[A] = Iterator(elem1) - override def foreach(f: A => Unit): Unit = { + override def foreach[U](f: A => U): Unit = { f(elem1) } } @@ -61,16 +61,16 @@ object FlexSet extends SetFactory[Set] { override def size: Int = 2 def contains(elem: A): Boolean = elem == elem1 || elem == elem2 - def + (elem: A): Set[A] = + def plus (elem: A): Set[A] = if (contains(elem)) this else new Set3(elem1, elem2, elem) - def - (elem: A): Set[A] = + def minus (elem: A): Set[A] = if (elem == elem1) new Set1(elem2) else if (elem == elem2) new Set1(elem1) else this def elements: Iterator[A] = Iterator(elem1, elem2) - override def foreach(f: A => Unit): Unit = { + override def foreach[U](f: A => U): Unit = { f(elem1); f(elem2) } } @@ -81,17 +81,17 @@ object FlexSet extends SetFactory[Set] { override def size: Int = 3 def contains(elem: A): Boolean = elem == elem1 || elem == elem2 || elem == elem3 - def + (elem: A): Set[A] = + def plus (elem: A): Set[A] = if (contains(elem)) this else new Set4(elem1, elem2, elem3, elem) - def - (elem: A): Set[A] = + def minus (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 elements: Iterator[A] = Iterator(elem1, elem2, elem3) - override def foreach(f: A => Unit): Unit = { + override def foreach[U](f: A => U): Unit = { f(elem1); f(elem2); f(elem3) } } @@ -102,10 +102,10 @@ object FlexSet extends SetFactory[Set] { override def size: Int = 4 def contains(elem: A): Boolean = elem == elem1 || elem == elem2 || elem == elem3 || elem == elem4 - def + (elem: A): Set[A] = + def plus (elem: A): Set[A] = if (contains(elem)) this else new HashSet[A] + (elem1, elem2, elem3, elem4, elem) - def - (elem: A): Set[A] = + def minus (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) @@ -113,7 +113,7 @@ object FlexSet extends SetFactory[Set] { else this def elements: Iterator[A] = Iterator(elem1, elem2, elem3, elem4) - override def foreach(f: A => Unit): Unit = { + override def foreach[U](f: A => U): Unit = { f(elem1); f(elem2); f(elem3); f(elem4) } } diff --git a/src/library/scala/collection/immutable/HashMap.scala b/src/library/scala/collection/immutable/HashMap.scala index 6a515e829c..6f0277a43f 100644 --- a/src/library/scala/collection/immutable/HashMap.scala +++ b/src/library/scala/collection/immutable/HashMap.scala @@ -53,7 +53,7 @@ class HashMap[A, +B] extends Map[A,B] with ImmutableMapTemplate[A, B, HashMap[A, else Some(getValue(e)) } - def add [B1 >: B] (key: A, value: B1): HashMap[A, B1] = synchronized { + def updated [B1 >: B] (key: A, value: B1): HashMap[A, B1] = synchronized { makeCopyIfUpdated() val e = findEntry(key) if (e == null) { @@ -70,7 +70,7 @@ class HashMap[A, +B] extends Map[A,B] with ImmutableMapTemplate[A, B, HashMap[A, * @param kv the key/value pair * @return A new map with the new binding added to this map */ - override def + [B1 >: B] (kv: (A, B1)): HashMap[A, B1] = add(kv._1, kv._2) + override def + [B1 >: B] (kv: (A, B1)): HashMap[A, B1] = updated(kv._1, kv._2) /** Adds two or more elements to this collection and returns * either the collection itself (if it is mutable), or a new collection @@ -81,9 +81,9 @@ class HashMap[A, +B] extends Map[A,B] with ImmutableMapTemplate[A, B, HashMap[A, * @param elems the remaining elements to add. */ override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): HashMap[A, B1] = - this + elem1 + elem2 ++ collection.Iterable.fromOld(elems) + this + elem1 + elem2 ++ elems - def - (key: A): HashMap[A, B] = synchronized { + def minus (key: A): HashMap[A, B] = synchronized { makeCopyIfUpdated() val e = findEntry(key) if (e == null) this diff --git a/src/library/scala/collection/immutable/HashSet.scala b/src/library/scala/collection/immutable/HashSet.scala index 3abffa6ec4..9433ec9c8f 100644 --- a/src/library/scala/collection/immutable/HashSet.scala +++ b/src/library/scala/collection/immutable/HashSet.scala @@ -46,7 +46,7 @@ class HashSet[A] extends Set[A] with SetTemplate[A, HashSet[A]] with mutable.Fla m.containsEntry(elem) } - def + (elem: A): HashSet[A] = synchronized { + def plus (elem: A): HashSet[A] = synchronized { makeCopyIfUpdated() if (containsEntry(elem)) this else { @@ -56,7 +56,7 @@ class HashSet[A] extends Set[A] with SetTemplate[A, HashSet[A]] with mutable.Fla } } - def - (elem: A): HashSet[A] = synchronized { + def minus (elem: A): HashSet[A] = synchronized { makeCopyIfUpdated() if (!containsEntry(elem)) this else { diff --git a/src/library/scala/collection/immutable/IntMap.scala b/src/library/scala/collection/immutable/IntMap.scala index 69c68eb135..e243a63bf3 100644 --- a/src/library/scala/collection/immutable/IntMap.scala +++ b/src/library/scala/collection/immutable/IntMap.scala @@ -42,7 +42,7 @@ object IntMap{ def empty[T] : IntMap[T] = IntMap.Nil; def singleton[T](key : Int, value : T) : IntMap[T] = IntMap.Tip(key, value); def apply[T](elems : (Int, T)*) : IntMap[T] = - elems.foldLeft(empty[T])((x, y) => x.add(y._1, y._2)); + elems.foldLeft(empty[T])((x, y) => x.updated(y._1, y._2)); private[immutable] case object Nil extends IntMap[Nothing]{ @@ -153,7 +153,7 @@ sealed abstract class IntMap[+T] extends scala.collection.immutable.Map[Int, T] /** * Loops over the key, value pairs of the map in unsigned order of the keys. */ - override final def foreach(f : ((Int, T)) => Unit) : Unit = this match { + override final def foreach[U](f : ((Int, T)) => U) : Unit = this match { case IntMap.Bin(_, _, left, right) => {left.foreach(f); right.foreach(f); } case IntMap.Tip(key, value) => f((key, value)); case IntMap.Nil => {}; @@ -239,17 +239,17 @@ sealed abstract class IntMap[+T] extends scala.collection.immutable.Map[Int, T] case IntMap.Nil => error("key not found"); } - def add[S >: T](key : Int, value : S) : IntMap[S] = this match { + def updated[S >: T](key : Int, value : S) : IntMap[S] = this match { case IntMap.Bin(prefix, mask, left, right) => if (!hasMatch(key, prefix, mask)) join(key, IntMap.Tip(key, value), prefix, this); - else if (zero(key, mask)) IntMap.Bin(prefix, mask, left.add(key, value), right) - else IntMap.Bin(prefix, mask, left, right.add(key, value)); + else if (zero(key, mask)) IntMap.Bin(prefix, mask, left.updated(key, value), right) + else IntMap.Bin(prefix, mask, left, right.updated(key, value)); case IntMap.Tip(key2, value2) => if (key == key2) IntMap.Tip(key, value); else join(key, IntMap.Tip(key, value), key2, this); case IntMap.Nil => IntMap.Tip(key, value); } - /** @deprecated use add instead */ - @deprecated override def update[S >: T](key: Int, value: S): IntMap[S] = add(key, value) + /** @deprecated use updated instead */ + @deprecated override def update[S >: T](key: Int, value: S): IntMap[S] = updated(key, value) /** * Updates the map, using the provided function to resolve conflicts if the key is already present. @@ -272,7 +272,7 @@ sealed abstract class IntMap[+T] extends scala.collection.immutable.Map[Int, T] case IntMap.Nil => IntMap.Tip(key, value); } - def -(key : Int) : IntMap[T] = this match { + def minus (key : Int) : IntMap[T] = this match { case IntMap.Bin(prefix, mask, left, right) => if (!hasMatch(key, prefix, mask)) this; else if (zero(key, mask)) bin(prefix, mask, left - key, right); diff --git a/src/library/scala/collection/immutable/ListMap.scala b/src/library/scala/collection/immutable/ListMap.scala index 1451bfef47..389c0c63bb 100644 --- a/src/library/scala/collection/immutable/ListMap.scala +++ b/src/library/scala/collection/immutable/ListMap.scala @@ -60,13 +60,13 @@ class ListMap[A, +B] extends Map[A, B] with ImmutableMapTemplate[A, B, ListMap[A * @param key the key element of the updated entry. * @param value the value element of the updated entry. */ - def add [B1 >: B] (key: A, value: B1): ListMap[A, B1] = new Node[B1](key, value) + def updated [B1 >: B] (key: A, value: B1): ListMap[A, B1] = new Node[B1](key, value) /** Add a key/value pair to this map. * @param kv the key/value pair * @return A new map with the new binding added to this map */ - override def + [B1 >: B] (kv: (A, B1)): ListMap[A, B1] = add(kv._1, kv._2) + override def + [B1 >: B] (kv: (A, B1)): ListMap[A, B1] = updated(kv._1, kv._2) /** Adds two or more elements to this collection and returns * either the collection itself (if it is mutable), or a new collection @@ -85,7 +85,7 @@ class ListMap[A, +B] extends Map[A, B] with ImmutableMapTemplate[A, B, ListMap[A * * @param key a map without a mapping for the given key. */ - def - (key: A): ListMap[A, B] = this + def minus (key: A): ListMap[A, B] = this /** Returns an iterator over key-value pairs. */ @@ -144,7 +144,7 @@ class ListMap[A, +B] extends Map[A, B] with ImmutableMapTemplate[A, B, ListMap[A * @param k ... * @param v ... */ - override def add [B2 >: B1](k: A, v: B2): ListMap[A, B2] = { + override def updated [B2 >: B1](k: A, v: B2): ListMap[A, B2] = { val m = if (contains(k)) this - k else this new m.Node[B2](k, v) } @@ -156,7 +156,7 @@ class ListMap[A, +B] extends Map[A, B] with ImmutableMapTemplate[A, B, ListMap[A * @param k ... * @return ... */ - override def - (k: A): ListMap[A, B1] = + override def minus (k: A): ListMap[A, B1] = if (k == key) next else { diff --git a/src/library/scala/collection/immutable/ListSet.scala b/src/library/scala/collection/immutable/ListSet.scala index f81a1271ed..6872d96464 100644 --- a/src/library/scala/collection/immutable/ListSet.scala +++ b/src/library/scala/collection/immutable/ListSet.scala @@ -52,12 +52,12 @@ class ListSet[A] extends Set[A] with SetTemplate[A, ListSet[A]] { self => /** This method creates a new set with an additional element. */ - def +(elem: A): ListSet[A] = new Node(elem) + def plus (elem: A): ListSet[A] = new Node(elem) /** <code>-</code> can be used to remove a single element from * a set. */ - def -(elem: A): ListSet[A] = this + def minus (elem: A): ListSet[A] = this /** Creates a new iterator over all elements contained in this set. * @@ -106,12 +106,12 @@ class ListSet[A] extends Set[A] with SetTemplate[A, ListSet[A]] { self => /** This method creates a new set with an additional element. */ - override def +(e: A): ListSet[A] = if (contains(e)) this else new Node(e) + override def plus(e: A): ListSet[A] = if (contains(e)) this else new Node(e) /** <code>-</code> can be used to remove a single element from * a set. */ - override def -(e: A): ListSet[A] = if (e == elem) self else { + override def minus(e: A): ListSet[A] = if (e == elem) self else { val tail = self - e; new tail.Node(elem) } diff --git a/src/library/scala/collection/immutable/LongMap.scala b/src/library/scala/collection/immutable/LongMap.scala index 043fe2e882..9e8ac7627e 100644 --- a/src/library/scala/collection/immutable/LongMap.scala +++ b/src/library/scala/collection/immutable/LongMap.scala @@ -43,7 +43,7 @@ object LongMap{ def empty[T] : LongMap[T] = LongMap.Nil; def singleton[T](key : Long, value : T) : LongMap[T] = LongMap.Tip(key, value); def apply[T](elems : (Long, T)*) : LongMap[T] = - elems.foldLeft(empty[T])((x, y) => x.add(y._1, y._2)); + elems.foldLeft(empty[T])((x, y) => x.updated(y._1, y._2)); private[immutable] case object Nil extends LongMap[Nothing]{ @@ -154,7 +154,7 @@ sealed abstract class LongMap[+T] extends scala.collection.immutable.Map[Long, T /** * Loops over the key, value pairs of the map in unsigned order of the keys. */ - override final def foreach(f : ((Long, T)) => Unit) : Unit = this match { + override final def foreach[U](f : ((Long, T)) => U) : Unit = this match { case LongMap.Bin(_, _, left, right) => {left.foreach(f); right.foreach(f); } case LongMap.Tip(key, value) => f((key, value)); case LongMap.Nil => {}; @@ -240,7 +240,7 @@ sealed abstract class LongMap[+T] extends scala.collection.immutable.Map[Long, T case LongMap.Nil => error("key not found"); } - def add[S >: T](key : Long, value : S) : LongMap[S] = this match { + def updated[S >: T](key : Long, value : S) : LongMap[S] = this match { case LongMap.Bin(prefix, mask, left, right) => if (!hasMatch(key, prefix, mask)) join(key, LongMap.Tip(key, value), prefix, this); else if (zero(key, mask)) LongMap.Bin(prefix, mask, left.update(key, value), right) else LongMap.Bin(prefix, mask, left, right.update(key, value)); @@ -249,8 +249,8 @@ sealed abstract class LongMap[+T] extends scala.collection.immutable.Map[Long, T case LongMap.Nil => LongMap.Tip(key, value); } - /** @deprecated use add instead */ - @deprecated override def update[S >: T](key: Long, value: S): LongMap[S] = add(key, value) + /** @deprecated use updated instead */ + @deprecated override def update[S >: T](key: Long, value: S): LongMap[S] = updated(key, value) /** * Updates the map, using the provided function to resolve conflicts if the key is already present. @@ -273,7 +273,7 @@ sealed abstract class LongMap[+T] extends scala.collection.immutable.Map[Long, T case LongMap.Nil => LongMap.Tip(key, value); } - def -(key : Long) : LongMap[T] = this match { + def minus(key : Long) : LongMap[T] = this match { case LongMap.Bin(prefix, mask, left, right) => if (!hasMatch(key, prefix, mask)) this; else if (zero(key, mask)) bin(prefix, mask, left - key, right); diff --git a/src/library/scala/collection/immutable/Map.scala b/src/library/scala/collection/immutable/Map.scala index 47126df856..ba8ad9ab6b 100644 --- a/src/library/scala/collection/immutable/Map.scala +++ b/src/library/scala/collection/immutable/Map.scala @@ -25,31 +25,15 @@ trait Map[A, +B] extends Iterable[(A, B)] * @param value the value * @return A new map with the new binding added to this map */ - def add [B1 >: B](key: A, value: B1): Map[A, B1] - - /** Add a key/value pair to this map. - * @param kv the key/value pair - * @return A new map with the new binding added to this map - */ - override def + [B1 >: B] (kv: (A, B1)): Map[A, B1] = add(kv._1, kv._2) - - /** Adds two or more elements to this collection and returns - * either the collection itself (if it is mutable), or a new collection - * with the added elements. - * - * @param elem1 the first element to add. - * @param elem2 the second element to add. - * @param elems the remaining elements to add. - */ - override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): Map[A, B1] = - this + elem1 + elem2 ++ collection.Iterable.fromOld(elems) + def updated [B1 >: B](key: A, value: B1): Map[A, B1] /** A hash method compatible with <code>equals</code> */ override def hashCode() = (Map.hashSeed /: this) (_ * 41 + _.hashCode) - /** The same map with a given default function */ + + /** The same map with a given default function !!! todo: move to general maps? */ def withDefault[B1 >: B](d: A => B1): Map[A, B1] = new Map.WithDefault[A, B1](this, d) /** The same map with a given default value */ @@ -68,8 +52,8 @@ object Map extends ImmutableMapFactory[Map] { def get(key: A) = underlying.get(key) def elements = underlying.elements override def empty = new WithDefault(underlying.empty, d) - def add[B1 >: B](key: A, value: B1): WithDefault[A, B1] = new WithDefault[A, B1](underlying.add[B1](key, value), d) - def - (key: A): WithDefault[A, B] = new WithDefault(underlying - key, d) + def updated[B1 >: B](key: A, value: B1): WithDefault[A, B1] = new WithDefault[A, B1](underlying.updated[B1](key, value), d) + def minus (key: A): WithDefault[A, B] = new WithDefault(underlying - key, d) override def default(key: A): B = d(key) } } diff --git a/src/library/scala/collection/immutable/RedBlack.scala b/src/library/scala/collection/immutable/RedBlack.scala index efe85a12b1..c50dba65f5 100644 --- a/src/library/scala/collection/immutable/RedBlack.scala +++ b/src/library/scala/collection/immutable/RedBlack.scala @@ -28,7 +28,7 @@ abstract class RedBlack[A] { def lookup(x: A): Tree[B] def update[B1 >: B](k: A, v: B1): Tree[B1] = blacken(upd(k, v)) def delete(k: A): Tree[B] = del(k) - def foreach(f: (A, B) => Unit) + def foreach[U](f: (A, B) => U) /** @deprecated use foreach instead */ @deprecated def visit[T](input : T)(f : (T,A,B) => Tuple2[Boolean,T]) : Tuple2[Boolean,T]; def toStream: Stream[(A,B)] @@ -90,7 +90,7 @@ abstract class RedBlack[A] { def elements: Iterator[Pair[A, B]] = left.elements append Iterator.single(Pair(key, value)) append right.elements - def foreach(f: (A, B) => Unit) { + def foreach[U](f: (A, B) => U) { left foreach f f(key, value) right foreach f @@ -132,7 +132,7 @@ abstract class RedBlack[A] { def elements: Iterator[Pair[A, Nothing]] = Iterator.empty def toStream: Stream[(A,Nothing)] = Stream.empty - def foreach(f: (A, Nothing) => Unit) {} + def foreach[U](f: (A, Nothing) => U) {} /** @deprecated use foreach instead */ @deprecated diff --git a/src/library/scala/collection/immutable/SortedMap.scala b/src/library/scala/collection/immutable/SortedMap.scala index 00970464e6..f1e1c4b9b8 100644 --- a/src/library/scala/collection/immutable/SortedMap.scala +++ b/src/library/scala/collection/immutable/SortedMap.scala @@ -37,26 +37,79 @@ trait SortedMap[A, +B] extends Map[A, B] * @param value the value * @return A new map with the new binding added to this map */ - def add [B1 >: B](key: A, value: B1): SortedMap[A, B1] + def updated [B1 >: B](key: A, value: B1): SortedMap[A, B1] - /** Add a key/value pair to this map. + /** Add a key/value pair to this map, returning a new map. + * @param kv the key/value pair + * @return A new map with the new binding added to this map + */ + override def plus [B1 >: B] (kv: (A, B1)): SortedMap[A, B1] = updated(kv._1, kv._2) + + /** Add a key/value pair to this map, returning a new map. * @param kv the key/value pair * @return A new map with the new binding added to this map + * @note same as `plus` + */ + override def + [B1 >: B] (kv: (A, B1)): SortedMap[A, B1] = updated(kv._1, kv._2) + + /** Adds two or more elements to this collection and returns + * a new collection. + * + * @param elem1 the first element to add. + * @param elem2 the second element to add. + * @param elems the remaining elements to add. */ - override def + [B1 >: B] (kv: (A, B1)): SortedMap[A, B1] = add(kv._1, kv._2) + override def plus [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): SortedMap[A, B1] = + this plus elem1 plus elem2 plusAll elems /** Adds two or more elements to this collection and returns - * either the collection itself (if it is mutable), or a new collection - * with the added elements. + * a new collection. * * @param elem1 the first element to add. * @param elem2 the second element to add. * @param elems the remaining elements to add. */ - override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): SortedMap[A, B1] = { - var m = this + elem1 + elem2; - for (e <- elems) m = m + e - m - } + override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): SortedMap[A, B1] = + plus(elem1, elem2, elems: _*) + + /** Adds a number of elements provided by a traversable object + * and returns a new collection with the added elements. + * + * @param elems the traversable object. + */ + override def plusAll[B1 >: B](elems: collection.Traversable[(A, B1)]): SortedMap[A, B1] = + ((thisCollection: SortedMap[A, B1]) /: elems) (_ plus _) + + /** Adds a number of elements provided by a traversable object + * and returns a new collection with the added elements. + * + * @param elems the traversable object. + * @note same as `plusAll` + * @note This is a more efficient version of Traversable.++ which avoids + * copying of the collection's elements. However, it applies only if + * the type of the added elements is a subtype of the element type of the + * collection. + */ + override def ++ [B1 >: B](elems: collection.Traversable[(A, B1)]): SortedMap[A, B1] = plusAll(elems) + + /** Adds a number of elements provided by an iterator + * and returns a new collection with the added elements. + * + * @param iter the iterator + */ + override def plusAll[B1 >: B] (iter: Iterator[(A, B1)]): SortedMap[A, B1] = + ((thisCollection: SortedMap[A, B1]) /: iter) (_ plus _) + + /** Adds a number of elements provided by an iterator + * and returns a new collection with the added elements. + * + * @param iter the iterator + * @note same as `plusAll` + * @note This is a more efficient version of Traversable.++ which avoids + * copying of the collection's elements. However, it applies only if + * the type of the added elements is a subtype of the element type of the + * collection. + */ + override def ++ [B1 >: B](iter: Iterator[(A, B1)]): SortedMap[A, B1] = plusAll(iter) } diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala index 612978a4b7..5e678edddf 100644 --- a/src/library/scala/collection/immutable/Stream.scala +++ b/src/library/scala/collection/immutable/Stream.scala @@ -508,12 +508,12 @@ object Stream extends SequenceFactory[Stream] { /** The concatenation of a sequence of streams * @deprecated use xs.flatten instead */ - @deprecated def concat[A](xs: Iterable[Stream[A]]): Stream[A] = concat(xs.elements) + def concat[A](xs: Iterable[Stream[A]]): Stream[A] = concat(xs.elements) /** The concatenation of all streams returned by an iterator * @deprecated use xs.toStream.flatten instead */ - @deprecated def concat[A](xs: Iterator[Stream[A]]): Stream[A] = + def concat[A](xs: Iterator[Stream[A]]): Stream[A] = if (xs.hasNext) xs.next append concat(xs) else empty diff --git a/src/library/scala/collection/immutable/TreeHashMap.scala b/src/library/scala/collection/immutable/TreeHashMap.scala index 80a227b9ad..8ebc680e6d 100644 --- a/src/library/scala/collection/immutable/TreeHashMap.scala +++ b/src/library/scala/collection/immutable/TreeHashMap.scala @@ -71,7 +71,7 @@ class TreeHashMap[Key, +Value] private (private val underlying : IntMap[AssocMap // todo: probably drop to make conform with general equals/hashcode policy override def hashCode = underlying.hashCode - override def foreach(f : ((Key, Value)) => Unit) = underlying.foreachValue(_.foreach(f)); + override def foreach[U](f : ((Key, Value)) => U) = underlying.foreachValue(_.foreach(f)); override def toList : List[(Key, Value)] = { val buffer = new scala.collection.mutable.ListBuffer[(Key, Value)]; @@ -112,7 +112,7 @@ class TreeHashMap[Key, +Value] private (private val underlying : IntMap[AssocMap underlying.updateWith[AssocMap[Key, S]](hash(key), AssocMap.singleton[Key, S](key, value), (x, y) => y.merge(x)) ) - def -(key : Key) : TreeHashMap[Key, Value] = { + def minus(key : Key) : TreeHashMap[Key, Value] = { val h = hash(key); underlying.get(h) match { case None => this; @@ -184,7 +184,7 @@ private[collection] sealed abstract class AssocMap[Key, +Value] extends immutabl def elements : Iterator[(Key, Value)] = new AssocMapIterator(this); - override final def foreach(f : ((Key, Value)) => Unit) = this match { + override final def foreach[U](f : ((Key, Value)) => U) = this match { case Cons(key, value, tail) => { f((key, value)); tail.foreach(f); } case Nil() => {} } @@ -254,7 +254,7 @@ private[collection] sealed abstract class AssocMap[Key, +Value] extends immutabl else Cons(key, newval, newtail); } - def -(key : Key) : AssocMap[Key, Value]= this match { + def minus(key : Key) : AssocMap[Key, Value]= this match { case Nil() => this; case Cons(key2, value, tail) => if (key == key2) tail; diff --git a/src/library/scala/collection/immutable/TreeMap.scala b/src/library/scala/collection/immutable/TreeMap.scala index c991b71698..34e5e977e8 100644 --- a/src/library/scala/collection/immutable/TreeMap.scala +++ b/src/library/scala/collection/immutable/TreeMap.scala @@ -74,7 +74,7 @@ class TreeMap[A <% Ordered[A], +B](override val size: Int, t: RedBlack[A]#Tree[B * @param value ... * @return ... */ - def add [B1 >: B](key: A, value: B1): TreeMap[A, B1] = { + def updated [B1 >: B](key: A, value: B1): TreeMap[A, B1] = { val newsize = if (tree.lookup(key).isEmpty) size + 1 else size newMap(newsize, tree.update(key, value)) } @@ -83,7 +83,7 @@ class TreeMap[A <% Ordered[A], +B](override val size: Int, t: RedBlack[A]#Tree[B * @param kv the key/value pair * @return A new map with the new binding added to this map */ - override def + [B1 >: B] (kv: (A, B1)): TreeMap[A, B1] = add(kv._1, kv._2) + override def + [B1 >: B] (kv: (A, B1)): TreeMap[A, B1] = updated(kv._1, kv._2) /** Adds two or more elements to this collection and returns * either the collection itself (if it is mutable), or a new collection @@ -104,7 +104,7 @@ class TreeMap[A <% Ordered[A], +B](override val size: Int, t: RedBlack[A]#Tree[B newMap(size + 1, tree.update(key, value)) } - def - (key:A): TreeMap[A, B] = + def minus (key:A): TreeMap[A, B] = if (tree.lookup(key).isEmpty) this else newMap(size - 1, tree.delete(key)) @@ -128,7 +128,7 @@ class TreeMap[A <% Ordered[A], +B](override val size: Int, t: RedBlack[A]#Tree[B override def toStream: Stream[(A, B)] = tree.toStream - override def foreach(f : ((A,B)) => Unit) = tree foreach { case (x, y) => f(x, y) } + override def foreach[U](f : ((A,B)) => U) = tree foreach { case (x, y) => f(x, y) } } diff --git a/src/library/scala/collection/immutable/TreeSet.scala b/src/library/scala/collection/immutable/TreeSet.scala index 6dd57c43f1..84715d2363 100644 --- a/src/library/scala/collection/immutable/TreeSet.scala +++ b/src/library/scala/collection/immutable/TreeSet.scala @@ -53,7 +53,7 @@ class TreeSet[A <% Ordered[A]](override val size: Int, t: RedBlack[A]#Tree[Unit] /** A new TreeSet with the entry added is returned, */ - def + (elem: A): TreeSet[A] = { + def plus (elem: A): TreeSet[A] = { val newsize = if (tree.lookup(elem).isEmpty) size + 1 else size newSet(newsize, tree.update(elem, ())) } @@ -66,7 +66,7 @@ class TreeSet[A <% Ordered[A]](override val size: Int, t: RedBlack[A]#Tree[Unit] newSet(size + 1, tree.update(elem, ())) } - def - (elem:A): TreeSet[A] = + def minus (elem:A): TreeSet[A] = if (tree.lookup(elem).isEmpty) this else newSet(size - 1, tree.delete(elem)) @@ -86,7 +86,7 @@ class TreeSet[A <% Ordered[A]](override val size: Int, t: RedBlack[A]#Tree[Unit] override def toStream: Stream[A] = tree.toStream map (_._1) - override def foreach(f: A => Unit) = tree foreach { (x, y) => f(x) } + override def foreach[U](f: A => U) = tree foreach { (x, y) => f(x) } override def rangeImpl(from: Option[A], until: Option[A]): TreeSet[A] = { val tree = this.tree.range(from, until) diff --git a/src/library/scala/collection/mutable/ArrayBuffer.scala b/src/library/scala/collection/mutable/ArrayBuffer.scala index 8daa796cbc..2e37d55ed2 100644 --- a/src/library/scala/collection/mutable/ArrayBuffer.scala +++ b/src/library/scala/collection/mutable/ArrayBuffer.scala @@ -50,10 +50,11 @@ class ArrayBuffer[A](override protected val initialSize: Int) * * @param elem the element to append. */ - def +=(elem: A) { + def +=(elem: A): this.type = { ensureSize(size0 + 1) array(size0) = elem.asInstanceOf[AnyRef] size0 += 1 + this } /** Appends a number of elements provided by an iterable object @@ -63,12 +64,13 @@ class ArrayBuffer[A](override protected val initialSize: Int) * @param iter the iterfable object. * @return the updated buffer. */ - override def ++=(iter: Traversable[A]) = iter match { + override def ++=(iter: Traversable[A]): this.type = iter match { case v: Vector[_] => val n = v.length ensureSize(size0 + n) v.copyToArray(array.asInstanceOf[scala.Array[Any]], size0, n) size0 += n + this case _ => super.++=(iter) } diff --git a/src/library/scala/collection/mutable/ArrayStack.scala b/src/library/scala/collection/mutable/ArrayStack.scala index 984f8a4c0a..8461191ff8 100644 --- a/src/library/scala/collection/mutable/ArrayStack.scala +++ b/src/library/scala/collection/mutable/ArrayStack.scala @@ -94,7 +94,7 @@ class ArrayStack[T] private(private var table : Array[AnyRef], * * @param x The source of elements to push */ - def ++=(x : Iterable[T]) = x.foreach(this +=(_)) + def ++=(x : Iterable[T]): this.type = { x.foreach(this +=(_)); this } /** @@ -102,14 +102,14 @@ class ArrayStack[T] private(private var table : Array[AnyRef], * * @param x The source of elements to push */ - def ++=(x : Iterator[T]) = x.foreach(this +=(_)) + def ++=(x : Iterator[T]): this.type = { x.foreach(this +=(_)); this } /** * Alias for push. * * @param x The element to push */ - def +=(x : T) = push(x); + def +=(x : T): this.type = { push(x); this } @@ -162,7 +162,7 @@ class ArrayStack[T] private(private var table : Array[AnyRef], } } - override def foreach(f : T => Unit){ + override def foreach[U](f : T => U){ var currentIndex = index; while(currentIndex > 0){ currentIndex -= 1; diff --git a/src/library/scala/collection/mutable/BitSet.scala b/src/library/scala/collection/mutable/BitSet.scala index 5ec33a059c..67ca52fba8 100644 --- a/src/library/scala/collection/mutable/BitSet.scala +++ b/src/library/scala/collection/mutable/BitSet.scala @@ -1,20 +1,22 @@ package scala.collection.mutable import generic._ +import BitSetTemplate.{LogWL, updateArray} /** A class for mutable bitsets */ -class BitSet(initSize: Int) extends Set[Int] with collection.BitSet with BitSetTemplate[BitSet] { +class BitSet (protected var elems: Array[Long]) extends Set[Int] with collection.BitSet with BitSetTemplate[BitSet] with MutableSetTemplate[Int, BitSet] { override def empty = BitSet.empty - def this() = this(0) + def this(initSize: Int) = this(new Array[Long]((initSize + 63) >> 6 max 1)) - protected var elems: Array[Long] = new Array[Long]((initSize + 63) >> 6 max 1) + def this() = this(0) protected def nwords = elems.length protected def word(idx: Int): Long = if (idx < nwords) elems(idx) else 0L - protected def updateWord(idx: Int, w: Long): BitSet = { + + private def updateWord(idx: Int, w: Long) { if (idx >= nwords) { var newlen = nwords while (idx >= newlen) newlen = newlen * 2 @@ -23,22 +25,42 @@ class BitSet(initSize: Int) extends Set[Int] with collection.BitSet with BitSetT elems = elems1 } elems(idx) = w - this } - protected def fromArray(words: Array[Long]): BitSet = { - val s = new BitSet; s.elems = words; s - } - - override def += (elem: Int) { super.+(elem) } - - override def -= (elem: Int) { super.-(elem) } + protected def fromArray(words: Array[Long]): BitSet = new BitSet(words) - override def + (elem: Int): this.type = { +=(elem); this } + /** Adds element to bitset, + * @return element was already present. + */ + def put (elem: Int): Boolean = { + require(elem >= 0) + if (contains(elem)) true + else { + val idx = elem >> LogWL + updateWord(idx, word(idx) | (1L << elem)) + false + } + } - override def - (elem: Int): this.type = { -=(elem); this } + /** Removes element from bitset. + * @return element was already present. + */ + def remove (elem: Int): Boolean = { + require(elem >= 0) + if (contains(elem)) { + val idx = elem >> LogWL + updateWord(idx, word(idx) & ~(1L << elem)) + true + } else false + } def toImmutable = immutable.BitSet.fromArray(elems) + + override def clone(): BitSet = { + val elems1 = new Array[Long](elems.length) + Array.copy(elems, 0, elems1, 0, elems.length) + new BitSet(elems1) + } } /** A factory object for mutable bitsets */ diff --git a/src/library/scala/collection/mutable/BufferProxy.scala b/src/library/scala/collection/mutable/BufferProxy.scala index e6406e2bb2..f430cae83d 100644 --- a/src/library/scala/collection/mutable/BufferProxy.scala +++ b/src/library/scala/collection/mutable/BufferProxy.scala @@ -43,7 +43,7 @@ trait BufferProxy[A] extends Buffer[A] with Proxy { * * @param elem the element to append. */ - def +=(elem: A): Unit = self.+=(elem) + def +=(elem: A): this.type = { self.+=(elem); this } override def readOnly = self.readOnly @@ -61,7 +61,7 @@ trait BufferProxy[A] extends Buffer[A] with Proxy { * * @param iter the iterable object. */ - override def ++=(iter: Iterable[A]): Unit = self.++=(iter) + override def ++=(iter: Iterable[A]): this.type = { self.++=(iter); this } /** Appends a sequence of elements to this buffer. * diff --git a/src/library/scala/collection/mutable/DefaultMapModel.scala b/src/library/scala/collection/mutable/DefaultMapModel.scala index de515ded7d..ee1b978065 100644 --- a/src/library/scala/collection/mutable/DefaultMapModel.scala +++ b/src/library/scala/collection/mutable/DefaultMapModel.scala @@ -29,13 +29,13 @@ trait DefaultMapModel[A, B] extends Map[A, B] { def get(key: A): Option[B] = { val e = findEntry(key) if (e == null) None - else Some(e.value); + else Some(e.value) } - def update(key: A, value: B) { + def put(key: A, value: B): Option[B] = { val e = findEntry(key) - if (e == null) addEntry(new Entry(key, value)) - else e.value = value + if (e == null) { addEntry(new Entry(key, value)); None } + else { val v = e.value; e.value = value; Some(v) } } def elements = entries map {e => (e.key, e.value)} diff --git a/src/library/scala/collection/mutable/HashMap.scala b/src/library/scala/collection/mutable/HashMap.scala index fef1a6541e..a44a6bdcc5 100644 --- a/src/library/scala/collection/mutable/HashMap.scala +++ b/src/library/scala/collection/mutable/HashMap.scala @@ -20,7 +20,10 @@ class HashMap[A, B] extends Map[A, B] with MutableMapTemplate[A, B, HashMap[A, B override def empty: HashMap[A, B] = HashMap.empty[A, B] override def mapBuilder[A, B]: Builder[(A, B), HashMap[A, B]] = HashMap.newBuilder[A, B] - def -= (key: A) { removeEntry(key) } + def remove(key: A): Option[B] = removeEntry(key) match { + case Some(e) => Some(e.value) + case None => None + } override def clear() = super.clear() diff --git a/src/library/scala/collection/mutable/HashSet.scala b/src/library/scala/collection/mutable/HashSet.scala index d4a2434b43..971e70ad1a 100644 --- a/src/library/scala/collection/mutable/HashSet.scala +++ b/src/library/scala/collection/mutable/HashSet.scala @@ -20,7 +20,7 @@ import generic._ * @version 2.0, 31/12/2006 */ @serializable -class HashSet[A] extends Set[A] with SetTemplate[A, HashSet[A]] with FlatHashTable[A] { +class HashSet[A] extends Set[A] with MutableSetTemplate[A, HashSet[A]] with FlatHashTable[A] { override def empty = HashSet.empty override def traversableBuilder[B]: Builder[B, HashSet[B]] = HashSet.newBuilder[B] @@ -29,13 +29,13 @@ class HashSet[A] extends Set[A] with SetTemplate[A, HashSet[A]] with FlatHashTab def contains(elem: A): Boolean = containsEntry(elem) - def +=(elem: A) { addEntry(elem) } + def put(elem: A): Boolean = addEntry(elem) - def -=(elem: A) { removeEntry(elem) } + def remove(elem: A): Boolean = !removeEntry(elem).isEmpty override def clear() = super.clear() - override def foreach(f: A => Unit) { + override def foreach[U](f: A => U) { var i = 0 val len = table.length while (i < len) { diff --git a/src/library/scala/collection/mutable/ImmutableMapAdaptor.scala b/src/library/scala/collection/mutable/ImmutableMapAdaptor.scala index 0aa3dddcaa..12c8140d8a 100644 --- a/src/library/scala/collection/mutable/ImmutableMapAdaptor.scala +++ b/src/library/scala/collection/mutable/ImmutableMapAdaptor.scala @@ -52,7 +52,7 @@ extends Map[A, B] def update(key: A, value: B): Unit = { imap = imap.update(key, value) } - def -= (key: A): Unit = { imap = imap - key } + def -= (key: A): this.type = { imap = imap - key; this } override def clear(): Unit = { imap = imap.empty } diff --git a/src/library/scala/collection/mutable/ImmutableSetAdaptor.scala b/src/library/scala/collection/mutable/ImmutableSetAdaptor.scala index 894802cecf..af25841f84 100644 --- a/src/library/scala/collection/mutable/ImmutableSetAdaptor.scala +++ b/src/library/scala/collection/mutable/ImmutableSetAdaptor.scala @@ -31,7 +31,7 @@ class ImmutableSetAdaptor[A](protected var set: immutable.Set[A]) extends Set[A] def contains(elem: A): Boolean = set.contains(elem) - override def foreach(f: A => Unit): Unit = set.foreach(f) + override def foreach[U](f: A => U): Unit = set.foreach(f) override def exists(p: A => Boolean): Boolean = set.exists(p) @@ -41,7 +41,7 @@ class ImmutableSetAdaptor[A](protected var set: immutable.Set[A]) extends Set[A] def elements: Iterator[A] = set.elements - def +=(elem: A): Unit = { set = set + elem } + def +=(elem: A): this.type = { set = set + elem; this } def -=(elem: A): Unit = { set = set - elem } diff --git a/src/library/scala/collection/mutable/JavaMapAdaptor.scala b/src/library/scala/collection/mutable/JavaMapAdaptor.scala deleted file mode 100644 index cb5d8b8931..0000000000 --- a/src/library/scala/collection/mutable/JavaMapAdaptor.scala +++ /dev/null @@ -1,69 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection.mutable - - -/** This class can be used as an adaptor to create mutable maps from - * Java classes that implementat the <code>java.util.Map</code> interface. - * - * @author Matthias Zenger - * @version 1.0, 21/07/2003 - * @deprecated Use <code>scala.collection.jcl.Map(jmap)</code> instead - */ -@deprecated class JavaMapAdaptor[A, B](jmap: java.util.Map[A, B]) extends Map[A, B] { - - override def size: Int = jmap.size() - - def get(key: A): Option[B] = - if (jmap.containsKey(key)) Some(jmap.get(key).asInstanceOf[B]) else None - - override def isEmpty: Boolean = jmap.isEmpty() - - override def apply(key: A): B = jmap.get(key).asInstanceOf[B] - - override def contains(key: A): Boolean = jmap.containsKey(key) - - override def isDefinedAt(key: A) = jmap.containsKey(key) - - override def keys: Iterator[A] = new Iterator[A] { - val iter = jmap.keySet().iterator() - def hasNext = iter.hasNext() - def next = iter.next().asInstanceOf[A] - } - - override def values: Iterator[B] = new Iterator[B] { - val iter = jmap.values().iterator() - def hasNext = iter.hasNext() - def next = iter.next().asInstanceOf[B] - } - - def elements: Iterator[(A, B)] = new Iterator[(A, B)] { - val iter = jmap.keySet().iterator() - def hasNext = iter.hasNext() - def next = { - val key = iter.next().asInstanceOf[A] - (key, apply(key)) - } - } - - def update(key: A, value: B) { jmap.put(key, value) } - - def -= (key: A): Unit = { val x = jmap.remove(key); } - - override def clear(): Unit = jmap.clear() - - override def clone(): Map[A, B] = { - val res = new HashMap[A, B] - res ++= this - res - } -} diff --git a/src/library/scala/collection/mutable/JavaSetAdaptor.scala b/src/library/scala/collection/mutable/JavaSetAdaptor.scala deleted file mode 100644 index ae82f3fd0f..0000000000 --- a/src/library/scala/collection/mutable/JavaSetAdaptor.scala +++ /dev/null @@ -1,48 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection.mutable - - -/** This class can be used as an adaptor to create mutable sets from - * Java classes that implement interface <code>java.util.Set</code>. - * - * @author Matthias Zenger - * @version 1.0, 19/09/2003 - * @deprecated Use <code>scala.collection.jcl.Set(jmap)</code> instead - */ -@deprecated class JavaSetAdaptor[A](jset: java.util.Set[A]) extends Set[A] { - - override def size: Int = jset.size() - - override def isEmpty: Boolean = jset.isEmpty() - - def contains(elem: A): Boolean = jset.contains(elem) - - def elements: Iterator[A] = new Iterator[A] { - val iter = jset.iterator() - def hasNext = iter.hasNext() - def next = iter.next().asInstanceOf[A] - } - - def +=(elem: A): Unit = { val x = jset.add(elem); } - - def -=(elem: A): Unit = { val x = jset.remove(elem); } - - override def clear(): Unit = jset.clear() - - override def clone(): Set[A] = { - val res = new HashSet[A] - res ++= this - res - } -} - diff --git a/src/library/scala/collection/mutable/LinkedHashMap.scala b/src/library/scala/collection/mutable/LinkedHashMap.scala index bee3da8472..954821715c 100644 --- a/src/library/scala/collection/mutable/LinkedHashMap.scala +++ b/src/library/scala/collection/mutable/LinkedHashMap.scala @@ -37,14 +37,14 @@ class LinkedHashMap[A, B] extends Map[A,B] with HashTable[A] with DefaultMapMode private var ordered = List[Entry]() - def remove(key: A): Option[B] = removeEntry(key) match { + override def remove(key: A): Option[B] = removeEntry(key) match { case None => None case Some(e) => ordered = ordered.filter(_ ne e) Some(e.value) } - def -= (key: A) { remove(key) } + override def delete (key: A) { remove(key) } override def put(key: A, value: B): Option[B] = { val e = findEntry(key) diff --git a/src/library/scala/collection/mutable/LinkedHashSet.scala b/src/library/scala/collection/mutable/LinkedHashSet.scala index 620cd31c15..481c8667dc 100644 --- a/src/library/scala/collection/mutable/LinkedHashSet.scala +++ b/src/library/scala/collection/mutable/LinkedHashSet.scala @@ -23,7 +23,7 @@ object LinkedHashSet { } @serializable -class LinkedHashSet[A] extends Set[A] with SetTemplate[A, LinkedHashSet[A]] with FlatHashTable[A] { +class LinkedHashSet[A] extends Set[A] with MutableSetTemplate[A, LinkedHashSet[A]] with FlatHashTable[A] { override def empty = LinkedHashSet.empty @@ -33,16 +33,13 @@ class LinkedHashSet[A] extends Set[A] with SetTemplate[A, LinkedHashSet[A]] with def contains(elem: A): Boolean = containsEntry(elem) - def +=(elem: A) { add(elem) } - - def add(elem: A): Boolean = { + def put(elem: A): Boolean = if (addEntry(elem)) { ordered = elem :: ordered true } else false - } - def -=(elem: A) { remove(elem) } - def remove(elem: A) : Boolean = removeEntry(elem) match { + + def remove(elem: A): Boolean = removeEntry(elem) match { case None => false case Some(elem) => ordered = ordered.filter(_ != elem); true } @@ -51,7 +48,7 @@ class LinkedHashSet[A] extends Set[A] with SetTemplate[A, LinkedHashSet[A]] with ordered = Nil super.clear() } - override def clone(): Set[A] = new LinkedHashSet[A] ++ this + override def elements = Iterator.fromOld(ordered.reverse.elements) } diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala index 32ce0b4373..22396f1322 100644 --- a/src/library/scala/collection/mutable/ListBuffer.scala +++ b/src/library/scala/collection/mutable/ListBuffer.scala @@ -87,7 +87,7 @@ final class ListBuffer[A] * * @param x the element to append. */ - def += (x: A) { + def += (x: A): this.type = { if (exported) copy() if (start.isEmpty) { last0 = new :: (x, Nil) @@ -98,6 +98,7 @@ final class ListBuffer[A] last1.tl = last0 } len += 1 + this } /** Clears the buffer contents. @@ -254,7 +255,7 @@ final class ListBuffer[A] * * @param x the element to remove. */ - override def -= (elem: A) { + override def -= (elem: A): this.type = { if (exported) copy() if (start.isEmpty) {} else if (start.head == elem) { @@ -273,6 +274,7 @@ final class ListBuffer[A] len -= 1 } } + this } override def elements = new Iterator[A] { diff --git a/src/library/scala/collection/mutable/Map.scala b/src/library/scala/collection/mutable/Map.scala index acff09e253..17c3e81472 100644 --- a/src/library/scala/collection/mutable/Map.scala +++ b/src/library/scala/collection/mutable/Map.scala @@ -28,7 +28,7 @@ trait Map[A, B] override def update(key: A, value: B) = self.update(key, value) override def - (elem: A) = self - elem override def elements = self.elements - override def foreach(f: ((A, B)) => Unit) = self.foreach(f) + override def foreach[U](f: ((A, B)) => U) = self.foreach(f) override def empty[C] = self.empty[C] def get(key: A) = self.get(key) } diff --git a/src/library/scala/collection/mutable/MapProxy.scala b/src/library/scala/collection/mutable/MapProxy.scala index a215702f19..eda46ac702 100644 --- a/src/library/scala/collection/mutable/MapProxy.scala +++ b/src/library/scala/collection/mutable/MapProxy.scala @@ -30,18 +30,18 @@ trait MapProxy[A, B] extends Map[A, B] with collection.MapProxy[A, B] { def self: Map[A, B] override def update(key: A, value: B): Unit = self.update(key, value) - override def += (kv: (A, B)) = self += kv - override def += (kv1: (A, B), kv2: (A, B), kvs: (A, B)*) = self.+=(kv1, kv2, kvs: _*) - override def ++= (kvs: Iterable[(A, B)]) = self ++= kvs - override def ++= (kvs: Iterator[(A, B)]) = self ++= kvs + override def += (kv: (A, B)): this.type = { self += kv; this } + override def += (kv1: (A, B), kv2: (A, B), kvs: (A, B)*): this.type = { self.+=(kv1, kv2, kvs: _*); this } + override def ++= (kvs: Iterable[(A, B)]): this.type = { self ++= kvs; this } + override def ++= (kvs: Iterator[(A, B)]): this.type = { self ++= kvs; this } override def + (kv: (A, B)): Map[A, B] = self + kv override def + (kv1: (A, B), kv2: (A, B), kvs: (A, B)*): Map[A, B] = self.+(kv1, kv2, kvs: _*) override def ++ (kvs: Iterable[(A, B)]): Map[A, B] = self ++ kvs override def ++ (kvs: Iterator[(A, B)]): Map[A, B] = self ++ kvs - override def -= (key: A) = self -= key - override def -= (key1: A, key2: A, keys: A*) = self.-=(key1, key2, keys: _*) - override def --= (keys: Iterable[A]) = self --= keys - override def --= (keys: Iterator[A]) = self --= keys + override def -= (key: A): this.type = { self -= key; this } + override def -= (key1: A, key2: A, keys: A*): this.type = { self.-=(key1, key2, keys: _*); this } + override def --= (keys: Iterable[A]): this.type = { self --= keys; this } + override def --= (keys: Iterator[A]): this.type = { self --= keys; this } override def - (key: A): Map[A, B] = self - key override def - (key1: A, key2: A, keys: A*): Map[A, B] = self.-(key1, key2, keys: _*) override def -- (keys: Iterable[A]): Map[A, B] = self -- keys diff --git a/src/library/scala/collection/mutable/MutableList.scala b/src/library/scala/collection/mutable/MutableList.scala index 2e01c1a153..605d511f05 100644 --- a/src/library/scala/collection/mutable/MutableList.scala +++ b/src/library/scala/collection/mutable/MutableList.scala @@ -111,7 +111,7 @@ class MutableList[A] extends LinearSequence[A] with LinearSequenceTemplate[A, Mu * * @param elem the element to append. */ - def +=(elem: A) = appendElem(elem) + def +=(elem: A): this.type = { appendElem(elem); this } def clear() { first0 = null diff --git a/src/library/scala/collection/mutable/ObservableSet.scala b/src/library/scala/collection/mutable/ObservableSet.scala index 6885c368fd..a9408f406e 100644 --- a/src/library/scala/collection/mutable/ObservableSet.scala +++ b/src/library/scala/collection/mutable/ObservableSet.scala @@ -27,9 +27,12 @@ trait ObservableSet[A, This <: ObservableSet[A, This]] with Undoable, This] { self: This => - abstract override def +=(elem: A): Unit = if (!contains(elem)) { - super.+=(elem) - publish(new Include(elem) with Undoable { def undo = -=(elem) }) + abstract override def +=(elem: A): this.type = { + if (!contains(elem)) { + super.+=(elem) + publish(new Include(elem) with Undoable { def undo = -=(elem) }) + } + this } abstract override def -=(elem: A): Unit = if (contains(elem)) { diff --git a/src/library/scala/collection/mutable/OpenHashMap.scala b/src/library/scala/collection/mutable/OpenHashMap.scala index f4ae7204aa..3c84721b9e 100644 --- a/src/library/scala/collection/mutable/OpenHashMap.scala +++ b/src/library/scala/collection/mutable/OpenHashMap.scala @@ -102,11 +102,14 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl private[this] def addEntry(entry : Entry[Key, Value]) = if (entry != null) table(findIndex(entry.key, entry.hash)) = entry; - def update(key : Key, value : Value){ - update(key, hashOf(key), value); + override def update(key : Key, value : Value) { + put(key, hashOf(key), value); } - private def update(key : Key, hash : Int, value : Value) { + override def put(key : Key, value : Value): Option[Value] = + put(key, hashOf(key), value) + + private def put(key : Key, hash : Int, value : Value): Option[Value] = { if (2 * (size + deleted) > mask) growTable; val index = findIndex(key, hash); val entry = table(index); @@ -114,22 +117,28 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl table(index) = new Entry(key, hash, Some(value)); modCount += 1; size += 1; - } - else { + None + } else { + val res = entry.value if (entry.value == None) { size += 1; modCount += 1 } entry.value = Some(value); + res } } - def -=(key : Key) = { + override def remove(key : Key): Option[Value] = { val index = findIndex(key); if (table(index) != null && table(index).value != None){ + val res = table(index).value table(index).value = None; size -= 1; deleted += 1; - } + res + } else None } + override def delete(key: Key) { remove(key) } + def get(key : Key) : Option[Value] = { val hash = hashOf(key); @@ -176,7 +185,7 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl override def clone : OpenHashMap[Key, Value] = { val it = new OpenHashMap[Key, Value] - foreachUndeletedEntry(entry => it.update(entry.key, entry.hash, entry.value.get)); + foreachUndeletedEntry(entry => it.put(entry.key, entry.hash, entry.value.get)); it } @@ -193,7 +202,7 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl * * @param f The function to apply to each key, value mapping. */ - override def foreach(f : ((Key, Value)) => Unit){ + override def foreach[U](f : ((Key, Value)) => U){ val startModCount = modCount; foreachUndeletedEntry(entry => { if (modCount != startModCount) error("Concurrent Modification") diff --git a/src/library/scala/collection/mutable/PriorityQueue.scala b/src/library/scala/collection/mutable/PriorityQueue.scala index 70dfd56fff..d013497389 100644 --- a/src/library/scala/collection/mutable/PriorityQueue.scala +++ b/src/library/scala/collection/mutable/PriorityQueue.scala @@ -64,11 +64,12 @@ class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] with CloneableCol * * @param elem the element to insert */ - def +=(elem: A): Unit = { + def +=(elem: A): this.type = { ensureSize(size0+1) array(size0) = elem.asInstanceOf[AnyRef] fixUp(array, size0) size0 = size0 + 1 + this } def +(elem: A): PriorityQueue[A] = { this += elem; this } @@ -78,7 +79,7 @@ class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] with CloneableCol * @param kv2 the second element. * @param kvs the remaining elements. */ - def += (elem1: A, elem2: A, elems: A*) { this += elem1; this += elem2; this ++= elems } + def += (elem1: A, elem2: A, elems: A*) = { this += elem1; this += elem2; this ++= elems } def + (elem1: A, elem2: A, elems: A*) = { this.+=(elem1, elem2, elems: _*); this } diff --git a/src/library/scala/collection/mutable/ResizableArray.scala b/src/library/scala/collection/mutable/ResizableArray.scala index 9a57904712..b7d0c14806 100644 --- a/src/library/scala/collection/mutable/ResizableArray.scala +++ b/src/library/scala/collection/mutable/ResizableArray.scala @@ -67,7 +67,7 @@ trait ResizableArray[A] extends Vector[A] with VectorTemplate[A, ResizableArray[ } */ - override def foreach(f: A => Unit) { + override def foreach[U](f: A => U) { var i = 0 while (i < size) { f(array(i).asInstanceOf[A]) diff --git a/src/library/scala/collection/mutable/Set.scala b/src/library/scala/collection/mutable/Set.scala index 62bf6a6874..c2b8ca1c2d 100644 --- a/src/library/scala/collection/mutable/Set.scala +++ b/src/library/scala/collection/mutable/Set.scala @@ -14,10 +14,12 @@ package scala.collection.mutable import generic._ /** This class represents mutable sets. Concrete set implementations - * just have to provide functionality for the abstract methods in - * <a href="../Set.html" target="contentFrame"> - * <code>scala.collection.Set</code></a> as well as for <code>+=</code>, - * <code>-= and <code>clear</code>. + * have to provide functionality for the abstract methods in Set: + * + * def contains(elem: A): Boolean + * def put(elem: A): Boolean + * def remove(elem: A): Boolean + * def elements: Iterator[A] * * @author Matthias Zenger * @author Martin Odersky @@ -25,82 +27,12 @@ import generic._ */ trait Set[A] extends Iterable[A] with collection.Set[A] - with SetTemplate[A, Set[A]] - with Growable[A] - with Shrinkable[A] - with Cloneable[Set[A]] + with MutableSetTemplate[A, Set[A]] with Unhashable { -self => override def empty = Set.empty override def traversableBuilder[B]: Builder[B, Set[B]] = Set.newBuilder[B] - - /** This method allows one to add or remove an element <code>elem</code> - * from this set depending on the value of parameter <code>included</code>. - * Typically, one would use the following syntax: - * <pre>set(elem) = true</pre> - * - */ - def update(elem: A, included: Boolean) { - if (included) this += elem else this -= elem - } - - /** Adds a new element to the set. - * - * @param elem the element to be added - */ - def +=(elem: A) - - /** Removes a single element from a set. - * @param elem The element to be removed. - */ - def -=(elem: A) - - /** Adds a new element to the set and returns the set itself. - * - * @param elem the element to be added - */ - override def +(elem: A): this.type = { +=(elem); this } - - /** Removed a new element from the set and returns the set itself. - * - * @param elem the element to be added - */ - override def -(elem: A): this.type = { -=(elem); this } - - /** This method computes an intersection with set <code>that</code>. - * It removes all the elements that are not present in <code>that</code>. - * - * @param that the set to intersect with. - */ - override def intersect(that: collection.Set[A]): this.type = { retain(that.contains); this } - - /** Method <code>retain removes all elements from the set for - * which the predicate <code>p</code> yields the value <code>false</code>. - */ - def retain(p: A => Boolean): Unit = for (elem <- this.toList) if (!p(elem)) this -= elem - - /** Removes all elements from the set. After this operation is completed, - * the set will be empty. - */ - def clear() { foreach(-=) } - - override def clone(): Set[A] = { val b = newBuilder; b ++= this; b.result } - - /** Send a message to this scriptable object. - * - * @param cmd the message to send. - * @throws <code>Predef.UnsupportedOperationException</code> - * if the message was not understood. - def <<(cmd: Message[A]): Unit = cmd match { - case Include(elem) => this += elem - case Remove(elem) => this -= elem - case Reset() => clear - case s: Script[_] => s.elements foreach << - case _ => throw new UnsupportedOperationException("message " + cmd + " not understood") - } - */ } /** The canonical factory methods for <a href="Set.html">mutable sets</a>. diff --git a/src/library/scala/collection/mutable/SetProxy.scala b/src/library/scala/collection/mutable/SetProxy.scala index e13f18691b..1c86472259 100644 --- a/src/library/scala/collection/mutable/SetProxy.scala +++ b/src/library/scala/collection/mutable/SetProxy.scala @@ -27,15 +27,15 @@ trait SetProxy[A] extends Set[A] with collection.SetProxy[A] { override def update(elem: A, included: Boolean): Unit = self(elem) = included - def +=(elem: A): Unit = self += elem + def +=(elem: A): this.type = { self += elem; this } - override def ++=(that: Iterable[A]): Unit = self ++= that + override def ++=(that: Iterable[A]): this.type = { self ++= that; this } - override def ++=(it: Iterator[A]): Unit = self ++= it + override def ++=(it: Iterator[A]): this.type = { self ++= it; this } override def incl(elems: A*): Unit = self ++= elems - def -=(elem: A): Unit = self -= elem + def -=(elem: A): this.type = { self -= elem; this } override def --=(that: Iterable[A]): Unit = self --= that diff --git a/src/library/scala/collection/mutable/Stack.scala b/src/library/scala/collection/mutable/Stack.scala index 2ad08c68c4..b63a07dbb1 100644 --- a/src/library/scala/collection/mutable/Stack.scala +++ b/src/library/scala/collection/mutable/Stack.scala @@ -72,9 +72,9 @@ class Stack[A] private (var elems: List[A]) extends collection.Sequence[A] with def pushAll(elems: collection.Traversable[A]): this.type = { for (elem <- elems) { push(elem); () }; this } /** @deprecated use pushAll */ - @deprecated def ++=(it: Iterator[A]): Unit = pushAll(it) + @deprecated def ++=(it: Iterator[A]): this.type = pushAll(it) /** @deprecated use pushAll */ - @deprecated def ++=(it: Iterable[A]): Unit = pushAll(it) + @deprecated def ++=(it: Iterable[A]): this.type = pushAll(it) /** Returns the top element of the stack. This method will not remove * the element from the stack. An error is signaled if there is no diff --git a/src/library/scala/collection/mutable/StringBuilder.scala b/src/library/scala/collection/mutable/StringBuilder.scala index da3ebfcc13..bcb85fe97f 100644 --- a/src/library/scala/collection/mutable/StringBuilder.scala +++ b/src/library/scala/collection/mutable/StringBuilder.scala @@ -226,7 +226,7 @@ final class StringBuilder(initCapacity: Int, private val initValue: String) /* Appends the string representation of the <code>Any</code> argument. */ - def +=(x: Char) { append(x) } + def +=(x: Char): this.type = { append(x); this } def +(x: Char): this.type = { +=(x); this } diff --git a/src/library/scala/collection/mutable/SynchronizedBuffer.scala b/src/library/scala/collection/mutable/SynchronizedBuffer.scala index 66dba50249..4209dff391 100644 --- a/src/library/scala/collection/mutable/SynchronizedBuffer.scala +++ b/src/library/scala/collection/mutable/SynchronizedBuffer.scala @@ -47,7 +47,7 @@ trait SynchronizedBuffer[A] extends Buffer[A] { * * @param elem the element to append. */ - abstract override def +=(elem: A): Unit = synchronized { + abstract override def +=(elem: A): this.type = synchronized[this.type] { super.+=(elem) } @@ -66,7 +66,7 @@ trait SynchronizedBuffer[A] extends Buffer[A] { * * @param iter the iterable object. */ - override def ++=(iter: Traversable[A]): Unit = synchronized { + override def ++=(iter: Traversable[A]): this.type = synchronized[this.type] { super.++=(iter) } diff --git a/src/library/scala/collection/mutable/SynchronizedQueue.scala b/src/library/scala/collection/mutable/SynchronizedQueue.scala index 0b3f870a9f..7a803c5879 100644 --- a/src/library/scala/collection/mutable/SynchronizedQueue.scala +++ b/src/library/scala/collection/mutable/SynchronizedQueue.scala @@ -32,7 +32,7 @@ class SynchronizedQueue[A] extends Queue[A] { * * @param elem the element to insert */ - override def +=(elem: A): Unit = synchronized { super.+=(elem) } + override def +=(elem: A): this.type = synchronized[this.type] { super.+=(elem) } /** Adds all elements provided by an <code>Iterable</code> object * at the end of the queue. The elements are prepended in the order they @@ -40,7 +40,7 @@ class SynchronizedQueue[A] extends Queue[A] { * * @param iter an iterable object */ - override def ++=(iter: Traversable[A]): Unit = synchronized { super.++=(iter) } + override def ++=(iter: Traversable[A]): this.type = synchronized[this.type] { super.++=(iter) } /** Adds all elements provided by an iterator * at the end of the queue. The elements are prepended in the order they @@ -48,7 +48,7 @@ class SynchronizedQueue[A] extends Queue[A] { * * @param it an iterator */ - override def ++=(it: Iterator[A]): Unit = synchronized { super.++=(it) } + override def ++=(it: Iterator[A]): this.type = synchronized[this.type] { super.++=(it) } /** Adds all elements to the queue. * diff --git a/src/library/scala/collection/mutable/SynchronizedSet.scala b/src/library/scala/collection/mutable/SynchronizedSet.scala index 47396fdb84..0c57996019 100644 --- a/src/library/scala/collection/mutable/SynchronizedSet.scala +++ b/src/library/scala/collection/mutable/SynchronizedSet.scala @@ -37,31 +37,31 @@ trait SynchronizedSet[A] extends Set[A] { super.update(elem, included) } - abstract override def +=(elem: A): Unit = synchronized { + abstract override def +=(elem: A): this.type = synchronized[this.type] { super.+=(elem) } - override def ++=(that: Traversable[A]) = synchronized { + override def ++=(that: Traversable[A]): this.type = synchronized[this.type] { super.++=(that) } - override def ++=(it: Iterator[A]) = synchronized { + override def ++=(it: Iterator[A]): this.type = synchronized[this.type] { super.++=(it) } - abstract override def -=(elem: A): Unit = synchronized { + abstract override def -=(elem: A): this.type = synchronized[this.type] { super.-=(elem) } - override def --=(that: Traversable[A]) = synchronized { + override def --=(that: Traversable[A]): this.type = synchronized[this.type] { super.--=(that) } - override def --=(it: Iterator[A]) = synchronized { + override def --=(it: Iterator[A]): this.type = synchronized[this.type] { super.--=(it) } - override def intersect(that: collection.Set[A]) = synchronized[this.type] { + override def intersect(that: collection.Set[A]) = synchronized { super.intersect(that) } @@ -73,7 +73,7 @@ trait SynchronizedSet[A] extends Set[A] { super.subsetOf(that) } - override def foreach(f: A => Unit) = synchronized { + override def foreach[U](f: A => U) = synchronized { super.foreach(f) } diff --git a/src/library/scala/util/automata/DetWordAutom.scala b/src/library/scala/util/automata/DetWordAutom.scala index 0f812b2270..07ffdaf21b 100644 --- a/src/library/scala/util/automata/DetWordAutom.scala +++ b/src/library/scala/util/automata/DetWordAutom.scala @@ -62,7 +62,7 @@ abstract class DetWordAutom[T <: AnyRef] { var map = scala.collection.immutable.Map[Int,Int]() var j = 0; while( j < nstates ) { if (j < finals.length) - map = map.add(j, finals(j)) + map = map.updated(j, finals(j)) j += 1 } sb.append(map.toString()) diff --git a/src/library/scala/util/automata/NondetWordAutom.scala b/src/library/scala/util/automata/NondetWordAutom.scala index 89686c47d4..66f49360ac 100644 --- a/src/library/scala/util/automata/NondetWordAutom.scala +++ b/src/library/scala/util/automata/NondetWordAutom.scala @@ -92,7 +92,7 @@ abstract class NondetWordAutom[T <: AnyRef] { var map = scala.collection.immutable.Map[Int,Int]() var j = 0; while (j < nstates) { if (isFinal(j)) - map = map.add(j, finals(j)); + map = map.updated(j, finals(j)); j += 1 } sb.append(map.toString) diff --git a/src/library/scala/util/automata/SubsetConstruction.scala b/src/library/scala/util/automata/SubsetConstruction.scala index 3d8cb34f29..314704d44e 100644 --- a/src/library/scala/util/automata/SubsetConstruction.scala +++ b/src/library/scala/util/automata/SubsetConstruction.scala @@ -94,8 +94,8 @@ class SubsetConstruction[T <: AnyRef](val nfa: NondetWordAutom[T]) { val sink = _emptyBitSet states = states + sink - deftrans = deftrans.add(q0,sink); - deftrans = deftrans.add(sink,sink); + deftrans = deftrans.updated(q0,sink); + deftrans = deftrans.updated(sink,sink); val rest = new mutable.ArrayStack[BitSet](); @@ -104,7 +104,7 @@ class SubsetConstruction[T <: AnyRef](val nfa: NondetWordAutom[T]) { states = states + Q rest.push(Q) if (nfa.containsFinal(Q)) - finals = finals.add(Q, selectTag(Q,nfa.finals)); + finals = finals.updated(Q, selectTag(Q,nfa.finals)); } } rest.push(sink) @@ -113,24 +113,24 @@ class SubsetConstruction[T <: AnyRef](val nfa: NondetWordAutom[T]) { while (!rest.isEmpty) { // assign a number to this bitset val P = rest.pop - indexMap = indexMap.add(P,ix) - invIndexMap = invIndexMap.add(ix,P) + indexMap = indexMap.updated(P,ix) + invIndexMap = invIndexMap.updated(ix,P) ix += 1 // make transitiion map val Pdelta = new mutable.HashMap[T, BitSet] - delta.add(P, Pdelta) + delta.update(P, Pdelta) val it = labels.elements; while(it.hasNext) { val label = it.next val Q = nfa.next(P,label) - Pdelta.add(label, Q) + Pdelta.update(label, Q) add(Q) } // collect default transitions val Pdef = nfa.nextDefault(P) - deftrans = deftrans.add(P, Pdef) + deftrans = deftrans.updated(P, Pdef) add(Pdef) }; @@ -151,7 +151,7 @@ class SubsetConstruction[T <: AnyRef](val nfa: NondetWordAutom[T]) { val label = it.next val p = indexMap(trans(label)) if (p != qDef) - ntrans.add(label, p) + ntrans.update(label, p) } deltaR(q) = ntrans defaultR(q) = qDef diff --git a/src/library/scala/util/automata/WordBerrySethi.scala b/src/library/scala/util/automata/WordBerrySethi.scala index 72b065f3e1..873f85cf4f 100644 --- a/src/library/scala/util/automata/WordBerrySethi.scala +++ b/src/library/scala/util/automata/WordBerrySethi.scala @@ -80,7 +80,7 @@ abstract class WordBerrySethi extends BaseBerrySethi { case x:Letter => //val i = posMap(x) val i = x.pos - this.follow.add(i, fol1) + this.follow.update(i, fol1) emptySet + i case Eps => emptySet /*ignore*/ @@ -97,7 +97,7 @@ abstract class WordBerrySethi extends BaseBerrySethi { protected def seenLabel(r: RegExp, i: Int, label: _labelT) { //Console.println("seenLabel (1)"); //this.posMap.add(r, i) - this.labelAt = this.labelAt.add(i, label) + this.labelAt = this.labelAt.updated(i, label) //@ifdef if( label != Wildcard ) { this.labels += label //@ifdef } @@ -124,7 +124,7 @@ abstract class WordBerrySethi extends BaseBerrySethi { //@ifdef compiler defaultq.add(src, dest::defaultq( src )) //@ifdef compiler else val q = deltaq(src) - q.add(label, dest::(q.get(label) match { + q.update(label, dest::(q.get(label) match { case Some(x) => x case _ => Nil })) @@ -171,7 +171,7 @@ abstract class WordBerrySethi extends BaseBerrySethi { while (it.hasNext) { val k = it.next if (pos == k) - finals = finals.add(j, finalTag) + finals = finals.updated(j, finalTag) else makeTransition( j, k, labelAt(k)) } @@ -195,14 +195,14 @@ abstract class WordBerrySethi extends BaseBerrySethi { collectTransitions() if (x.isNullable) // initial state is final - finals = finals.add(0, finalTag) + finals = finals.updated(0, finalTag) var delta1: immutable.Map[Int, Map[_labelT, List[Int]]] = immutable.Map[Int, Map[_labelT, List[Int]]]() var i = 0 while (i < deltaq.length) { - delta1 = delta1.add(i, deltaq(i)) + delta1 = delta1.updated(i, deltaq(i)) i += 1 } val finalsArr = new Array[Int](pos) @@ -239,7 +239,7 @@ abstract class WordBerrySethi extends BaseBerrySethi { val x = new mutable.BitSet(pos) for (q <- trans(lab)) x += q - hmap.add(lab, x.toImmutable) + hmap.update(lab, x.toImmutable) } deltaArr(k) = hmap k += 1 diff --git a/src/library/scala/util/control/Breaks.scala b/src/library/scala/util/control/Breaks.scala index b3eaad23ae..12a06c0d1c 100755 --- a/src/library/scala/util/control/Breaks.scala +++ b/src/library/scala/util/control/Breaks.scala @@ -12,7 +12,6 @@ object Breaks { op } catch { case ex: BreakException => - case ex: ContinueException => breakable(op) } } diff --git a/src/library/scala/util/parsing/json/JSON.scala b/src/library/scala/util/parsing/json/JSON.scala index 4683a48f90..7f7bb34b52 100644 --- a/src/library/scala/util/parsing/json/JSON.scala +++ b/src/library/scala/util/parsing/json/JSON.scala @@ -69,10 +69,10 @@ object JSON extends Parser { if (input.forall { case (key: String, value: List[_]) => - objMap += (key -> resolveType(value)) + objMap = objMap.+[Any](key -> resolveType(value)) true case (key : String, value : Any) => - objMap += (key -> value) + objMap += key -> value true case _ => false }) objMap |