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 all 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
23 changes: 21 additions & 2 deletions 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 @@ -78,6 +78,13 @@ sealed trait Tuple extends Product {
*/
inline def splitAt[This >: this.type <: Tuple](n: Int): Split[This, n.type] =
runtime.Tuples.splitAt(this, n).asInstanceOf[Split[This, n.type]]

/** 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] =
runtime.Tuples.reverse(this).asInstanceOf[Reverse[This]]
}

object Tuple {
Expand Down Expand Up @@ -193,6 +200,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 @@ -297,7 +317,6 @@ sealed trait NonEmptyTuple extends Tuple {
*/
inline def tail[This >: this.type <: NonEmptyTuple]: Tail[This] =
runtime.Tuples.tail(this).asInstanceOf[Tail[This]]

}

@showAsInfix
Expand Down
64 changes: 64 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,68 @@ object Tuples {
}
}

// Reverse for TupleXXL
private def xxlReverse(xxl: TupleXXL): Tuple =
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 @@ -13,6 +13,8 @@ object MiMaFilters {
ProblemFilters.exclude[MissingClassProblem]("scala.runtime.stdLibPatches.language$experimental$clauseInterleaving$"),
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
)
val TastyCore: Seq[ProblemFilter] = Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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.Tuple.reverse",
"scala.Tuple$.Helpers",
"scala.Tuple$.Helpers$",
"scala.Tuple$.Helpers$.ReverseImpl",
"scala.Tuple$.Reverse",
"scala.runtime.Tuples$.reverse"
)


Expand Down
78 changes: 78 additions & 0 deletions tests/run-deep-subtype/Tuple-reverse.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
(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)
73 changes: 73 additions & 0 deletions tests/run-deep-subtype/Tuple-reverse.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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)

val tuple: Tuple3[Int, Boolean, String] = (1, true, "hello")
val reversedTuple: Tuple3[String, Boolean, Int] = tuple.reverse

assert(reversedTuple == ("hello", true, 1))

println(EmptyTuple.reverse)
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(Tuple().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)
}
}