-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 5 commits
d8dfef6
ebd5f2b
ab1a19f
ebf8d8c
a1d8b66
5006b2f
cb2f91f
6aef25a
ae14dab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -447,6 +449,79 @@ object Tuples { | |
} | ||
} | ||
|
||
// Reverse for TupleXXL | ||
private def xxlReverse(xxl: TupleXXL): Tuple = { | ||
if (xxl.productArity == 22) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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+ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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"), | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
ProblemFilters.exclude[MissingFieldProblem]("scala.Tuple.Helpers"), | ||||
// end of New experimental features in 3.3.X | ||||
|
||||
// Added java.io.Serializable as LazyValControlState supertype | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will be affected when you move |
||
"scala.Tuple$.Helpers", | ||
"scala.Tuple$.Helpers$", | ||
"scala.Tuple$.Helpers$.ReverseImpl", | ||
"scala.Tuple$.Reverse", | ||
"scala.runtime.Tuples$.reverse" | ||
) | ||
|
||
|
||
|
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) |
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) | ||
} | ||
} |
There was a problem hiding this comment.
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 inNonEmptyTuple