diff options
author | Martin Odersky <odersky@gmail.com> | 2012-02-15 15:33:09 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2012-02-15 15:36:32 +0100 |
commit | 280192f2c5e92f9f8fb3238f1b4bfdf2566e6fdf (patch) | |
tree | 0d2ed2836614f967f4cdc6e341197c05b09e2e43 /src/library | |
parent | cd6426a8c501eda2105196fc5a254d8f0dd77633 (diff) | |
download | scala-280192f2c5e92f9f8fb3238f1b4bfdf2566e6fdf.tar.gz scala-280192f2c5e92f9f8fb3238f1b4bfdf2566e6fdf.tar.bz2 scala-280192f2c5e92f9f8fb3238f1b4bfdf2566e6fdf.zip |
New scheme for "Rows" of value classes.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Boxed.scala | 18 | ||||
-rw-r--r-- | src/library/scala/collection/generic/RowFactory.scala | 49 |
2 files changed, 67 insertions, 0 deletions
diff --git a/src/library/scala/Boxed.scala b/src/library/scala/Boxed.scala new file mode 100644 index 0000000000..6055b3f436 --- /dev/null +++ b/src/library/scala/Boxed.scala @@ -0,0 +1,18 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala + +/** A trait that can be implemented by user-defined value classes + */ +trait Boxed[Unboxed] extends Any { + + /** The underlying value wrapped by the value class */ + def unbox: Unboxed +} + diff --git a/src/library/scala/collection/generic/RowFactory.scala b/src/library/scala/collection/generic/RowFactory.scala new file mode 100644 index 0000000000..c1559c9397 --- /dev/null +++ b/src/library/scala/collection/generic/RowFactory.scala @@ -0,0 +1,49 @@ +package scala.collection +package generic + +import mutable.{Builder, ArrayBuffer} +import scala.specialized + +/** A factory class for rows -- flat arrays of value classes that use the unboxed representation + * of the element type. + * + */ +abstract class RowFactory[@specialized Unboxed: ClassManifest, Boxed <: scala.Boxed[Unboxed]] extends (Unboxed => Boxed) { box => + + /** Convert to boxed representation + */ + def apply(x: Unboxed): Boxed + + class Row(elems: Array[Unboxed]) extends mutable.IndexedSeq[Boxed] with mutable.IndexedSeqLike[Boxed, Row] { + + override protected[this] def newBuilder: Builder[Boxed, Row] = Row.newBuilder + + def apply(idx: Int): Boxed = box(elems(idx)) + + def update(idx: Int, elem: Boxed): Unit = elems(idx) = elem.unbox + + def length = elems.length + + override def foreach[U](f: Boxed => U): Unit = elems foreach (elem => f(box(elem))) + + } + + object Row { + def fromSeq(elems: Seq[Boxed]): Row = { + val xs: Array[Unboxed] = new Array[Unboxed](elems.length) + var i = 0 + for (elem <- elems) { xs(i) = elem.unbox; i += 1 } + new Row(xs) + } + + def apply(elems: Boxed*) = fromSeq(elems) + + def newBuilder: Builder[Boxed, Row] = new ArrayBuffer mapResult fromSeq + + implicit def canBuildFrom: CanBuildFrom[Row, Boxed, Row] = + new CanBuildFrom[Row, Boxed, Row] { + def apply(): Builder[Boxed, Row] = newBuilder + def apply(from: Row): Builder[Boxed, Row] = newBuilder + } + } +} |