From 4d842529db519f2302ea1963f4fa07a47abb0abb Mon Sep 17 00:00:00 2001 From: extempore Date: Mon, 31 Aug 2009 03:02:27 +0000 Subject: [PATCH] Further work creating pure interfaces for the collections so we can validate their contents. Also fixed an overly loose upper bound in SequenceTemplate. git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@18613 5e8d7ff9-d8ef-0310-90f0-a4852d11357a --- .../collection/generic/SequenceTemplate.scala | 2 +- .../interfaces/IterableMethods.scala | 10 +++- .../collection/interfaces/MapMethods.scala | 41 ++++++++++++++ .../interfaces/SequenceMethods.scala | 4 +- .../collection/interfaces/SetMethods.scala | 53 +++++++++++++++++++ .../interfaces/TraversableMethods.scala | 4 +- 6 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 src/library/scala/collection/interfaces/MapMethods.scala create mode 100644 src/library/scala/collection/interfaces/SetMethods.scala diff --git a/src/library/scala/collection/generic/SequenceTemplate.scala b/src/library/scala/collection/generic/SequenceTemplate.scala index 7f087c76e..1d2df49ec 100644 --- a/src/library/scala/collection/generic/SequenceTemplate.scala +++ b/src/library/scala/collection/generic/SequenceTemplate.scala @@ -29,7 +29,7 @@ import generic._ * @author Matthias Zenger * @version 1.0, 16/07/2003 */ -trait SequenceTemplate[+A, +This <: IterableTemplate[A, This] with Sequence[A]] extends IterableTemplate[A, This] { self => +trait SequenceTemplate[+A, +This <: SequenceTemplate[A, This] with Sequence[A]] extends IterableTemplate[A, This] { self => import Traversable.breaks._ diff --git a/src/library/scala/collection/interfaces/IterableMethods.scala b/src/library/scala/collection/interfaces/IterableMethods.scala index dcad0c16f..f7959e24a 100644 --- a/src/library/scala/collection/interfaces/IterableMethods.scala +++ b/src/library/scala/collection/interfaces/IterableMethods.scala @@ -12,13 +12,21 @@ import scala.collection._ import generic._ import mutable.Buffer import scala.reflect.ClassManifest +import annotation.unchecked.uncheckedVariance trait IterableMethods[+A, +This <: IterableTemplate[A, This] with Iterable[A]] extends TraversableMethods[A, This] { - def dropRight(n: Int): Iterable[A] + // abstract def iterator: Iterator[A] + + // concrete + def dropRight(n: Int): Iterable[A] def sameElements[B >: A](that: Iterable[B]): Boolean + def sortWith(lt: (A, A) => Boolean)(implicit m: ClassManifest[A @uncheckedVariance]): This def takeRight(n: Int): Iterable[A] + def zipAll[B, A1 >: A, That](that: Iterable[B], e1: A1, e2: B)(implicit bf: BuilderFactory[(A1, B), That, This]): That + def zipWithIndex[A1 >: A, That](implicit bf: BuilderFactory[(A1, Int), That, This]): That + def zip[A1 >: A, B, That](that: Iterable[B])(implicit bf: BuilderFactory[(A1, B), That, This]): That override def view: IterableView[A, This] override def view(from: Int, until: Int): IterableView[A, This] diff --git a/src/library/scala/collection/interfaces/MapMethods.scala b/src/library/scala/collection/interfaces/MapMethods.scala new file mode 100644 index 000000000..ae1388ac6 --- /dev/null +++ b/src/library/scala/collection/interfaces/MapMethods.scala @@ -0,0 +1,41 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection.interfaces + +import scala.collection._ +import generic._ + +trait MapMethods[A, +B, +This <: MapTemplate[A, B, This] with Map[A, B]] +extends IterableMethods[(A, B), This] +with SubtractableMethods[A, This] +{ + // abstract + def empty: This + def get(key: A): Option[B] + def iterator: Iterator[(A, B)] + def + [B1 >: B] (kv: (A, B1)): Map[A, B1] + def - (key: A): This + + // concrete + def getOrElse[B1 >: B](key: A, default: => B1): B1 + def apply(key: A): B + def contains(key: A): Boolean + def isDefinedAt(key: A): Boolean + def keySet: Set[A] + def keysIterator: Iterator[A] + def valueIterable: Iterable[B] + def valuesIterator: Iterator[B] + def default(key: A): B + def filterKeys(p: A => Boolean): DefaultMap[A, B] + def mapValues[C](f: B => C): DefaultMap[A, C] + def updated [B1 >: B](key: A, value: B1): Map[A, B1] + def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): Map[A, B1] + def ++[B1 >: B](elems: Traversable[(A, B1)]): Map[A, B1] + def ++[B1 >: B] (iter: Iterator[(A, B1)]): Map[A, B1] +} diff --git a/src/library/scala/collection/interfaces/SequenceMethods.scala b/src/library/scala/collection/interfaces/SequenceMethods.scala index 04dbbe966..3aef613a9 100644 --- a/src/library/scala/collection/interfaces/SequenceMethods.scala +++ b/src/library/scala/collection/interfaces/SequenceMethods.scala @@ -15,7 +15,10 @@ import scala.reflect.ClassManifest trait SequenceMethods[+A, +This <: SequenceTemplate[A, This] with Sequence[A]] extends IterableMethods[A, This] { + // abstract def apply(idx: Int): A + def length: Int + def contains(elem: Any): Boolean def diff[B >: A, That](that: Sequence[B]): This def endsWith[B](that: Sequence[B]): Boolean @@ -34,7 +37,6 @@ trait SequenceMethods[+A, +This <: SequenceTemplate[A, This] with Sequence[A]] e def lastIndexOf[B >: A](elem: B, end: Int): Int def lastIndexWhere(p: A => Boolean): Int def lastIndexWhere(p: A => Boolean, end: Int): Int - def length: Int def lengthCompare(len: Int): Int def padTo[B >: A, That](len: Int, elem: B)(implicit bf: BuilderFactory[B, That, This]): That def patch[B >: A, That](from: Int, patch: Sequence[B], replaced: Int)(implicit bf: BuilderFactory[B, That, This]): That diff --git a/src/library/scala/collection/interfaces/SetMethods.scala b/src/library/scala/collection/interfaces/SetMethods.scala new file mode 100644 index 000000000..52d66209b --- /dev/null +++ b/src/library/scala/collection/interfaces/SetMethods.scala @@ -0,0 +1,53 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection.interfaces + +import scala.collection._ +import generic._ +import mutable.Buffer +import scala.reflect.ClassManifest +import annotation.unchecked.uncheckedVariance + +trait AddableMethods[A, +This <: Addable[A, This]] { + protected def thisCollection: This + def +(elem: A): This + def + (elem1: A, elem2: A, elems: A*): This + def ++ (elems: Traversable[A]): This + def ++ (iter: Iterator[A]): This +} + +trait SubtractableMethods[A, +This <: Subtractable[A, This]] { + protected def thisCollection: This + def -(elem: A): This + def -(elem1: A, elem2: A, elems: A*): This + def --(elems: Traversable[A]): This + def --(iter: Iterator[A]): This +} + +trait SetMethods[A, +This <: SetTemplate[A, This] with Set[A]] +extends IterableMethods[A, This] +with AddableMethods[A, This] +with SubtractableMethods[A, This] +{ + // abstract + def empty: This + def contains(elem: A): Boolean + def + (elem: A): This + def - (elem: A): This + + // concrete + def apply(elem: A): Boolean + def intersect(that: Set[A]): This + def &(that: Set[A]): This + def union(that: Set[A]): This + def | (that: Set[A]): This + def diff(that: Set[A]): This + def &~(that: Set[A]): This + def subsetOf(that: Set[A]): Boolean +} diff --git a/src/library/scala/collection/interfaces/TraversableMethods.scala b/src/library/scala/collection/interfaces/TraversableMethods.scala index 9f8cdbccf..939914cc8 100644 --- a/src/library/scala/collection/interfaces/TraversableMethods.scala +++ b/src/library/scala/collection/interfaces/TraversableMethods.scala @@ -15,11 +15,13 @@ import scala.reflect.ClassManifest trait TraversableMethods[+A, +This <: TraversableTemplate[A, This] with Traversable[A]] { + // abstract + def foreach[U](f: A => U): Unit + // maps/iteration def flatMap[B, That](f: A => Traversable[B])(implicit bf: BuilderFactory[B, That, This]): That def map[B, That](f: A => B)(implicit bf: BuilderFactory[B, That, This]): That def filterMap[B, That](pf: PartialFunction[Any, B])(implicit bf: BuilderFactory[B, That, This]): That - def foreach[U](f: A => U): Unit // new collections def ++[B >: A, That](that: Iterator[B])(implicit bf: BuilderFactory[B, That, This]): That