|
| 1 | +package scala |
| 2 | +import annotation.showAsInfix |
| 3 | +import compiletime._ |
| 4 | +import internal._ |
| 5 | + |
| 6 | +import scala.runtime.DynamicTuple |
| 7 | + |
| 8 | +sealed trait Tuple extends Any { |
| 9 | + import Tuple._ |
| 10 | + |
| 11 | + inline def toArray: Array[Object] = |
| 12 | + inline constValueOpt[BoundedSize[this.type]] match { |
| 13 | + case Some(0) => |
| 14 | + DynamicTuple.empty$Array |
| 15 | + case Some(1) => |
| 16 | + val t = asInstanceOf[Tuple1[Object]] |
| 17 | + Array(t._1) |
| 18 | + case Some(2) => |
| 19 | + val t = asInstanceOf[Tuple2[Object, Object]] |
| 20 | + Array(t._1, t._2) |
| 21 | + case Some(3) => |
| 22 | + val t = asInstanceOf[Tuple3[Object, Object, Object]] |
| 23 | + Array(t._1, t._2, t._3) |
| 24 | + case Some(4) => |
| 25 | + val t = asInstanceOf[Tuple4[Object, Object, Object, Object]] |
| 26 | + Array(t._1, t._2, t._3, t._4) |
| 27 | + case Some(n) if n <= DynamicTuple.MaxSpecialized => |
| 28 | + DynamicTuple.to$Array(this, n) |
| 29 | + case Some(n) => |
| 30 | + asInstanceOf[TupleXXL].elems |
| 31 | + case None => |
| 32 | + DynamicTuple.dynamicToArray(this) |
| 33 | + } |
| 34 | + |
| 35 | + def *: [H, This >: this.type <: Tuple] (x: H): H *: This = ??? |
| 36 | + |
| 37 | + inline def ++ [This >: this.type <: Tuple](that: Tuple): Concat[This, that.type] = { |
| 38 | + type Result = Concat[This, that.type] |
| 39 | + inline constValueOpt[BoundedSize[this.type]] match { |
| 40 | + case Some(0) => |
| 41 | + that.asInstanceOf[Result] |
| 42 | + case Some(1) => |
| 43 | + if (constValue[BoundedSize[that.type]] == 0) this.asInstanceOf[Result] |
| 44 | + else (asInstanceOf[Tuple1[_]]._1 *: that).asInstanceOf[Result] |
| 45 | + case Some(2) => |
| 46 | + val t = asInstanceOf[Tuple2[_, _]] |
| 47 | + inline constValue[BoundedSize[that.type]] match { |
| 48 | + case 0 => this.asInstanceOf[Result] |
| 49 | + case 1 => |
| 50 | + val u = that.asInstanceOf[Tuple1[_]] |
| 51 | + Tuple3(t._1, t._2, u._1).asInstanceOf[Result] |
| 52 | + case 2 => |
| 53 | + val u = that.asInstanceOf[Tuple2[_, _]] |
| 54 | + Tuple4(t._1, t._2, u._1, u._2).asInstanceOf[Result] |
| 55 | + case m => |
| 56 | + knownTupleFromItrator[Result](2 + m, this.asInstanceOf[Product].productIterator ++ that.asInstanceOf[Product].productIterator) |
| 57 | + } |
| 58 | + case Some(3) => |
| 59 | + val t = asInstanceOf[Tuple3[_, _, _]] |
| 60 | + inline constValue[BoundedSize[that.type]] match { |
| 61 | + case 0 => this.asInstanceOf[Result] |
| 62 | + case 1 => |
| 63 | + val u = that.asInstanceOf[Tuple1[_]] |
| 64 | + Tuple4(t._1, t._2, t._3, u._1).asInstanceOf[Result] |
| 65 | + case m => |
| 66 | + knownTupleFromItrator[Result](3 + m, this.asInstanceOf[Product].productIterator ++ that.asInstanceOf[Product].productIterator) |
| 67 | + } |
| 68 | + case Some(n) => |
| 69 | + inline constValue[BoundedSize[that.type]] match { |
| 70 | + case 0 => this.asInstanceOf[Result] |
| 71 | + case m => knownTupleFromItrator[Result](n + m, this.asInstanceOf[Product].productIterator ++ that.asInstanceOf[Product].productIterator) |
| 72 | + } |
| 73 | + case None => |
| 74 | + DynamicTuple.dynamic_++[This, that.type](this, that) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + inline def size[This >: this.type <: Tuple]: Size[This] = { |
| 79 | + type Result = Size[This] |
| 80 | + inline constValueOpt[BoundedSize[this.type]] match { |
| 81 | + case Some(n) => n.asInstanceOf[Result] |
| 82 | + case _ => DynamicTuple.dynamicSize(this) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +object Tuple { |
| 89 | + |
| 90 | + type Head[X <: NonEmptyTuple] = X match { |
| 91 | + case x *: _ => x |
| 92 | + } |
| 93 | + |
| 94 | + type Tail[X <: NonEmptyTuple] <: Tuple = X match { |
| 95 | + case _ *: xs => xs |
| 96 | + } |
| 97 | + |
| 98 | + type Concat[X <: Tuple, +Y <: Tuple] <: Tuple = X match { |
| 99 | + case Unit => Y |
| 100 | + case x1 *: xs1 => x1 *: Concat[xs1, Y] |
| 101 | + } |
| 102 | + |
| 103 | + type Elem[X <: Tuple, N] = X match { |
| 104 | + case x *: xs => |
| 105 | + N match { |
| 106 | + case 0 => x |
| 107 | + case S[n1] => Elem[xs, n1] |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + type Size[X] <: Int = X match { |
| 112 | + case Unit => 0 |
| 113 | + case x *: xs => S[Size[xs]] |
| 114 | + } |
| 115 | + |
| 116 | + private[scala] type BoundedSizeRecur[X, L <: Int] <: Int = X match { |
| 117 | + case Unit => 0 |
| 118 | + case x *: xs => |
| 119 | + L match { |
| 120 | + case 0 => 0 |
| 121 | + case S[n] => S[BoundedSizeRecur[xs, n]] |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private[scala] type BoundedSize[X] = BoundedSizeRecur[X, 24] |
| 126 | + |
| 127 | + private[scala] inline def knownTupleFromItrator[T <: Tuple](n: Int, it: Iterator[Any]): T = |
| 128 | + inline n match { |
| 129 | + case 0 => ().asInstanceOf[T] |
| 130 | + case 1 => Tuple1(it.next()).asInstanceOf[T] |
| 131 | + case 2 => Tuple2(it.next(), it.next()).asInstanceOf[T] |
| 132 | + case 3 => Tuple3(it.next(), it.next(), it.next()).asInstanceOf[T] |
| 133 | + case 4 => Tuple4(it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 134 | + case 5 => Tuple5(it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 135 | + case 6 => Tuple6(it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 136 | + case 7 => Tuple7(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 137 | + case 8 => Tuple8(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 138 | + case 9 => Tuple9(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 139 | + case 10 => Tuple10(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 140 | + case 11 => Tuple11(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 141 | + case 12 => Tuple12(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 142 | + case 13 => Tuple13(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 143 | + case 14 => Tuple14(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 144 | + case 15 => Tuple15(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 145 | + case 16 => Tuple16(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 146 | + case 17 => Tuple17(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 147 | + case 18 => Tuple18(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 148 | + case 19 => Tuple19(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 149 | + case 20 => Tuple20(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 150 | + case 21 => Tuple21(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 151 | + case 22 => Tuple22(it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next(), it.next()).asInstanceOf[T] |
| 152 | + case _ => TupleXXL(it.asInstanceOf[Iterator[Object]].toArray).asInstanceOf[T] |
| 153 | + } |
| 154 | + |
| 155 | + def fromArray[T](xs: Array[T]): Tuple = { |
| 156 | + val xs2 = xs match { |
| 157 | + case xs: Array[Object] => xs |
| 158 | + case xs => xs.map(_.asInstanceOf[Object]) |
| 159 | + } |
| 160 | + DynamicTuple.dynamicFromArray[Tuple](xs2) |
| 161 | + } |
| 162 | + |
| 163 | + def fromProduct(product: Product): Tuple = |
| 164 | + runtime.DynamicTuple.dynamicFromProduct[Tuple](product) |
| 165 | + |
| 166 | +} |
| 167 | + |
| 168 | +sealed trait NonEmptyTuple extends Tuple { |
| 169 | + import Tuple._ |
| 170 | + |
| 171 | + inline def head[This >: this.type <: NonEmptyTuple]: Head[This] = { |
| 172 | + type Result = Head[This] |
| 173 | + val resVal = inline constValueOpt[BoundedSize[this.type]] match { |
| 174 | + case Some(1) => |
| 175 | + val t = asInstanceOf[Tuple1[_]] |
| 176 | + t._1 |
| 177 | + case Some(2) => |
| 178 | + val t = asInstanceOf[Tuple2[_, _]] |
| 179 | + t._1 |
| 180 | + case Some(3) => |
| 181 | + val t = asInstanceOf[Tuple3[_, _, _]] |
| 182 | + t._1 |
| 183 | + case Some(4) => |
| 184 | + val t = asInstanceOf[Tuple4[_, _, _, _]] |
| 185 | + t._1 |
| 186 | + case Some(n) if n > 4 && n <= DynamicTuple.MaxSpecialized => |
| 187 | + asInstanceOf[Product].productElement(0) |
| 188 | + case Some(n) if n > DynamicTuple.MaxSpecialized => |
| 189 | + val t = asInstanceOf[TupleXXL] |
| 190 | + t.elems(0) |
| 191 | + case None => |
| 192 | + DynamicTuple.dynamicHead[this.type](this) |
| 193 | + } |
| 194 | + resVal.asInstanceOf[Result] |
| 195 | + } |
| 196 | + |
| 197 | + def tail[This >: this.type <: NonEmptyTuple]: Tail[This] |
| 198 | + |
| 199 | + inline def fallbackApply(n: Int) = |
| 200 | + inline constValueOpt[n.type] match { |
| 201 | + case Some(n: Int) => error("index out of bounds: ", n) |
| 202 | + case None => DynamicTuple.dynamicApply[this.type, n.type](this, n) |
| 203 | + } |
| 204 | + |
| 205 | + inline def apply[This >: this.type <: NonEmptyTuple](n: Int): Elem[This, n.type] = { |
| 206 | + type Result = Elem[This, n.type] |
| 207 | + inline constValueOpt[Size[this.type]] match { |
| 208 | + case Some(1) => |
| 209 | + val t = asInstanceOf[Tuple1[_]] |
| 210 | + inline constValueOpt[n.type] match { |
| 211 | + case Some(0) => t._1.asInstanceOf[Result] |
| 212 | + case _ => fallbackApply(n).asInstanceOf[Result] |
| 213 | + } |
| 214 | + case Some(2) => |
| 215 | + val t = asInstanceOf[Tuple2[_, _]] |
| 216 | + inline constValueOpt[n.type] match { |
| 217 | + case Some(0) => t._1.asInstanceOf[Result] |
| 218 | + case Some(1) => t._2.asInstanceOf[Result] |
| 219 | + case _ => fallbackApply(n).asInstanceOf[Result] |
| 220 | + } |
| 221 | + case Some(3) => |
| 222 | + val t = asInstanceOf[Tuple3[_, _, _]] |
| 223 | + inline constValueOpt[n.type] match { |
| 224 | + case Some(0) => t._1.asInstanceOf[Result] |
| 225 | + case Some(1) => t._2.asInstanceOf[Result] |
| 226 | + case Some(2) => t._3.asInstanceOf[Result] |
| 227 | + case _ => fallbackApply(n).asInstanceOf[Result] |
| 228 | + } |
| 229 | + case Some(4) => |
| 230 | + val t = asInstanceOf[Tuple4[_, _, _, _]] |
| 231 | + inline constValueOpt[n.type] match { |
| 232 | + case Some(0) => t._1.asInstanceOf[Result] |
| 233 | + case Some(1) => t._2.asInstanceOf[Result] |
| 234 | + case Some(2) => t._3.asInstanceOf[Result] |
| 235 | + case Some(3) => t._4.asInstanceOf[Result] |
| 236 | + case _ => fallbackApply(n).asInstanceOf[Result] |
| 237 | + } |
| 238 | + case Some(s) if s > 4 && s <= DynamicTuple.MaxSpecialized => |
| 239 | + val t = asInstanceOf[Product] |
| 240 | + inline constValueOpt[n.type] match { |
| 241 | + case Some(n) if n >= 0 && n < s => t.productElement(n).asInstanceOf[Result] |
| 242 | + case _ => fallbackApply(n).asInstanceOf[Result] |
| 243 | + } |
| 244 | + case Some(s) if s > DynamicTuple.MaxSpecialized => |
| 245 | + val t = asInstanceOf[TupleXXL] |
| 246 | + inline constValueOpt[n.type] match { |
| 247 | + case Some(n) if n >= 0 && n < s => t.elems(n).asInstanceOf[Result] |
| 248 | + case _ => fallbackApply(n).asInstanceOf[Result] |
| 249 | + } |
| 250 | + case _ => fallbackApply(n).asInstanceOf[Result] |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | +} |
| 255 | + |
| 256 | +@showAsInfix |
| 257 | +sealed abstract class *:[+H, +T <: Tuple] extends NonEmptyTuple |
| 258 | + |
| 259 | +object *: { |
| 260 | + inline def unapply[H, T <: Tuple](x: H *: T) = (x.head, x.tail) |
| 261 | +} |
0 commit comments