Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #468 from scala/wip/default-iterable-factory
Browse files Browse the repository at this point in the history
Remove the boilerplate associated with the new design (and more)
  • Loading branch information
szeiger committed Feb 20, 2018
2 parents a42fc3d + bcd4413 commit 701a25f
Show file tree
Hide file tree
Showing 74 changed files with 225 additions and 408 deletions.
Expand Up @@ -11,6 +11,11 @@ trait MultiDict[K, V]
with MultiDictOps[K, V, MultiDict, MultiDict[K, V]]
with Equals {

def multiMapFactory: MapFactory[MultiDictCC] = MultiDict

override protected[this] def fromSpecificIterable(coll: Iterable[(K, V)]): MultiDictCC[K, V] = multiMapFactory.from(coll)
override protected[this] def newSpecificBuilder(): mutable.Builder[(K, V), MultiDictCC[K, V]] = multiMapFactory.newBuilder[K, V]()

def canEqual(that: Any): Boolean = true

override def equals(o: Any): Boolean = o match {
Expand All @@ -35,7 +40,9 @@ trait MultiDict[K, V]
trait MultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
extends IterableOps[(K, V), Iterable, C] {

def multiMapFactory: MapFactory[CC]
protected[this] type MultiDictCC[K, V] = CC[K, V]

def multiMapFactory: MapFactory[MultiDictCC]

protected[this] def multiMapFromIterable[L, W](it: Iterable[(L, W)]): CC[L, W] =
multiMapFactory.from(it)
Expand Down
Expand Up @@ -12,14 +12,17 @@ trait SortedMultiDict[K, V]

def unsorted: MultiDict[K, V] = this

override protected[this] def fromSpecificIterable(coll: Iterable[(K, V)]): SortedMultiDictCC[K, V] = sortedMultiMapFactory.from(coll)
override protected[this] def newSpecificBuilder(): mutable.Builder[(K, V), SortedMultiDictCC[K, V]] = sortedMultiMapFactory.newBuilder[K, V]()
}

trait SortedMultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
extends MultiDictOps[K, V, MultiDict, C]
with SortedOps[K, C] {

def multiMapFactory: MapFactory[MultiDict] = MultiDict
def sortedMultiMapFactory: SortedMapFactory[CC]
protected[this] type SortedMultiDictCC[K, V] = CC[K, V]

def sortedMultiMapFactory: SortedMapFactory[SortedMultiDictCC]

protected[this] def sortedFromIterable[L : Ordering, W](it: Iterable[(L, W)]): CC[L, W]
protected[this] def sortedFromSets[L : Ordering, W](it: Iterable[(L, Set[W])]): CC[L, W] =
Expand Down
Expand Up @@ -12,15 +12,23 @@ trait SortedMultiSet[A]

def unsorted: MultiSet[A] = this

override protected[this] def fromSpecificIterable(coll: Iterable[A]): SortedIterableCC[A] = sortedIterableFactory.from(coll)
override protected[this] def newSpecificBuilder(): mutable.Builder[A, SortedIterableCC[A]] = sortedIterableFactory.newBuilder[A]()

protected[this] def sortedFromIterable[B : Ordering](it: strawman.collection.Iterable[B]): SortedIterableCC[B] = sortedIterableFactory.from(it)

def sortedIterableFactory: SortedIterableFactory[SortedIterableCC] = SortedMultiSet
}

trait SortedMultiSetOps[A, +CC[X] <: MultiSet[X], +C <: MultiSet[A]]
extends MultiSetOps[A, MultiSet, C]
with SortedOps[A, C] {

def sortedIterableFactory: SortedIterableFactory[CC]
protected[this] type SortedIterableCC[X] = CC[X]

def sortedIterableFactory: SortedIterableFactory[SortedIterableCC]

protected[this] def sortedFromIterable[B : Ordering](it: Iterable[B]): CC[B]
protected[this] def sortedFromIterable[B : Ordering](it: Iterable[B]): SortedIterableCC[B]
protected[this] def sortedFromOccurrences[B : Ordering](it: Iterable[(B, Int)]): CC[B] =
sortedFromIterable(it.view.flatMap { case (b, n) => View.Fill(n)(b) })

Expand Down
Expand Up @@ -18,11 +18,7 @@ class MultiDict[K, V] private (elems: Map[K, Set[V]])

def sets: Map[K, Set[V]] = elems

def iterableFactory: IterableFactory[Iterable] = Iterable
def multiMapFactory: MapFactory[MultiDict] = MultiDict

protected[this] def fromSpecificIterable(coll: collection.Iterable[(K, V)]): MultiDict[K, V] = multiMapFromIterable(coll)
protected[this] def newSpecificBuilder(): Builder[(K, V), MultiDict[K, V]] = multiMapFactory.newBuilder[K, V]()
override def multiMapFactory: MapFactory[MultiDict] = MultiDict

/**
* @return a new multidict that contains all the entries of this multidict
Expand Down
Expand Up @@ -17,9 +17,7 @@ class MultiSet[A] private (elems: Map[A, Int])

def occurrences: Map[A, Int] = elems

def iterableFactory: IterableFactory[MultiSet] = MultiSet
protected[this] def fromSpecificIterable(coll: collection.Iterable[A]): MultiSet[A] = fromIterable(coll)
protected[this] def newSpecificBuilder(): Builder[A, MultiSet[A]] = iterableFactory.newBuilder()
override def iterableFactory: IterableFactory[MultiSet] = MultiSet

/**
* @return an immutable multiset containing all the elements of this multiset
Expand Down
Expand Up @@ -17,11 +17,8 @@ class SortedMultiDict[K, V] private (elems: SortedMap[K, Set[V]])(implicit val o
with collection.IterableOps[(K, V), Iterable, SortedMultiDict[K, V]] {

def sortedMultiMapFactory: SortedMapFactory[SortedMultiDict] = SortedMultiDict
def iterableFactory: IterableFactory[Iterable] = Iterable

protected[this] def fromSpecificIterable(coll: collection.Iterable[(K, V)]): SortedMultiDict[K, V] = sortedMultiMapFactory.from(coll)
protected[this] def sortedFromIterable[L: Ordering, W](it: collection.Iterable[(L, W)]): SortedMultiDict[L, W] = sortedMultiMapFactory.from(it)
protected[this] def newSpecificBuilder(): Builder[(K, V), SortedMultiDict[K, V]] = sortedMultiMapFactory.newBuilder()

def sets: SortedMap[K, Set[V]] = elems

Expand Down
Expand Up @@ -17,12 +17,8 @@ class SortedMultiSet[A] private (elems: SortedMap[A, Int])(implicit val ordering

def occurrences: SortedMap[A, Int] = elems

def iterableFactory: IterableFactory[MultiSet] = MultiSet
def sortedIterableFactory: SortedIterableFactory[SortedMultiSet] = SortedMultiSet

protected[this] def fromSpecificIterable(coll: collection.Iterable[A]): SortedMultiSet[A] = sortedFromIterable(coll)
protected[this] def sortedFromIterable[B : Ordering](it: collection.Iterable[B]): SortedMultiSet[B] = sortedIterableFactory.from(it)
protected[this] def newSpecificBuilder(): Builder[A, SortedMultiSet[A]] = sortedIterableFactory.newBuilder()
override def iterableFactory: IterableFactory[MultiSet] = MultiSet
override def sortedIterableFactory: SortedIterableFactory[SortedMultiSet] = SortedMultiSet

def rangeImpl(from: Option[A], until: Option[A]): SortedMultiSet[A] =
new SortedMultiSet(elems.rangeImpl(from, until))
Expand Down
Expand Up @@ -15,10 +15,7 @@ class MultiDict[K, V] private (elems: Map[K, Set[V]])
with Growable[(K, V)]
with Shrinkable[(K, V)] {

def iterableFactory: IterableFactory[collection.Iterable] = collection.Iterable
def multiMapFactory: MapFactory[MultiDict] = MultiDict
protected[this] def fromSpecificIterable(coll: collection.Iterable[(K, V)]): MultiDict[K, V] = multiMapFactory.from(coll)
protected[this] def newSpecificBuilder(): Builder[(K, V), MultiDict[K, V]] = multiMapFactory.newBuilder()
override def multiMapFactory: MapFactory[MultiDict] = MultiDict

def sets: collection.Map[K, collection.Set[V]] = elems

Expand Down
Expand Up @@ -13,9 +13,7 @@ class MultiSet[A] private (val elems: Map[A, Int])
with Growable[A]
with Shrinkable [A] {

def iterableFactory: IterableFactory[MultiSet] = MultiSet
protected[this] def fromSpecificIterable(coll: collection.Iterable[A]): MultiSet[A] = fromIterable(coll)
protected[this] def newSpecificBuilder(): Builder[A, MultiSet[A]] = iterableFactory.newBuilder()
override def iterableFactory: IterableFactory[MultiSet] = MultiSet

def occurrences: collection.Map[A, Int] = elems

Expand Down
Expand Up @@ -17,12 +17,9 @@ class SortedMultiDict[K, V] private (elems: SortedMap[K, Set[V]])(implicit val o

def sets: collection.SortedMap[K, collection.Set[V]] = elems

def iterableFactory: IterableFactory[collection.Iterable] = collection.Iterable
def sortedMultiMapFactory: SortedMapFactory[SortedMultiDict] = SortedMultiDict

protected[this] def fromSpecificIterable(coll: collection.Iterable[(K, V)]): SortedMultiDict[K, V] = sortedMultiMapFactory.from(coll)
protected[this] def sortedFromIterable[L: Ordering, W](it: collection.Iterable[(L, W)]): SortedMultiDict[L, W] = sortedMultiMapFactory.from(it)
protected[this] def newSpecificBuilder(): Builder[(K, V), SortedMultiDict[K, V]] = sortedMultiMapFactory.newBuilder()

def rangeImpl(from: Option[K], until: Option[K]): SortedMultiDict[K, V] =
new SortedMultiDict(elems.rangeImpl(from, until))
Expand Down
Expand Up @@ -17,12 +17,7 @@ class SortedMultiSet[A] private (elems: SortedMap[A, Int])(implicit val ordering

def occurrences: collection.SortedMap[A, Int] = elems

def iterableFactory: IterableFactory[MultiSet] = MultiSet
def sortedIterableFactory: SortedIterableFactory[SortedMultiSet] = SortedMultiSet

protected[this] def fromSpecificIterable(coll: collection.Iterable[A]): SortedMultiSet[A] = sortedFromIterable(coll)
protected[this] def sortedFromIterable[B: Ordering](it: collection.Iterable[B]): SortedMultiSet[B] = sortedIterableFactory.from(it)
protected[this] def newSpecificBuilder(): Builder[A, SortedMultiSet[A]] = sortedIterableFactory.newBuilder()
override def sortedIterableFactory: SortedIterableFactory[SortedMultiSet] = SortedMultiSet

def rangeImpl(from: Option[A], until: Option[A]): SortedMultiSet[A] =
new SortedMultiSet(elems.rangeImpl(from, until))
Expand Down
10 changes: 9 additions & 1 deletion collections/src/main/scala/strawman/collection/BitSet.scala
Expand Up @@ -18,7 +18,11 @@ import scala.Predef.{assert, intWrapper}
* @define coll bitset
* @define Coll `BitSet`
*/
trait BitSet extends SortedSet[Int] with BitSetOps[BitSet]
trait BitSet extends SortedSet[Int] with BitSetOps[BitSet] {
override protected[this] def fromSpecificIterable(coll: Iterable[Int]): BitSetC = bitSetFactory.fromSpecific(coll)
override protected[this] def newSpecificBuilder(): Builder[Int, BitSetC] = bitSetFactory.newBuilder()
override def empty: BitSetC = bitSetFactory.empty
}

object BitSet extends SpecificIterableFactory[Int, BitSet] {
def empty: BitSet = immutable.BitSet.empty
Expand All @@ -31,6 +35,10 @@ trait BitSetOps[+C <: BitSet with BitSetOps[C]]
extends SortedSetOps[Int, SortedSet, C] { self =>
import BitSetOps._

def bitSetFactory: SpecificIterableFactory[Int, BitSetC]

protected[this] type BitSetC = C

final def ordering: Ordering[Int] = Ordering.Int

/** The number of words (each with 64 bits) making up the set */
Expand Down
24 changes: 4 additions & 20 deletions collections/src/main/scala/strawman/collection/DefaultMap.scala
Expand Up @@ -9,28 +9,12 @@
package strawman
package collection

import scala.deprecated

/** A default map which builds a default `immutable.Map` implementation for all
* transformations.
*
* Instances that inherit from `DefaultMap[K, V]` still have to define:
* {{{
* def get(key: K): Option[V]
* def iterator(): Iterator[(K, V)]
* }}}
*
* It might also be advisable to override `foreach` or `size` if efficient
* implementations can be found.
*
* @since 2.8
*/
trait DefaultMap[K, +V] extends Map[K, V] { self =>

// Members declared in IterableOps
def iterableFactory: IterableFactory[Iterable] = Iterable
protected[this] def fromSpecificIterable(coll: Iterable[(K, V)]): Map[K,V] = mapFactory.from(coll)
protected[this] def newSpecificBuilder(): mutable.Builder[(K, V), Map[K,V]] = mapFactory.newBuilder()

// Members declared in MapOps
def mapFactory: MapFactory[Map] = Map
def empty: Map[K,V] = mapFactory.empty
}
@deprecated("DefaultMap is no longer necessary; extend Map directly", "2.13.0")
trait DefaultMap[K, +V] extends Map[K, V]
10 changes: 8 additions & 2 deletions collections/src/main/scala/strawman/collection/Iterable.scala
Expand Up @@ -26,6 +26,10 @@ trait Iterable[+A] extends IterableOnce[A] with IterableOps[A, Iterable, Iterabl
//TODO scalac generates an override for this in AbstractMap; Making it final leads to a VerifyError
protected[this] def coll: this.type = this

protected[this] def fromSpecificIterable(coll: Iterable[A]): IterableCC[A] = iterableFactory.from(coll)
protected[this] def newSpecificBuilder(): Builder[A, IterableCC[A]] = iterableFactory.newBuilder[A]()

def iterableFactory: IterableFactory[IterableCC] = Iterable
}

/** Base trait for Iterable operations
Expand Down Expand Up @@ -64,6 +68,8 @@ trait Iterable[+A] extends IterableOnce[A] with IterableOps[A, Iterable, Iterabl
*/
trait IterableOps[+A, +CC[_], +C] extends Any with IterableOnce[A] {

protected[this] type IterableCC[X] = CC[X]

/**
* @return This collection as an `Iterable[A]`. No new collection will be built if `this` is already an `Iterable[A]`.
*/
Expand Down Expand Up @@ -91,7 +97,7 @@ trait IterableOps[+A, +CC[_], +C] extends Any with IterableOnce[A] {
/**
* @return The companion object of this ${coll}, providing various factory methods.
*/
def iterableFactory: IterableFactory[CC]
def iterableFactory: IterableFactory[IterableCC]

/**
* @return a strict builder for the same collection type.
Expand Down Expand Up @@ -1251,4 +1257,4 @@ object Iterable extends IterableFactory.Delegate[Iterable](immutable.Iterable) {
implicit def toLazyZipOps[A, CC[X] <: Iterable[X]](that: CC[A]): LazyZipOps[A, CC[A]] = new LazyZipOps(that)
}

abstract class AbstractIterable[+A] extends Iterable[A]
abstract class AbstractIterable[+A] extends Iterable[A]
20 changes: 15 additions & 5 deletions collections/src/main/scala/strawman/collection/Map.scala
Expand Up @@ -15,6 +15,13 @@ trait Map[K, +V]
with MapOps[K, V, Map, Map[K, V]]
with Equals {

override protected[this] def fromSpecificIterable(coll: Iterable[(K, V)]): MapCC[K, V] = mapFactory.from(coll)
override protected[this] def newSpecificBuilder(): mutable.Builder[(K, V), MapCC[K, V]] = mapFactory.newBuilder[K, V]()

def mapFactory: strawman.collection.MapFactory[MapCC] = Map

def empty: MapCC[K, V] = mapFactory.empty

def canEqual(that: Any): Boolean = true

override def equals(o: Any): Boolean = o match {
Expand Down Expand Up @@ -52,7 +59,14 @@ trait MapOps[K, +V, +CC[X, Y] <: MapOps[X, Y, CC, _], +C]
with PartialFunction[K, V]
with Equals {

def mapFactory: MapFactory[CC]
protected[this] type MapCC[K, V] = CC[K, V]

/** Similar to `fromIterable`, but returns a Map collection type.
* Note that the return type is now `CC[K2, V2]` aka `MapCC[K2, V2]` rather than `IterableCC[(K2, V2)]`.
*/
@`inline` protected[this] final def mapFromIterable[K2, V2](it: Iterable[(K2, V2)]): CC[K2, V2] = mapFactory.from(it)

def mapFactory: MapFactory[MapCC]

/** Optionally returns the value associated with a key.
*
Expand Down Expand Up @@ -104,11 +118,7 @@ trait MapOps[K, +V, +CC[X, Y] <: MapOps[X, Y, CC, _], +C]
*/
@SerialVersionUID(3L)
protected class KeySet extends Set[K] with GenKeySet {
def iterableFactory: IterableFactory[Set] = Set
protected[this] def fromSpecificIterable(coll: Iterable[K]): Set[K] = fromIterable(coll)
protected[this] def newSpecificBuilder(): Builder[K, Set[K]] = iterableFactory.newBuilder()
def diff(that: Set[K]): Set[K] = fromSpecificIterable(view.filterNot(that))
def empty: Set[K] = iterableFactory.empty
}

/** A generic trait that is reused by keyset implementations */
Expand Down
2 changes: 1 addition & 1 deletion collections/src/main/scala/strawman/collection/Seq.scala
Expand Up @@ -20,7 +20,7 @@ trait Seq[+A]
with SeqOps[A, Seq, Seq[A]]
with Equals {

def iterableFactory: SeqFactory[Seq]
override def iterableFactory: SeqFactory[IterableCC] = Seq

/** Method called from equality methods, so that user-defined subclasses can
* refuse to be equal to other collections of the same kind.
Expand Down
1 change: 1 addition & 0 deletions collections/src/main/scala/strawman/collection/Set.scala
Expand Up @@ -28,6 +28,7 @@ trait Set[A]

override def hashCode(): Int = Set.setHash(toIterable)

def empty: IterableCC[A] = iterableFactory.empty
}

/** Base trait for set operations
Expand Down
25 changes: 17 additions & 8 deletions collections/src/main/scala/strawman/collection/SortedMap.scala
Expand Up @@ -5,20 +5,35 @@ import strawman.collection.immutable.TreeMap
import strawman.collection.mutable.Builder

import scala.annotation.unchecked.uncheckedVariance
import scala.{Boolean, Int, Option, Ordering, PartialFunction, Serializable, SerialVersionUID, `inline`}
import scala.{Boolean, Int, Option, Ordering, PartialFunction, Serializable, SerialVersionUID, `inline`, Any}

/** Base type of sorted sets */
trait SortedMap[K, +V]
extends Map[K, V]
with SortedMapOps[K, V, SortedMap, SortedMap[K, V]] {

def unsorted: Map[K, V] = this

override protected[this] def fromSpecificIterable(coll: Iterable[(K, V)]): SortedMapCC[K, V] = sortedMapFactory.from(coll)
override protected[this] def newSpecificBuilder(): mutable.Builder[(K, V), SortedMapCC[K, V]] = sortedMapFactory.newBuilder[K, V]()

def sortedMapFactory: SortedMapFactory[SortedMapCC] = SortedMap

override def empty: SortedMapCC[K, V] = sortedMapFactory.empty
}

trait SortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC, _], +C <: SortedMapOps[K, V, CC, C]]
extends MapOps[K, V, Map, C]
with SortedOps[K, C] {

def sortedMapFactory: SortedMapFactory[CC]
protected[this] type SortedMapCC[K, V] = CC[K, V]

def sortedMapFactory: SortedMapFactory[SortedMapCC]

/** Similar to `mapFromIterable`, but returns a SortedMap collection type.
* Note that the return type is now `CC[K2, V2]` aka `SortedMapCC[K2, V2]` rather than `MapCC[(K2, V2)]`.
*/
@`inline` protected[this] final def sortedMapFromIterable[K2, V2](it: Iterable[(K2, V2)])(implicit ordering: Ordering[K2]): CC[K2, V2] = sortedMapFactory.from(it)

def unsorted: Map[K, V]

Expand Down Expand Up @@ -89,13 +104,7 @@ trait SortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC, _],
/** The implementation class of the set returned by `keySet` */
@SerialVersionUID(3L)
protected class KeySortedSet extends SortedSet[K] with GenKeySet with GenKeySortedSet {
def iterableFactory: IterableFactory[Set] = Set
def sortedIterableFactory: SortedIterableFactory[SortedSet] = SortedSet
protected[this] def fromSpecificIterable(coll: Iterable[K]): SortedSet[K] = sortedFromIterable(coll)
protected[this] def newSpecificBuilder(): Builder[K, SortedSet[K]] = sortedIterableFactory.newBuilder()
protected[this] def sortedFromIterable[B: Ordering](it: Iterable[B]): SortedSet[B] = sortedFromIterable(it)
def diff(that: Set[K]): SortedSet[K] = fromSpecificIterable(view.filterNot(that))
def empty: SortedSet[K] = sortedIterableFactory.empty
def rangeImpl(from: Option[K], until: Option[K]): SortedSet[K] = {
val map = SortedMapOps.this.rangeImpl(from, until)
new map.KeySortedSet
Expand Down

0 comments on commit 701a25f

Please sign in to comment.