diff options
author | Martin Odersky <odersky@gmail.com> | 2009-05-12 18:33:22 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2009-05-12 18:33:22 +0000 |
commit | 39fdbddb881220f7e64e5cb9016458edc9e314e5 (patch) | |
tree | a6ada1c0b7640865ea625edd068b5037f1978bfb | |
parent | 7cfc53fb4b77f70af5de28a057b1d333bee415d8 (diff) | |
download | scala-39fdbddb881220f7e64e5cb9016458edc9e314e5.tar.gz scala-39fdbddb881220f7e64e5cb9016458edc9e314e5.tar.bz2 scala-39fdbddb881220f7e64e5cb9016458edc9e314e5.zip |
changes to maps and sets
46 files changed, 405 insertions, 266 deletions
diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala index 3dfd9bb740..d91e59f867 100644 --- a/src/library/scala/Enumeration.scala +++ b/src/library/scala/Enumeration.scala @@ -113,12 +113,14 @@ abstract class Enumeration(initial: Int, names: String*) { * the argument <var>s</var>. * You must pass a String* set of names to the constructor, * or initialize each Enumeration with Value(String), - * for withName to work. + * for valueOf to work. * @param s an enumeration name * @return <tt>Some(Value)</tt> if an enumeration's name matches <var>s</var>, * else <tt>None</tt> + * Note the change here is intentional. You should know whether + * a name is in an Enumeration beforehand. If not, just use find on values. */ - def withName(s: String): Option[Value] = values.find(_.toString == s) + def withName(s: String): Value = values.find(_.toString == s).get /** Creates a fresh value, part of this enumeration. */ protected final def Value: Value = Value(nextId) @@ -215,8 +217,8 @@ 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 plus (value: Value) = new ValueSet(ids + value.id) - def minus (value: Value) = new ValueSet(ids - value.id) + def + (value: Value) = new ValueSet(ids + value.id) + def - (value: Value) = new ValueSet(ids - value.id) def elements = ids.elements map Enumeration.this.apply override def stringPrefix = Enumeration.this + ".ValueSet" } @@ -236,4 +238,53 @@ 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 } } + + /** The name of this enumeration. + * @deprecated use toString instead + */ + @deprecated def name = toString + + /** @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/collection/DefaultMap.scala b/src/library/scala/collection/DefaultMap.scala index ec83382660..fe5b7a2c1b 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 `updated` and `minus` methods of maps. +/* A default map which implements the `updated` and `-` methods of maps. * Instances that inherit from DefaultMap[A, B] still have to define: * * def get(key: A): Option[B] @@ -28,10 +28,18 @@ trait DefaultMap[A, +B] extends Map[A, B] { self => /** A default implementation which creates a new immutable map. */ - override def updated[B1 >: B](key: A, value: B1): Map[A, B1] = - Map[A, B1]() ++ this + ((key, value)) + override def +[B1 >: B](kv: (A, B1)): Map[A, B1] = { + val b = Map.newBuilder[A, B1] + b ++= this + b += ((kv._1, kv._2)) + b.result + } /** A default implementation which creates a new immutable map. */ - override def minus (key: A): Map[A, B] = Map[A, B]() ++ this - key + override def - (key: A): Map[A, B] = { + val b = newBuilder + b ++= this filter (key !=) + b.result + } } diff --git a/src/library/scala/collection/JavaConversions.scala b/src/library/scala/collection/JavaConversions.scala index 698d1579c5..3801fbb4cd 100644 --- a/src/library/scala/collection/JavaConversions.scala +++ b/src/library/scala/collection/JavaConversions.scala @@ -393,9 +393,11 @@ object JavaConversions { def contains(elem: A): Boolean = underlying.contains(elem) - def put(elem: A): Boolean = underlying.add(elem) + def +=(elem: A): this.type = { underlying.add(elem); this } + def -=(elem: A): this.type = { underlying.remove(elem); this } - def remove(elem: A): Boolean = underlying.remove(elem) + override def put(elem: A): Boolean = underlying.add(elem) + override def remove(elem: A): Boolean = underlying.remove(elem) override def clear = underlying.clear @@ -467,6 +469,9 @@ object JavaConversions { None } + def +=(kv: (A, B)): this.type = { underlying.put(kv._1, kv._2); this } + def -=(key: A): this.type = { underlying.remove(key); this } + override def put(k : A, v : B): Option[B] = { val r = underlying.put(k, v) if (r != null) Some(r) else None @@ -479,8 +484,6 @@ object JavaConversions { if (r != null) Some(r) else None } - override def delete(k : A) { underlying.remove(k) } - def elements = new Iterator[(A, B)] { val ui = underlying.entrySet.iterator def hasNext = ui.hasNext diff --git a/src/library/scala/collection/Map.scala b/src/library/scala/collection/Map.scala index 4028fe870d..3544c195dd 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 updated[B1 >: B](key: A, value: B1): This - * def minus(key: A): This + * def + [B1 >: B](kv: (A, B1)): This + * def -(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 1150a88728..39a7a188f0 100644 --- a/src/library/scala/collection/Traversable.scala +++ b/src/library/scala/collection/Traversable.scala @@ -95,7 +95,8 @@ 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] } + 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] diff --git a/src/library/scala/collection/generic/Addable.scala b/src/library/scala/collection/generic/Addable.scala index db580c3771..56bfe4bd49 100644 --- a/src/library/scala/collection/generic/Addable.scala +++ b/src/library/scala/collection/generic/Addable.scala @@ -24,15 +24,15 @@ trait Addable[A, +This <: Addable[A, This]] { self => * @param elem the element to be added * @return a fresh collection */ - def plus(elem: A): This + def +(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 * - * @note same as `plus` + * @note same as `+` */ - def + (elem: A): This = plus(elem) + def plus (elem: A): This = this.+(elem) /** Adds two or more elements to this collection and returns * a new collection. @@ -41,8 +41,8 @@ trait Addable[A, +This <: Addable[A, This]] { self => * @param elem2 the second element to add. * @param elems the remaining elements to add. */ - def plus (elem1: A, elem2: A, elems: A*): This = - this plus elem1 plus elem2 plusAll Iterable.fromOld(elems) + def + (elem1: A, elem2: A, elems: A*): This = + this + elem1 + elem2 ++ Iterable.fromOld(elems) /** Adds two or more elements to this collection and returns * a new collection. @@ -50,16 +50,16 @@ trait Addable[A, +This <: Addable[A, This]] { self => * @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` + * @note same as `+` */ - def + (elem1: A, elem2: A, elems: A*): This = plus(elem1, elem2, elems: _*) + def plus (elem1: A, elem2: A, elems: A*): This = this.+(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. */ - def plusAll(elems: Traversable[A]): This = (thisCollection /: elems) (_ plus _) + def ++(elems: Traversable[A]): This = (thisCollection /: elems) (_ + _) /** Adds a number of elements provided by a traversable object * and returns a new collection with the added elements. @@ -70,14 +70,14 @@ trait Addable[A, +This <: Addable[A, This]] { self => * the type of the added elements is a subtype of the element type of the * collection. */ - def ++ (elems: Traversable[A]): This = plusAll(elems) + def plusAll (elems: Traversable[A]): This = this.++(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 (iter: Iterator[A]): This = (thisCollection /: iter) (_ plus _) + def ++ (iter: Iterator[A]): This = (thisCollection /: iter) (_ + _) /** Adds a number of elements provided by an iterator * and returns a new collection with the added elements. @@ -88,7 +88,7 @@ trait Addable[A, +This <: Addable[A, This]] { self => * the type of the added elements is a subtype of the element type of the * collection. */ - def ++ (iter: Iterator[A]): This = plusAll(iter) + def plusAll (iter: Iterator[A]): This = this.++(iter) } diff --git a/src/library/scala/collection/generic/BufferTemplate.scala b/src/library/scala/collection/generic/BufferTemplate.scala index 56ae754ec7..981da34d02 100644 --- a/src/library/scala/collection/generic/BufferTemplate.scala +++ b/src/library/scala/collection/generic/BufferTemplate.scala @@ -114,13 +114,22 @@ trait BufferTemplate[A, +This <: BufferTemplate[A, This] with Buffer[A]] this } - /** 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 } + /** Perform a += on a clone of this collection */ + override def plus(elem: A): This = clone() += elem + /** Perform a += on a clone of this collection */ + override def plus(elem1: A, elem2: A, elems: A*): This = clone() += (elem1, elem2, elems: _*) + /** Perform a -= on a clone of this collection */ + override def minus(elem: A): This = clone() -= elem + /** Perform a -= on a clone of this collection */ + override def minus(elem1: A, elem2: A, elems: A*): This = clone() -= (elem1, elem2, elems: _*) + /** Perform a ++= on a clone of this collection */ + override def plusAll(elems: Traversable[A]): This = clone() ++= elems + /** Perform a ++= on a clone of this collection */ + override def plusAll(elems: Iterator[A]): This = clone() ++= elems + /** Perform a --= on a clone of this collection */ + override def minusAll(elems: Traversable[A]): This = clone() --= elems + /** Perform a --= on a clone of this collection */ + override def minusAll(elems: Iterator[A]): This = clone() --= elems /** Prepend two ore more elements to this buffer and return * the identity of the buffer. @@ -131,7 +140,7 @@ trait BufferTemplate[A, +This <: BufferTemplate[A, This] with Buffer[A]] /** Prepends a number of elements provided by an iterable object * via its <code>elements</code> method. The identity of the - * buffer is returned. + * buffer is returned.(thisCollection /: elems) (_ plus _) * * @param iter the iterable object. */ diff --git a/src/library/scala/collection/generic/ImmutableMapTemplate.scala b/src/library/scala/collection/generic/ImmutableMapTemplate.scala index 165cc365f7..db1b232d1b 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 updated(key: A, value: B): This - * def minus(key: A): This + * def + [B1 >: B](kv: (A, B)): Map[A, B1] + * def - (key: A): This * * If you wish that methods like, take, drop, filter return the same kind of map, you should also * override: @@ -38,20 +38,20 @@ self => * @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] + override def updated [B1 >: B](key: A, value: B1): immutable.Map[A, B1] = this + ((key, value)) /** 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) + override def plus [B1 >: B] (kv: (A, B1)): immutable.Map[A, B1] = this + kv /** 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) + def + [B1 >: B] (kv: (A, B1)): immutable.Map[A, B1] /** Adds two or more elements to this collection and returns * a new collection. @@ -60,8 +60,8 @@ self => * @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 + override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): immutable.Map[A, B1] = + this + elem1 + elem2 ++ elems /** Adds two or more elements to this collection and returns * a new collection. @@ -70,48 +70,48 @@ 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) *): immutable.Map[A, B1] = - plus(elem1, elem2, elems: _*) + override def plus [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): immutable.Map[A, B1] = + this.+(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 _) + override def ++[B1 >: B](elems: Traversable[(A, B1)]): immutable.Map[A, B1] = + ((thisCollection: immutable.Map[A, B1]) /: 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. - * @note same as `plusAll` + * @note same as `++` * @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) + override def plusAll [B1 >: B](elems: Traversable[(A, B1)]): immutable.Map[A, B1] = this.++(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 _) + override def ++[B1 >: B] (iter: Iterator[(A, B1)]): immutable.Map[A, B1] = + ((thisCollection: immutable.Map[A, B1]) /: iter) (_ + _) /** 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 same as `++` * @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) + override def plusAll [B1 >: B](iter: Iterator[(A, B1)]): immutable.Map[A, B1] = this.++(iter) /** This function transforms all the values of mappings contained * in this map with function <code>f</code>. @@ -136,7 +136,7 @@ self => 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 + if (p(kv)) res = (res - kv._1).asInstanceOf[This] // !!! concrete overrides abstract problem res } diff --git a/src/library/scala/collection/generic/MapTemplate.scala b/src/library/scala/collection/generic/MapTemplate.scala index 90c5372230..3f2c9d2fb0 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 updated[B1 >: B](key: A, value: B1): This - * def minus(key: A): This + * def + [B1 >: B](kv: (A, B1)): This + * def -(key: A): This * * If you wish that methods like, take, drop, filter return the same kind of map, you should also * override: @@ -101,8 +101,8 @@ self => protected class DefaultKeySet extends Set[A] { def contains(key : A) = self.contains(key) def elements = self.elements.map(_._1) - 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 + 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 override def size = self.size override def foreach[B](f: A => B) = for ((k, v) <- self) f(k) } @@ -164,20 +164,20 @@ self => * @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] + def updated [B1 >: B](key: A, value: B1): Map[A, B1] = this + ((key, value)) /** 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 plus [B1 >: B] (kv: (A, B1)): Map[A, B1] = updated(kv._1, kv._2) + def + [B1 >: B] (kv: (A, 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 - * @note same as `plus` + * @note same as `+` */ - def + [B1 >: B] (kv: (A, B1)): Map[A, B1] = updated(kv._1, kv._2) + def plus [B1 >: B] (kv: (A, B1)): Map[A, B1] = this + kv /** Adds two or more elements to this collection and returns * a new collection. @@ -186,8 +186,8 @@ self => * @param elem2 the second element to add. * @param elems the remaining elements to add. */ - def plus [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): Map[A, B1] = - this plus elem1 plus elem2 plusAll elems + def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): Map[A, B1] = + this + elem1 + elem2 ++ elems /** Adds two or more elements to this collection and returns * a new collection. @@ -196,54 +196,54 @@ self => * @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] = - plus(elem1, elem2, elems: _*) + def plus [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): Map[A, B1] = + this.+(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. */ - def plusAll[B1 >: B](elems: Traversable[(A, B1)]): Map[A, B1] = - ((thisCollection: Map[A, B1]) /: elems) (_ plus _) + def ++[B1 >: B](elems: Traversable[(A, B1)]): Map[A, B1] = + ((thisCollection: Map[A, B1]) /: 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. - * @note same as `plusAll` + * @note same as `++` * @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](elems: Traversable[(A, B1)]): Map[A, B1] = plusAll(elems) + def plusAll [B1 >: B](elems: Traversable[(A, B1)]): Map[A, B1] = this.++(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 _) + def ++[B1 >: B] (iter: Iterator[(A, B1)]): Map[A, B1] = + ((thisCollection: Map[A, B1]) /: iter) (_ + _) /** 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 same as `++` * @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) + def plusAll [B1 >: B](iter: Iterator[(A, B1)]): Map[A, B1] = this.++(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 + def - (key: A): This /** Creates a string representation for this map. * diff --git a/src/library/scala/collection/generic/MutableMapTemplate.scala b/src/library/scala/collection/generic/MutableMapTemplate.scala index 81ebe42997..72c4f9ceda 100644 --- a/src/library/scala/collection/generic/MutableMapTemplate.scala +++ b/src/library/scala/collection/generic/MutableMapTemplate.scala @@ -16,8 +16,8 @@ package scala.collection.generic * * def get(key: A): Option[B] * def elements: Iterator[(A, B)] - * def put(key: A, value: B): Option[B] - * def remove(key: A): Option[B] + * def += (kv: (A, B)): this.type + * def -= (key: A): this.type * * If you wish that methods like, take, drop, filter return the same kind of map, you should also * override: @@ -30,7 +30,7 @@ package scala.collection.generic * */ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with mutable.Map[A, B]] - extends MapTemplate[A, B, This] + extends MutableMapTemplateBase[A, B, This] with Builder[(A, B), This] with Growable[(A, B)] with Shrinkable[A] @@ -46,7 +46,11 @@ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with muta * @param key The key to update * @param value The new value */ - def put(key: A, value: B): Option[B] + def put(key: A, value: B): Option[B] = { + val r = get(key) + update(key, value) + r + } /** Adds a new mapping from <code>key</code> * to <code>value</code> to the map. If the map already contains a @@ -56,13 +60,13 @@ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with muta * @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 update(key: A, elem: B) { put(key, elem) } + def update(key: A, elem: B) { this += ((key, elem)) } /** Add a new key/value mapping this map. * @param kv the key/value pair. * @return the map itself */ - def += (kv: (A, B)): this.type = { update(kv._1, kv._2); this } + def += (kv: (A, B)): this.type /** Create a new map consisting of all elements of the current map * plus the given mapping from `key` to `value`. @@ -70,15 +74,25 @@ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with muta * @param value The new value * @return A fresh immutable map */ - def updated[B1 >: B](key: A, value: B1): collection.Map[A, B1] = - (Map[A, B1]() plusAll thisCollection).updated[B1](key, value) + override def updated[B1 >: B](key: A, value: B1): collection.Map[A, B1] = + plus((key, value)) - /** 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 minus (key: A): This = clone() minus key + /** Perform a += on a clone of this collection */ + override def plus[B1 >: B](kv: (A, B1)): mutable.Map[A, B1] = clone().asInstanceOf[mutable.Map[A, B1]] += kv + /** Perform a += on a clone of this collection */ + override def plus[B1 >: B](kv1: (A, B1), kv2: (A, B1), kvs: (A, B1)*): mutable.Map[A, B1] = clone().asInstanceOf[mutable.Map[A, B1]] += (kv1, kv2, kvs: _*) + /** Perform a -= on a clone of this collection */ + override def minus(key: A): This = clone() -= key + /** Perform a -= on a clone of this collection */ + override def minus(key1: A, key2: A, keys: A*): This = clone() -= (key1, key2, keys: _*) + /** Perform a ++= on a clone of this collection */ + override def plusAll[B1 >: B](kvs: Traversable[(A, B1)]): mutable.Map[A, B1] = clone().asInstanceOf[mutable.Map[A, B1]] ++= kvs + /** Perform a ++= on a clone of this collection */ + override def plusAll[B1 >: B](kvs: Iterator[(A, B1)]): mutable.Map[A, B1] = clone().asInstanceOf[mutable.Map[A, B1]] ++= kvs + /** Perform a --= on a clone of this collection */ + override def minusAll(keys: Traversable[A]): This = clone() --= keys + /** Perform a --= on a clone of this collection */ + override def minusAll(keys: Iterator[A]): This = clone() --= keys /** Add a new key/value mapping and return the map itself. * @@ -88,7 +102,7 @@ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with muta * that map itself, use +=. If you do want to create a fresh map, * you can use `plus` to avoid a @deprecated warning. */ - @deprecated def +(kv: (A, B)): this.type = { update(kv._1, kv._2); this } + @deprecated def + (kv: (A, B)): this.type = { update(kv._1, kv._2); this } /** Adds two or more key/value mappings and return the map itself. * with the added elements. @@ -132,18 +146,17 @@ trait MutableMapTemplate[A, B, +This <: MutableMapTemplate[A, B, This] with muta * If key is not present return None. * @param key the key to be removed */ - def remove(key: A): Option[B] - - /** Delete a key from this map if it is present. - * @param key the key to be removed - */ - def delete (key: A) { remove(key) } + def remove(key: A): Option[B] = { + val r = get(key) + this -= key + r + } /** 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 } + def -= (key: A): this.type /** Delete a key from this map if it is present and return the map itself. * @param key the key to be removed diff --git a/src/library/scala/collection/generic/MutableMapTemplateBase.scala b/src/library/scala/collection/generic/MutableMapTemplateBase.scala new file mode 100755 index 0000000000..8a0809792b --- /dev/null +++ b/src/library/scala/collection/generic/MutableMapTemplateBase.scala @@ -0,0 +1,35 @@ +/* __ *\ +** ________ ___ / / ___ 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 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 += (kv: (A, B)): this.type + * def -= (key: A): this.type + * + * If you wish that methods like, take, drop, filter return the same kind of map, you should also + * override: + * + * def empty: This + * + * 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 MutableMapTemplateBase[A, B, +This <: MutableMapTemplateBase[A, B, This] with mutable.Map[A, B]] + extends MapTemplate[A, B, This] { + def + [B1 >: B] (kv: (A, B1)): Map[A, B1] = plus(kv) +} diff --git a/src/library/scala/collection/generic/MutableSetTemplate.scala b/src/library/scala/collection/generic/MutableSetTemplate.scala index f9f4b52dc8..8c5ee54c25 100644 --- a/src/library/scala/collection/generic/MutableSetTemplate.scala +++ b/src/library/scala/collection/generic/MutableSetTemplate.scala @@ -15,9 +15,9 @@ package scala.collection.generic * 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] + * def += (elem: A): this.type + * def -= (elem: A): this.type * * If you wish that methods like, take, drop, filter return the same kind of map, you should also * override: @@ -42,13 +42,21 @@ trait MutableSetTemplate[A, +This <: MutableSetTemplate[A, This] with mutable.Se * @param elem the element to be added * @return true if the element was already present in the set. */ - def put(elem: A): Boolean + def put(elem: A): Boolean = { + val r = contains(elem) + this += elem + r + } /** 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 + def remove(elem: A): Boolean = { + val r = contains(elem) + this -= elem + r + } /** 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>. @@ -57,19 +65,21 @@ trait MutableSetTemplate[A, +This <: MutableSetTemplate[A, This] with mutable.Se * */ def update(elem: A, included: Boolean) { - if (included) this put elem else this remove elem + if (included) this += elem else this -= elem } + + /** Adds a new element to the set. * * @param elem the element to be added */ - def +=(elem: A): this.type = { put(elem); this } + def +=(elem: A): this.type /** Removes a single element from a set. * @param elem The element to be removed. */ - def -=(elem: A): this.type = { remove(elem); this } + def -=(elem: A): this.type /** Removes all elements from the set for which the predicate <code>p</code> * yields the value <code>false</code>. @@ -83,9 +93,22 @@ trait MutableSetTemplate[A, +This <: MutableSetTemplate[A, This] with mutable.Se override def clone(): This = empty ++= thisCollection - def plus(elem: A): This = clone() += elem - - def minus(elem: A): This = clone() -= elem + /** Perform a += on a clone of this collection */ + override def plus(elem: A): This = clone() += elem + /** Perform a += on a clone of this collection */ + override def plus(elem1: A, elem2: A, elems: A*): This = clone() += (elem1, elem2, elems: _*) + /** Perform a -= on a clone of this collection */ + override def minus(elem: A): This = clone() -= elem + /** Perform a -= on a clone of this collection */ + override def minus(elem1: A, elem2: A, elems: A*): This = clone() -= (elem1, elem2, elems: _*) + /** Perform a ++= on a clone of this collection */ + override def plusAll(elems: Traversable[A]): This = clone() ++= elems + /** Perform a ++= on a clone of this collection */ + override def plusAll(elems: Iterator[A]): This = clone() ++= elems + /** Perform a --= on a clone of this collection */ + override def minusAll(elems: Traversable[A]): This = clone() --= elems + /** Perform a --= on a clone of this collection */ + override def minusAll(elems: Iterator[A]): This = clone() --= elems def result: This = thisCollection diff --git a/src/library/scala/collection/generic/SetTemplate.scala b/src/library/scala/collection/generic/SetTemplate.scala index 5d69abe389..5d5abca295 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 plus(elem: A): This - * def minus(elem: A): This + * def +(elem: A): This + * def -(elem: A): This * * If you wish that methods like, take, drop, filter return the same kind of set, you should also * override: @@ -44,12 +44,12 @@ self => /** Creates a new set with an additional element, unless the element is already present. * @param elem the element to be added */ - def plus (elem: A): This + def + (elem: A): This /** 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 minus (elem: A): This + def - (elem: A): This /** Checks if this set is empty. * @@ -97,7 +97,7 @@ self => * @return a set containing the elements of this * set and those of the given set <code>that</code>. */ - def union(that: Set[A]): This = plusAll(that) + def union(that: Set[A]): This = this.++(that) /** The union of this set and the given set <code>that</code>. * @@ -114,7 +114,7 @@ self => * @return a set containing those elements of this * set that are not also contained in the given set <code>that</code>. */ - def diff(that: Set[A]): This = minusAll(that) + def diff(that: Set[A]): This = --(that) /** The difference of this set and the given set <code>that</code>. * diff --git a/src/library/scala/collection/generic/SortedMapTemplate.scala b/src/library/scala/collection/generic/SortedMapTemplate.scala index a7bed88567..eefd257136 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 plus (elem: A): SortedSet[A] = throw new UnsupportedOperationException("keySet.+") - override def minus (elem: A): SortedSet[A] = throw new UnsupportedOperationException("keySet.-") + override def + (elem: A): SortedSet[A] = throw new UnsupportedOperationException("keySet.+") + override def - (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,13 @@ self => * @param value the value * @return A new map with the new binding added to this map */ - def updated[B1 >: B](key: A, value: B1): SortedMap[A, B1] + override def updated[B1 >: B](key: A, value: B1): SortedMap[A, B1] = this+((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 plus [B1 >: B] (kv: (A, B1)): SortedMap[A, B1] = updated(kv._1, kv._2) + def + [B1 >: B] (kv: (A, B1)): SortedMap[A, B1] // todo: Add generic +,-, and so on. @@ -63,9 +63,9 @@ self => * @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) *): SortedMap[A, B1] = { - var m = this plus elem1 plus elem2; - for (e <- elems) m = m plus e + 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 } } diff --git a/src/library/scala/collection/generic/Subtractable.scala b/src/library/scala/collection/generic/Subtractable.scala index c83e70ca13..5d264759ee 100644 --- a/src/library/scala/collection/generic/Subtractable.scala +++ b/src/library/scala/collection/generic/Subtractable.scala @@ -25,15 +25,15 @@ trait Subtractable[A, +This <: Subtractable[A, This]] { self => * * @param elem the element to remove. */ - def minus(elem: A): This + def -(elem: A): This /** 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` + * @note same as `-` */ - def -(elem: A): This = minus(elem) + def minus(elem: A): This = this - elem /** Returns a new collection that contains all elements of the current collection * except a two or more given elements. @@ -42,8 +42,8 @@ trait Subtractable[A, +This <: Subtractable[A, This]] { self => * @param elem2 the second element to remove. * @param elems the remaining elements to remove. */ - def minus(elem1: A, elem2: A, elems: A*): This = - this minus elem1 minus elem2 minusAll Iterable.fromOld(elems) + def -(elem1: A, elem2: A, elems: A*): This = + this - elem1 - elem2 -- elems /** Returns a new collection that contains all elements of the current collection * except two or more given elements. @@ -51,38 +51,38 @@ trait Subtractable[A, +This <: Subtractable[A, This]] { self => * @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 + * @note same as - */ - def - (elem1: A, elem2: A, elems: A*): This = minus(elem1, elem2, elems: _*) + def minus (elem1: A, elem2: A, elems: A*): This = this - (elem1, elem2, elems: _*) /** 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. */ - def minusAll(elems: Traversable[A]): This = (thisCollection /: elems) (_ minus _) + def --(elems: Traversable[A]): This = (thisCollection /: elems) (_ - _) /** 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 + * @note same as -- */ - def --(elems: Traversable[A]): This = minusAll(elems) + def minusAll(elems: Traversable[A]): This = this -- 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 + * @note same as -- */ - def minusAll(iter: Iterator[A]): This = (thisCollection /: iter) (_ minus _) + def --(iter: Iterator[A]): This = (thisCollection /: iter) (_ - _) /** 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 + * @note same as -- */ - def --(iter: Iterator[A]): This = minusAll(iter) + def minusAll(iter: Iterator[A]): This = this -- iter } diff --git a/src/library/scala/collection/generic/TraversableBuildable.scala b/src/library/scala/collection/generic/TraversableBuildable.scala deleted file mode 100755 index d1137632fc..0000000000 --- a/src/library/scala/collection/generic/TraversableBuildable.scala +++ /dev/null @@ -1,14 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ 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/TraversableTemplate.scala b/src/library/scala/collection/generic/TraversableTemplate.scala index 735f883a2a..aa651de27b 100644 --- a/src/library/scala/collection/generic/TraversableTemplate.scala +++ b/src/library/scala/collection/generic/TraversableTemplate.scala @@ -87,8 +87,8 @@ self => /** 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) + def concat[B >: A, That](that: Traversable[B])(implicit bf: BuilderFactory[B, That, This]): That = this.++(that)(bf) + def concat[B >: A, That](that: Iterator[B])(implicit bf: BuilderFactory[B, That, This]): That = this.++(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 diff --git a/src/library/scala/collection/immutable/BitSet.scala b/src/library/scala/collection/immutable/BitSet.scala index ac6970a9f9..f3bfffff81 100644 --- a/src/library/scala/collection/immutable/BitSet.scala +++ b/src/library/scala/collection/immutable/BitSet.scala @@ -27,7 +27,7 @@ abstract class BitSet extends Set[Int] with collection.BitSet with BitSetTemplat /** Adds element to bitset, returning a new set. */ - def plus (elem: Int): BitSet = { + def + (elem: Int): BitSet = { require(elem >= 0) if (contains(elem)) this else { @@ -38,7 +38,7 @@ abstract class BitSet extends Set[Int] with collection.BitSet with BitSetTemplat /** Removes element from bitset, returning a new set */ - def minus (elem: Int): BitSet = { + def - (elem: Int): BitSet = { require(elem >= 0) if (contains(elem)) { val idx = elem >> LogWL diff --git a/src/library/scala/collection/immutable/FlexMap.scala b/src/library/scala/collection/immutable/FlexMap.scala index 7ea21b3c64..8370acd07c 100644 --- a/src/library/scala/collection/immutable/FlexMap.scala +++ b/src/library/scala/collection/immutable/FlexMap.scala @@ -20,6 +20,7 @@ import generic._ */ trait FlexMap[A, +B] extends Map[A, B] { self => override def empty: Map[A, B] = FlexMap.empty + def + [B1 >: B] (kv: (A, B1)): Map[A, B1] = updated(kv._1, kv._2) } /* Factory object for `FlexMap class */ @@ -31,8 +32,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 updated [B1 >: B] (key: A, value: B1): Map[A, B1] = new Map1(key, value) - def minus (key: A): Map[A, B] = this + override def updated [B1 >: B] (key: A, value: B1): Map[A, B1] = new Map1(key, value) + def - (key: A): Map[A, B] = this } @serializable @@ -41,10 +42,10 @@ object FlexMap extends ImmutableMapFactory[Map] { def get(key: A): Option[B] = if (key == key1) Some(value1) else None def elements = Iterator((key1, value1)) - def updated [B1 >: B] (key: A, value: B1): Map[A, B1] = + override 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 minus (key: A): Map[A, B] = + def - (key: A): Map[A, B] = if (key == key1) empty else this override def foreach[U](f: ((A, B)) => U): Unit = { f((key1, value1)) @@ -59,11 +60,11 @@ object FlexMap extends ImmutableMapFactory[Map] { else if (key == key2) Some(value2) else None def elements = Iterator((key1, value1), (key2, value2)) - def updated [B1 >: B] (key: A, value: B1): Map[A, B1] = + override 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 minus (key: A): Map[A, B] = + def - (key: A): Map[A, B] = if (key == key1) new Map1(key2, value2) else if (key == key2) new Map1(key1, value1) else this @@ -81,12 +82,12 @@ object FlexMap extends ImmutableMapFactory[Map] { else if (key == key3) Some(value3) else None def elements = Iterator((key1, value1), (key2, value2), (key3, value3)) - def updated [B1 >: B] (key: A, value: B1): Map[A, B1] = + override 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 minus (key: A): Map[A, B] = + def - (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) @@ -106,13 +107,13 @@ 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 updated [B1 >: B] (key: A, value: B1): Map[A, B1] = + override 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 minus (key: A): Map[A, B] = + def - (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) diff --git a/src/library/scala/collection/immutable/FlexSet.scala b/src/library/scala/collection/immutable/FlexSet.scala index b1410961b2..37082a4407 100644 --- a/src/library/scala/collection/immutable/FlexSet.scala +++ b/src/library/scala/collection/immutable/FlexSet.scala @@ -30,8 +30,8 @@ object FlexSet extends SetFactory[Set] { class EmptySet[A] extends FlexSet[A] { override def size: Int = 0 def contains(elem: A): Boolean = false - def plus (elem: A): Set[A] = new Set1(elem) - def minus (elem: A): Set[A] = this + def + (elem: A): Set[A] = new Set1(elem) + def - (elem: A): Set[A] = this def elements: Iterator[A] = Iterator.empty override def foreach[U](f: A => U): Unit = {} } @@ -42,10 +42,10 @@ object FlexSet extends SetFactory[Set] { override def size: Int = 1 def contains(elem: A): Boolean = elem == elem1 - def plus (elem: A): Set[A] = + def + (elem: A): Set[A] = if (contains(elem)) this else new Set2(elem1, elem) - def minus (elem: A): Set[A] = + def - (elem: A): Set[A] = if (elem == elem1) new EmptySet[A] else this def elements: Iterator[A] = @@ -61,10 +61,10 @@ object FlexSet extends SetFactory[Set] { override def size: Int = 2 def contains(elem: A): Boolean = elem == elem1 || elem == elem2 - def plus (elem: A): Set[A] = + def + (elem: A): Set[A] = if (contains(elem)) this else new Set3(elem1, elem2, elem) - def minus (elem: A): Set[A] = + def - (elem: A): Set[A] = if (elem == elem1) new Set1(elem2) else if (elem == elem2) new Set1(elem1) else this @@ -81,10 +81,10 @@ object FlexSet extends SetFactory[Set] { override def size: Int = 3 def contains(elem: A): Boolean = elem == elem1 || elem == elem2 || elem == elem3 - def plus (elem: A): Set[A] = + def + (elem: A): Set[A] = if (contains(elem)) this else new Set4(elem1, elem2, elem3, elem) - def minus (elem: A): Set[A] = + def - (elem: A): Set[A] = if (elem == elem1) new Set2(elem2, elem3) else if (elem == elem2) new Set2(elem1, elem3) else if (elem == elem3) new Set2(elem1, elem2) @@ -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 plus (elem: A): Set[A] = + def + (elem: A): Set[A] = if (contains(elem)) this else new HashSet[A] + (elem1, elem2, elem3, elem4, elem) - def minus (elem: A): Set[A] = + def - (elem: A): Set[A] = if (elem == elem1) new Set3(elem2, elem3, elem4) else if (elem == elem2) new Set3(elem1, elem3, elem4) else if (elem == elem3) new Set3(elem1, elem2, elem4) diff --git a/src/library/scala/collection/immutable/HashMap.scala b/src/library/scala/collection/immutable/HashMap.scala index 6f0277a43f..7979d096aa 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 updated [B1 >: B] (key: A, value: B1): HashMap[A, B1] = synchronized { + override def updated [B1 >: B] (key: A, value: B1): HashMap[A, B1] = synchronized { makeCopyIfUpdated() val e = findEntry(key) if (e == null) { @@ -83,7 +83,7 @@ class HashMap[A, +B] extends Map[A,B] with ImmutableMapTemplate[A, B, HashMap[A, override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): HashMap[A, B1] = this + elem1 + elem2 ++ elems - def minus (key: A): HashMap[A, B] = synchronized { + def - (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 9433ec9c8f..3abffa6ec4 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 plus (elem: A): HashSet[A] = synchronized { + def + (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 minus (elem: A): HashSet[A] = synchronized { + def - (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 e243a63bf3..a7b66628bb 100644 --- a/src/library/scala/collection/immutable/IntMap.scala +++ b/src/library/scala/collection/immutable/IntMap.scala @@ -239,7 +239,9 @@ sealed abstract class IntMap[+T] extends scala.collection.immutable.Map[Int, T] case IntMap.Nil => error("key not found"); } - def updated[S >: T](key : Int, value : S) : IntMap[S] = this match { + def + [S >: T] (kv: (Int, S)): IntMap[S] = updated(kv._1, kv._2) + + override 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.updated(key, value), right) else IntMap.Bin(prefix, mask, left, right.updated(key, value)); @@ -272,7 +274,7 @@ sealed abstract class IntMap[+T] extends scala.collection.immutable.Map[Int, T] case IntMap.Nil => IntMap.Tip(key, value); } - def minus (key : Int) : IntMap[T] = this match { + def - (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 389c0c63bb..b812cef8df 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 updated [B1 >: B] (key: A, value: B1): ListMap[A, B1] = new Node[B1](key, value) + override 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] = updated(kv._1, kv._2) + 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 minus (key: A): ListMap[A, B] = this + def - (key: A): ListMap[A, B] = this /** Returns an iterator over key-value pairs. */ @@ -156,7 +156,7 @@ class ListMap[A, +B] extends Map[A, B] with ImmutableMapTemplate[A, B, ListMap[A * @param k ... * @return ... */ - override def minus (k: A): ListMap[A, B1] = + override def - (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 6872d96464..6257e8c9cc 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 plus (elem: A): ListSet[A] = new Node(elem) + def + (elem: A): ListSet[A] = new Node(elem) /** <code>-</code> can be used to remove a single element from * a set. */ - def minus (elem: A): ListSet[A] = this + def - (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 plus(e: A): ListSet[A] = if (contains(e)) this else new Node(e) + override def +(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 minus(e: A): ListSet[A] = if (e == elem) self else { + override def -(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 9e8ac7627e..f30f9eb17d 100644 --- a/src/library/scala/collection/immutable/LongMap.scala +++ b/src/library/scala/collection/immutable/LongMap.scala @@ -240,7 +240,9 @@ sealed abstract class LongMap[+T] extends scala.collection.immutable.Map[Long, T case LongMap.Nil => error("key not found"); } - def updated[S >: T](key : Long, value : S) : LongMap[S] = this match { + def + [S >: T] (kv: (Long, S)): LongMap[S] = updated(kv._1, kv._2) + + override 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)); @@ -273,7 +275,7 @@ sealed abstract class LongMap[+T] extends scala.collection.immutable.Map[Long, T case LongMap.Nil => LongMap.Tip(key, value); } - def minus(key : Long) : LongMap[T] = this match { + def -(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 ba8ad9ab6b..8a1325735f 100644 --- a/src/library/scala/collection/immutable/Map.scala +++ b/src/library/scala/collection/immutable/Map.scala @@ -25,7 +25,8 @@ 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 updated [B1 >: B](key: A, value: B1): Map[A, B1] + override def updated [B1 >: B](key: A, value: B1): Map[A, B1] + def + [B1 >: B](kv: (A, B1)): Map[A, B1] /** A hash method compatible with <code>equals</code> */ @@ -52,8 +53,9 @@ 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 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 updated[B1 >: B](key: A, value: B1): WithDefault[A, B1] = new WithDefault[A, B1](underlying.updated[B1](key, value), d) + def + [B1 >: B](kv: (A, B1)): WithDefault[A, B1] = updated(kv._1, kv._2) + def - (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/SortedMap.scala b/src/library/scala/collection/immutable/SortedMap.scala index f1e1c4b9b8..43a7562f84 100644 --- a/src/library/scala/collection/immutable/SortedMap.scala +++ b/src/library/scala/collection/immutable/SortedMap.scala @@ -26,31 +26,27 @@ trait SortedMap[A, +B] extends Map[A, B] with SortedMapTemplate[A, B, SortedMap[A, B]] { /** Needs to be overridden in subclasses. */ - override def empty: SortedMap[A, B] = throw new UnsupportedOperationException("SortedMap.empty") + override def empty: SortedMap[A, B] = throw new AbstractMethodError("SortedMap.empty") /** Needs to be overridden in subclasses. */ override protected[this] def newBuilder : Builder[(A, B), SortedMap[A, B]] = - throw new UnsupportedOperationException("SortedMap.newBuilder") + throw new AbstractMethodError("SortedMap.newBuilder") + + override def updated [B1 >: B](key: A, value: B1): SortedMap[A, B1] = this + ((key, value)) /** Add a key/value pair to this map. * @param key the key * @param value the value * @return A new map with the new binding added to this map + * @note needs to be overridden in subclasses */ - def updated [B1 >: B](key: A, value: B1): SortedMap[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)): SortedMap[A, B1] = updated(kv._1, kv._2) + def + [B1 >: B](kv: (A, B1)): SortedMap[A, B1] = throw new AbstractMethodError("SortedMap.+") /** 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) + override def plus [B1 >: B] (kv: (A, B1)): SortedMap[A, B1] = this + kv /** Adds two or more elements to this collection and returns * a new collection. @@ -59,8 +55,8 @@ trait SortedMap[A, +B] extends Map[A, B] * @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) *): SortedMap[A, B1] = - this plus elem1 plus elem2 plusAll elems + override def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): SortedMap[A, B1] = + this + elem1 + elem2 ++ elems /** Adds two or more elements to this collection and returns * a new collection. @@ -69,47 +65,47 @@ trait SortedMap[A, +B] extends Map[A, B] * @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] = - plus(elem1, elem2, elems: _*) + override def plus [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): SortedMap[A, B1] = + this.+(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 _) + override def ++[B1 >: B](elems: collection.Traversable[(A, B1)]): SortedMap[A, B1] = + ((thisCollection: SortedMap[A, B1]) /: 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. - * @note same as `plusAll` + * @note same as `++` * @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) + override def plusAll [B1 >: B](elems: collection.Traversable[(A, B1)]): SortedMap[A, B1] = this.++(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 _) + override def ++[B1 >: B] (iter: Iterator[(A, B1)]): SortedMap[A, B1] = + ((thisCollection: SortedMap[A, B1]) /: iter) (_ + _) /** 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 same as `++` * @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) + override def plusAll [B1 >: B](iter: Iterator[(A, B1)]): SortedMap[A, B1] = this.++(iter) } diff --git a/src/library/scala/collection/immutable/TreeHashMap.scala b/src/library/scala/collection/immutable/TreeHashMap.scala index 8ebc680e6d..780a69dfde 100644 --- a/src/library/scala/collection/immutable/TreeHashMap.scala +++ b/src/library/scala/collection/immutable/TreeHashMap.scala @@ -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 minus(key : Key) : TreeHashMap[Key, Value] = { + def -(key : Key) : TreeHashMap[Key, Value] = { val h = hash(key); underlying.get(h) match { case None => this; @@ -254,7 +254,7 @@ private[collection] sealed abstract class AssocMap[Key, +Value] extends immutabl else Cons(key, newval, newtail); } - def minus(key : Key) : AssocMap[Key, Value]= this match { + def -(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 34e5e977e8..fd596465a0 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 updated [B1 >: B](key: A, value: B1): TreeMap[A, B1] = { + override 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)) } @@ -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 minus (key:A): TreeMap[A, B] = + def - (key:A): TreeMap[A, B] = if (tree.lookup(key).isEmpty) this else newMap(size - 1, tree.delete(key)) diff --git a/src/library/scala/collection/immutable/TreeSet.scala b/src/library/scala/collection/immutable/TreeSet.scala index 84715d2363..0f352356b8 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 plus (elem: A): TreeSet[A] = { + def + (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 minus (elem:A): TreeSet[A] = + def - (elem:A): TreeSet[A] = if (tree.lookup(elem).isEmpty) this else newSet(size - 1, tree.delete(elem)) diff --git a/src/library/scala/collection/mutable/BitSet.scala b/src/library/scala/collection/mutable/BitSet.scala index 67ca52fba8..9518597b2f 100644 --- a/src/library/scala/collection/mutable/BitSet.scala +++ b/src/library/scala/collection/mutable/BitSet.scala @@ -32,7 +32,7 @@ class BitSet (protected var elems: Array[Long]) extends Set[Int] with collection /** Adds element to bitset, * @return element was already present. */ - def put (elem: Int): Boolean = { + override def put (elem: Int): Boolean = { require(elem >= 0) if (contains(elem)) true else { @@ -45,7 +45,7 @@ class BitSet (protected var elems: Array[Long]) extends Set[Int] with collection /** Removes element from bitset. * @return element was already present. */ - def remove (elem: Int): Boolean = { + override def remove (elem: Int): Boolean = { require(elem >= 0) if (contains(elem)) { val idx = elem >> LogWL @@ -54,6 +54,9 @@ class BitSet (protected var elems: Array[Long]) extends Set[Int] with collection } else false } + def += (elem: Int): this.type = { put(elem); this } + def -= (elem: Int): this.type = { remove(elem); this } + def toImmutable = immutable.BitSet.fromArray(elems) override def clone(): BitSet = { diff --git a/src/library/scala/collection/mutable/DefaultMapModel.scala b/src/library/scala/collection/mutable/DefaultMapModel.scala index ee1b978065..9acf089506 100644 --- a/src/library/scala/collection/mutable/DefaultMapModel.scala +++ b/src/library/scala/collection/mutable/DefaultMapModel.scala @@ -32,12 +32,14 @@ trait DefaultMapModel[A, B] extends Map[A, B] { else Some(e.value) } - def put(key: A, value: B): Option[B] = { + override def put(key: A, value: B): Option[B] = { val e = findEntry(key) if (e == null) { addEntry(new Entry(key, value)); None } else { val v = e.value; e.value = value; Some(v) } } + def += (kv: (A, B)): this.type = { put(kv._1, kv._2); this } + 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 a44a6bdcc5..a2cce3358b 100644 --- a/src/library/scala/collection/mutable/HashMap.scala +++ b/src/library/scala/collection/mutable/HashMap.scala @@ -20,11 +20,13 @@ 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 remove(key: A): Option[B] = removeEntry(key) match { + override def remove(key: A): Option[B] = removeEntry(key) match { case Some(e) => Some(e.value) case None => None } + override def -=(key: A): this.type = { remove(key); this } + override def clear() = super.clear() override def size: Int = super[HashTable].size diff --git a/src/library/scala/collection/mutable/HashSet.scala b/src/library/scala/collection/mutable/HashSet.scala index 971e70ad1a..b72f3dc6fd 100644 --- a/src/library/scala/collection/mutable/HashSet.scala +++ b/src/library/scala/collection/mutable/HashSet.scala @@ -29,9 +29,11 @@ class HashSet[A] extends Set[A] with MutableSetTemplate[A, HashSet[A]] with Flat def contains(elem: A): Boolean = containsEntry(elem) - def put(elem: A): Boolean = addEntry(elem) + def += (elem: A): this.type = { addEntry(elem); this } + def -= (elem: A): this.type = { removeEntry(elem); this } - def remove(elem: A): Boolean = !removeEntry(elem).isEmpty + override def put(elem: A): Boolean = addEntry(elem) + override def remove(elem: A): Boolean = !removeEntry(elem).isEmpty override def clear() = super.clear() diff --git a/src/library/scala/collection/mutable/LinkedHashMap.scala b/src/library/scala/collection/mutable/LinkedHashMap.scala index 954821715c..e02e574c43 100644 --- a/src/library/scala/collection/mutable/LinkedHashMap.scala +++ b/src/library/scala/collection/mutable/LinkedHashMap.scala @@ -37,6 +37,8 @@ class LinkedHashMap[A, B] extends Map[A,B] with HashTable[A] with DefaultMapMode private var ordered = List[Entry]() + def -= (key: A): this.type = { remove(key); this } + override def remove(key: A): Option[B] = removeEntry(key) match { case None => None case Some(e) => @@ -44,8 +46,6 @@ class LinkedHashMap[A, B] extends Map[A,B] with HashTable[A] with DefaultMapMode Some(e.value) } - override def delete (key: A) { remove(key) } - override def put(key: A, value: B): Option[B] = { val e = findEntry(key) if (e == null) { diff --git a/src/library/scala/collection/mutable/LinkedHashSet.scala b/src/library/scala/collection/mutable/LinkedHashSet.scala index 481c8667dc..1b3c9e29fc 100644 --- a/src/library/scala/collection/mutable/LinkedHashSet.scala +++ b/src/library/scala/collection/mutable/LinkedHashSet.scala @@ -33,13 +33,16 @@ class LinkedHashSet[A] extends Set[A] with MutableSetTemplate[A, LinkedHashSet[A def contains(elem: A): Boolean = containsEntry(elem) - def put(elem: A): Boolean = + def += (elem: A): this.type = { put(elem); this } + def -= (elem: A): this.type = { remove(elem); this } + + override def put(elem: A): Boolean = if (addEntry(elem)) { ordered = elem :: ordered true } else false - def remove(elem: A): Boolean = removeEntry(elem) match { + override def remove(elem: A): Boolean = removeEntry(elem) match { case None => false case Some(elem) => ordered = ordered.filter(_ != elem); true } diff --git a/src/library/scala/collection/mutable/OpenHashMap.scala b/src/library/scala/collection/mutable/OpenHashMap.scala index 3c84721b9e..5a0810cfe4 100644 --- a/src/library/scala/collection/mutable/OpenHashMap.scala +++ b/src/library/scala/collection/mutable/OpenHashMap.scala @@ -106,6 +106,9 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl put(key, hashOf(key), value); } + def += (kv: (Key, Value)): this.type = { put(kv._1, kv._2); this } + def -= (key: Key): this.type = { remove(key); this } + override def put(key : Key, value : Value): Option[Value] = put(key, hashOf(key), value) @@ -137,8 +140,6 @@ class OpenHashMap[Key, Value](initialSize : Int) extends scala.collection.mutabl } else None } - override def delete(key: Key) { remove(key) } - def get(key : Key) : Option[Value] = { val hash = hashOf(key); diff --git a/src/library/scala/collection/mutable/Set.scala b/src/library/scala/collection/mutable/Set.scala index c2b8ca1c2d..79a31cc5ef 100644 --- a/src/library/scala/collection/mutable/Set.scala +++ b/src/library/scala/collection/mutable/Set.scala @@ -17,9 +17,9 @@ import generic._ * 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] + * def += (elem: A): this.type + * def -= (elem: A): this.type * * @author Matthias Zenger * @author Martin Odersky diff --git a/src/swing/scala/swing/ButtonGroup.scala b/src/swing/scala/swing/ButtonGroup.scala index ace17bd57b..4b5eeab74c 100644 --- a/src/swing/scala/swing/ButtonGroup.scala +++ b/src/swing/scala/swing/ButtonGroup.scala @@ -15,8 +15,8 @@ class ButtonGroup(initialButtons: AbstractButton*) { val peer: javax.swing.ButtonGroup = new javax.swing.ButtonGroup val buttons: mutable.Set[AbstractButton] = new mutable.Set[AbstractButton] { - def remove(b: AbstractButton): Boolean = { peer.remove(b.peer); true } // !!! Ingo: what to return? - def put(b: AbstractButton): Boolean = { peer.add(b.peer); true } // !!! Ingo: what to return? + def -=(b: AbstractButton): this.type = { peer.remove(b.peer); this } + def +=(b: AbstractButton): this.type = { peer.add(b.peer); this } def contains(b: AbstractButton) = elements.contains(b) override def size = peer.getButtonCount def elements: Iterator[AbstractButton] = new Iterator[AbstractButton] { diff --git a/src/swing/scala/swing/Container.scala b/src/swing/scala/swing/Container.scala index b56e2adcd7..0bea4d8c46 100644 --- a/src/swing/scala/swing/Container.scala +++ b/src/swing/scala/swing/Container.scala @@ -21,7 +21,7 @@ object Container { wrap(c) } protected def insertAt(n: Int, c: Component) { peer.add(c.peer, n) } - def +=(c: Component): this.type = { peer.add(c.peer); this } + def +=(c: Component): this.type = { peer.add(c.peer) ; this } def length = peer.getComponentCount def apply(n: Int) = wrap(peer.getComponent(n)) } diff --git a/src/swing/scala/swing/LayoutContainer.scala b/src/swing/scala/swing/LayoutContainer.scala index 35375742cd..e05273ab5f 100644 --- a/src/swing/scala/swing/LayoutContainer.scala +++ b/src/swing/scala/swing/LayoutContainer.scala @@ -44,13 +44,13 @@ trait LayoutContainer extends Container.Wrapper { * also ensures that myComponent is properly add to this container. */ def layout: Map[Component, Constraints] = new Map[Component, Constraints] { - def remove(c: Component): Option[Constraints] = { val r = get(c); _contents -= c; r } - def put(c: Component, l: Constraints): Option[Constraints] = { - val r = get(c) + def -= (c: Component): this.type = { _contents -= c; this } + def += (cl: (Component, Constraints)): this.type = { update(cl._1, cl._2); this } + override def update (c: Component, l: Constraints) { val (v, msg) = areValid(l) if (!v) throw new IllegalArgumentException(msg) add(c, l) - r + this } def get(c: Component) = Swing.toOption(constraintsFor(c)) override def size = peer.getComponentCount diff --git a/src/swing/scala/swing/ListView.scala b/src/swing/scala/swing/ListView.scala index 695c6476db..fe9f34de57 100644 --- a/src/swing/scala/swing/ListView.scala +++ b/src/swing/scala/swing/ListView.scala @@ -171,8 +171,8 @@ class ListView[A] extends Component { */ object selection extends Publisher { protected abstract class Indices[A](a: =>Seq[A]) extends scala.collection.mutable.Set[A] { - def put(n: A): Boolean - def remove(n: A): Boolean + def -=(n: A): this.type + def +=(n: A): this.type def contains(n: A) = a.contains(n) override def size = a.length def elements = a.elements @@ -182,8 +182,8 @@ class ListView[A] extends Component { * The indices of the currently selected items. */ object indices extends Indices(peer.getSelectedIndices) { - def remove(n: Int): Boolean = { peer.removeSelectionInterval(n,n); true } // !!! Ingo; What to return? } - def put(n: Int): Boolean = { peer.addSelectionInterval(n,n); true } // !!! Ingo; What to return? } + def -=(n: Int): this.type = { peer.removeSelectionInterval(n,n); this } + def +=(n: Int): this.type = { peer.addSelectionInterval(n,n); this } def leadIndex: Int = peer.getSelectionModel.getLeadSelectionIndex def anchorIndex: Int = peer.getSelectionModel.getAnchorSelectionIndex diff --git a/src/swing/scala/swing/Publisher.scala b/src/swing/scala/swing/Publisher.scala index fa8887fe88..f586af8813 100644 --- a/src/swing/scala/swing/Publisher.scala +++ b/src/swing/scala/swing/Publisher.scala @@ -155,9 +155,9 @@ abstract class RefBuffer[A <: AnyRef] extends Buffer[A] with SingleRefCollection private[swing] abstract class RefSet[A <: AnyRef] extends Set[A] with SingleRefCollection[A] { self => protected val underlying: Set[Reference[A]] - def remove(el: A): Boolean = { val r = underlying remove Ref(el); purgeReferences(); r } - def put(el: A): Boolean = { purgeReferences(); underlying put Ref(el) } - def contains(el: A) = { purgeReferences(); underlying.contains(Ref(el)) } + def -=(el: A): this.type = { underlying -= Ref(el); purgeReferences(); this } + def +=(el: A): this.type = { purgeReferences(); underlying += Ref(el); this } + def contains(el: A): Boolean = { purgeReferences(); underlying.contains(Ref(el)) } override def size = { purgeReferences(); underlying.size } protected[this] def removeReference(ref: Reference[A]) { underlying -= ref } diff --git a/src/swing/scala/swing/Table.scala b/src/swing/scala/swing/Table.scala index e6af3a0c5b..f9568782c3 100644 --- a/src/swing/scala/swing/Table.scala +++ b/src/swing/scala/swing/Table.scala @@ -160,24 +160,24 @@ class Table extends Component with Scrollable with Publisher { object selection extends Publisher { // TODO: could be a sorted set protected abstract class SelectionSet[A](a: =>Seq[A]) extends scala.collection.mutable.Set[A] { - def put(n: A): Boolean - def remove(n: A): Boolean + def -=(n: A): this.type + def +=(n: A): this.type def contains(n: A) = a.contains(n) override def size = a.length def elements = a.elements } object rows extends SelectionSet(peer.getSelectedRows) { - def remove(n: Int): Boolean = { peer.removeRowSelectionInterval(n,n); true } // !!! Ingo; What to return? } - def put(n: Int): Boolean = { peer.addRowSelectionInterval(n,n); true } // !!! Ingo; What to return? } + def -=(n: Int) = { peer.removeRowSelectionInterval(n,n); this } + def +=(n: Int) = { peer.addRowSelectionInterval(n,n); this } def leadIndex: Int = peer.getSelectionModel.getLeadSelectionIndex def anchorIndex: Int = peer.getSelectionModel.getAnchorSelectionIndex } object columns extends SelectionSet(peer.getSelectedColumns) { - def remove(n: Int): Boolean = { peer.removeColumnSelectionInterval(n,n); true } // !!! Ingo; What to return? } - def put(n: Int): Boolean = { peer.addColumnSelectionInterval(n,n); true } // !!! Ingo; What to return? } + def -=(n: Int) = { peer.removeColumnSelectionInterval(n,n); this } + def +=(n: Int) = { peer.addColumnSelectionInterval(n,n); this } def leadIndex: Int = peer.getColumnModel.getSelectionModel.getLeadSelectionIndex def anchorIndex: Int = peer.getColumnModel.getSelectionModel.getAnchorSelectionIndex @@ -185,15 +185,15 @@ class Table extends Component with Scrollable with Publisher { def cells: Set[(Int, Int)] = new SelectionSet[(Int, Int)]((for(r <- selection.rows; c <- selection.columns) yield (r,c)).toSeq) { outer => - def remove(n: (Int, Int)): Boolean = { + def -=(n: (Int, Int)) = { peer.removeRowSelectionInterval(n._1,n._1) peer.removeColumnSelectionInterval(n._2,n._2) - true// !!! Ingo: what to return? + this } - def put(n: (Int, Int)): Boolean = { + def +=(n: (Int, Int)) = { peer.addRowSelectionInterval(n._1,n._1) peer.addColumnSelectionInterval(n._2,n._2) - false// !!! Ingo: what to return? + this } override def size = peer.getSelectedRowCount * peer.getSelectedColumnCount } diff --git a/test/files/run/t1505.scala b/test/files/run/t1505.scala index caadee8a0f..fdcd4f3fb3 100644 --- a/test/files/run/t1505.scala +++ b/test/files/run/t1505.scala @@ -11,15 +11,9 @@ object R extends Enumeration { } object Test extends Application { - assert(Some(P(0)) == P.withName("A")) - assert(Some(P.C) == P.withName("C")) - assert(None == P.withName("Q")) + assert(P(0) == P.withName("A")) + assert(P.C == P.withName("C")) - assert(Some(Q(0)) == Q.withName("A")) - assert(Some(Q.C) == Q.withName("C")) - assert(None == Q.withName("Q")) - - assert(None == R.withName("A")) - assert(None == R.withName("C")) - assert(None == R.withName("Q")) + assert(Q(0) == Q.withName("A")) + assert(Q.C == Q.withName("C")) } |