diff --git a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Apply.scala b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Apply.scala index 06ea29711bf8..bc7189606348 100644 --- a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Apply.scala +++ b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Apply.scala @@ -22,7 +22,7 @@ class Apply { @Benchmark def tupleApply(): Any = { - DynamicTuple.dynamicApply(tuple, index) + DynamicTuple.apply(tuple, index) } @Benchmark diff --git a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Concat.scala b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Concat.scala index c3eab96e5662..efd09ec2f6f1 100644 --- a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Concat.scala +++ b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Concat.scala @@ -28,6 +28,6 @@ class Concat { @Benchmark def tupleConcat(): Tuple = { - DynamicTuple.dynamicConcat(tuple1, tuple2) + DynamicTuple.concat(tuple1, tuple2) } } diff --git a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Cons.scala b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Cons.scala index 474e76671841..1772ab4ae908 100644 --- a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Cons.scala +++ b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Cons.scala @@ -24,7 +24,7 @@ class Cons { @Benchmark def tupleCons(): Tuple = { - DynamicTuple.dynamicCons("elem", tuple) + DynamicTuple.cons("elem", tuple) } @Benchmark diff --git a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Conversions.scala b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Conversions.scala index e2e64d85df76..b4bffb99b462 100644 --- a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Conversions.scala +++ b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Conversions.scala @@ -24,22 +24,22 @@ class Conversions { @Benchmark def tupleToArray(): Array[Object] = { - DynamicTuple.dynamicToArray(tuple) + DynamicTuple.toArray(tuple) } @Benchmark def tupleToIArray(): IArray[Object] = { - DynamicTuple.dynamicToIArray(tuple) + DynamicTuple.toIArray(tuple) } @Benchmark def tupleFromArray(): Tuple = { - DynamicTuple.dynamicFromArray(array) + DynamicTuple.fromArray(array) } @Benchmark def tupleFromIArray(): Tuple = { - DynamicTuple.dynamicFromIArray(iarray) + DynamicTuple.fromIArray(iarray) } @Benchmark diff --git a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Map.scala b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Map.scala index 9b3807355087..fe2add6247e4 100644 --- a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Map.scala +++ b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Map.scala @@ -30,7 +30,7 @@ class Map { @Benchmark def tupleMap(): Tuple = { - DynamicTuple.dynamicMap[Tuple, Id](tuple, [T] => (x:T) => x.asInstanceOf[String].updated(0, 'a').asInstanceOf[T]) + DynamicTuple.map[Id](tuple, [T] => (x:T) => x.asInstanceOf[String].updated(0, 'a').asInstanceOf[T]) } @Benchmark diff --git a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Tail.scala b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Tail.scala index f685759b5989..f77dc98ee5ce 100644 --- a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Tail.scala +++ b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Tail.scala @@ -22,7 +22,7 @@ class Tail { @Benchmark def tupleTail(): Tuple = { - DynamicTuple.dynamicTail(tuple) + DynamicTuple.tail(tuple) } @Benchmark diff --git a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Zip.scala b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Zip.scala index 1b95922a01f9..9193963ee15b 100644 --- a/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Zip.scala +++ b/bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Zip.scala @@ -28,7 +28,7 @@ class Zip { @Benchmark def tupleZip(): Tuple = { - DynamicTuple.dynamicZip(tuple1, tuple2) + DynamicTuple.zip(tuple1, tuple2) } @Benchmark diff --git a/bench-run/src/main/scala/tuples/Drop.scala b/bench-run/src/main/scala/tuples/Drop.scala index cf824957b639..bd1d2a439c6f 100644 --- a/bench-run/src/main/scala/tuples/Drop.scala +++ b/bench-run/src/main/scala/tuples/Drop.scala @@ -24,7 +24,7 @@ class Drop { @Benchmark def tupleDrop(): Tuple = { - DynamicTuple.dynamicDrop(tuple, half) + DynamicTuple.drop(tuple, half) } @Benchmark diff --git a/bench-run/src/main/scala/tuples/Split.scala b/bench-run/src/main/scala/tuples/Split.scala index eff527dad90b..40ed1f5ee7c3 100644 --- a/bench-run/src/main/scala/tuples/Split.scala +++ b/bench-run/src/main/scala/tuples/Split.scala @@ -24,7 +24,7 @@ class Split { @Benchmark def tupleSplit(): (Tuple, Tuple) = { - DynamicTuple.dynamicSplitAt(tuple, half) + DynamicTuple.splitAt(tuple, half) } @Benchmark diff --git a/bench-run/src/main/scala/tuples/Take.scala b/bench-run/src/main/scala/tuples/Take.scala index a64295e7b729..07e9fdd7b75a 100644 --- a/bench-run/src/main/scala/tuples/Take.scala +++ b/bench-run/src/main/scala/tuples/Take.scala @@ -24,7 +24,7 @@ class Take { @Benchmark def tupleTake(): Tuple = { - DynamicTuple.dynamicTake(tuple, half) + DynamicTuple.take(tuple, half) } @Benchmark diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 3b471d97087e..17328d4e5fbb 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -758,12 +758,12 @@ class Definitions { @tu lazy val DynamicTupleModuleClass: Symbol = DynamicTupleModule.moduleClass lazy val DynamicTuple_consIterator: Symbol = DynamicTupleModule.requiredMethod("consIterator") lazy val DynamicTuple_concatIterator: Symbol = DynamicTupleModule.requiredMethod("concatIterator") - lazy val DynamicTuple_dynamicApply: Symbol = DynamicTupleModule.requiredMethod("dynamicApply") - lazy val DynamicTuple_dynamicCons: Symbol = DynamicTupleModule.requiredMethod("dynamicCons") - lazy val DynamicTuple_dynamicSize: Symbol = DynamicTupleModule.requiredMethod("dynamicSize") - lazy val DynamicTuple_dynamicTail: Symbol = DynamicTupleModule.requiredMethod("dynamicTail") - lazy val DynamicTuple_dynamicConcat: Symbol = DynamicTupleModule.requiredMethod("dynamicConcat") - lazy val DynamicTuple_dynamicToArray: Symbol = DynamicTupleModule.requiredMethod("dynamicToArray") + lazy val DynamicTuple_apply: Symbol = DynamicTupleModule.requiredMethod("apply") + lazy val DynamicTuple_cons: Symbol = DynamicTupleModule.requiredMethod("cons") + lazy val DynamicTuple_size: Symbol = DynamicTupleModule.requiredMethod("size") + lazy val DynamicTuple_tail: Symbol = DynamicTupleModule.requiredMethod("tail") + lazy val DynamicTuple_concat: Symbol = DynamicTupleModule.requiredMethod("concat") + lazy val DynamicTuple_toArray: Symbol = DynamicTupleModule.requiredMethod("toArray") lazy val DynamicTuple_productToArray: Symbol = DynamicTupleModule.requiredMethod("productToArray") @tu lazy val TupledFunctionTypeRef: TypeRef = ctx.requiredClassRef("scala.TupledFunction") diff --git a/compiler/src/dotty/tools/dotc/transform/TupleOptimizations.scala b/compiler/src/dotty/tools/dotc/transform/TupleOptimizations.scala index 252c6e56e90b..f649857f3e61 100644 --- a/compiler/src/dotty/tools/dotc/transform/TupleOptimizations.scala +++ b/compiler/src/dotty/tools/dotc/transform/TupleOptimizations.scala @@ -25,17 +25,17 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer { override def transformApply(tree: tpd.Apply)(implicit ctx: Context): tpd.Tree = if (!tree.symbol.exists || tree.symbol.owner != defn.DynamicTupleModuleClass) tree - else if (tree.symbol == defn.DynamicTuple_dynamicCons) transformTupleCons(tree) - else if (tree.symbol == defn.DynamicTuple_dynamicTail) transformTupleTail(tree) - else if (tree.symbol == defn.DynamicTuple_dynamicSize) transformTupleSize(tree) - else if (tree.symbol == defn.DynamicTuple_dynamicConcat) transformTupleConcat(tree) - else if (tree.symbol == defn.DynamicTuple_dynamicApply) transformTupleApply(tree) - else if (tree.symbol == defn.DynamicTuple_dynamicToArray) transformTupleToArray(tree) + else if (tree.symbol == defn.DynamicTuple_cons) transformTupleCons(tree) + else if (tree.symbol == defn.DynamicTuple_tail) transformTupleTail(tree) + else if (tree.symbol == defn.DynamicTuple_size) transformTupleSize(tree) + else if (tree.symbol == defn.DynamicTuple_concat) transformTupleConcat(tree) + else if (tree.symbol == defn.DynamicTuple_apply) transformTupleApply(tree) + else if (tree.symbol == defn.DynamicTuple_toArray) transformTupleToArray(tree) else tree private def transformTupleCons(tree: tpd.Apply)(implicit ctx: Context): Tree = { val head :: tail :: Nil = tree.args - defn.tupleTypes(tree.tpe) match { + defn.tupleTypes(tree.tpe.widenTermRefExpr.dealias) match { case Some(tpes) => // Generate a the tuple directly with TupleN+1.apply val size = tpes.size @@ -56,14 +56,14 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer { } case _ => // No optimization, keep: - // DynamicTuple.dynamicCons:(tail, head) + // DynamicTuple.cons(tail, head) tree } } private def transformTupleTail(tree: tpd.Apply)(implicit ctx: Context): Tree = { - val Apply(TypeApply(_, tpt :: Nil), tup :: Nil) = tree - defn.tupleTypes(tpt.tpe, MaxTupleArity + 1) match { + val Apply(_, tup :: Nil) = tree + defn.tupleTypes(tup.tpe.widenTermRefExpr.dealias, MaxTupleArity + 1) match { case Some(tpes) => // Generate a the tuple directly with TupleN-1.apply val size = tpes.size @@ -93,7 +93,7 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer { tup.asInstance(defn.TupleXXLClass.typeRef).select("tailXXL".toTermName) case None => // No optimization, keep: - // DynamicTuple.dynamicTail(tup) + // DynamicTuple.tail(tup) tree } } @@ -105,9 +105,8 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer { } private def transformTupleConcat(tree: tpd.Apply)(implicit ctx: Context): Tree = { - val Apply(TypeApply(_, selfTp :: thatTp :: Nil), self :: that :: Nil) = tree - - (defn.tupleTypes(selfTp.tpe), defn.tupleTypes(that.tpe.widenTermRefExpr)) match { + val Apply(_, self :: that :: Nil) = tree + (defn.tupleTypes(self.tpe.widenTermRefExpr.dealias), defn.tupleTypes(that.tpe.widenTermRefExpr.dealias)) match { case (Some(tpes1), Some(tpes2)) => // Generate a the tuple directly with TupleN+M.apply val n = tpes1.size @@ -135,14 +134,14 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer { } case _ => // No optimization, keep: - // DynamicTuple.dynamicCons[This, that.type](self, that) + // DynamicTuple.cons(self, that) tree } } private def transformTupleApply(tree: tpd.Apply)(implicit ctx: Context): Tree = { - val Apply(TypeApply(_, tpt :: nTpt :: Nil), tup :: nTree :: Nil) = tree - (defn.tupleTypes(tpt.tpe), nTpt.tpe) match { + val Apply(_, tup :: nTree :: Nil) = tree + (defn.tupleTypes(tup.tpe.widenTermRefExpr.dealias), nTree.tpe) match { case (Some(tpes), nTpe: ConstantType) => // Get the element directly with TupleM._n+1 or TupleXXL.productElement(n) val size = tpes.size @@ -162,7 +161,7 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer { tree case _ => // No optimization, keep: - // DynamicTuple.dynamicApply(tup, n) + // DynamicTuple.apply(tup, n) tree } } @@ -183,7 +182,7 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer { tup.asInstance(defn.TupleXXLClass.typeRef).select(nme.toArray) case None => // No optimization, keep: - // DynamicTuple.dynamicToArray(tup) + // DynamicTuple.toArray(tup) tree } } @@ -205,7 +204,7 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer { // TODO outline this code for the 22 alternatives (or less, may not need the smallest ones)? // This would yield smaller bytecode at the cost of an extra (easily JIT inlinable) call. - // def dynamicTupleN(it: Iterator[Any]): TupleN[Any, ..., Any] = Tuple(it.next(), ..., it.next()) + // def tupleN(it: Iterator[Any]): TupleN[Any, ..., Any] = Tuple(it.next(), ..., it.next()) val tpes = List.fill(size)(defn.AnyType) val elements = (0 until size).map(_ => it.select(nme.next)).toList knownTupleFromElements(tpes, elements) diff --git a/library/src/scala/Tuple.scala b/library/src/scala/Tuple.scala index 9547df5f5cca..9112597fb3be 100644 --- a/library/src/scala/Tuple.scala +++ b/library/src/scala/Tuple.scala @@ -11,27 +11,27 @@ sealed trait Tuple extends Any { /** Create a copy this tuple as an Array */ inline def toArray: Array[Object] = - DynamicTuple.dynamicToArray(this) + DynamicTuple.toArray(this) /** Create a copy this tuple as an IArray */ inline def toIArray: IArray[Object] = - DynamicTuple.dynamicToIArray(this) + DynamicTuple.toIArray(this) /** Return a new tuple by prepending the element to `this` tuple. * This opteration is O(this.size) */ inline def *: [H, This >: this.type <: Tuple] (x: H): H *: This = - DynamicTuple.dynamicCons[H, This](x, this) + DynamicTuple.cons(x, this).asInstanceOf[H *: This] /** Return a new tuple by concatenating `this` tuple with `that` tuple. * This opteration is O(this.size + that.size) */ inline def ++ [This >: this.type <: Tuple](that: Tuple): Concat[This, that.type] = - DynamicTuple.dynamicConcat[This, that.type](this, that) + DynamicTuple.concat(this, that).asInstanceOf[Concat[This, that.type]] /** Return the size (or arity) of the tuple */ inline def size[This >: this.type <: Tuple]: Size[This] = - DynamicTuple.dynamicSize(this) + DynamicTuple.size(this).asInstanceOf[Size[This]] /** Given two tuples, `(a1, ..., an)` and `(a1, ..., an)`, returns a tuple * `((a1, b1), ..., (an, bn))`. If the two tuples have different sizes, @@ -41,7 +41,7 @@ sealed trait Tuple extends Any { * `(A1, B1) *: ... *: (Ai, Bi) *: Tuple` */ inline def zip[This >: this.type <: Tuple, T2 <: Tuple](t2: T2): Zip[This, T2] = - DynamicTuple.dynamicZip(this, t2) + DynamicTuple.zip(this, t2).asInstanceOf[Zip[This, T2]] /** Called on a tuple `(a1, ..., an)`, returns a new tuple `(f(a1), ..., f(an))`. * The result is typed as `(F[A1], ..., F[An])` if the tuple type is fully known. @@ -49,27 +49,27 @@ sealed trait Tuple extends Any { * to be the cons type. */ inline def map[F[_]](f: [t] => t => F[t]): Map[this.type, F] = - DynamicTuple.dynamicMap(this, f) + DynamicTuple.map(this, f).asInstanceOf[Map[this.type, F]] /** Given a tuple `(a1, ..., am)`, returns the tuple `(a1, ..., an)` consisting * of its first n elements. */ inline def take[This >: this.type <: Tuple](n: Int): Take[This, n.type] = - DynamicTuple.dynamicTake[This, n.type](this, n) + DynamicTuple.take(this, n).asInstanceOf[Take[This, n.type]] /** Given a tuple `(a1, ..., am)`, returns the tuple `(an+1, ..., am)` consisting * all its elements except the first n ones. */ inline def drop[This >: this.type <: Tuple](n: Int): Drop[This, n.type] = - DynamicTuple.dynamicDrop[This, n.type](this, n) + DynamicTuple.drop(this, n).asInstanceOf[Drop[This, n.type]] /** Given a tuple `(a1, ..., am)`, returns a pair of the tuple `(a1, ..., an)` * consisting of the first n elements, and the tuple `(an+1, ..., am)` consisting * of the remaining elements. */ inline def splitAt[This >: this.type <: Tuple](n: Int): Split[This, n.type] = - DynamicTuple.dynamicSplitAt[This, n.type](this, n) + DynamicTuple.splitAt(this, n).asInstanceOf[Split[This, n.type]] } object Tuple { @@ -165,7 +165,7 @@ object Tuple { case xs: Array[Object] => xs case xs => xs.map(_.asInstanceOf[Object]) } - DynamicTuple.dynamicFromArray[Tuple](xs2) + DynamicTuple.fromArray(xs2).asInstanceOf[Tuple] } /** Convert an immutable array into a tuple of unknown arity and types */ @@ -176,12 +176,12 @@ object Tuple { // TODO suport IArray.map xs.asInstanceOf[Array[T]].map(_.asInstanceOf[Object]).asInstanceOf[IArray[Object]] } - DynamicTuple.dynamicFromIArray[Tuple](xs2) + DynamicTuple.fromIArray(xs2).asInstanceOf[Tuple] } /** Convert a Product into a tuple of unknown arity and types */ def fromProduct(product: Product): Tuple = - runtime.DynamicTuple.dynamicFromProduct[Tuple](product) + runtime.DynamicTuple.fromProduct(product) def fromProductTyped[P <: Product](p: P)(using m: scala.deriving.Mirror.ProductOf[P]): m.MirroredElemTypes = Tuple.fromArray(p.productIterator.toArray).asInstanceOf[m.MirroredElemTypes] // TODO use toIArray of Object to avoid double/triple array copy @@ -195,17 +195,17 @@ sealed trait NonEmptyTuple extends Tuple { * Equivalent to productElement but with a precise return type. */ inline def apply[This >: this.type <: NonEmptyTuple](n: Int): Elem[This, n.type] = - DynamicTuple.dynamicApply[This, n.type](this, n) + DynamicTuple.apply(this, n).asInstanceOf[Elem[This, n.type]] /** Get the head of this tuple */ inline def head[This >: this.type <: NonEmptyTuple]: Head[This] = - DynamicTuple.dynamicApply[This, 0](this, 0) + DynamicTuple.apply(this, 0).asInstanceOf[Head[This]] /** Get the tail of this tuple. * This opteration is O(this.size) */ inline def tail[This >: this.type <: NonEmptyTuple]: Tail[This] = - DynamicTuple.dynamicTail[This](this) + DynamicTuple.tail(this).asInstanceOf[Tail[This]] } diff --git a/library/src/scala/runtime/DynamicTuple.scala b/library/src/scala/runtime/DynamicTuple.scala index 6d3c9f6bf6c3..795cc3254ed7 100644 --- a/library/src/scala/runtime/DynamicTuple.scala +++ b/library/src/scala/runtime/DynamicTuple.scala @@ -1,36 +1,22 @@ package scala.runtime -import scala.Tuple.{ Concat, Size, Head, Tail, Elem, Zip, Map, Take, Drop, Split } - +// TODO: Rename to scala.runtime.Tuple object DynamicTuple { inline val MaxSpecialized = 22 - inline private val XXL = MaxSpecialized + 1 - - def to$Array(xs: Tuple, n: Int) = { - val arr = new Array[Object](n) - var i = 0 - var it = xs.asInstanceOf[Product].productIterator - while (i < n) { - arr(i) = it.next().asInstanceOf[Object] - i += 1 - } - arr - } - def cons$Array[H](x: H, elems: Array[Object]): Array[Object] = { - val elems1 = new Array[Object](elems.length + 1) - elems1(0) = x.asInstanceOf[Object] - System.arraycopy(elems, 0, elems1, 1, elems.length) - elems1 - } + @deprecated("use toArray", "") + def dynamicToArray(self: Tuple): Array[Object] = toArray(self) - def dynamicToArray(self: Tuple): Array[Object] = (self: Any) match { + def toArray(self: Tuple): Array[Object] = (self: Any) match { case self: TupleXXL => self.toArray case self: Product => productToArray(self) case self: Unit => Array.emptyObjectArray } - def dynamicToIArray(self: Tuple): IArray[Object] = (self: Any) match { + @deprecated("use toIArray", "") + def dynamicToIArray(self: Tuple): IArray[Object] = toIArray(self) + + def toIArray(self: Tuple): IArray[Object] = (self: Any) match { case self: TupleXXL => self.elems case self: Product => productToArray(self).asInstanceOf[IArray[Object]] case self: Unit => Array.emptyObjectArray.asInstanceOf[IArray[Object]] @@ -46,38 +32,47 @@ object DynamicTuple { arr } - def dynamicFromArray[T <: Tuple](xs: Array[Object]): T = xs.length match { - case 0 => ().asInstanceOf[T] - case 1 => Tuple1(xs(0)).asInstanceOf[T] - case 2 => Tuple2(xs(0), xs(1)).asInstanceOf[T] - case 3 => Tuple3(xs(0), xs(1), xs(2)).asInstanceOf[T] - case 4 => Tuple4(xs(0), xs(1), xs(2), xs(3)).asInstanceOf[T] - case 5 => Tuple5(xs(0), xs(1), xs(2), xs(3), xs(4)).asInstanceOf[T] - case 6 => Tuple6(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5)).asInstanceOf[T] - case 7 => Tuple7(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6)).asInstanceOf[T] - case 8 => Tuple8(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7)).asInstanceOf[T] - case 9 => Tuple9(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8)).asInstanceOf[T] - case 10 => Tuple10(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9)).asInstanceOf[T] - case 11 => Tuple11(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10)).asInstanceOf[T] - case 12 => Tuple12(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11)).asInstanceOf[T] - case 13 => Tuple13(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12)).asInstanceOf[T] - case 14 => Tuple14(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13)).asInstanceOf[T] - case 15 => Tuple15(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14)).asInstanceOf[T] - case 16 => Tuple16(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15)).asInstanceOf[T] - case 17 => Tuple17(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16)).asInstanceOf[T] - case 18 => Tuple18(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17)).asInstanceOf[T] - case 19 => Tuple19(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18)).asInstanceOf[T] - case 20 => Tuple20(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19)).asInstanceOf[T] - case 21 => Tuple21(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19), xs(20)).asInstanceOf[T] - case 22 => Tuple22(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19), xs(20), xs(21)).asInstanceOf[T] - case _ => TupleXXL.fromIArray(xs.clone().asInstanceOf[IArray[Object]]).asInstanceOf[T] + @deprecated("Use fromArray") + def dynamicFromArray[T <: Tuple](xs: Array[Object]): T = fromArray(xs).asInstanceOf[T] + + def fromArray(xs: Array[Object]): Tuple = xs.length match { + case 0 => () + case 1 => Tuple1(xs(0)) + case 2 => Tuple2(xs(0), xs(1)) + case 3 => Tuple3(xs(0), xs(1), xs(2)) + case 4 => Tuple4(xs(0), xs(1), xs(2), xs(3)) + case 5 => Tuple5(xs(0), xs(1), xs(2), xs(3), xs(4)) + case 6 => Tuple6(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5)) + case 7 => Tuple7(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6)) + case 8 => Tuple8(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7)) + case 9 => Tuple9(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8)) + case 10 => Tuple10(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9)) + case 11 => Tuple11(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10)) + case 12 => Tuple12(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11)) + case 13 => Tuple13(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12)) + case 14 => Tuple14(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13)) + case 15 => Tuple15(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14)) + case 16 => Tuple16(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15)) + case 17 => Tuple17(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16)) + case 18 => Tuple18(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17)) + case 19 => Tuple19(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18)) + case 20 => Tuple20(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19)) + case 21 => Tuple21(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19), xs(20)) + case 22 => Tuple22(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19), xs(20), xs(21)) + case _ => TupleXXL.fromIArray(xs.clone().asInstanceOf[IArray[Object]]).asInstanceOf[Tuple] } - def dynamicFromIArray[T <: Tuple](xs: IArray[Object]): T = - if (xs.length <= 22) dynamicFromArray(xs.asInstanceOf[Array[Object]]) - else TupleXXL.fromIArray(xs).asInstanceOf[T] + @deprecated("Use fromIArray", "") + def dynamicFromIArray[T <: Tuple](xs: IArray[Object]): T = fromIArray(xs).asInstanceOf[T] + + def fromIArray(xs: IArray[Object]): Tuple = + if (xs.length <= 22) fromArray(xs.asInstanceOf[Array[Object]]) + else TupleXXL.fromIArray(xs).asInstanceOf[Tuple] + + @deprecated("Use fromProduct", "") + def dynamicFromProduct[T <: Tuple](xs: Product): T = fromProduct(xs).asInstanceOf[T] - def dynamicFromProduct[T <: Tuple](xs: Product): T = (xs.productArity match { + def fromProduct(xs: Product): Tuple = (xs.productArity match { case 1 => xs match { case xs: Tuple1[_] => xs @@ -189,15 +184,15 @@ object DynamicTuple { case xs => Tuple22(xs.productElement(0), xs.productElement(1), xs.productElement(2), xs.productElement(3), xs.productElement(4), xs.productElement(5), xs.productElement(6), xs.productElement(7), xs.productElement(8), xs.productElement(9), xs.productElement(10), xs.productElement(11), xs.productElement(12), xs.productElement(13), xs.productElement(14), xs.productElement(15), xs.productElement(16), xs.productElement(17), xs.productElement(18), xs.productElement(19), xs.productElement(20), xs.productElement(21)) } case _ => - xs match { + (xs match { case xs: TupleXXL => xs case xs => TupleXXL.fromIArray(xs.productIterator.map(_.asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]]) // TODO use Iterator.toIArray - } - }).asInstanceOf[T] + }).asInstanceOf[Tuple] + }) // Cons for Tuple1 to Tuple22 - def specialCaseCons[H, This <: Tuple](x: H, self: This): H *: This = { - val res = (self: Any) match { + private def specialCaseCons(x: Any, self: Tuple): Tuple = { + (self: Any) match { case self: Unit => Tuple1(x) case self: Tuple1[_] => @@ -253,35 +248,38 @@ object DynamicTuple { self._18.asInstanceOf[Object], self._19.asInstanceOf[Object], self._20.asInstanceOf[Object], self._21.asInstanceOf[Object], self._22.asInstanceOf[Object], ) - TupleXXL.fromIArray(arr.asInstanceOf[IArray[Object]]) + TupleXXL.fromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Tuple] } - res.asInstanceOf[H *: This] } // Cons for TupleXXL - def xxlCons[H, This <: Tuple](x: H, xxl: TupleXXL): H *: This = { + private def xxlCons(x: Any, xxl: TupleXXL): TupleXXL = { val arr = new Array[Object](xxl.productArity + 1) - System.arraycopy(xxl.elems, 0, arr, 1, xxl.productArity) arr(0) = x.asInstanceOf[Object] - TupleXXL.fromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[H *: This] + System.arraycopy(xxl.elems, 0, arr, 1, xxl.productArity) + TupleXXL.fromIArray(arr.asInstanceOf[IArray[Object]]) } - def dynamicCons[H, This <: Tuple](x: H, self: This): H *: This = (self: Any) match { - case xxl: TupleXXL => xxlCons(x, xxl) + @deprecated("Use cons", "") + def dynamicCons[H, This <: Tuple](x: H, self: This): H *: This = cons(x, self).asInstanceOf[H *: This] + + def cons(x: Any, self: Tuple): Tuple = (self: Any) match { + case xxl: TupleXXL => xxlCons(x, xxl).asInstanceOf[Tuple] case _ => specialCaseCons(x, self) } - def dynamicConcat[This <: Tuple, That <: Tuple](self: This, that: That): Concat[This, That] = { - type Result = Concat[This, That] + @deprecated("Use concat", "") + def dynamicConcat[This <: Tuple, That <: Tuple](self: This, that: That): scala.Tuple.Concat[This, That] = concat(self, that).asInstanceOf[scala.Tuple.Concat[This, That]] + def concat[This <: Tuple, That <: Tuple](self: This, that: That): Tuple = { val selfSize: Int = self.size // If one of the tuples is empty, we can leave early if selfSize == 0 then - return that.asInstanceOf[Result] + return that val thatSize: Int = that.size if thatSize == 0 then - return self.asInstanceOf[Result] + return self val arr = new Array[Object](selfSize + thatSize) @@ -297,17 +295,20 @@ object DynamicTuple { // In the general case, we copy the two tuples to an array, and convert it back to a tuple copyToArray(self, selfSize, arr, 0) copyToArray(that, thatSize, arr, selfSize) - dynamicFromIArray[Result](arr.asInstanceOf[IArray[Object]]) + fromIArray(arr.asInstanceOf[IArray[Object]]) } - def dynamicSize[This <: Tuple](self: This): Size[This] = (self: Any) match { - case self: Unit => 0.asInstanceOf[Size[This]] - case self: Product => self.productArity.asInstanceOf[Size[This]] + @deprecated("Use size", "") + def dynamicSize[This <: Tuple](self: This): scala.Tuple.Size[This] = size(self).asInstanceOf[scala.Tuple.Size[This]] + + def size(self: Tuple): Int = (self: Any) match { + case self: Unit => 0 + case self: Product => self.productArity } // Tail for Tuple1 to Tuple22 - def specialCaseTail[This <: NonEmptyTuple] (self: This): Tail[This] = { - val res = (self: Any) match { + private def specialCaseTail(self: Tuple): Tuple = { + (self: Any) match { case self: Tuple1[_] => () case self: Tuple2[_, _] => @@ -353,11 +354,10 @@ object DynamicTuple { case self: Tuple22[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => Tuple21(self._2, self._3, self._4, self._5, self._6, self._7, self._8, self._9, self._10, self._11, self._12, self._13, self._14, self._15, self._16, self._17, self._18, self._19, self._20, self._21, self._22) } - res.asInstanceOf[Tail[This]] } // Tail for TupleXXL - def xxlTail[This <: NonEmptyTuple](xxl: TupleXXL): Tail[This] = { + private def xxlTail(xxl: TupleXXL): Tuple = { if (xxl.productArity == 23) { val elems = xxl.elems Tuple22( @@ -365,30 +365,34 @@ object DynamicTuple { elems(8), elems(9), elems(10), elems(11), elems(12), elems(13), elems(14), elems(15), elems(16), elems(17), elems(18), elems(19), elems(20), elems(21), elems(22) - ).asInstanceOf[Tail[This]] + ) } else { val arr = new Array[Object](xxl.elems.length - 1) System.arraycopy(xxl.elems, 1, arr, 0, xxl.elems.length - 1) - TupleXXL.fromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Tail[This]] + TupleXXL.fromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Tuple] } } - def dynamicTail[This <: NonEmptyTuple](self: This): Tail[This] = (self: Any) match { + @deprecated("Use tail", "") + def dynamicTail[This <: NonEmptyTuple](self: This): scala.Tuple.Tail[This] = tail(self).asInstanceOf[scala.Tuple.Tail[This]] + + def tail(self: NonEmptyTuple): Tuple = (self: Any) match { case xxl: TupleXXL => xxlTail(xxl) case _ => specialCaseTail(self) } - def dynamicApply[This <: NonEmptyTuple, N <: Int] (self: This, n: N): Elem[This, N] = { - type Result = Elem[This, N] - val res = (self: Any) match { - case self: Unit => throw new IndexOutOfBoundsException(n.toString) + @deprecated("Use apply", "") + def dynamicApply[This <: NonEmptyTuple, N <: Int](self: This, n: N): scala.Tuple.Elem[This, N] = apply(self, n).asInstanceOf[scala.Tuple.Elem[This, N]] + + def apply(self: NonEmptyTuple, n: Int): Any = { + (self: Any) match { + // case self: Unit => throw new IndexOutOfBoundsException(n.toString) case self: Product => self.productElement(n) } - res.asInstanceOf[Result] } // Benchmarks showed that this is faster than doing (it1 zip it2).copyToArray(...) - def zipIterators(it1: Iterator[Any], it2: Iterator[Any], size: Int): IArray[Object] = { + private def zipIterators(it1: Iterator[Any], it2: Iterator[Any], size: Int): IArray[Object] = { val arr = new Array[Object](size) var i = 0 while (i < size) { @@ -398,89 +402,40 @@ object DynamicTuple { arr.asInstanceOf[IArray[Object]] } - def dynamicZip[This <: Tuple, T2 <: Tuple](t1: This, t2: T2): Zip[This, T2] = { + @deprecated("Use zip", "") + def dynamicZip[This <: Tuple, T2 <: Tuple](t1: This, t2: T2): scala.Tuple.Zip[This, T2] = zip(t1, t2).asInstanceOf[scala.Tuple.Zip[This, T2]] + + def zip(t1: Tuple, t2: Tuple): Tuple = { val t1Size: Int = t1.size val t2Size: Int = t2.size val size = Math.min(t1Size, t2Size) - if size == 0 then ().asInstanceOf[Zip[This, T2]] + if size == 0 then () else Tuple.fromIArray( zipIterators( t1.asInstanceOf[Product].productIterator, t2.asInstanceOf[Product].productIterator, size ) - ).asInstanceOf[Zip[This, T2]] + ) } - // Map for Tuple1 to Tuple22 - def specialCaseMap[This <: Tuple, F[_]](self: This, f: [t] => t => F[t]): Map[This, F] = { - type Result = Map[This, F] - val res = (self: Any) match { - case sekf: Unit => - () - case self: Tuple1[_] => - Tuple1(f[Object](self._1.asInstanceOf[Object])) - case self: Tuple2[_, _] => - Tuple2(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object])) - case self: Tuple3[_, _, _] => - Tuple3(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object])) - case self: Tuple4[_, _, _, _] => - Tuple4(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object])) - case self: Tuple5[_, _, _, _, _] => - Tuple5(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object])) - case self: Tuple6[_, _, _, _, _, _] => - Tuple6(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object])) - case self: Tuple7[_, _, _, _, _, _, _] => - Tuple7(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object])) - case self: Tuple8[_, _, _, _, _, _, _, _] => - Tuple8(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object])) - case self: Tuple9[_, _, _, _, _, _, _, _, _] => - Tuple9(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object])) - case self: Tuple10[_, _, _, _, _, _, _, _, _, _] => - Tuple10(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object])) - case self: Tuple11[_, _, _, _, _, _, _, _, _, _, _] => - Tuple11(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object])) - case self: Tuple12[_, _, _, _, _, _, _, _, _, _, _, _] => - Tuple12(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object])) - case self: Tuple13[_, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple13(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object])) - case self: Tuple14[_, _, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple14(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object]), f[Object](self._14.asInstanceOf[Object])) - case self: Tuple15[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple15(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object]), f[Object](self._14.asInstanceOf[Object]), f[Object](self._15.asInstanceOf[Object])) - case self: Tuple16[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple16(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object]), f[Object](self._14.asInstanceOf[Object]), f[Object](self._15.asInstanceOf[Object]), f[Object](self._16.asInstanceOf[Object])) - case self: Tuple17[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple17(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object]), f[Object](self._14.asInstanceOf[Object]), f[Object](self._15.asInstanceOf[Object]), f[Object](self._16.asInstanceOf[Object]), f[Object](self._17.asInstanceOf[Object])) - case self: Tuple18[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple18(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object]), f[Object](self._14.asInstanceOf[Object]), f[Object](self._15.asInstanceOf[Object]), f[Object](self._16.asInstanceOf[Object]), f[Object](self._17.asInstanceOf[Object]), f[Object](self._18.asInstanceOf[Object])) - case self: Tuple19[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple19(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object]), f[Object](self._14.asInstanceOf[Object]), f[Object](self._15.asInstanceOf[Object]), f[Object](self._16.asInstanceOf[Object]), f[Object](self._17.asInstanceOf[Object]), f[Object](self._18.asInstanceOf[Object]), f[Object](self._19.asInstanceOf[Object])) - case self: Tuple20[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple20(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object]), f[Object](self._14.asInstanceOf[Object]), f[Object](self._15.asInstanceOf[Object]), f[Object](self._16.asInstanceOf[Object]), f[Object](self._17.asInstanceOf[Object]), f[Object](self._18.asInstanceOf[Object]), f[Object](self._19.asInstanceOf[Object]), f[Object](self._20.asInstanceOf[Object])) - case self: Tuple21[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple21(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object]), f[Object](self._14.asInstanceOf[Object]), f[Object](self._15.asInstanceOf[Object]), f[Object](self._16.asInstanceOf[Object]), f[Object](self._17.asInstanceOf[Object]), f[Object](self._18.asInstanceOf[Object]), f[Object](self._19.asInstanceOf[Object]), f[Object](self._20.asInstanceOf[Object]), f[Object](self._21.asInstanceOf[Object])) - case self: Tuple22[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] => - Tuple22(f[Object](self._1.asInstanceOf[Object]), f[Object](self._2.asInstanceOf[Object]), f[Object](self._3.asInstanceOf[Object]), f[Object](self._4.asInstanceOf[Object]), f[Object](self._5.asInstanceOf[Object]), f[Object](self._6.asInstanceOf[Object]), f[Object](self._7.asInstanceOf[Object]), f[Object](self._8.asInstanceOf[Object]), f[Object](self._9.asInstanceOf[Object]), f[Object](self._10.asInstanceOf[Object]), f[Object](self._11.asInstanceOf[Object]), f[Object](self._12.asInstanceOf[Object]), f[Object](self._13.asInstanceOf[Object]), f[Object](self._14.asInstanceOf[Object]), f[Object](self._15.asInstanceOf[Object]), f[Object](self._16.asInstanceOf[Object]), f[Object](self._17.asInstanceOf[Object]), f[Object](self._18.asInstanceOf[Object]), f[Object](self._19.asInstanceOf[Object]), f[Object](self._20.asInstanceOf[Object]), f[Object](self._21.asInstanceOf[Object]), f[Object](self._22.asInstanceOf[Object])) - } - res.asInstanceOf[Result] - } + @deprecated("Use map", "") + def dynamicMap[This <: Tuple, F[_]](self: This, f: [t] => t => F[t]): scala.Tuple.Map[This, F] = map(self, f).asInstanceOf[scala.Tuple.Map[This, F]] - def dynamicMap[This <: Tuple, F[_]](self: This, f: [t] => t => F[t]): Map[This, F] = (self: Any) match { - case self: Unit => ().asInstanceOf[Map[This, F]] - case _ => - Tuple.fromArray(self.asInstanceOf[Product].productIterator.map(f(_)).toArray) // TODO use toIArray of Object to avoid double/triple array copy - .asInstanceOf[Map[This, F]] + def map[F[_]](self: Tuple, f: [t] => t => F[t]): Tuple = (self: Any) match { + case self: Unit => () + case _ => Tuple.fromArray(self.asInstanceOf[Product].productIterator.map(f(_)).toArray) // TODO use toIArray of Object to avoid double/triple array copy } - def dynamicTake[This <: Tuple, N <: Int](self: This, n: N): Take[This, N] = { + @deprecated("Use take", "") + def dynamicTake[This <: Tuple, N <: Int](self: This, n: N): scala.Tuple.Take[This, N] = take(self, n).asInstanceOf[scala.Tuple.Take[This, N]] + + def take(self: Tuple, n: Int): Tuple = { if (n < 0) throw new IndexOutOfBoundsException(n.toString) val selfSize: Int = self.size val actualN = Math.min(n, selfSize) - type Result = Take[This, N] - - if (actualN == 0) ().asInstanceOf[Result] + if (actualN == 0) () else { val arr = (self: Any) match { case xxl: TupleXXL => @@ -492,19 +447,20 @@ object DynamicTuple { arr } - dynamicFromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Result] + fromIArray(arr.asInstanceOf[IArray[Object]]) } } - def dynamicDrop[This <: Tuple, N <: Int](self: This, n: N): Drop[This, N] = { + @deprecated("Use drop", "") + def dynamicDrop[This <: Tuple, N <: Int](self: This, n: N): scala.Tuple.Drop[This, N] = drop(self, n).asInstanceOf[scala.Tuple.Drop[This, N]] + + def drop(self: Tuple, n: Int): Tuple = { if (n < 0) throw new IndexOutOfBoundsException(n.toString) val size = self.size val actualN = Math.min(n, size) val rem = size - actualN - type Result = Drop[This, N] - - if (rem == 0) ().asInstanceOf[Result] + if (rem == 0) () else { val arr = (self: Any) match { case xxl: TupleXXL => @@ -516,17 +472,17 @@ object DynamicTuple { arr } - dynamicFromIArray(arr.asInstanceOf[IArray[Object]]).asInstanceOf[Result] + fromIArray(arr.asInstanceOf[IArray[Object]]) } } - def dynamicSplitAt[This <: Tuple, N <: Int](self: This, n: N): Split[This, N] = { + @deprecated("Use splitAt", "") + def dynamicSplitAt[This <: Tuple, N <: Int](self: This, n: N): scala.Tuple.Split[This, N] = splitAt(self, n).asInstanceOf[scala.Tuple.Split[This, N]] + + def splitAt(self: Tuple, n: Int): (Tuple, Tuple) = { if (n < 0) throw new IndexOutOfBoundsException(n.toString) val size = self.size val actualN = Math.min(n, size) - - type Result = Split[This, N] - val (arr1, arr2) = (self: Any) match { case () => (Array.empty[Object], Array.empty[Object]) case xxl: TupleXXL => @@ -541,9 +497,9 @@ object DynamicTuple { } ( - dynamicFromIArray(arr1.asInstanceOf[IArray[Object]]), - dynamicFromIArray(arr2.asInstanceOf[IArray[Object]]) - ).asInstanceOf[Result] + fromIArray(arr1.asInstanceOf[IArray[Object]]), + fromIArray(arr2.asInstanceOf[IArray[Object]]) + ) } def consIterator(head: Any, tail: Tuple): Iterator[Any] = @@ -551,4 +507,5 @@ object DynamicTuple { def concatIterator(tup1: Tuple, tup2: Tuple): Iterator[Any] = tup1.asInstanceOf[Product].productIterator ++ tup2.asInstanceOf[Product].productIterator + } diff --git a/library/src/scala/runtime/TupleXXL.scala b/library/src/scala/runtime/TupleXXL.scala index c4d52ebcf57b..3b548a1ea99f 100644 --- a/library/src/scala/runtime/TupleXXL.scala +++ b/library/src/scala/runtime/TupleXXL.scala @@ -28,8 +28,8 @@ final class TupleXXL private (es: IArray[Object]) extends Product { def toArray: Array[Object] = es.asInstanceOf[Array[Object]].clone // TODO use IArray.toArray } object TupleXXL { - def fromIterator(elems: Iterator[Any]) = new TupleXXL(elems.map(_.asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]]) // TODO use Iterator.toIArray - def fromIArray(elems: IArray[Object]) = new TupleXXL(elems) - def apply(elems: Any*) = new TupleXXL(IArray(elems.asInstanceOf[Seq[AnyRef]]: _*)) + def fromIterator(elems: Iterator[Any]): TupleXXL = new TupleXXL(elems.map(_.asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]]) // TODO use Iterator.toIArray + def fromIArray(elems: IArray[Object]): TupleXXL = new TupleXXL(elems) + def apply(elems: Any*): TupleXXL = new TupleXXL(IArray(elems.asInstanceOf[Seq[AnyRef]]: _*)) def unapplySeq(x: TupleXXL): Option[Seq[Any]] = Some(x.elems.asInstanceOf[Array[Object]].toSeq) // TODO use IArray.toSeq } diff --git a/tests/run-staging/staged-tuples/StagedTuple.scala b/tests/run-staging/staged-tuples/StagedTuple.scala index a3fefd3cef13..984f5dcf2d0b 100644 --- a/tests/run-staging/staged-tuples/StagedTuple.scala +++ b/tests/run-staging/staged-tuples/StagedTuple.scala @@ -27,8 +27,6 @@ object StagedTuple { tup.as[Tuple3[Object, Object, Object]].bind(t => '{Array($t._1, $t._2, $t._3)}) case Some(4) => tup.as[Tuple4[Object, Object, Object, Object]].bind(t => '{Array($t._1, $t._2, $t._3, $t._4)}) - case Some(n) if n <= MaxSpecialized => - '{to$Array($tup, ${ Expr(n) })} case Some(n) => '{ ${tup.as[TupleXXL]}.toArray } case None => @@ -201,8 +199,6 @@ object StagedTuple { self.as[Tuple3[_, _, _]].bind(t => '{Tuple4($x, $t._1, $t._2, $t._3)}) case Some(4) => self.as[Tuple4[_, _, _, _]].bind(t => '{Tuple5($x, $t._1, $t._2, $t._3, $t._4)}) - case Some(n) => - fromArrayStaged[H *: T]('{cons$Array($x, ${ toArrayStaged(self, tailSize) })}, Some(n + 1)) case _ => '{dynamicCons[H, T]($x, $self)} }