Skip to content

Commit 57eec5b

Browse files
committed
Define TupleXXL using IArray
1 parent 1faa30f commit 57eec5b

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

library/src/scala/Tuple.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ sealed trait Tuple extends Any {
1313
inline def toArray: Array[Object] =
1414
DynamicTuple.dynamicToArray(this)
1515

16+
/** Create a copy this tuple as an IArray */
17+
inline def toIArray: IArray[Object] =
18+
DynamicTuple.dynamicToIArray(this)
19+
1620
/** Return a new tuple by prepending the element to `this` tuple.
1721
* This opteration is O(this.size)
1822
*/
@@ -79,6 +83,17 @@ object Tuple {
7983
DynamicTuple.dynamicFromArray[Tuple](xs2)
8084
}
8185

86+
/** Convert an immutable array into a tuple of unknown arity and types */
87+
def fromIArray[T](xs: IArray[T]): Tuple = {
88+
val xs2: IArray[Object] = xs match {
89+
case xs: IArray[Object] => xs
90+
case xs =>
91+
// TODO suport IArray.map
92+
xs.asInstanceOf[Array[T]].map(_.asInstanceOf[Object]).asInstanceOf[IArray[Object]]
93+
}
94+
DynamicTuple.dynamicFromIArray[Tuple](xs2)
95+
}
96+
8297
/** Convert a Product into a tuple of unknown arity and types */
8398
def fromProduct(product: Product): Tuple =
8499
runtime.DynamicTuple.dynamicFromProduct[Tuple](product)

library/src/scala/TupleXXL.scala

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
package scala
22
import java.util.Arrays.{deepEquals, deepHashCode}
33

4-
final class TupleXXL private (es: Array[Object]) extends Product {
4+
final class TupleXXL private (es: IArray[Object]) extends Product {
55
assert(es.length > 22)
66

77
def productElement(n: Int): Any = es(n)
88
def productArity: Int = es.length
99

10-
override def toString = elems.mkString("(", ",", ")")
11-
override def hashCode = getClass.hashCode * 41 + deepHashCode(elems)
10+
override def toString = elems.asInstanceOf[Array[Object]].mkString("(", ",", ")")
11+
override def hashCode = getClass.hashCode * 41 + deepHashCode(elems.asInstanceOf[Array[Object]])
1212
override def canEqual(that: Any): Boolean = that match {
1313
case that: TupleXXL => that.productArity == this.productArity
1414
case _ => false
1515
}
1616

1717
override def equals(that: Any) = that match {
18-
case that: TupleXXL => deepEquals(this.elems, that.elems)
18+
case that: TupleXXL => deepEquals(this.elems.asInstanceOf[Array[Object]], that.elems.asInstanceOf[Array[Object]])
1919
case _ => false
2020
}
21-
def elems: Array[Object] = es
21+
def elems: IArray[Object] = es
2222

2323
def tailXXL: TupleXXL = {
2424
assert(es.length > 23)
25-
new TupleXXL(es.tail)
25+
new TupleXXL(es.asInstanceOf[Array[Object]].tail.asInstanceOf[IArray[Object]])
2626
}
2727

28-
def toArray: Array[Object] = es.clone
28+
def toArray: Array[Object] = es.asInstanceOf[Array[Object]].clone
2929
}
3030
object TupleXXL {
31-
def fromIterator(elems: Iterator[Any]) = new TupleXXL(elems.map(_.asInstanceOf[Object]).toArray)
32-
def apply(elems: Array[Object]) = new TupleXXL(elems.clone)
33-
def apply(elems: Any*) = new TupleXXL(elems.asInstanceOf[Seq[Object]].toArray)
34-
def unapplySeq(x: TupleXXL): Option[Seq[Any]] = Some(x.elems.toSeq)
31+
def fromIterator(elems: Iterator[Any]) = new TupleXXL(elems.map(_.asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]])
32+
def fromIArray(elems: IArray[Object]) = new TupleXXL(elems)
33+
def apply(elems: Array[Object]) = new TupleXXL(elems.clone.asInstanceOf[IArray[Object]])
34+
def apply(elems: Any*) = new TupleXXL(elems.asInstanceOf[Seq[Object]].toArray.asInstanceOf[IArray[Object]])
35+
def unapplySeq(x: TupleXXL): Option[Seq[Any]] = Some(x.elems.asInstanceOf[Array[Object]].toSeq)
3536
}

library/src/scala/TupledFunction.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ package internal {
167167
}
168168

169169
def tupledFunctionXXL[F, G]: TupledFunction[F, G] = new TupledFunction {
170-
def tupled(f: F): G = ((args: TupleXXL) => f.asInstanceOf[FunctionXXL].apply(args.elems)).asInstanceOf[G]
170+
def tupled(f: F): G = ((args: TupleXXL) => f.asInstanceOf[FunctionXXL].apply(args.elems.asInstanceOf[Array[Object]])).asInstanceOf[G]
171171
def untupled(g: G): F = new FunctionXXL {
172172
override def apply(xs: Array[Object]): AnyRef = g.asInstanceOf[TupleXXL => AnyRef].apply(TupleXXL(xs))
173173
}.asInstanceOf[F]

library/src/scala/runtime/DynamicTuple.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ object DynamicTuple {
5555
case _ => TupleXXL(xs).asInstanceOf[T]
5656
}
5757

58+
def dynamicFromIArray[T <: Tuple](xs: IArray[Object]): T =
59+
if (xs.length <= 22) dynamicFromArray(xs.asInstanceOf[Array[Object]])
60+
else TupleXXL.fromIArray(xs).asInstanceOf[T]
61+
5862
def dynamicFromProduct[T <: Tuple](xs: Product): T = (xs.productArity match {
5963
case 1 =>
6064
xs match {
@@ -180,6 +184,12 @@ object DynamicTuple {
180184
case self: Product => productToArray(self)
181185
}
182186

187+
def dynamicToIArray(self: Tuple): IArray[Object] = (self: Any) match {
188+
case self: Unit => Array.emptyObjectArray.asInstanceOf[IArray[Object]]
189+
case self: TupleXXL => self.elems
190+
case self: Product => productToArray(self).asInstanceOf[IArray[Object]]
191+
}
192+
183193
def productToArray(self: Product): Array[Object] = {
184194
val arr = new Array[Object](self.productArity)
185195
for (i <- 0 until arr.length) arr(i) = self.productElement(i).asInstanceOf[Object]

0 commit comments

Comments
 (0)