array.scala
来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 490 行 · 第 1/2 页
SCALA
490 行
/* __ *\** ________ ___ / / ___ Scala API **** / __/ __// _ | / / / _ | (c) 2002-2008, LAMP/EPFL **** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **** /____/\___/_/ |_/____/_/ | | **** |/ **\* */// $Id: Array.scala 14726 2008-04-21 10:14:06Z odersky $package scalaimport Predef._import compat.Platform.arraycopy/** This object contains utility methods operating on arrays. * * @author Martin Odersky * @version 1.0 */object Array { /** Copy one array to another. * Equivalent to * <code>System.arraycopy(src, srcPos, dest, destPos, length)</code>, * except that this works also for polymorphic and boxed arrays. * * @param src ... * @param srcPos ... * @param dest ... * @param destPos ... * @param length ... */ def copy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int) { src match { case xs: runtime.BoxedArray => xs.copyTo(srcPos, dest, destPos, length) case _ => dest match { case xs: runtime.BoxedArray => xs.copyFrom(src, srcPos, destPos, length) case _ => arraycopy(src, srcPos, dest, destPos, length) } } } /** Concatenate all argument sequences into a single array. * * @param xs the given argument sequences * @return the array created from the concatenated arguments */ def concat[T](xs: Seq[T]*): Array[T] = { var len = 0 for (x <- xs) len += x.length val result = new Array[T](len) var start = 0 for (x <- xs) { copy(x.toArray, 0, result, start, x.length) start += x.length } result } /** Create a an array containing of successive integers. * * @param from the value of the first element of the array * @param end the value of the last element fo the array plus 1 * @return the sorted array of all integers in range [from;end). */ def range(start: Int, end: Int): Array[Int] = { val result = new Array[Int](end - start) for (i <- start until end) result(i - start) = i result } /** Create an array with given elements. * * @param xs the elements to put in the array * @return the array containing elements xs. */ def apply[A <: AnyRef](xs: A*): Array[A] = { val array = new Array[A](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array }/* The following metod clashes with the previous one, and has therefore been * removed. Note that this is a choice between efficiency and generality. * The previous factory method is more efficient than the one that has been * commented out. Since it is anyway possible to create a polymorphic array * using * new Array[T] * it was preferred to restrict the definition of the factory method. def Array[A](xs: A*): Array[A] = { val array = new Array[A](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array }*/ def apply(xs: Boolean*): Array[Boolean] = { val array = new Array[Boolean](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } def apply(xs: Byte*): Array[Byte] = { val array = new Array[Byte](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } def apply(xs: Short*): Array[Short] = { val array = new Array[Short](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } def apply(xs: Char*): Array[Char] = { val array = new Array[Char](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } def apply(xs: Int*): Array[Int] = { val array = new Array[Int](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } def apply(xs: Long*): Array[Long] = { val array = new Array[Long](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } def apply(xs: Float*): Array[Float] = { val array = new Array[Float](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } def apply(xs: Double*): Array[Double] = { val array = new Array[Double](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } def apply(xs: Unit*): Array[Unit] = { val array = new Array[Unit](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } /** Create an array containing several copies of an element. * * @param n the length of the resulting array * @param elem the element composing the resulting array * @return an array composed of n elements all equal to elem */ def make[A](n: Int, elem: A): Array[A] = { val a = new Array[A](n) var i = 0 while (i < n) { a(i) = elem i += 1 } a } /** Create an array containing the values of a given function <code>f</code> * over given range <code>[0..n)</code> */ def fromFunction[A](f: Int => A)(n: Int): Array[A] = { val a = new Array[A](n) var i = 0 while (i < n) { a(i) = f(i) i += 1 } a } /** Create an array containing the values of a given function <code>f</code> * over given range <code>[0..n1, 0..n2)</code> */ def fromFunction[A](f: (Int, Int) => A)(n1: Int, n2: Int): Array[Array[A]] = fromFunction(i => fromFunction(f(i, _))(n2))(n1) /** Create an array containing the values of a given function <code>f</code> * over given range <code>[0..n1, 0..n2, 0..n3)</code> */ def fromFunction[A](f: (Int, Int, Int) => A)(n1: Int, n2: Int, n3: Int): Array[Array[Array[A]]] = fromFunction(i => fromFunction(f(i, _, _))(n2, n3))(n1) /** Create an array containing the values of a given function <code>f</code> * over given range <code>[0..n1, 0..n2, 0..n3, 0..n4)</code> */ def fromFunction[A](f: (Int, Int, Int, Int) => A)(n1: Int, n2: Int, n3: Int, n4: Int): Array[Array[Array[Array[A]]]] = fromFunction(i => fromFunction(f(i, _, _, _))(n2, n3, n4))(n1) /** Create an array containing the values of a given function <code>f</code> * over given range <code>[0..n1, 0..n2, 0..n3, 0..n4, 0..n5)</code> */ def fromFunction[A](f: (Int, Int, Int, Int, Int) => A)(n1: Int, n2: Int, n3: Int, n4: Int, n5: Int): Array[Array[Array[Array[Array[A]]]]] = fromFunction(i => fromFunction(f(i, _, _, _, _))(n2, n3, n4, n5))(n1) /** This method is called as a result of a pattern match { case Array(...) => } or val Array(...) = .... * * @param x the selector value * @return array wrapped in an option */ def unapplySeq[A](x: Array[A]): Option[Seq[A]] = Some(x) trait ArrayLike[A] extends RandomAccessSeq.Mutable[A] { def force : Array[A] } trait Projection[A] extends RandomAccessSeq.MutableProjection[A] with ArrayLike[A] { protected def newArray[B >: A](length : Int, elements : Iterator[A]) : Array[B] override def toArray[B >: A] = (newArray(length, elements))//:Any).asInstanceOf[Array[B]] override def force : Array[A] = toArray override def drop( from: Int) = slice(from, length) override def take(until: Int) = slice(0, until) override def dropWhile(p: A => Boolean) = { val c = length + 1 drop((findIndexOf(!p(_)) + c) % c) } override def takeWhile(p: A => Boolean) = {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?