1
1
package scala
2
2
import annotation .showAsInfix
3
3
import typelevel ._
4
+ import scala .StagedTuple
4
5
5
6
sealed trait Tuple extends Any {
6
7
import Tuple ._
7
8
8
9
inline def toArray : Array [Object ] =
9
- if (specialize)
10
- inline constValueOpt[BoundedSize [this .type ]] match {
10
+ if (stageIt) stagedToArray
11
+ else inline constValueOpt[BoundedSize [this .type ]] match {
11
12
case Some (0 ) =>
12
13
$emptyArray
13
14
case Some (1 ) =>
@@ -29,10 +30,13 @@ sealed trait Tuple extends Any {
29
30
case None =>
30
31
dynamicToArray(this )
31
32
}
32
- else dynamicToArray(this )
33
+
34
+ inline def stagedToArray : Array [Object ] =
35
+ ~ StagedTuple .toArrayStaged('(this), constValueOpt[BoundedSize[this.type]])
33
36
34
37
inline def *: [H ] (x : H ): H *: this .type =
35
- if (specialize) {
38
+ if (stageIt) stagedCons[H ](x)
39
+ else {
36
40
type Result = H *: this .type
37
41
inline constValueOpt[BoundedSize [this .type ]] match {
38
42
case Some (0 ) =>
@@ -54,10 +58,13 @@ sealed trait Tuple extends Any {
54
58
dynamic_*: [this .type , H ](this , x)
55
59
}
56
60
}
57
- else dynamic_*: [this .type , H ](this , x)
61
+
62
+ inline def stagedCons [H ] (x : H ): H *: this .type =
63
+ ~ StagedTuple .stagedCons('(this), ' (x), constValueOpt[BoundedSize [this .type ]])
58
64
59
65
inline def ++ (that : Tuple ): Concat [this .type , that.type ] =
60
- if (specialize) {
66
+ if (stageIt) stagedConcat(that)
67
+ else {
61
68
type Result = Concat [this .type , that.type ]
62
69
inline constValueOpt[BoundedSize [this .type ]] match {
63
70
case Some (0 ) =>
@@ -95,27 +102,33 @@ sealed trait Tuple extends Any {
95
102
dynamic_++ [this .type , that.type ](this , that)
96
103
}
97
104
}
98
- else dynamic_++ [this .type , that.type ](this , that)
105
+
106
+ inline def stagedConcat (that : Tuple ): Concat [this .type , that.type ] =
107
+ ~ StagedTuple .stagedConcat('(this), constValueOpt[BoundedSize[this.type]],
108
+ '(that), constValueOpt[BoundedSize[that.type]])
99
109
100
110
inline def genericConcat [T <: Tuple ](xs : Tuple , ys : Tuple ): Tuple =
101
111
fromArray[T ](xs.toArray ++ ys.toArray)
102
112
103
113
inline def size : Size [this .type ] =
104
- if (specialize) {
114
+ if (stageIt) stagedSize
115
+ else {
105
116
type Result = Size [this .type ]
106
117
inline constValueOpt[BoundedSize [this .type ]] match {
107
118
case Some (n) => n.asInstanceOf [Result ]
108
119
case _ => dynamicSize(this )
109
120
}
110
121
}
111
- else dynamicSize(this )
122
+
123
+ inline def stagedSize : Size [this .type ] =
124
+ ~ StagedTuple .sizeStaged[Size [this .type ]]('(this), constValueOpt[BoundedSize[this.type]])
112
125
}
113
126
114
127
object Tuple {
115
128
inline val $MaxSpecialized = 22
116
129
inline private val XXL = $MaxSpecialized + 1
117
130
118
- final val specialize = true
131
+ final val stageIt = false
119
132
120
133
type Head [+ X <: NonEmptyTuple ] = X match {
121
134
case x *: _ => x
@@ -175,34 +188,36 @@ object Tuple {
175
188
}
176
189
177
190
inline def fromArray [T <: Tuple ](xs : Array [Object ]): T =
178
- if (specialize)
179
- inline constValue[BoundedSize [T ]] match {
180
- case 0 => ().asInstanceOf [T ]
181
- case 1 => Tuple1 (xs(0 )).asInstanceOf [T ]
182
- case 2 => Tuple2 (xs(0 ), xs(1 )).asInstanceOf [T ]
183
- case 3 => Tuple3 (xs(0 ), xs(1 ), xs(2 )).asInstanceOf [T ]
184
- case 4 => Tuple4 (xs(0 ), xs(1 ), xs(2 ), xs(3 )).asInstanceOf [T ]
185
- case 5 => Tuple5 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 )).asInstanceOf [T ]
186
- case 6 => Tuple6 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 )).asInstanceOf [T ]
187
- case 7 => Tuple7 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 )).asInstanceOf [T ]
188
- case 8 => Tuple8 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 )).asInstanceOf [T ]
189
- case 9 => Tuple9 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 )).asInstanceOf [T ]
190
- 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 ]
191
- 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 ]
192
- 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 ]
193
- 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 ]
194
- 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 ]
195
- 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 ]
196
- 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 ]
197
- 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 ]
198
- 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 ]
199
- 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 ]
200
- 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 ]
201
- 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 ]
202
- 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 ]
203
- case _ => TupleXXL (xs).asInstanceOf [T ]
204
- }
205
- else dynamicFromArray[T ](xs)
191
+ if (stageIt) stagedFromArray[T ](xs)
192
+ else inline constValue[BoundedSize [T ]] match {
193
+ case 0 => ().asInstanceOf [T ]
194
+ case 1 => Tuple1 (xs(0 )).asInstanceOf [T ]
195
+ case 2 => Tuple2 (xs(0 ), xs(1 )).asInstanceOf [T ]
196
+ case 3 => Tuple3 (xs(0 ), xs(1 ), xs(2 )).asInstanceOf [T ]
197
+ case 4 => Tuple4 (xs(0 ), xs(1 ), xs(2 ), xs(3 )).asInstanceOf [T ]
198
+ case 5 => Tuple5 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 )).asInstanceOf [T ]
199
+ case 6 => Tuple6 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 )).asInstanceOf [T ]
200
+ case 7 => Tuple7 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 )).asInstanceOf [T ]
201
+ case 8 => Tuple8 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 )).asInstanceOf [T ]
202
+ case 9 => Tuple9 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 )).asInstanceOf [T ]
203
+ 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 ]
204
+ 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 ]
205
+ 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 ]
206
+ 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 ]
207
+ 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 ]
208
+ 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 ]
209
+ 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 ]
210
+ 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 ]
211
+ 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 ]
212
+ 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 ]
213
+ 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 ]
214
+ 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 ]
215
+ 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 ]
216
+ case _ => TupleXXL (xs).asInstanceOf [T ]
217
+ }
218
+
219
+ inline def stagedFromArray [T <: Tuple ](xs : Array [Object ]): T =
220
+ ~ StagedTuple .fromArrayStaged[T ]('(xs), constValueOpt[BoundedSize[this.type]])
206
221
207
222
def dynamicFromArray [T <: Tuple ](xs : Array [Object ]): T = xs.length match {
208
223
case 0 => ().asInstanceOf [T ]
@@ -297,7 +312,8 @@ abstract sealed class NonEmptyTuple extends Tuple {
297
312
import NonEmptyTuple ._
298
313
299
314
inline def head : Head [this .type ] =
300
- if (specialize) {
315
+ if (stageIt) stagedHead
316
+ else {
301
317
type Result = Head [this .type ]
302
318
val resVal = inline constValueOpt[BoundedSize [this .type ]] match {
303
319
case Some (1 ) =>
@@ -322,10 +338,13 @@ abstract sealed class NonEmptyTuple extends Tuple {
322
338
}
323
339
resVal.asInstanceOf [Result ]
324
340
}
325
- else dynamicHead[this .type ](this )
341
+
342
+ inline def stagedHead : Head [this .type ] =
343
+ ~ StagedTuple .headStaged[this .type ]('(this), constValueOpt[BoundedSize[this.type]])
326
344
327
345
inline def tail : Tail [this .type ] =
328
- if (specialize) {
346
+ if (stageIt) stagedTail
347
+ else {
329
348
type Result = Tail [this .type ]
330
349
inline constValueOpt[BoundedSize [this .type ]] match {
331
350
case Some (1 ) =>
@@ -348,7 +367,9 @@ abstract sealed class NonEmptyTuple extends Tuple {
348
367
dynamicTail[this .type ](this )
349
368
}
350
369
}
351
- else dynamicTail[this .type ](this )
370
+
371
+ inline def stagedTail : Tail [this .type ] =
372
+ ~ StagedTuple .tailStaged[this .type ]('(this), constValueOpt[BoundedSize[this.type]])
352
373
353
374
inline def fallbackApply (n : Int ) =
354
375
inline constValueOpt[n.type ] match {
@@ -357,7 +378,8 @@ abstract sealed class NonEmptyTuple extends Tuple {
357
378
}
358
379
359
380
inline def apply (n : Int ): Elem [this .type , n.type ] =
360
- if (specialize) {
381
+ if (stageIt) stagedApply(n)
382
+ else {
361
383
type Result = Elem [this .type , n.type ]
362
384
inline constValueOpt[Size [this .type ]] match {
363
385
case Some (1 ) =>
@@ -405,7 +427,11 @@ abstract sealed class NonEmptyTuple extends Tuple {
405
427
case _ => fallbackApply(n).asInstanceOf [Result ]
406
428
}
407
429
}
408
- else dynamicApply[this .type ](this , n)
430
+
431
+ inline def stagedApply (n : Int ): Elem [this .type , n.type ] =
432
+ ~ StagedTuple .applyStaged[this .type , n.type ](
433
+ '(this), constValueOpt[Size[this.type]],
434
+ '(n), constValueOpt[n.type])
409
435
}
410
436
411
437
object NonEmptyTuple {
0 commit comments