summaryrefslogtreecommitdiff
path: root/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedArray.scala
diff options
context:
space:
mode:
Diffstat (limited to 'examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedArray.scala')
-rw-r--r--examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedArray.scala92
1 files changed, 92 insertions, 0 deletions
diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedArray.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedArray.scala
new file mode 100644
index 0000000..3626c15
--- /dev/null
+++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedArray.scala
@@ -0,0 +1,92 @@
+/* __ *\
+** ________ ___ / / ___ __ ____ Scala.js API **
+** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | |__/ /____/ **
+** |/____/ **
+\* */
+
+package scala.scalajs.js
+
+import scala.language.implicitConversions
+
+import scala.collection.mutable
+import mutable.Builder
+
+import scala.collection.generic.{CanBuildFrom, GenericCompanion, SeqFactory}
+
+/** Equivalent of scm.WrappedArray for js.Array */
+@inline
+final class WrappedArray[A](val array: Array[A])
+ extends mutable.AbstractBuffer[A]
+ with scala.collection.generic.GenericTraversableTemplate[A, WrappedArray]
+ with mutable.IndexedSeq[A]
+ with mutable.BufferLike[A, WrappedArray[A]]
+ with mutable.ArrayLike[A, WrappedArray[A]]
+ with Builder[A, WrappedArray[A]] {
+
+ /** Creates a new empty [[WrappedArray]]. */
+ def this() = this(Array())
+
+ override def companion: GenericCompanion[WrappedArray] = WrappedArray
+
+ // IndexedSeq interface
+
+ @inline def update(index: Int, elem: A): Unit = array(index) = elem
+ @inline def apply(index: Int): A = array(index)
+ @inline def length: Int = array.length
+
+ // Builder interface
+
+ @inline def +=(elem: A): this.type = {
+ array.push(elem)
+ this
+ }
+
+ @inline def clear(): Unit =
+ array.length = 0
+
+ @inline def result(): WrappedArray[A] = this
+
+ // Rest of BufferLike interface
+
+ @inline def +=:(elem: A): this.type = {
+ array.unshift(elem)
+ this
+ }
+
+ @inline override def ++=:(xs: TraversableOnce[A]): this.type = {
+ array.unshift(xs.toSeq: _*)
+ this
+ }
+
+ @inline def insertAll(n: Int,
+ elems: scala.collection.Traversable[A]): Unit = {
+ array.splice(n, 0, elems.toSeq: _*)
+ }
+
+ @inline def remove(n: Int): A =
+ array.splice(n, 1)(0)
+
+ @inline override def remove(n: Int, count: Int): Unit =
+ array.splice(n, count)
+
+ @inline override def stringPrefix: String = "WrappedArray"
+
+}
+
+/** $factoryInfo
+ * @define coll wrapped array
+ * @define Coll `WrappedArray`
+ */
+object WrappedArray extends SeqFactory[WrappedArray] {
+ /** $genericCanBuildFromInfo */
+ implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, WrappedArray[A]] =
+ ReusableCBF.asInstanceOf[GenericCanBuildFrom[A]]
+
+ def newBuilder[A]: Builder[A, WrappedArray[A]] = new WrappedArray[A]
+
+ implicit def toJSArray[A](wrappedArray: WrappedArray[A]): Array[A] =
+ wrappedArray.array
+
+}