-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Define TupleXXL and FunctionXXL using IArray #6929
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
Changes from 1 commit
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,35 +1,35 @@ | ||
package scala | ||
import java.util.Arrays.{deepEquals, deepHashCode} | ||
|
||
final class TupleXXL private (es: Array[Object]) extends Product { | ||
final class TupleXXL private (es: IArray[Object]) extends Product { | ||
assert(es.length > 22) | ||
|
||
def productElement(n: Int): Any = es(n) | ||
def productArity: Int = es.length | ||
|
||
override def toString = elems.mkString("(", ",", ")") | ||
override def hashCode = getClass.hashCode * 41 + deepHashCode(elems) | ||
override def toString = elems.asInstanceOf[Array[Object]].mkString("(", ",", ")") | ||
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. After the bound on |
||
override def hashCode = getClass.hashCode * 41 + deepHashCode(elems.asInstanceOf[Array[Object]]) | ||
override def canEqual(that: Any): Boolean = that match { | ||
case that: TupleXXL => that.productArity == this.productArity | ||
case _ => false | ||
} | ||
|
||
override def equals(that: Any) = that match { | ||
case that: TupleXXL => deepEquals(this.elems, that.elems) | ||
case that: TupleXXL => deepEquals(this.elems.asInstanceOf[Array[Object]], that.elems.asInstanceOf[Array[Object]]) | ||
case _ => false | ||
} | ||
def elems: Array[Object] = es | ||
def elems: IArray[Object] = es | ||
|
||
def tailXXL: TupleXXL = { | ||
assert(es.length > 23) | ||
new TupleXXL(es.tail) | ||
new TupleXXL(es.asInstanceOf[Array[Object]].tail.asInstanceOf[IArray[Object]]) | ||
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. It would be nice to support 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. Adding WrappedIArray would do it. Also imlementing all ArrayOps on IArray would help |
||
} | ||
|
||
def toArray: Array[Object] = es.clone | ||
def toArray: Array[Object] = es.asInstanceOf[Array[Object]].clone | ||
} | ||
object TupleXXL { | ||
def fromIterator(elems: Iterator[Any]) = new TupleXXL(elems.map(_.asInstanceOf[Object]).toArray) | ||
def apply(elems: Array[Object]) = new TupleXXL(elems.clone) | ||
def apply(elems: Any*) = new TupleXXL(elems.asInstanceOf[Seq[Object]].toArray) | ||
def unapplySeq(x: TupleXXL): Option[Seq[Any]] = Some(x.elems.toSeq) | ||
def fromIterator(elems: Iterator[Any]) = new TupleXXL(elems.map(_.asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]]) | ||
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. It's better to avoid 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.
|
||
def fromIArray(elems: IArray[Object]) = new TupleXXL(elems) | ||
def apply(elems: Any*) = new TupleXXL(elems.asInstanceOf[Seq[Object]].toArray.asInstanceOf[IArray[Object]]) | ||
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. It's better to create |
||
def unapplySeq(x: TupleXXL): Option[Seq[Any]] = Some(x.elems.asInstanceOf[Array[Object]].toSeq) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,9 +52,13 @@ object DynamicTuple { | |
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] | ||
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] | ||
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] | ||
case _ => TupleXXL(xs).asInstanceOf[T] | ||
case _ => TupleXXL.fromIArray(xs.clone().asInstanceOf[IArray[Object]]).asInstanceOf[T] | ||
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. It's better to create IArray through its constructors instead of cast. 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. Good point. It might also be good to have a |
||
} | ||
|
||
def dynamicFromIArray[T <: Tuple](xs: IArray[Object]): T = | ||
if (xs.length <= 22) dynamicFromArray(xs.asInstanceOf[Array[Object]]) | ||
else TupleXXL.fromIArray(xs).asInstanceOf[T] | ||
|
||
def dynamicFromProduct[T <: Tuple](xs: Product): T = (xs.productArity match { | ||
case 1 => | ||
xs match { | ||
|
@@ -169,7 +173,7 @@ object DynamicTuple { | |
case _ => | ||
xs match { | ||
case xs: TupleXXL => xs | ||
case xs => TupleXXL(xs.productIterator.map(_.asInstanceOf[Object]).toArray) | ||
case xs => TupleXXL.fromIArray(xs.productIterator.map(_.asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]]) | ||
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. It's better to create IArray through its constructors instead of cast. 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. Same as before, need |
||
} | ||
}).asInstanceOf[T] | ||
|
||
|
@@ -180,6 +184,12 @@ object DynamicTuple { | |
case self: Product => productToArray(self) | ||
} | ||
|
||
def dynamicToIArray(self: Tuple): IArray[Object] = (self: Any) match { | ||
case self: Unit => Array.emptyObjectArray.asInstanceOf[IArray[Object]] | ||
case self: TupleXXL => self.elems | ||
case self: Product => productToArray(self).asInstanceOf[IArray[Object]] | ||
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. It's better to create IArray through its constructors instead of cast. 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. The array constructor would create a new empty array each time |
||
} | ||
|
||
def productToArray(self: Product): Array[Object] = { | ||
val arr = new Array[Object](self.productArity) | ||
for (i <- 0 until arr.length) arr(i) = self.productElement(i).asInstanceOf[Object] | ||
|
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.
Will a bound on
IArray
work, likeopaque type IArray[+T] <: Seq[T] = Array[_ <: T]
?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.
Probably not. We need to do the same as array does and define a WrappedIArray for it.