Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reverse method to NonEmptyTuple #13752

Merged
merged 9 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scala

import annotation.showAsInfix
import annotation.{experimental, showAsInfix}
import compiletime._
import compiletime.ops.int._

Expand Down Expand Up @@ -193,6 +193,19 @@ object Tuple {
*/
type IsMappedBy[F[_]] = [X <: Tuple] =>> X =:= Map[InverseMap[X, F], F]

/** Type of the reversed tuple */
@experimental
type Reverse[X <: Tuple] = Helpers.ReverseImpl[EmptyTuple, X]

@experimental
object Helpers:

/** Type of the reversed tuple */
@experimental
type ReverseImpl[Acc <: Tuple, X <: Tuple] <: Tuple = X match
case x *: xs => ReverseImpl[x *: Acc, xs]
case EmptyTuple => Acc

/** Transforms a tuple `(T1, ..., Tn)` into `(T1, ..., Ti)`. */
type Take[T <: Tuple, N <: Int] <: Tuple = N match {
case 0 => EmptyTuple
Expand Down Expand Up @@ -298,6 +311,12 @@ sealed trait NonEmptyTuple extends Tuple {
inline def tail[This >: this.type <: NonEmptyTuple]: Tail[This] =
runtime.Tuples.tail(this).asInstanceOf[Tail[This]]

/** Given a tuple `(a1, ..., am)`, returns the reversed tuple `(am, ..., a1)`
* consisting all its elements.
*/
@experimental
inline def reverse[This >: this.type <: Tuple]: Reverse[This] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should also be moved to the Tuple trait, currently it is in NonEmptyTuple

runtime.Tuples.reverse(this).asInstanceOf[Reverse[This]]
}

@showAsInfix
Expand Down
75 changes: 75 additions & 0 deletions library/src/scala/runtime/Tuples.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package scala.runtime

import scala.annotation.experimental

object Tuples {

inline val MaxSpecialized = 22
Expand Down Expand Up @@ -447,6 +449,79 @@ object Tuples {
}
}

// Reverse for TupleXXL
private def xxlReverse(xxl: TupleXXL): Tuple = {
if (xxl.productArity == 22) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this branch should be removed - TupleXXL should not be constructed unless its arity is 23+

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, indeed. Good call, thanks

val elems = xxl.elems
Tuple22(
elems(21), elems(20), elems(19), elems(18), elems(17), elems(16),
elems(15), elems(14), elems(13), elems(12), elems(11), elems(10),
elems(9), elems(8), elems(7), elems(6), elems(5), elems(4),
elems(3), elems(2), elems(1), elems(0)
)
} else {
TupleXXL.fromIArray(xxl.elems.reverse.asInstanceOf[IArray[Object]]).asInstanceOf[Tuple]
}
}

// Reverse for Tuple0 to Tuple22
private def specialCaseReverse(self: Tuple): Tuple = {
(self: Any) match {
case EmptyTuple =>
EmptyTuple
case self: Tuple1[_] =>
self
case self: Tuple2[_, _] =>
Tuple2(self._2, self._1)
case self: Tuple3[_, _, _] =>
Tuple3(self._3, self._2, self._1)
case self: Tuple4[_, _, _, _] =>
Tuple4(self._4, self._3, self._2, self._1)
case self: Tuple5[_, _, _, _, _] =>
Tuple5(self._5, self._4, self._3, self._2, self._1)
case self: Tuple6[_, _, _, _, _, _] =>
Tuple6(self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple7[_, _, _, _, _, _, _] =>
Tuple7(self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple8[_, _, _, _, _, _, _, _] =>
Tuple8(self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple9[_, _, _, _, _, _, _, _, _] =>
Tuple9(self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple10[_, _, _, _, _, _, _, _, _, _] =>
Tuple10(self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple11[_, _, _, _, _, _, _, _, _, _, _] =>
Tuple11(self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple12[_, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple12(self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple13[_, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple13(self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple14[_, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple14(self._14, self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple15[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple15(self._15, self._14, self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple16[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple16(self._16, self._15, self._14, self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple17[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple17(self._17, self._16, self._15, self._14, self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple18[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple18(self._18, self._17, self._16, self._15, self._14, self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple19[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple19(self._19, self._18, self._17, self._16, self._15, self._14, self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple20[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple20(self._20, self._19, self._18, self._17, self._16, self._15, self._14, self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple21[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple21(self._21, self._20, self._19, self._18, self._17, self._16, self._15, self._14, self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
case self: Tuple22[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =>
Tuple22(self._22, self._21, self._20, self._19, self._18, self._17, self._16, self._15, self._14, self._13, self._12, self._11, self._10, self._9, self._8, self._7, self._6, self._5, self._4, self._3, self._2, self._1)
}
}

@experimental
def reverse(self: Tuple): Tuple = (self: Any) match {
case xxl: TupleXXL => xxlReverse(xxl)
case _ => specialCaseReverse(self)
}

// Init for Tuple1 to Tuple22
private def specialCaseInit(self: Tuple): Tuple = {
(self: Any) match {
Expand Down
2 changes: 2 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ object MiMaFilters {
ProblemFilters.exclude[MissingClassProblem]("scala.runtime.stdLibPatches.language$experimental$into$"),
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.stdLibPatches.language#experimental.relaxedExtensionImports"),
ProblemFilters.exclude[MissingClassProblem]("scala.runtime.stdLibPatches.language$experimental$relaxedExtensionImports$"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.reverse"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.reverse"),

ProblemFilters.exclude[MissingFieldProblem]("scala.Tuple.Helpers"),
// end of New experimental features in 3.3.X

// Added java.io.Serializable as LazyValControlState supertype
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ val experimentalDefinitionInLibrary = Set(
"scala.quoted.Quotes.reflectModule.MethodTypeMethods.hasErasedParams",
"scala.quoted.Quotes.reflectModule.TermParamClauseMethods.erasedArgs",
"scala.quoted.Quotes.reflectModule.TermParamClauseMethods.hasErasedArgs",
"scala.quoted.Quotes.reflectModule.defnModule.ErasedFunctionClass"
"scala.quoted.Quotes.reflectModule.defnModule.ErasedFunctionClass",

// New feature: reverse method on Tuple
"scala.NonEmptyTuple.reverse",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be affected when you move reverse to Tuple

"scala.Tuple$.Helpers",
"scala.Tuple$.Helpers$",
"scala.Tuple$.Helpers$.ReverseImpl",
"scala.Tuple$.Reverse",
"scala.runtime.Tuples$.reverse"
)


Expand Down
76 changes: 76 additions & 0 deletions tests/run-deep-subtype/Tuple-reverse.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
(0)
(0,0)
(1,0,0)
(2,1,0,0)
(3,2,1,0,0)
(4,3,2,1,0,0)
(5,4,3,2,1,0,0)
(6,5,4,3,2,1,0,0)
(7,6,5,4,3,2,1,0,0)
(8,7,6,5,4,3,2,1,0,0)
(9,8,7,6,5,4,3,2,1,0,0)
(10,9,8,7,6,5,4,3,2,1,0,0)
(11,10,9,8,7,6,5,4,3,2,1,0,0)
(12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,0)
(1)
(2,1)
(3,2,1)
(4,3,2,1)
(5,4,3,2,1)
(6,5,4,3,2,1)
(7,6,5,4,3,2,1)
(8,7,6,5,4,3,2,1)
(9,8,7,6,5,4,3,2,1)
(10,9,8,7,6,5,4,3,2,1)
(11,10,9,8,7,6,5,4,3,2,1)
(12,11,10,9,8,7,6,5,4,3,2,1)
(13,12,11,10,9,8,7,6,5,4,3,2,1)
(14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(1)
(2,1)
(3,2,1)
(4,3,2,1)
(5,4,3,2,1)
(6,5,4,3,2,1)
(7,6,5,4,3,2,1)
(8,7,6,5,4,3,2,1)
(9,8,7,6,5,4,3,2,1)
(10,9,8,7,6,5,4,3,2,1)
(11,10,9,8,7,6,5,4,3,2,1)
(12,11,10,9,8,7,6,5,4,3,2,1)
(13,12,11,10,9,8,7,6,5,4,3,2,1)
(14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
(25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
66 changes: 66 additions & 0 deletions tests/run-deep-subtype/Tuple-reverse.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import scala.reflect.ClassTag

object Test {
def main(args: Array[String]): Unit = {

def testArray[T: ClassTag](n: Int, elem: Int => T): Unit = {
val t: Int *: Tuple = 0 *: Tuple.fromArray(Array.tabulate(n)(elem))
println(t.reverse)
}

for (i <- 0 to 25)
testArray(i, j => j)

println(Tuple1(1).reverse)
bishabosha marked this conversation as resolved.
Show resolved Hide resolved
println((1, 2).reverse)
println((1, 2, 3).reverse)
println((1, 2, 3, 4).reverse)
println((1, 2, 3, 4, 5).reverse)
println((1, 2, 3, 4, 5, 6).reverse)
println((1, 2, 3, 4, 5, 6, 7).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24).reverse)
println((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25).reverse)

println((1 *: Tuple()).reverse)
println((1 *: 2 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: 18 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: 18 *: 19 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: 18 *: 19 *: 20 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: 18 *: 19 *: 20 *: 21 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: 18 *: 19 *: 20 *: 21 *: 22 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: 18 *: 19 *: 20 *: 21 *: 22 *: 23 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: 18 *: 19 *: 20 *: 21 *: 22 *: 23 *: 24 *: Tuple()).reverse)
println((1 *: 2 *: 3 *: 4 *: 5 *: 6 *: 7 *: 8 *: 9 *: 10 *: 11 *: 12 *: 13 *: 14 *: 15 *: 16 *: 17 *: 18 *: 19 *: 20 *: 21 *: 22 *: 23 *: 24 *: 25 *: Tuple()).reverse)
}
}