From 2ef5d4c6d81e5008fdb2ae1b4f2ecdd9d9901fd2 Mon Sep 17 00:00:00 2001 From: Sean McDirmid Date: Thu, 19 Apr 2007 05:16:02 +0000 Subject: Switching over to position objects from positio... Switching over to position objects from position type parameters. Positions are no longer ints. --- src/library/scala/Iterable.scala | 77 ++++++++++++---------- src/library/scala/collection/Map.scala | 13 ++++ src/library/scala/collection/jcl/Buffer.scala | 7 +- src/library/scala/collection/jcl/Collection.scala | 19 +++--- src/library/scala/collection/jcl/Map.scala | 31 ++++++--- .../scala/collection/jcl/MutableIterable.scala | 10 ++- src/library/scala/collection/jcl/MutableSeq.scala | 9 +-- src/library/scala/collection/jcl/Set.scala | 17 ++--- src/library/scala/collection/jcl/SortedMap.scala | 37 +++++++---- src/library/scala/collection/jcl/SortedSet.scala | 10 ++- src/library/scala/collection/jcl/Tests.scala | 10 +-- 11 files changed, 152 insertions(+), 88 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/Iterable.scala b/src/library/scala/Iterable.scala index 2f09f4e136..934afd685f 100644 --- a/src/library/scala/Iterable.scala +++ b/src/library/scala/Iterable.scala @@ -426,42 +426,53 @@ trait Iterable[+A] { /** Is this collection empty? */ def isEmpty = elements.hasNext - private class Projection[+B](elements0 : () => Iterator[B]) extends Iterable[B] { + private class ProjectionImpl[+B](elements0 : () => Iterator[B]) extends Iterable[B] { def elements = elements0(); } - /** Returns all the elements of this iterable that satisfy the - * predicate p. The order of the elements is preserved. - * Unlike filter, this API is not strict - * and will terminate on infinite-sized collections. - * - * @param p the predicate used to filter the list. - * @return the elements of this list satisfying p. - */ - def pfilter(p: A => Boolean) : Iterable[A] = new Projection[A](() => { - Iterable.this.elements.filter(p) - }) - /** Returns the iterable resulting from applying the given function - * f to each element of this iterable. Unlike map, - * this API is not strict and will terminate on infinite-sized collections. - * - * @param f function to apply to each element. - * @return f(a0), ..., f(an) - * if this iterable is a0, ..., an. - */ - def pmap[B](f: A => B) : Iterable[B] = new Projection[B](() => { - Iterable.this.elements.map(f) - }) - /** Applies the given function f to each element of - * this iterable, then concatenates the results. Unlike flatMap, - * this API is not strict and will terminate on infinite-sized collections. - * - * @param f the function to apply on each element. - * @return f(a0) ::: ... ::: f(an) if - * this iterable is a0, ..., an. + + trait Projection { + /** Returns all the elements of this iterable that satisfy the + * predicate p. The order of the elements is preserved. + * Unlike filter in Iterable, this API is + * not strict and will terminate on infinite-sized collections. + * + * @param p the predicate used to filter the list. + * @return the elements of this list satisfying p. + */ + def filter(p : A => Boolean) : Iterable[A] = new ProjectionImpl[A](() => { + Iterable.this.elements.filter(p) + }) + /** Returns the iterable resulting from applying the given function + * f to each element of this iterable. Unlike map + * in Iterable, this API is not strict and will terminate on + * infinite-sized collections. + * + * @param f function to apply to each element. + * @return f(a0), ..., f(an) + * if this iterable is a0, ..., an. + */ + def map[B](f: A => B) : Iterable[B] = new ProjectionImpl[B](() => { + Iterable.this.elements.map(f) + }) + /** Applies the given function f to each element of + * this iterable, then concatenates the results. Unlike flatMap + * in Iterable, + * this API is not strict and will terminate on infinite-sized collections. + * + * @param f the function to apply on each element. + * @return f(a0) ::: ... ::: f(an) if + * this iterable is a0, ..., an. + */ + def flatMap[B](f: A => Iterable[B]) : Iterable[B] = new ProjectionImpl[B](() => { + Iterable.this.elements.flatMap(a => f(a).elements) + }) + } + /** + * returns a facade that can be used to call non-strict filter, + * map, and flatMap methods that build projections + * of the collection. */ - def pflatMap[B](f: A => Iterable[B]) : Iterable[B] = new Projection[B](() => { - Iterable.this.elements.flatMap(a => f(a).elements) - }) + def projection : Projection = new Projection {}; /** returns true iff this collection has a bound size. * Only true if this iterable is a Collection. diff --git a/src/library/scala/collection/Map.scala b/src/library/scala/collection/Map.scala index 30ce8a1ab9..6047513b20 100644 --- a/src/library/scala/collection/Map.scala +++ b/src/library/scala/collection/Map.scala @@ -163,4 +163,17 @@ trait Map[A, +B] extends PartialFunction[A, B] with Collection[(A, B)] { */ def default(key: A): B = throw new NoSuchElementException("key not found: " + key) + + trait Projection extends super.Projection { + /** filter based on keys only */ + def filterKeys(p : A => Boolean) = filter(e => p(e._1)) + /** map elements using existing key set */ + def mapElements[C](f : B => C) : Map[A,C] = new Map[A,C] { + def elements = Map.this.elements.map(e => (e._1, f(e._2))) + def size = Map.this.size + override def contains(key : A) = Map.this.contains(key) + override def get(key : A) = Map.this.get(key).map(f) + } + } + override def projection : Projection = new Projection {} } diff --git a/src/library/scala/collection/jcl/Buffer.scala b/src/library/scala/collection/jcl/Buffer.scala index 28aa87dc98..15033867e9 100644 --- a/src/library/scala/collection/jcl/Buffer.scala +++ b/src/library/scala/collection/jcl/Buffer.scala @@ -17,6 +17,12 @@ package scala.collection.jcl; trait Buffer[A] extends MutableSeq[A] with Collection[A] with Ranged[Int,A] { final protected type SortedSelf = Buffer[A]; + trait MutableSeqProjection extends super[MutableSeq].Projection; + trait Projection extends MutableSeqProjection with super[Collection].Projection { + override def filter(p : A => Boolean) = super[MutableSeqProjection].filter(p); + } + override def projection = new Projection {} + override def elements : BufferIterator[Int,A]; /** The first index of a buffer is 0. */ override def first = 0; @@ -83,7 +89,6 @@ trait Buffer[A] extends MutableSeq[A] with Collection[A] with Ranged[Int,A] { override def +(a : A) : this.type = super[Collection].+(a); override def -=(a : A) = super[Collection].-=(a); override def isEmpty = super[MutableSeq].isEmpty; - override def pfilter(p : A => Boolean) : MutableSeq[A] = super[MutableSeq].pfilter(p); override def rangeImpl(from : Option[Int], until : Option[Int]) : Buffer[A] = new Range(from, until); diff --git a/src/library/scala/collection/jcl/Collection.scala b/src/library/scala/collection/jcl/Collection.scala index e9344c0ef8..74ed7345af 100644 --- a/src/library/scala/collection/jcl/Collection.scala +++ b/src/library/scala/collection/jcl/Collection.scala @@ -56,13 +56,11 @@ trait Collection[A] extends MutableIterable[A] { */ def transform(f: A => A): Boolean; - /** Used for non-strict filtered collection that only includes elements of this collection that are true for "p." - ** Any elements added to or removed from the resulting - ** filter will update this collection. Any changes to this collection - ** can update the filtered collection. - ** @return a non-strict filter of this collection. - **/ - override def pfilter(p : A => Boolean) : MutableIterable[A] = new Filter(p); + trait Projection extends super.Projection { + override def filter(p : A => Boolean) : MutableIterable[A] = new Filter(p); + } + override def projection : Projection = new Projection {} + /** Base implementation of a filtered collection */ class Filter(p : A => Boolean) extends Collection[A] { @@ -77,8 +75,11 @@ trait Collection[A] extends MutableIterable[A] { if (!p(a)) throw new IllegalArgumentException; Collection.this.remove(a); } - override def pfilter(p1 : A => Boolean) : MutableIterable[A] = - Collection.this.pfilter(a => p(a) && p1(a)); + class Projection extends super.Projection { + override def filter(p0 : A => Boolean) : MutableIterable[A] = + Collection.this.projection.filter(a => p(a) && p0(a)); + } + override def projection : Projection = new Projection; def elements = Collection.this.elements.filter(p); def size = size0; } diff --git a/src/library/scala/collection/jcl/Map.scala b/src/library/scala/collection/jcl/Map.scala index 5c66986626..34b5d92918 100644 --- a/src/library/scala/collection/jcl/Map.scala +++ b/src/library/scala/collection/jcl/Map.scala @@ -22,7 +22,7 @@ trait Map[K,E] extends MutableIterable[Tuple2[K,E]] with scala.collection.mutabl /** The values of this map as a projection, which means removals from the returned collection will remove the element from this map. @returns a projection of this map's elements. */ - def valueSet : MutableIterable[E] = pmap(._2); + def valueSet : MutableIterable[E] = projection.map(._2); def put(key : K, elem : E) : Option[E]; def putAll(that : Iterable[Tuple2[K,E]]) : Unit = that.foreach(p => put(p._1, p._2)); @@ -52,10 +52,14 @@ trait Map[K,E] extends MutableIterable[Tuple2[K,E]] with scala.collection.mutabl } override def -=(key : K) : Unit = remove(key); override def elements : MutableIterator[Tuple2[K,E]]; - /** Produces a filtered projection of this map that includes only entries of the map - * whose keys are true with respect to predicate "p." - */ - def pfilterKeys(p : K => Boolean) : jcl.Map[K,E] = new Filter(p); + + trait MutableIterableProjection extends super[MutableIterable].Projection; + trait Projection extends MutableIterableProjection with super[Map].Projection { + override def filterKeys(p : K => Boolean) : jcl.Map[K,E] = new Filter(p); + override def map[B](f : ((K,E)) => B) : MutableIterable[B] = super[MutableIterableProjection].map(f); + } + override def projection : Projection = new Projection {} + /** */ def lense[F](f : E => F, g : F => E) : jcl.Map[K,F] = new Lense[F](f,g); @@ -65,14 +69,19 @@ trait Map[K,E] extends MutableIterable[Tuple2[K,E]] with scala.collection.mutabl override def remove(key : K) = Map.this.remove(key).map(f); override def put(key : K, elem : F) = Map.this.put(key, g(elem)).map(f); override def get(key : K) = Map.this.get(key).map(f); - override def pfilterKeys(p : K => Boolean) : jcl.Map[K,F] = - Map.this.pfilterKeys(p).lense(f, g); + + trait Projection extends super.Projection { + override def filterKeys(p : K => Boolean) : jcl.Map[K,F] = + Map.this.projection.filterKeys(p).lense(f, g); + } + override def projection = new Projection {} + override def lense[G](f0 : F => G, g0 : G => F) : jcl.Map[K,G] = Map.this.lense[G](x => f0(f(x)), y => g(g0(y))); override def size = size0; } protected class Filter(p : K => Boolean) extends jcl.Map[K,E] { - override def elements = Map.this.elements.filter(k => p(k._1)); + override def elements = Map.this.elements.filter(e => p(e._1)); override def remove(key : K) = { if (!p(key)) throw new IllegalArgumentException; Map.this.remove(key); @@ -86,8 +95,10 @@ trait Map[K,E] extends MutableIterable[Tuple2[K,E]] with scala.collection.mutabl if (!p(key)) None; else Map.this.get(key); } - override def pfilterKeys(p0 : K => Boolean) : jcl.Map[K,E] = - Map.this.pfilterKeys(k => p(k) && p0(k)); + class Projection extends super.Projection { + override def filterKeys(p0 : K => Boolean) : jcl.Map[K,E] = + Map.this.projection.filterKeys(e => p(e) && p0(e)); + } override def size = size0; } protected class KeySet extends Set[K] { diff --git a/src/library/scala/collection/jcl/MutableIterable.scala b/src/library/scala/collection/jcl/MutableIterable.scala index fef43d9ea1..411a7ffc01 100644 --- a/src/library/scala/collection/jcl/MutableIterable.scala +++ b/src/library/scala/collection/jcl/MutableIterable.scala @@ -63,12 +63,10 @@ trait MutableIterable[A] extends scala.Collection[A] { i.next; i.remove; } } - /** Creates a non-strict map of this collection. Any removals from the returned - * collection will remove from this collection, while any changes to this collection will also be - * reflected in the mapped collection. - * @return a non-strict map of this collection. - */ - override def pmap[B](f : A => B) : MutableIterable[B] = new Map[B](f); + trait Projection extends super.Projection { + override def map[B](f : A => B) : MutableIterable[B] = new Map[B](f); + } + override def projection = new Projection {} /** The default implementation of a map over mutable iterable collections. **/ protected class Map[B](f : A => B) extends MutableIterable[B] { diff --git a/src/library/scala/collection/jcl/MutableSeq.scala b/src/library/scala/collection/jcl/MutableSeq.scala index 2d3147ca6d..95325b2e22 100644 --- a/src/library/scala/collection/jcl/MutableSeq.scala +++ b/src/library/scala/collection/jcl/MutableSeq.scala @@ -20,10 +20,11 @@ trait MutableSeq[A] extends MutableIterable[A] with Seq[A] { override def isEmpty = super[MutableIterable].isEmpty; override def apply(idx : Int) = elements.seek(idx); - - override def pfilter(p : A => Boolean) : MutableSeq[A] = new Filter(p); - - override def pmap[B](f : A => B) : MutableSeq[B] = new Map[B](f); + trait Projection extends super.Projection { + override def filter(p : A => Boolean) : MutableSeq[A] = new Filter(p); + override def map[B](f : A => B) : MutableSeq[B] = new Map[B](f); + } + override def projection = new Projection {} /** Find the index of "a" in this sequence. * @returns None if the "a" is not in this sequence. diff --git a/src/library/scala/collection/jcl/Set.scala b/src/library/scala/collection/jcl/Set.scala index 975b76a167..c42faf4c31 100644 --- a/src/library/scala/collection/jcl/Set.scala +++ b/src/library/scala/collection/jcl/Set.scala @@ -44,13 +44,14 @@ trait Set[A] extends Collection[A] with scala.collection.mutable.Set[A] { } addAll(toAdd) } - - override def pfilter(p: A => Boolean): Set[A] = new Filter(p) - - class Filter(p: A => Boolean) extends super.Filter(p) with Set[A] { - override def filter(p: A => Boolean): scala.collection.mutable.Set[A] = { - super[Set].retain(p) - this - } + trait Projection extends super.Projection { + override def filter(p : A => Boolean) : Set[A] = new Filter(p); + } + override def projection : Projection = new Projection {} + class Filter(p : A => Boolean) extends super.Filter(p) with Set[A] { + override def retain(p0 : A => Boolean): Unit = + Set.this.retain(e => !p(e) || p0(e)) + trait Projection extends super[Filter].Projection with super[Set].Projection {} + override def projection : Projection = new Projection {} } } diff --git a/src/library/scala/collection/jcl/SortedMap.scala b/src/library/scala/collection/jcl/SortedMap.scala index 1f6dacddd6..3188d4d6bf 100644 --- a/src/library/scala/collection/jcl/SortedMap.scala +++ b/src/library/scala/collection/jcl/SortedMap.scala @@ -26,13 +26,21 @@ trait SortedMap[K,E] extends scala.collection.SortedMap[K,E] with Map[K,E] with } override def rangeImpl(from : Option[K], until : Option[K]) : SortedMap[K,E] = Range(from, until); override def keySet : SortedSet[K] = new KeySet; - override def pfilterKeys(p : K => Boolean) : SortedMap[K,E] = new Filter(p); + + class Projection extends super.Projection { + override def filterKeys(p : K => Boolean) : SortedMap[K,E] = new Filter(p); + } + override def projection = new Projection; + override def lense[F](f : E => F, g : F => E) : jcl.SortedMap[K,F] = new Lense[F](f,g); protected class Lense[F](f : E => F, g : F => E) extends super.Lense[F](f,g) with SortedMap[K,F] { def compare(k0 : K, k1 : K) = SortedMap.this.compare(k0, k1); - override def pfilterKeys(p : K => Boolean) : jcl.SortedMap[K,F] = - SortedMap.this.pfilterKeys(p).lense(f, g); + class Projection extends super.Projection { + override def filterKeys(p : K => Boolean) : SortedMap[K,F] = + SortedMap.this.projection.filterKeys(p).lense(f,g); + } + override def projection = new Projection; override def lense[G](f0 : F => G, g0 : G => F) : jcl.SortedMap[K,G] = SortedMap.this.lense[G]({x:E => f0(f(x))}, {y:G => g(g0(y))}); override def rangeImpl(from : Option[K], until : Option[K]) = @@ -48,11 +56,13 @@ trait SortedMap[K,E] extends scala.collection.SortedMap[K,E] with Map[K,E] with protected class SuperFilter(p : K => Boolean) extends super.Filter(p); protected class Filter(p : K => Boolean) extends SuperFilter(p) with SortedMap[K,E] { def compare(k0 : K, k1 : K) = SortedMap.this.compare(k0,k1); - override def pfilterKeys(p0 : K => Boolean) : SortedMap[K,E] = - SortedMap.this.pfilterKeys(k => p(k) && p0(k)); - + class Projection extends super.Projection { + override def filterKeys(p0 : K => Boolean) : SortedMap[K,E] = + SortedMap.this.projection.filterKeys(k => p(k) && p0(k)); + } + override def projection = new Projection; override def rangeImpl(from : Option[K], until : Option[K]) : SortedMap[K,E] = - SortedMap.this.Range(from, until).pfilterKeys(p); + SortedMap.this.Range(from, until).projection.filterKeys(p); } protected def Range(from : Option[K], until : Option[K]) : SortedMap[K,E] = new Range(from,until); @@ -78,13 +88,18 @@ trait SortedMap[K,E] extends scala.collection.SortedMap[K,E] with Map[K,E] with if (until != None && compare(this.until.get, until.get) < 0) return rangeImpl(from, this.until); SortedMap.this.Range(from, until); } - override def pfilterKeys(p : K => Boolean) : SortedMap[K,E] = new Filter(p); + class Projection extends super.Projection { + override def filterKeys(p : K => Boolean) : SortedMap[K,E] = new Filter(p); + } + override def projection = new Projection; protected class Filter(p : K => Boolean) extends SuperFilter(p) with SortedMap[K,E] { def compare(k0 : K, k1 : K) = Range.this.compare(k0, k1); - override def pfilterKeys(p0 : K => Boolean) = - Range.this.pfilterKeys(key => p(key) && p0(key)); + class Projection extends super.Projection { + override def filterKeys(p0 : K => Boolean) = Range.this.projection.filterKeys(k => p(k) && p0(k)); + } + override def projection = new Projection; override def rangeImpl(from : Option[K], until : Option[K]) : SortedMap[K,E] = - Range.this.rangeImpl(from,until).pfilterKeys(p); + Range.this.rangeImpl(from,until).projection.filterKeys(p); } } } diff --git a/src/library/scala/collection/jcl/SortedSet.scala b/src/library/scala/collection/jcl/SortedSet.scala index bc381edb83..0223c04a70 100644 --- a/src/library/scala/collection/jcl/SortedSet.scala +++ b/src/library/scala/collection/jcl/SortedSet.scala @@ -34,9 +34,17 @@ trait SortedSet[A] extends scala.collection.SortedSet[A] with jcl.Set[A] with So else last; } override def rangeImpl(from : Option[A], until : Option[A]) : SortedSet[A] = new Range(from, until); - override def pfilter(p : A => Boolean) : SortedSet[A] = new Filter(p); + trait Projection extends super.Projection { + override def filter(p : A => Boolean) : SortedSet[A] = new Filter(p); + } + override def projection : Projection = new Projection {} + protected class Filter(p : A => Boolean) extends super.Filter(p) with SortedSet[A] { def compare(a0 : A, a1 : A) : Int = SortedSet.this.compare(a0, a1); + trait Projection extends super[Filter].Projection with super[SortedSet].Projection { + override def filter(p0 : A => Boolean) = SortedSet.this.projection.filter(k => p(k) && p0(k)); + } + override def projection : Projection = new Projection {}; } protected class Range(from : Option[A], until : Option[A]) extends Filter(key => { diff --git a/src/library/scala/collection/jcl/Tests.scala b/src/library/scala/collection/jcl/Tests.scala index b81a6f1984..3fab33be2f 100644 --- a/src/library/scala/collection/jcl/Tests.scala +++ b/src/library/scala/collection/jcl/Tests.scala @@ -29,11 +29,11 @@ private[jcl] object Tests { rset + "bad"; Console.println(rset); Console.println(set); - val fset : SortedSet[String] = rset.pfilter(.endsWith("d")); + val fset : SortedSet[String] = rset.projection.filter(.endsWith("d")); Console.println(fset); fset += "cd"; Console.println(set); - set.pmap(.length).retain(x => x == 3); + set.projection.map(.length).retain(x => x == 3); Console.println(set); Console.println(rset); Console.println(fset); @@ -47,7 +47,7 @@ private[jcl] object Tests { rmap + ("bad" -> 10); Console.println(rmap); //Console.println(map); - val fmap : SortedMap[String,Integer] = rmap.pfilterKeys(k => k.length == 2); + val fmap : SortedMap[String,Integer] = rmap.projection.filterKeys(k => k.length == 2); Console.println(fmap); } @@ -55,7 +55,7 @@ private[jcl] object Tests { val set = new HashSet[String]; set + "hello" + "world" + "you" + "to"; Console.println(set); - val fset = set.pfilter(s => s.length <= 3); + val fset = set.projection.filter(s => s.length <= 3); Console.println(fset); fset += "xxx"; Console.println(set); @@ -66,7 +66,7 @@ private[jcl] object Tests { case e : IllegalArgumentException => case _ => throw new Error; } - val mset : MutableIterable[Int] = set.pmap(s => s.length); + val mset : MutableIterable[Int] = set.projection.map(s => s.length); Console.println(mset); mset.retain(n => n < 5); Console.println(set); -- cgit v1.2.3