diff options
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/HashMap.scala | 5 | ||||
-rw-r--r-- | sources/scala/HashSet.scala | 7 | ||||
-rw-r--r-- | sources/scala/HashTable.scala | 7 | ||||
-rw-r--r-- | sources/scala/ListMap.scala | 2 | ||||
-rw-r--r-- | sources/scala/ListSet.scala | 2 | ||||
-rw-r--r-- | sources/scala/Map.scala | 15 | ||||
-rw-r--r-- | sources/scala/MultiMap.scala | 4 | ||||
-rw-r--r-- | sources/scala/MutableMap.scala | 13 | ||||
-rw-r--r-- | sources/scala/PartialFunction.scala | 11 | ||||
-rw-r--r-- | sources/scala/Publisher.scala | 2 | ||||
-rw-r--r-- | sources/scala/Set.scala | 33 | ||||
-rw-r--r-- | sources/scala/SynchronizedSet.scala | 5 |
12 files changed, 42 insertions, 64 deletions
diff --git a/sources/scala/HashMap.scala b/sources/scala/HashMap.scala index f9b2a07fac..fd5ba85580 100644 --- a/sources/scala/HashMap.scala +++ b/sources/scala/HashMap.scala @@ -16,4 +16,9 @@ class HashMap[A, B] extends MutableMap[A, B] with DefaultMapModel[A, B] { protected def entryKey(e: Entry) = e.key; + + override def clear = { + initTable(table.length - 1); + tableSize = 0; + } } diff --git a/sources/scala/HashSet.scala b/sources/scala/HashSet.scala index 7674a4f5a8..55d01dc54a 100644 --- a/sources/scala/HashSet.scala +++ b/sources/scala/HashSet.scala @@ -11,7 +11,7 @@ package scala; /** I promise, there will be some documentation soon! :-) Matthias */ -class HashSet[A] extends Set[A] with HashTable[A] { +class HashSet[A] extends MutableSet[A] with HashTable[A] { def contains(elem: A): Boolean = findEntry(elem) match { case None => false @@ -25,6 +25,11 @@ class HashSet[A] extends Set[A] with HashTable[A] { def elements = entries; + override def clear = { + initTable(table.length - 1); + tableSize = 0; + } + protected type Entry = A; protected def entryKey(e: Entry) = e; diff --git a/sources/scala/HashTable.scala b/sources/scala/HashTable.scala index 2e0171f8b2..caae9f2f98 100644 --- a/sources/scala/HashTable.scala +++ b/sources/scala/HashTable.scala @@ -65,11 +65,6 @@ abstract class HashTable[A] { } } - def clear = { - initTable(table.length - 1); - tableSize = 0; - } - protected type Entry; protected def entryKey(e: Entry): A; @@ -95,7 +90,7 @@ abstract class HashTable[A] { private def entryFor(key: A) = (e: Entry => elemEquals(entryKey(e), key)); - private def initTable(n: Int): Unit = { + protected def initTable(n: Int): Unit = { table(n) = Nil; if (n > 0) initTable(n - 1); } diff --git a/sources/scala/ListMap.scala b/sources/scala/ListMap.scala index 5c6b5ed5bc..eac7ca8478 100644 --- a/sources/scala/ListMap.scala +++ b/sources/scala/ListMap.scala @@ -16,7 +16,7 @@ class ListMap[A, B] extends MutableMap[A, B] with DefaultMapModel[A, B] { def size: Int = xs.length; - def clear: Unit = { xs = Nil; } + override def clear: Unit = { xs = Nil; } protected def findEntry(key: A) = xs find {e => e.key == key}; diff --git a/sources/scala/ListSet.scala b/sources/scala/ListSet.scala index 0ee60b1943..e56e44bb67 100644 --- a/sources/scala/ListSet.scala +++ b/sources/scala/ListSet.scala @@ -11,7 +11,7 @@ package scala; /** I promise, there will be some documentation soon! :-) Matthias */ -class ListSet[A] extends Set[A] { +class ListSet[A] extends MutableSet[A] { protected var elems: List[A] = Nil; def size: Int = elems.length; diff --git a/sources/scala/Map.scala b/sources/scala/Map.scala index 3441d6e99e..049f4a6998 100644 --- a/sources/scala/Map.scala +++ b/sources/scala/Map.scala @@ -11,12 +11,13 @@ package scala; /** I promise, there will be some documentation soon! :-) Matthias */ -trait Map[A, +B] with Function1[A, B] - with PartialFunction[A, B] +trait Map[A, +B] with PartialFunction[A, B] with Iterable[Pair[A, B]] { def size: Int; + def get(key: A): Option[B]; + def isEmpty: Boolean = (size == 0); def apply(key: A): B = get(key) match { @@ -24,10 +25,6 @@ trait Map[A, +B] with Function1[A, B] case Some(value) => value } - def get(key: A): Option[B]; - - def remove(key: A): Unit; - def contains(key: A): Boolean = get(key) match { case None => false case Some(_) => true @@ -35,8 +32,6 @@ trait Map[A, +B] with Function1[A, B] def isDefinedAt(key: A) = contains(key); - def clear: Unit; - def keys: Iterator[A] = new Iterator[A] { val iter = elements; def hasNext = iter.hasNext; @@ -57,10 +52,6 @@ trait Map[A, +B] with Function1[A, B] } } - def filter(p: (A, B) => Boolean): Unit = toList foreach { - case Pair(key, value) => if (p(key, value)) { val old = remove(key); } - } - def toList: List[Pair[A, B]] = { var res: List[Pair[A, B]] = Nil; val iter = elements; diff --git a/sources/scala/MultiMap.scala b/sources/scala/MultiMap.scala index 17425036b7..7bc6cc2518 100644 --- a/sources/scala/MultiMap.scala +++ b/sources/scala/MultiMap.scala @@ -10,8 +10,8 @@ package scala; -trait MultiMap[A, B] extends MutableMap[A, Set[B]] { - protected def makeSet: Set[B] = new HashSet[B]; +trait MultiMap[A, B] extends MutableMap[A, MutableSet[B]] { + protected def makeSet: MutableSet[B] = new HashSet[B]; def add(key: A, value: B): Unit = get(key) match { case None => val set = makeSet; diff --git a/sources/scala/MutableMap.scala b/sources/scala/MutableMap.scala index 1d7c3d6931..72e837f33c 100644 --- a/sources/scala/MutableMap.scala +++ b/sources/scala/MutableMap.scala @@ -15,6 +15,15 @@ trait MutableMap[A, B] with Map[A, B] { def update(key: A, value: B): Unit; + def remove(key: A): Unit; + + def clear: Unit = { + val iter = keys; + while (iter.hasNext) { + remove(iter.next); + } + } + def put(key: A, value: B): B = { val old = apply(key); update(key, value); @@ -34,6 +43,10 @@ trait MutableMap[A, B] with Map[A, B] { case Pair(key, value) => update(key, f(key, value)); } + def filter(p: (A, B) => Boolean): Unit = toList foreach { + case Pair(key, value) => if (p(key, value)) remove(key); + } + override def toString() = if (size == 0) "{}" diff --git a/sources/scala/PartialFunction.scala b/sources/scala/PartialFunction.scala index 3a5ece7f19..3959a5c3f5 100644 --- a/sources/scala/PartialFunction.scala +++ b/sources/scala/PartialFunction.scala @@ -4,13 +4,12 @@ ** __\ \/ /__/ __ |/ /__/ __ | ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** +** $Id$ \* */ -// $Id$ +package scala; -package scala { - trait PartialFunction[-A, +B] { - def apply(x: A): B; - def isDefinedAt(x: A): scala.Boolean; - } + +trait PartialFunction[-A, +B] with Function1[A, B] { + def isDefinedAt(x: A): Boolean; } diff --git a/sources/scala/Publisher.scala b/sources/scala/Publisher.scala index 212d64e957..5d6df1f140 100644 --- a/sources/scala/Publisher.scala +++ b/sources/scala/Publisher.scala @@ -14,7 +14,7 @@ package scala; * to all registered subscribers. */ class Publisher[A, This <: Publisher[A, This]]: This { - private val filters = new HashMap[Subscriber[A, This], Set[A => Boolean]] + private val filters = new HashMap[Subscriber[A, This], MutableSet[A => Boolean]] with MultiMap[Subscriber[A, This], A => Boolean]; private val suspended = new HashSet[Subscriber[A, This]]; diff --git a/sources/scala/Set.scala b/sources/scala/Set.scala index 5da0dddd08..3f9912d984 100644 --- a/sources/scala/Set.scala +++ b/sources/scala/Set.scala @@ -9,8 +9,7 @@ package scala; -/** I promise, there will be some documentation soon! :-) Matthias - */ + trait Set[A] with Iterable[A] { def size: Int; @@ -19,30 +18,6 @@ trait Set[A] with Iterable[A] { def contains(elem: A): Boolean; - def add(elem: A): Unit; - - def addAll(elems: A*): Unit = { - val ys = elems as List[A]; - ys foreach { y => add(y); }; - } - - def addSet(that: Iterable[A]): Unit = - that.elements.foreach(elem => add(elem)); - - def remove(elem: A): Unit; - - def removeAll(elems: A*): Unit = { - val ys = elems as List[A]; - ys foreach { y => remove(y); }; - } - - def removeSet(that: Iterable[A]): Unit = - that.elements.foreach(elem => remove(elem)); - - def intersect(that: Set[A]): Unit = filter(that.contains); - - def clear: Unit; - def subsetOf(that: Set[A]): Boolean = { val iter = elements; var res = true; @@ -52,17 +27,13 @@ trait Set[A] with Iterable[A] { res } - def foreach(f: A => Unit) = { + def foreach(f: A => Unit): Unit = { val iter = elements; while (iter.hasNext) { f(iter.next); } } - def filter(p: A => Boolean): Unit = toList foreach { - elem => if (p(elem)) remove(elem); - } - def exists(p: A => Boolean): Boolean = { val iter = elements; var res = false; diff --git a/sources/scala/SynchronizedSet.scala b/sources/scala/SynchronizedSet.scala index 8886ae18ed..1e0600afa9 100644 --- a/sources/scala/SynchronizedSet.scala +++ b/sources/scala/SynchronizedSet.scala @@ -9,9 +9,8 @@ package scala; -/** I promise, there will be some documentation soon! :-) Matthias - */ -trait SynchronizedSet[A] extends Set[A] with Monitor { + +trait SynchronizedSet[A] extends MutableSet[A] with Monitor { override def size: Int = synchronized { super.size |