diff options
author | Martin Odersky <odersky@gmail.com> | 2007-01-22 21:04:13 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2007-01-22 21:04:13 +0000 |
commit | 6c4b4f89c8f5230027a583c7b51b3d19195069f4 (patch) | |
tree | 7a9311f4241b31ef6241eb8be46cb09abdab68f0 /src/library | |
parent | 0e974bb3739d7cf5b375c88502f9c1c5eb7df78e (diff) | |
download | scala-6c4b4f89c8f5230027a583c7b51b3d19195069f4.tar.gz scala-6c4b4f89c8f5230027a583c7b51b3d19195069f4.tar.bz2 scala-6c4b4f89c8f5230027a583c7b51b3d19195069f4.zip |
Fxied bug 909;
Updated collection library
Diffstat (limited to 'src/library')
19 files changed, 37 insertions, 42 deletions
diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala index 8167b5cb6a..46b659caac 100644 --- a/src/library/scala/Option.scala +++ b/src/library/scala/Option.scala @@ -31,10 +31,14 @@ object Option { */ sealed abstract class Option[+A] extends Product { - /** True if the option is the <code>None</value>, false otherwise. + /** True if the option is the <code>None</code> value, false otherwise. */ def isEmpty: Boolean + /** True if the option is a <code>Some</code>(...) false otherwise. + */ + def isDefined: Boolean = !isEmpty + /** get the value of this option. * @requires that the option is nonEmpty. * @throws Predef.NoSuchElementException if the option is empty. diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index 05f38e8688..b562fbed97 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -61,11 +61,11 @@ object Predef { type Function[-a,+b] = Function1[a,b] - type Map[a, b] = collection.mutable.Map[a, b] - type Set[a] = collection.mutable.Set[a] + type Map[a, b] = collection.immutable.Map[a, b] + type Set[a] = collection.immutable.Set[a] - val Map = collection.mutable.Map - val Set = collection.mutable.Set + val Map = collection.immutable.Map + val Set = collection.immutable.Set // errors and asserts ------------------------------------------------- diff --git a/src/library/scala/collection/immutable/Map.scala b/src/library/scala/collection/immutable/Map.scala index 8c9b3b75b8..ef056e75f5 100644 --- a/src/library/scala/collection/immutable/Map.scala +++ b/src/library/scala/collection/immutable/Map.scala @@ -11,6 +11,7 @@ package scala.collection.immutable +import Predef._ /** <p> * This class extends the <code>Map</code> interface of collections @@ -36,11 +37,11 @@ package scala.collection.immutable object Map { /** The empty map of this type; this is implemented as a treemap */ - def empty[A <% Ordered[A], B] = new TreeMap[A, B] + def empty[A, B] = new EmptyMap[A, B] /** The canonical factory for this type */ - def apply[A <% Ordered[A], B](elems: Pair[A, B]*) = empty[A, B] ++ elems + def apply[A, B](elems: Pair[A, B]*) = empty[A, B] ++ elems } trait Map[A, +B] extends collection.Map[A, B] { diff --git a/src/library/scala/collection/immutable/Set.scala b/src/library/scala/collection/immutable/Set.scala index eadc656470..3ecd3df5e2 100644 --- a/src/library/scala/collection/immutable/Set.scala +++ b/src/library/scala/collection/immutable/Set.scala @@ -27,7 +27,7 @@ package scala.collection.immutable object Set { /** The empty set of this type */ - def empty[A <% Ordered[A]] = new TreeSet[A] + def empty[A] = new EmptySet[A] /** The canonical factory for this type */ diff --git a/src/library/scala/collection/mutable/ArrayBuffer.scala b/src/library/scala/collection/mutable/ArrayBuffer.scala index ab68478b87..f840fc3a2a 100644 --- a/src/library/scala/collection/mutable/ArrayBuffer.scala +++ b/src/library/scala/collection/mutable/ArrayBuffer.scala @@ -144,7 +144,7 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] { /** Clears the buffer contents. */ - def clear: Unit = { + def clear(): Unit = { size = 0 } diff --git a/src/library/scala/collection/mutable/BitSet.scala b/src/library/scala/collection/mutable/BitSet.scala index 02e876ba80..610b86e3ee 100644 --- a/src/library/scala/collection/mutable/BitSet.scala +++ b/src/library/scala/collection/mutable/BitSet.scala @@ -77,7 +77,7 @@ class BitSet(initSize: Int) extends collection.BitSet with Set[Int] { /** Clears all bits of the set. */ - def clear: Unit = { + override def clear(): Unit = { java.util.Arrays.fill(arr, 0) size = 0 } diff --git a/src/library/scala/collection/mutable/Buffer.scala b/src/library/scala/collection/mutable/Buffer.scala index 4bf49e3c57..5d8f24393c 100644 --- a/src/library/scala/collection/mutable/Buffer.scala +++ b/src/library/scala/collection/mutable/Buffer.scala @@ -196,7 +196,7 @@ trait Buffer[A] extends AnyRef /** Clears the buffer contents. */ - def clear: Unit + def clear(): Unit /** Send a message to this scriptable object. * diff --git a/src/library/scala/collection/mutable/BufferProxy.scala b/src/library/scala/collection/mutable/BufferProxy.scala index c43b34ef2b..5f1d60e333 100644 --- a/src/library/scala/collection/mutable/BufferProxy.scala +++ b/src/library/scala/collection/mutable/BufferProxy.scala @@ -136,7 +136,7 @@ trait BufferProxy[A] extends Buffer[A] with Proxy { /** Clears the buffer contents. */ - def clear: Unit = self.clear + def clear(): Unit = self.clear /** Send a message to this scriptable object. * diff --git a/src/library/scala/collection/mutable/DefaultMapModel.scala b/src/library/scala/collection/mutable/DefaultMapModel.scala index f5f6befcb0..9bf8b803b5 100644 --- a/src/library/scala/collection/mutable/DefaultMapModel.scala +++ b/src/library/scala/collection/mutable/DefaultMapModel.scala @@ -40,9 +40,6 @@ trait DefaultMapModel[A, B] extends Map[A, B] { else e.value = value } - def elements = entries map (e => Pair(e.key, e.value)) + def elements = entries map {e => {e.key, e.value}} } -[serializable] -final class DefaultEntry[A, B](val key: A, var value: B) - extends HashEntry[A, DefaultEntry[A, B]] diff --git a/src/library/scala/collection/mutable/HashMap.scala b/src/library/scala/collection/mutable/HashMap.scala index d7bd56498f..863a535aef 100644 --- a/src/library/scala/collection/mutable/HashMap.scala +++ b/src/library/scala/collection/mutable/HashMap.scala @@ -34,10 +34,7 @@ class HashMap[A, B] extends Map[A,B] with HashTable[A] with DefaultMapModel[A,B] def -= (key: A) { removeEntry(key) } - override def clear = { - initTable() - tableSize = 0 - } + override def clear() = super.clear() override def clone(): Map[A, B] = new HashMap[A, B] ++ this } diff --git a/src/library/scala/collection/mutable/HashSet.scala b/src/library/scala/collection/mutable/HashSet.scala index 3d53c7e223..c1c6096fa9 100644 --- a/src/library/scala/collection/mutable/HashSet.scala +++ b/src/library/scala/collection/mutable/HashSet.scala @@ -28,22 +28,16 @@ object HashSet { } [serializable] -class HashSet[A] extends Set[A] with HashTable[A] { +class HashSet[A] extends Set[A] with FlatHashTable[A] { - def contains(elem: A): Boolean = findEntry(elem) != null + def contains(elem: A): Boolean = containsEntry(elem) - def +=(elem: A) { if (findEntry(elem) == null) addEntry(new SetEntry(elem)) } + def +=(elem: A) { addEntry(elem) } def -=(elem: A) { removeEntry(elem) } - def elements = entries map (.key) - - def clear {initTable(); tableSize = 0 } - - protected type Entry = SetEntry[A] + override def clear() = super.clear() override def clone(): Set[A] = new HashSet[A] ++ this } -[serializable] -final class SetEntry[A](val key: A) extends HashEntry[A, SetEntry[A]] diff --git a/src/library/scala/collection/mutable/HashTable.scala b/src/library/scala/collection/mutable/HashTable.scala index cb52e6cb4a..e82a27e506 100644 --- a/src/library/scala/collection/mutable/HashTable.scala +++ b/src/library/scala/collection/mutable/HashTable.scala @@ -34,11 +34,11 @@ trait HashTable[A] extends AnyRef { /** The load factor for the hash table. */ - protected val loadFactor: Float = 0.75f + protected def loadFactor: Float = 0.75f /** The initial size of the hash table. */ - protected val initialSize: Int = 16 + protected def initialSize: Int = 16 /** The initial threshold */ @@ -46,7 +46,8 @@ trait HashTable[A] extends AnyRef { /** The actual hash table. */ - protected var table: Array[Entry] = new Array(initialSize) + protected var table: Array[Entry] = + if (initialSize == 0) null else new Array(initialSize) /** The number of mappings contained in this hash table. */ @@ -56,7 +57,7 @@ trait HashTable[A] extends AnyRef { */ protected var threshold: Int = initialThreshold - /** Returns the size of this hash map. + /** Returns the size of this hash table. */ def size = tableSize @@ -117,9 +118,10 @@ trait HashTable[A] extends AnyRef { } } - protected def initTable() { + def clear() { var i = table.length - 1 while (i >= 0) { table(i) = null; i = i - 1 } + tableSize = 0 } private def newThreshold(size: Int) = diff --git a/src/library/scala/collection/mutable/ImmutableSetAdaptor.scala b/src/library/scala/collection/mutable/ImmutableSetAdaptor.scala index 93323b750f..57570f327d 100644 --- a/src/library/scala/collection/mutable/ImmutableSetAdaptor.scala +++ b/src/library/scala/collection/mutable/ImmutableSetAdaptor.scala @@ -44,6 +44,6 @@ class ImmutableSetAdaptor[A](protected var set: immutable.Set[A]) extends Set[A] def -=(elem: A): Unit = { set = set - elem } - def clear: Unit = { set = set.empty } + override def clear: Unit = { set = set.empty } } diff --git a/src/library/scala/collection/mutable/JavaSetAdaptor.scala b/src/library/scala/collection/mutable/JavaSetAdaptor.scala index 31e12c598e..141c527caa 100644 --- a/src/library/scala/collection/mutable/JavaSetAdaptor.scala +++ b/src/library/scala/collection/mutable/JavaSetAdaptor.scala @@ -36,7 +36,7 @@ class JavaSetAdaptor[A](jset: java.util.Set) extends Set[A] { def -=(elem: A): Unit = { val x = jset.remove(elem); } - def clear: Unit = jset.clear() + override def clear: Unit = jset.clear() override def clone(): Set[A] = { val res = new HashSet[A] diff --git a/src/library/scala/collection/mutable/LinkedHashSet.scala b/src/library/scala/collection/mutable/LinkedHashSet.scala index b87ca30838..c55c16201c 100644 --- a/src/library/scala/collection/mutable/LinkedHashSet.scala +++ b/src/library/scala/collection/mutable/LinkedHashSet.scala @@ -37,7 +37,7 @@ class LinkedHashSet[A](private val set0 : java.util.LinkedHashSet) extends Set[A def next = i.next().asInstanceOf[A] } - def clear = set0.clear() + override def clear = set0.clear() def size = set0.size() diff --git a/src/library/scala/collection/mutable/Map.scala b/src/library/scala/collection/mutable/Map.scala index c94f9a6617..ce7bdcabf0 100644 --- a/src/library/scala/collection/mutable/Map.scala +++ b/src/library/scala/collection/mutable/Map.scala @@ -152,7 +152,7 @@ trait Map[A, B] extends AnyRef /** Removes all mappings from the map. After this operation is * completed, the map is empty. */ - def clear: Unit = keys foreach -= + def clear(): Unit = keys foreach -= /** This function transforms all the values of mappings contained * in this map with function <code>f</code>. diff --git a/src/library/scala/collection/mutable/Set.scala b/src/library/scala/collection/mutable/Set.scala index c05bfebfd6..7ac715d136 100644 --- a/src/library/scala/collection/mutable/Set.scala +++ b/src/library/scala/collection/mutable/Set.scala @@ -187,7 +187,7 @@ trait Set[A] extends collection.Set[A] with Scriptable[Message[A]] { /** Removes all elements from the set. After this operation is completed, * the set will be empty. */ - def clear: Unit + def clear(): Unit = elements foreach -= /** Send a message to this scriptable object. * diff --git a/src/library/scala/collection/mutable/SetProxy.scala b/src/library/scala/collection/mutable/SetProxy.scala index 739f2398c3..b13132c682 100644 --- a/src/library/scala/collection/mutable/SetProxy.scala +++ b/src/library/scala/collection/mutable/SetProxy.scala @@ -44,7 +44,7 @@ trait SetProxy[A] extends Set[A] with collection.SetProxy[A] { override def intersect(that: Set[A]): Unit = self.intersect(that) - def clear: Unit = self.clear + override def clear: Unit = self.clear override def retain(p: A => Boolean): Unit = self.retain(p) diff --git a/src/library/scala/xml/Atom.scala b/src/library/scala/xml/Atom.scala index 7f45458101..deea2c00e8 100644 --- a/src/library/scala/xml/Atom.scala +++ b/src/library/scala/xml/Atom.scala @@ -22,7 +22,7 @@ import compat.StringBuilder [serializable] class Atom[+A](val data: A) extends SpecialNode { - data match { + data.asInstanceOf[AnyRef] match { case null => new IllegalArgumentException("cannot construct Atom(null)") case _ => } |