|
| 1 | +package scala.collection.generic |
| 2 | + |
| 3 | +import scala.language.higherKinds |
| 4 | +import scala.annotation.migration |
| 5 | +import scala.annotation.unchecked.uncheckedVariance |
| 6 | +//import scala.collection.GenTraversableOnce |
| 7 | +import scala.collection.mutable.Builder |
| 8 | +import scala.collection.parallel.ParIterable |
| 9 | + |
| 10 | +// TODO inline in GenericParTempalte |
| 11 | +trait GenericTraversableTemplate[+A, +CC[X] /*<: GenTraversable[X]*/] extends HasNewBuilder[A, CC[A] @uncheckedVariance] { |
| 12 | + |
| 13 | + def seq: Iterable[A] |
| 14 | + |
| 15 | + /** Applies a function `f` to all elements of this $coll. |
| 16 | + * |
| 17 | + * @param f the function that is applied for its side-effect to every element. |
| 18 | + * The result of function `f` is discarded. |
| 19 | + * |
| 20 | + * @tparam U the type parameter describing the result of function `f`. |
| 21 | + * This result will always be ignored. Typically `U` is `Unit`, |
| 22 | + * but this is not necessary. |
| 23 | + * |
| 24 | + * @usecase def foreach(f: A => Unit): Unit |
| 25 | + */ |
| 26 | + def foreach[U](f: A => U): Unit |
| 27 | + |
| 28 | + /** Selects the first element of this $coll. |
| 29 | + * |
| 30 | + * @return the first element of this $coll. |
| 31 | + * @throws NoSuchElementException if the $coll is empty. |
| 32 | + */ |
| 33 | + def head: A |
| 34 | + |
| 35 | + /** Tests whether this $coll is empty. |
| 36 | + * |
| 37 | + * @return `true` if the $coll contain no elements, `false` otherwise. |
| 38 | + */ |
| 39 | + def isEmpty: Boolean |
| 40 | + |
| 41 | + /** The factory companion object that builds instances of class $Coll. |
| 42 | + * (or its `Iterable` superclass where class $Coll is not a `Seq`.) |
| 43 | + */ |
| 44 | +// def companion: GenericCompanion[CC] |
| 45 | + |
| 46 | + /** The builder that builds instances of type $Coll[A] |
| 47 | + */ |
| 48 | + protected[this] def newBuilder: Builder[A, CC[A]]/* = companion.newBuilder[A]*/ |
| 49 | + |
| 50 | + /** The generic builder that builds instances of $Coll |
| 51 | + * at arbitrary element types. |
| 52 | + */ |
| 53 | + def genericBuilder[B]: Builder[B, CC[B]]/* = companion.newBuilder[B]*/ |
| 54 | + |
| 55 | + private def sequential: IterableOnce[A] = this.asInstanceOf[ParIterable[A]].seq /*this.asInstanceOf[GenTraversableOnce[A]].seq*/ |
| 56 | + |
| 57 | + /** Converts this $coll of pairs into two collections of the first and second |
| 58 | + * half of each pair. |
| 59 | + * |
| 60 | + * {{{ |
| 61 | + * val xs = $Coll( |
| 62 | + * (1, "one"), |
| 63 | + * (2, "two"), |
| 64 | + * (3, "three")).unzip |
| 65 | + * // xs == ($Coll(1, 2, 3), |
| 66 | + * // $Coll(one, two, three)) |
| 67 | + * }}} |
| 68 | + * |
| 69 | + * @tparam A1 the type of the first half of the element pairs |
| 70 | + * @tparam A2 the type of the second half of the element pairs |
| 71 | + * @param asPair an implicit conversion which asserts that the element type |
| 72 | + * of this $coll is a pair. |
| 73 | + * @return a pair of ${coll}s, containing the first, respectively second |
| 74 | + * half of each element pair of this $coll. |
| 75 | + */ |
| 76 | + def unzip[A1, A2](implicit asPair: A => (A1, A2)): (CC[A1], CC[A2]) = { |
| 77 | + val b1 = genericBuilder[A1] |
| 78 | + val b2 = genericBuilder[A2] |
| 79 | + for (xy <- sequential.iterator) { |
| 80 | + val (x, y) = asPair(xy) |
| 81 | + b1 += x |
| 82 | + b2 += y |
| 83 | + } |
| 84 | + (b1.result(), b2.result()) |
| 85 | + } |
| 86 | + |
| 87 | + /** Converts this $coll of triples into three collections of the first, second, |
| 88 | + * and third element of each triple. |
| 89 | + * |
| 90 | + * {{{ |
| 91 | + * val xs = $Coll( |
| 92 | + * (1, "one", '1'), |
| 93 | + * (2, "two", '2'), |
| 94 | + * (3, "three", '3')).unzip3 |
| 95 | + * // xs == ($Coll(1, 2, 3), |
| 96 | + * // $Coll(one, two, three), |
| 97 | + * // $Coll(1, 2, 3)) |
| 98 | + * }}} |
| 99 | + * |
| 100 | + * @tparam A1 the type of the first member of the element triples |
| 101 | + * @tparam A2 the type of the second member of the element triples |
| 102 | + * @tparam A3 the type of the third member of the element triples |
| 103 | + * @param asTriple an implicit conversion which asserts that the element type |
| 104 | + * of this $coll is a triple. |
| 105 | + * @return a triple of ${coll}s, containing the first, second, respectively |
| 106 | + * third member of each element triple of this $coll. |
| 107 | + */ |
| 108 | + def unzip3[A1, A2, A3](implicit asTriple: A => (A1, A2, A3)): (CC[A1], CC[A2], CC[A3]) = { |
| 109 | + val b1 = genericBuilder[A1] |
| 110 | + val b2 = genericBuilder[A2] |
| 111 | + val b3 = genericBuilder[A3] |
| 112 | + |
| 113 | + for (xyz <- sequential.iterator) { |
| 114 | + val (x, y, z) = asTriple(xyz) |
| 115 | + b1 += x |
| 116 | + b2 += y |
| 117 | + b3 += z |
| 118 | + } |
| 119 | + (b1.result(), b2.result(), b3.result()) |
| 120 | + } |
| 121 | + |
| 122 | + /** Converts this $coll of traversable collections into |
| 123 | + * a $coll formed by the elements of these traversable |
| 124 | + * collections. |
| 125 | + * |
| 126 | + * @tparam B the type of the elements of each traversable collection. |
| 127 | + * @param asTraversable an implicit conversion which asserts that the element |
| 128 | + * type of this $coll is a `GenTraversable`. |
| 129 | + * @return a new $coll resulting from concatenating all element ${coll}s. |
| 130 | + * |
| 131 | + * @usecase def flatten[B]: $Coll[B] |
| 132 | + * |
| 133 | + * @inheritdoc |
| 134 | + * |
| 135 | + * The resulting collection's type will be guided by the |
| 136 | + * static type of $coll. For example: |
| 137 | + * |
| 138 | + * {{{ |
| 139 | + * val xs = List( |
| 140 | + * Set(1, 2, 3), |
| 141 | + * Set(1, 2, 3) |
| 142 | + * ).flatten |
| 143 | + * // xs == List(1, 2, 3, 1, 2, 3) |
| 144 | + * |
| 145 | + * val ys = Set( |
| 146 | + * List(1, 2, 3), |
| 147 | + * List(3, 2, 1) |
| 148 | + * ).flatten |
| 149 | + * // ys == Set(1, 2, 3) |
| 150 | + * }}} |
| 151 | + */ |
| 152 | + def flatten[B](implicit asTraversable: A => /*<:<!!!*/ IterableOnce[B]): CC[B] = { |
| 153 | + val b = genericBuilder[B] |
| 154 | + for (xs <- sequential.iterator) |
| 155 | + b ++= asTraversable(xs)/*.seq*/ |
| 156 | + b.result() |
| 157 | + } |
| 158 | + |
| 159 | + /** Transposes this $coll of traversable collections into |
| 160 | + * a $coll of ${coll}s. |
| 161 | + * |
| 162 | + * The resulting collection's type will be guided by the |
| 163 | + * static type of $coll. For example: |
| 164 | + * |
| 165 | + * {{{ |
| 166 | + * val xs = List( |
| 167 | + * Set(1, 2, 3), |
| 168 | + * Set(4, 5, 6)).transpose |
| 169 | + * // xs == List( |
| 170 | + * // List(1, 4), |
| 171 | + * // List(2, 5), |
| 172 | + * // List(3, 6)) |
| 173 | + * |
| 174 | + * val ys = Vector( |
| 175 | + * List(1, 2, 3), |
| 176 | + * List(4, 5, 6)).transpose |
| 177 | + * // ys == Vector( |
| 178 | + * // Vector(1, 4), |
| 179 | + * // Vector(2, 5), |
| 180 | + * // Vector(3, 6)) |
| 181 | + * }}} |
| 182 | + * |
| 183 | + * @tparam B the type of the elements of each traversable collection. |
| 184 | + * @param asTraversable an implicit conversion which asserts that the |
| 185 | + * element type of this $coll is a `Traversable`. |
| 186 | + * @return a two-dimensional $coll of ${coll}s which has as ''n''th row |
| 187 | + * the ''n''th column of this $coll. |
| 188 | + * @throws IllegalArgumentException if all collections in this $coll |
| 189 | + * are not of the same size. |
| 190 | + */ |
| 191 | + @migration("`transpose` throws an `IllegalArgumentException` if collections are not uniformly sized.", "2.9.0") |
| 192 | + def transpose[B](implicit asTraversable: A => /*<:<!!!*/ IterableOnce[B]): CC[CC[B] @uncheckedVariance] = { |
| 193 | + if (isEmpty) |
| 194 | + return genericBuilder[CC[B]].result() |
| 195 | + |
| 196 | + def fail = throw new IllegalArgumentException("transpose requires all collections have the same size") |
| 197 | + |
| 198 | + val headSize = asTraversable(head).iterator.size |
| 199 | + val bs: IndexedSeq[collection.mutable.Builder[B, CC[B]]] = IndexedSeq.fill(headSize)(genericBuilder[B]) |
| 200 | + for (xs <- sequential.iterator) { |
| 201 | + var i = 0 |
| 202 | + for (x <- asTraversable(xs).iterator/*.seq*/) { |
| 203 | + if (i >= headSize) fail |
| 204 | + bs(i) += x |
| 205 | + i += 1 |
| 206 | + } |
| 207 | + if (i != headSize) |
| 208 | + fail |
| 209 | + } |
| 210 | + val bb = genericBuilder[CC[B]] |
| 211 | + for (b <- bs) bb += b.result |
| 212 | + bb.result() |
| 213 | + } |
| 214 | +} |
0 commit comments