Skip to content

Commit

Permalink
another change to collection libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Jan 4, 2007
1 parent 78d2e50 commit 29da705
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/library/scala/Iterable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,19 @@ trait Iterable[+A] {
/** Appends two iterable objects
*
* @return the new iterable object
* @deprecated use <code>++</code> instead
*/
def concat [B >: A](that: Iterable[B]): Iterable[B] = {
[deprecated] def concat [B >: A](that: Iterable[B]): Iterable[B] = {
val buf = new ArrayBuffer[B]
this copyToBuffer buf
that copyToBuffer buf
buf
}

/** Appends two iterable objects
*
* @return the new iterable object
*/
def ++ [B >: A](that: Iterable[B]): Iterable[B] = {
val buf = new ArrayBuffer[B]
this copyToBuffer buf
Expand Down
9 changes: 9 additions & 0 deletions src/library/scala/Iterator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,21 @@ trait Iterator[+A] {

/** Returns a new iterator that first yields the elements of this
* iterator followed by the elements provided by iterator <code>that</code>.
* @deprecated use <code>++</code>
*/
def append[B >: A](that: Iterator[B]) = new Iterator[B] {
def hasNext = Iterator.this.hasNext || that.hasNext
def next = if (Iterator.this.hasNext) Iterator.this.next else that.next
}

/** Returns a new iterator that first yields the elements of this
* iterator followed by the elements provided by iterator <code>that</code>.
*/
def ++[B >: A](that: Iterator[B]) = new Iterator[B] {
def hasNext = Iterator.this.hasNext || that.hasNext
def next = if (Iterator.this.hasNext) Iterator.this.next else that.next
}

/** Applies the given function <code>f</code> to each element of
* this iterator, then concatenates the results.
*
Expand Down
2 changes: 2 additions & 0 deletions src/library/scala/Predef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ object Predef {

type Function[-a,+b] = Function1[a,b]

val Map = collection.mutable.Map
val Set = collection.mutable.Set

// errors and asserts -------------------------------------------------

Expand Down
7 changes: 6 additions & 1 deletion src/library/scala/Seq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,19 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Iterable[A] {
/** Appends two iterable objects
*
* @return the new iterable object
* @deprecated use <code>++</code> instead
*/
override def concat [B >: A](that: Iterable[B]): Seq[B] = {
[deprecated] override def concat [B >: A](that: Iterable[B]): Seq[B] = {
val buf = new ArrayBuffer[B]
this copyToBuffer buf
that copyToBuffer buf
buf
}

/** Appends two iterable objects
*
* @return the new iterable object
*/
override def ++ [B >: A](that: Iterable[B]): Seq[B] = {
val buf = new ArrayBuffer[B]
this copyToBuffer buf
Expand Down
2 changes: 2 additions & 0 deletions src/library/scala/collection/Map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package scala.collection

import Predef._


//import Predef.NoSuchElementException

Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/collection/immutable/ListMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object ListMap {

/** The canonical factory for this type
*/
//def apply[A, B](elems: Pair[A, B]*) = empty[A, B] ++ elems
def apply[A, B](elems: Pair[A, B]*) = empty[A, B] ++ elems
}

/** This class implements immutable maps using a list-based data
Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/collection/immutable/ListSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object ListSet {

/** The canonical factory for this type
*/
// def apply[A, B](elems: A*) = empty[A] ++ elems
def apply[A, B](elems: A*) = empty[A] ++ elems
}


Expand Down
11 changes: 10 additions & 1 deletion src/library/scala/collection/immutable/Map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,17 @@ package scala.collection.immutable
* @author Erik Stenman
* @author Martin Odersky
* @version 1.2, 31/06/2006
* todo: make contravariant in A?
*/
object Map {

/** The empty map of this type; this is implemented as a treemap */
def empty[A <% Ordered[A], B] = new TreeMap[A, B]

/** The canonical factory for this type
*/
def apply[A <% Ordered[A], B](elems: Pair[A, B]*) = empty[A, B] ++ elems
}

trait Map[A, +B] extends collection.Map[A, B] {

/** This method returns a new map instance of the same class
Expand Down
10 changes: 10 additions & 0 deletions src/library/scala/collection/immutable/Set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ package scala.collection.immutable
* @author Matthias Zenger
* @version 1.1, 03/05/2004
*/
object Set {
/** The empty set of this type
*/
def empty[A <% Ordered[A]] = new TreeSet[A]

/** The canonical factory for this type
*/
def apply[A <% Ordered[A]](elems: A*) = empty[A] ++ elems
}

trait Set[A] extends AnyRef with collection.Set[A] {

/** @return an empty set of arbitrary element type
Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/collection/immutable/TreeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object TreeMap {

/** The canonical factory for this type
*/
// def apply[A, B](elems: Pair[A, B]*) = empty[A, B] ++ elems
def apply[A <% Ordered[A], B](elems: Pair[A, B]*) = empty[A, B] ++ elems
}

/** This class implements immutable maps using a tree.
Expand Down
4 changes: 4 additions & 0 deletions src/library/scala/collection/immutable/TreeSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ object TreeSet {
/** The empty set of this type
*/
def empty[A <% Ordered[A]] = new TreeSet[A]

/** The canonical factory for this type
*/
def apply[A <% Ordered[A]](elems: A*) = empty[A] ++ elems
}

/** This class implements immutable sets using a tree.
Expand Down
10 changes: 8 additions & 2 deletions src/library/scala/collection/immutable/UnbalancedTreeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ package scala.collection.immutable


object UnbalancedTreeMap {
def Empty[A <% Ordered[A], B] = new UnbalancedTreeMap[A, B]

/** The empty map of this type */
def empty[A <% Ordered[A], B] = new UnbalancedTreeMap[A, B]

/** The canonical factory for this type
*/
def apply[A<% Ordered[A], B](elems: Pair[A, B]*) = empty[A, B] ++ elems
}

/** This class implements immutable maps using a tree.
Expand All @@ -28,7 +34,7 @@ class UnbalancedTreeMap[A <% Ordered[A], +B] extends Map[A, B] {

/** A factory to create empty maps of the same type of keys.
*/
def empty[C] = UnbalancedTreeMap.Empty[A, C]
def empty[C] = UnbalancedTreeMap.empty[A, C]

def size: Int = 0

Expand Down
2 changes: 2 additions & 0 deletions src/library/scala/collection/mutable/DefaultMapModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package scala.collection.mutable

import Predef._

/** This class is used internally. It implements the mutable <code>Map</code>
* class in terms of three functions: <code>findEntry</code>,
* <code>addEntry</code>, and <code>entries</code>.
Expand Down
12 changes: 12 additions & 0 deletions src/library/scala/collection/mutable/HashMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@

package scala.collection.mutable

import Predef._

/** This class implements mutable maps using a hashtable.
*
* @author Matthias Zenger
* @author Martin Odersky
* @version 2.0, 31/12/2006
*/
object HashMap {

/** The empty map of this type */
def empty[A, B] = new HashMap[A, B]

/** The canonical factory for this type
*/
def apply[A, B](elems: Pair[A, B]*) = empty[A, B] ++ elems
}

[serializable]
class HashMap[A, B] extends Map[A,B] with HashTable[A] with DefaultMapModel[A,B] {

Expand Down
13 changes: 12 additions & 1 deletion src/library/scala/collection/mutable/HashSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ package scala.collection.mutable
/** This class implements mutable sets using a hashtable.
*
* @author Matthias Zenger
* @version 1.0, 08/07/2003
* @author Martin Odersky
* @version 2.0, 31/12/2006
*/
object HashSet {

/** The empty map of this type */
def empty[A] = new HashSet[A]

/** The canonical factory for this type
*/
def apply[A](elems: A*) = empty[A] ++ elems
}

[serializable]
class HashSet[A] extends Set[A] with HashTable[A] {

Expand Down
16 changes: 15 additions & 1 deletion src/library/scala/collection/mutable/Map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package scala.collection.mutable

import Predef._

//import Predef.UnsupportedOperationException

/** This class represents mutable maps. Concrete map implementations
Expand All @@ -19,8 +21,20 @@ package scala.collection.mutable
* and <code>-=</code>.
*
* @author Matthias Zenger
* @version 1.1, 09/05/2004
* @author Martin Odersky
* @version 2.0, 31/12/2006
*/

object Map {

/** The empty map of this type; this is implemented as a hashtable */
def empty[A, B] = new HashMap[A, B]

/** The canonical factory for this type
*/
def apply[A, B](elems: Pair[A, B]*) = empty[A, B] ++ elems
}

[cloneable]
trait Map[A, B] extends AnyRef
with collection.Map[A, B]
Expand Down
12 changes: 11 additions & 1 deletion src/library/scala/collection/mutable/Set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ package scala.collection.mutable
* @author Matthias Zenger
* @version 1.1, 09/05/2004
*/
object Set {

/** The empty map of this type; this is implemented as a hashtable */
def empty[A] = new HashSet[A]

/** The canonical factory for this type
*/
def apply[A](elems: A*) = empty[A] ++ elems
}

[cloneable]
trait Set[A] extends collection.Set[A] with Scriptable[Message[A]] {

Expand Down Expand Up @@ -120,7 +130,7 @@ trait Set[A] extends collection.Set[A] with Scriptable[Message[A]] {
*
* @param elem the element to be removed
*/
def - (elem: A) { -=(elem); this }
def - (elem: A): Set[A] = { -=(elem); this }

/** Remove two or more elements from this set.
* @param elem1 the first element.
Expand Down

0 comments on commit 29da705

Please sign in to comment.