forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out strict implementations of transformation operations.
Add StrictOptimizedMapOps and StrictOptimizedSortedMapOps. Only operations that were implemented more than once have been factored out. Some other operations could be factored out so that they could be reused by custom collection implementations. Fixes scala/collection-strawman#325
- Loading branch information
Showing
20 changed files
with
268 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package scala.collection | ||
|
||
import scala.language.higherKinds | ||
|
||
/** | ||
* Trait that overrides map operations to take advantage of strict builders. | ||
* | ||
* @tparam K Type of keys | ||
* @tparam V Type of values | ||
* @tparam CC Collection type constructor | ||
* @tparam C Collection type | ||
*/ | ||
trait StrictOptimizedMapOps[K, +V, +CC[_, _] <: IterableOps[_, AnyConstr, _], +C] | ||
extends MapOps[K, V, CC, C] | ||
with StrictOptimizedIterableOps[(K, V), Iterable, C] { | ||
|
||
override def map[K2, V2](f: ((K, V)) => (K2, V2)): CC[K2, V2] = | ||
StrictOptimizedOps.map(iterator, mapFactory.newBuilder, f) | ||
|
||
override def flatMap[K2, V2](f: ((K, V)) => IterableOnce[(K2, V2)]): CC[K2, V2] = | ||
StrictOptimizedOps.flatMap(iterator, mapFactory.newBuilder, f) | ||
|
||
override def concat[V2 >: V](suffix: Iterable[(K, V2)]): CC[K, V2] = | ||
StrictOptimizedOps.concat(iterator, suffix.iterator, mapFactory.newBuilder) | ||
|
||
override def collect[K2, V2](pf: PartialFunction[(K, V), (K2, V2)]): CC[K2, V2] = | ||
StrictOptimizedOps.collect(iterator, mapFactory.newBuilder, pf) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package scala.collection | ||
|
||
/** | ||
* Convenient implementations of transformation operations | ||
* based on builders. | ||
* | ||
* All the methods defined here are expected to be called with | ||
* freshly created iterators and builders. | ||
*/ | ||
object StrictOptimizedOps { | ||
|
||
/** | ||
* @param it Iterator to map elements from | ||
* @param b Builder to use to build the resulting collection | ||
* @param f Element transformation function | ||
* @tparam A Type of elements of the source collection (e.g. `Int`) | ||
* @tparam B Type of elements of the resulting collection (e.g. `String`) | ||
* @tparam C Type of the resulting collection (e.g. `List[String]`) | ||
* @return The resulting collection | ||
*/ | ||
@inline def map[A, B, C](it: Iterator[A], b: mutable.Builder[B, C], f: A => B): C = { | ||
while (it.hasNext) { | ||
b += f(it.next()) | ||
} | ||
b.result() | ||
} | ||
|
||
/** | ||
* @param it Iterator to flatMap elements from | ||
* @param b Builder to use to build the resulting collection | ||
* @param f Element transformation function | ||
* @tparam A Type of elements of the source collection (e.g. `Int`) | ||
* @tparam B Type of elements of the resulting collection (e.g. `String`) | ||
* @tparam C Type of the resulting collection (e.g. `List[String]`) | ||
* @return The resulting collection | ||
*/ | ||
@inline def flatMap[A, B, C](it: Iterator[A], b: mutable.Builder[B, C], f: A => IterableOnce[B]): C = { | ||
while (it.hasNext) { | ||
b ++= f(it.next()) | ||
} | ||
b.result() | ||
} | ||
|
||
/** | ||
* @param it1 Iterator of the first collection | ||
* @param it2 Iterator of the second collection | ||
* @param b Builder to use to build the resulting collection | ||
* @tparam A Type of elements (e.g. `Int`) | ||
* @tparam C Type of the resulting collection (e.g. `List[Int]`) | ||
* @return The resulting collection | ||
*/ | ||
@inline def concat[A, C](it1: Iterator[A], it2: Iterator[A], b: mutable.Builder[A, C]): C = { | ||
b ++= it1 | ||
b ++= it2 | ||
b.result() | ||
} | ||
|
||
/** | ||
* @param it Iterator to collect elements from | ||
* @param b Builder to use to build the resulting collection | ||
* @param pf Element transformation partial function | ||
* @tparam A Type of elements of the source collection (e.g. `Int`) | ||
* @tparam B Type of elements of the resulting collection (e.g. `String`) | ||
* @tparam C Type of the resulting collection (e.g. `List[String]`) | ||
* @return The resulting collection | ||
*/ | ||
@inline def collect[A, B, C](it: Iterator[A], b: mutable.Builder[B, C], pf: PartialFunction[A, B]): C = { | ||
while (it.hasNext) { | ||
val elem = it.next() | ||
if (pf.isDefinedAt(elem)) { | ||
b += pf.apply(elem) | ||
} | ||
} | ||
b.result() | ||
} | ||
|
||
/** | ||
* @param it Iterator to flatten elements from | ||
* @param b Builder to use to build the resulting collection | ||
* @param toIterableOnce Evidence that `A` can be seen as an `IterableOnce[B]` | ||
* @tparam A Type of elements of the source collection (e.g. `List[Int]`) | ||
* @tparam B Type of elements of the resulting collection (e.g. `Int`) | ||
* @tparam C Type of the resulting collection (e.g. `List[Int]`) | ||
* @return The resulting collection | ||
*/ | ||
@inline def flatten[A, B, C](it: Iterator[A], b: mutable.Builder[B, C])(implicit toIterableOnce: A => IterableOnce[B]): C = { | ||
while (it.hasNext) { | ||
b ++= toIterableOnce(it.next()) | ||
} | ||
b.result() | ||
} | ||
|
||
/** | ||
* @param it1 Iterator of the first collection | ||
* @param it2 Iterator of the second collection | ||
* @param b Builder to use to build the resulting collection | ||
* @tparam A Type of elements of the first collection (e.g. `Int`) | ||
* @tparam B Type of elements of the second collection (e.g. `String`) | ||
* @tparam C Type of the resulting collection (e.g. `List[(Int, String)]`) | ||
* @return The resulting collection | ||
*/ | ||
@inline def zip[A, B, C](it1: Iterator[A], it2: Iterator[B], b: mutable.Builder[(A, B), C]): C = { | ||
while (it1.hasNext && it2.hasNext) { | ||
b += ((it1.next(), it2.next())) | ||
} | ||
b.result() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package scala.collection | ||
|
||
/** | ||
* Trait that overrides set operations to take advantage of strict builders. | ||
* | ||
* @tparam A Elements type | ||
* @tparam CC Collection type constructor | ||
* @tparam C Collection type | ||
*/ | ||
trait StrictOptimizedSetOps[A, +CC[_], +C <: SetOps[A, CC, C]] | ||
extends SetOps[A, CC, C] | ||
with StrictOptimizedIterableOps[A, CC, C] { | ||
|
||
override def concat(that: Iterable[A]): C = | ||
StrictOptimizedOps.concat(iterator, that.iterator, newSpecificBuilder) | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/library/scala/collection/StrictOptimizedSortedMapOps.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package scala.collection | ||
|
||
import scala.annotation.implicitNotFound | ||
|
||
/** | ||
* Trait that overrides sorted map operations to take advantage of strict builders. | ||
* | ||
* @tparam K Type of keys | ||
* @tparam V Type of values | ||
* @tparam CC Collection type constructor | ||
* @tparam C Collection type | ||
*/ | ||
trait StrictOptimizedSortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC, _], +C <: SortedMapOps[K, V, CC, C]] | ||
extends SortedMapOps[K, V, CC, C] | ||
with StrictOptimizedMapOps[K, V, Map, C] { | ||
|
||
override def map[K2, V2](f: ((K, V)) => (K2, V2))(implicit @implicitNotFound(SortedMapOps.ordMsg) ordering: Ordering[K2]): CC[K2, V2] = | ||
StrictOptimizedOps.map(iterator, sortedMapFactory.newBuilder, f) | ||
|
||
override def flatMap[K2, V2](f: ((K, V)) => IterableOnce[(K2, V2)])(implicit @implicitNotFound(SortedMapOps.ordMsg) ordering: Ordering[K2]): CC[K2, V2] = | ||
StrictOptimizedOps.flatMap(iterator, sortedMapFactory.newBuilder, f) | ||
|
||
override def concat[V2 >: V](xs: Iterable[(K, V2)]): CC[K, V2] = | ||
StrictOptimizedOps.concat(iterator, xs.iterator, sortedMapFactory.newBuilder) | ||
|
||
override def collect[K2, V2](pf: PartialFunction[(K, V), (K2, V2)])(implicit @implicitNotFound(SortedMapOps.ordMsg) ordering: Ordering[K2]): CC[K2, V2] = | ||
StrictOptimizedOps.collect(iterator, sortedMapFactory.newBuilder, pf) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.