Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #278 from scala/wip/specialized-arrays
Browse files Browse the repository at this point in the history
Primitive array types in ArrayOps / ArrayBuilder / ImmutableArray
  • Loading branch information
szeiger authored Jan 8, 2018
2 parents c50beb0 + 848f5d5 commit 875a21c
Show file tree
Hide file tree
Showing 15 changed files with 1,101 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole
import strawman.collection.{ArrayView, View}
import scala.{Any, AnyRef, Int, Long, Unit, Array}
import scala.Predef.intWrapper

Expand Down

This file was deleted.

55 changes: 35 additions & 20 deletions collections/src/main/scala/strawman/collection/ArrayOps.scala
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
package strawman
package collection

import scala.{AnyVal, Array, ArrayIndexOutOfBoundsException, Char, Int, throws}
import scala.{AnyVal, Array, ArrayIndexOutOfBoundsException, Char, Int, throws, Boolean, Serializable, Unit, `inline`}
import scala.Predef.???
import mutable.{ArrayBuffer, GrowableBuilder}

import immutable.ImmutableArray
import scala.reflect.ClassTag

class ArrayOps[A](val xs: Array[A])
extends AnyVal
with IterableOnce[A]
with IndexedSeqOps[A, immutable.IndexedSeq, Array[A]]
with StrictOptimizedIterableOps[A, Seq, Array[A]]
with ArrayLike[A] {
object ArrayOps {
class WithFilter[A](p: A => Boolean, ao: ArrayOps[A]) extends collection.WithFilter[A, immutable.IndexedSeq] {
protected[this] def filtered = View.Filter(ao.toIterable, p, isFlipped = false)
def map[B](f: A => B): immutable.IndexedSeq[B] = ao.iterableFactory.from(View.Map(filtered, f))
def flatMap[B](f: A => IterableOnce[B]): immutable.IndexedSeq[B] = ao.iterableFactory.from(View.FlatMap(filtered, f))
def foreach[U](f: A => U): Unit = filtered.foreach(f)
def map[B: ClassTag](f: A => B): Array[B] = ao.fromTaggedIterable(View.Map(filtered, f))
def flatMap[B: ClassTag](f: A => IterableOnce[B]): Array[B] = ao.fromTaggedIterable(View.FlatMap(filtered, f))
def withFilter(q: A => Boolean): WithFilter[A] = new WithFilter[A](a => p(a) && q(a), ao)
}
}

class ArrayOps[A](val xs: Array[A]) extends AnyVal
with IterableOnce[A]
with IndexedSeqOps[A, immutable.IndexedSeq, Array[A]]
with StrictOptimizedSeqOps[A, Seq, Array[A]]
with ArrayLike[A] {

protected def fromTaggedIterable[B: ClassTag](coll: Iterable[B]): Array[B] = coll.toArray[B]

override def withFilter(p: A => Boolean): ArrayOps.WithFilter[A] = new ArrayOps.WithFilter[A](p, this)

def toIterable = ArrayView(xs)
def toIterable: IndexedSeq[A] = ImmutableArray.unsafeWrapArray(xs)
protected[this] def coll: Array[A] = xs
override def toSeq: immutable.Seq[A] = fromIterable(toIterable)

def length = xs.length
@throws[ArrayIndexOutOfBoundsException]
def apply(i: Int) = xs.apply(i)

override def view = ArrayView(xs)

def elemTag: ClassTag[A] = ClassTag(xs.getClass.getComponentType)

def iterableFactory = immutable.IndexedSeq

protected[this] def fromTaggedIterable[B: ClassTag](coll: Iterable[B]): Array[B] = coll.toArray[B]
protected[this] def fromSpecificIterable(coll: Iterable[A]): Array[A] = coll.toArray[A](elemTag)

protected[this] def newSpecificBuilder() = ArrayBuffer.newBuilder[A]().mapResult(_.toArray(elemTag))
Expand All @@ -47,15 +60,17 @@ class ArrayOps[A](val xs: Array[A])

def flatMap[B: ClassTag](f: A => IterableOnce[B]): Array[B] = fromTaggedIterable(View.FlatMap(toIterable, f))

def ++[B >: A : ClassTag](xs: Iterable[B]): Array[B] = fromTaggedIterable(View.Concat(toIterable, xs))
@`inline` final def ++[B >: A : ClassTag](xs: Iterable[B]): Array[B] = appendedAll(xs)

def zip[B: ClassTag](that: Iterable[B]): Array[(A, B)] = fromTaggedIterable(View.Zip(toIterable, that))

}

case class ArrayView[A](xs: Array[A]) extends IndexedView[A] {
def length = xs.length
@throws[ArrayIndexOutOfBoundsException]
def apply(n: Int) = xs(n)
override def className = "ArrayView"
def appended[B >: A : ClassTag](x: B): Array[B] = fromTaggedIterable(View.Append(toIterable, x))
@`inline` final def :+ [B >: A : ClassTag](x: B): Array[B] = appended(x)
def prepended[B >: A : ClassTag](x: B): Array[B] = fromTaggedIterable(View.Prepend(x, toIterable))
@`inline` final def +: [B >: A : ClassTag](x: B): Array[B] = prepended(x)
def prependedAll[B >: A : ClassTag](prefix: Iterable[B]): Array[B] = fromTaggedIterable(View.Concat(prefix, toIterable))
@`inline` final def ++: [B >: A : ClassTag](prefix: Iterable[B]): Array[B] = prependedAll(prefix)
def appendedAll[B >: A : ClassTag](suffix: Iterable[B]): Array[B] = fromTaggedIterable(View.Concat(toIterable, suffix))
@`inline` final def :++ [B >: A : ClassTag](suffix: Iterable[B]): Array[B] = appendedAll(suffix)
@`inline` final def concat[B >: A : ClassTag](suffix: Iterable[B]): Array[B] = appendedAll(suffix)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package strawman.collection

import scala.{Int, math}
import scala.{Int, math, Any}

/**
* Trait that overrides operations on sequences in order
* to take advantage of strict builders.
*/
trait StrictOptimizedSeqOps [+A, +CC[_], +C]
extends SeqOps[A, CC, C]
extends Any
with SeqOps[A, CC, C]
with StrictOptimizedIterableOps[A, CC, C] {

override def distinctBy[B](f: A => B): C = {
Expand Down
Loading

0 comments on commit 875a21c

Please sign in to comment.