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