Skip to content

Add arrays to collection strawman #1414

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

Merged
merged 17 commits into from
Aug 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 61 additions & 26 deletions src/strawman/collections/CollectionStrawMan6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@ object CollectionStrawMan6 extends LowPriority {
trait Iterable[+A] extends IterableOnce[A] with IterableLike[A, Iterable] {
/** The collection itself */
protected def coll: this.type = this
}
}

/** Base trait for sequence collections */
trait Seq[+A] extends Iterable[A] with SeqLike[A, Seq] {
def apply(i: Int): A
/** A trait representing indexable collections with finite length */
trait ArrayLike[+A] extends Any {
def length: Int
def apply(i: Int): A
}

/** Base trait for sequence collections */
trait Seq[+A] extends Iterable[A] with SeqLike[A, Seq] with ArrayLike[A]

/** Base trait for linearly accessed sequences that have efficient `head` and
* `tail` operations.
* Known subclasses: List, LazyList
Expand Down Expand Up @@ -156,6 +159,8 @@ object CollectionStrawMan6 extends LowPriority {
}
}

type IndexedSeq[+A] = Seq[A] { def view: IndexedView[A] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do this then any call to view on a value with type IndexedSeq[Foo] will end up going through reflection, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Sun, Jul 31, 2016 at 12:07 AM, Guillaume Martres <
[email protected]> wrote:

In src/strawman/collections/CollectionStrawMan6.scala
#1414 (comment):

@@ -156,6 +159,8 @@ object CollectionStrawMan6 extends LowPriority {
}
}

  • type IndexedSeq[+A] = Seq[A] { def view: IndexedView[A] }

If you do this then any call to view on a value with type IndexedSeq[Foo]
will end up going through reflection, no?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/lampepfl/dotty/pull/1414/files/dabf044485c60245e0fdf64c28eaa90285242a75..8db8c110a2b2a287875222a74511511d80be2b15#r72894931,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAwlVn1LM5xD42dSM8pPrJcmLIQls_Cjks5qa8s5gaJpZM4JUZlv
.

I don't think so, since view is already a method on Seq.


/** Base trait for strict collections that can be built using a builder.
* @param A the element type of the collection
* @param Repr the type of the underlying collection
Expand Down Expand Up @@ -593,6 +598,7 @@ object CollectionStrawMan6 extends LowPriority {
}

class ArrayBufferView[A](val elems: Array[AnyRef], val start: Int, val end: Int) extends IndexedView[A] {
def length = end - start
def apply(n: Int) = elems(start + n).asInstanceOf[A]
override def className = "ArrayBufferView"
}
Expand Down Expand Up @@ -656,7 +662,8 @@ object CollectionStrawMan6 extends LowPriority {
extends AnyVal with IterableOps[Char]
with SeqMonoTransforms[Char, String]
with IterablePolyTransforms[Char, List]
with Buildable[Char, String] {
with Buildable[Char, String]
with ArrayLike[Char] {

protected def coll = new StringView(s)
def iterator = coll.iterator
Expand All @@ -671,6 +678,9 @@ object CollectionStrawMan6 extends LowPriority {

protected[this] def newBuilder = new StringBuilder

def length = s.length
def apply(i: Int) = s.charAt(i)

override def knownSize = s.length

override def className = "String"
Expand Down Expand Up @@ -720,8 +730,7 @@ object CollectionStrawMan6 extends LowPriority {
}

case class StringView(s: String) extends IndexedView[Char] {
val start = 0
val end = s.length
def length = s.length
def apply(n: Int) = s.charAt(n)
override def className = "StringView"
}
Expand All @@ -731,11 +740,15 @@ object CollectionStrawMan6 extends LowPriority {
implicit class ArrayOps[A](val xs: Array[A])
extends AnyVal with IterableOps[A]
with SeqMonoTransforms[A, Array[A]]
with Buildable[A, Array[A]] {
with Buildable[A, Array[A]]
with ArrayLike[A] {

protected def coll = new ArrayView(xs)
def iterator = coll.iterator

def length = xs.length
def apply(i: Int) = xs.apply(i)

override def view = new ArrayView(xs)

def elemTag: ClassTag[A] = ClassTag(xs.getClass.getComponentType)
Expand All @@ -757,8 +770,7 @@ object CollectionStrawMan6 extends LowPriority {
}

case class ArrayView[A](xs: Array[A]) extends IndexedView[A] {
val start = 0
val end = xs.length
def length = xs.length
def apply(n: Int) = xs(n)
override def className = "ArrayView"
}
Expand Down Expand Up @@ -822,15 +834,17 @@ object CollectionStrawMan6 extends LowPriority {
/** A view that drops leading elements of the underlying collection. */
case class Drop[A](underlying: Iterable[A], n: Int) extends View[A] {
def iterator = underlying.iterator.drop(n)
protected val normN = n max 0
override def knownSize =
if (underlying.knownSize >= 0) underlying.knownSize - n max 0 else -1
if (underlying.knownSize >= 0) (underlying.knownSize - normN) max 0 else -1
}

/** A view that takes leading elements of the underlying collection. */
case class Take[A](underlying: Iterable[A], n: Int) extends View[A] {
def iterator = underlying.iterator.take(n)
protected val normN = n max 0
override def knownSize =
if (underlying.knownSize >= 0) underlying.knownSize min n else -1
if (underlying.knownSize >= 0) underlying.knownSize min normN else -1
}

/** A view that maps elements of the underlying collection. */
Expand Down Expand Up @@ -872,29 +886,51 @@ object CollectionStrawMan6 extends LowPriority {
}

/** View defined in terms of indexing a range */
trait IndexedView[+A] extends View[A] { self =>
def start: Int
def end: Int
def apply(i: Int): A
trait IndexedView[+A] extends View[A] with ArrayLike[A] {

def iterator: Iterator[A] = new Iterator[A] {
private var current = start
def hasNext = current < end
private var current = 0
def hasNext = current < length
def next: A = {
val r = apply(current)
current += 1
r
}
}

def reverse = new IndexedView[A] {
def start = self.start
def end = self.end
def apply(i: Int) = self.apply(end - 1 - i)
override def take(n: Int): IndexedView[A] = new IndexedView.Take(this, n)
override def drop(n: Int): IndexedView[A] = new IndexedView.Drop(this, n)
override def map[B](f: A => B): IndexedView[B] = new IndexedView.Map(this, f)
def reverse: IndexedView[A] = new IndexedView.Reverse(this)
}

object IndexedView {

class Take[A](underlying: IndexedView[A], n: Int)
extends View.Take(underlying, n) with IndexedView[A] {
override def iterator = super.iterator
def length = underlying.length min normN
def apply(i: Int) = underlying.apply(i)
}

def length = end - start max 0
override def knownSize = length
class Drop[A](underlying: IndexedView[A], n: Int)
extends View.Take(underlying, n) with IndexedView[A] {
override def iterator = super.iterator
def length = (underlying.length - normN) max 0
def apply(i: Int) = underlying.apply(i + normN)
}

class Map[A, B](underlying: IndexedView[A], f: A => B)
extends View.Map(underlying, f) with IndexedView[B] {
override def iterator = super.iterator
def length = underlying.length
def apply(n: Int) = f(underlying.apply(n))
}

case class Reverse[A](underlying: IndexedView[A]) extends IndexedView[A] {
def length = underlying.length
def apply(i: Int) = underlying.apply(length - 1 - i)
}
}

/* ---------- Iterators ---------------------------------------------------*/
Expand Down Expand Up @@ -1002,8 +1038,7 @@ object CollectionStrawMan6 extends LowPriority {
def next = throw new NoSuchElementException("next on empty iterator")
}
def apply[A](xs: A*): Iterator[A] = new IndexedView[A] {
val start = 0
val end = xs.length
val length = xs.length
def apply(n: Int) = xs(n)
}.iterator
}
Expand Down
87 changes: 61 additions & 26 deletions tests/run/colltest6/CollectionStrawMan6_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,17 @@ object CollectionStrawMan6 extends LowPriority {
trait Iterable[+A] extends IterableOnce[A] with IterableLike[A, Iterable] {
/** The collection itself */
protected def coll: this.type = this
}
}

/** Base trait for sequence collections */
trait Seq[+A] extends Iterable[A] with SeqLike[A, Seq] {
def apply(i: Int): A
/** A trait representing indexable collections with finite length */
trait ArrayLike[+A] extends Any {
def length: Int
def apply(i: Int): A
}

/** Base trait for sequence collections */
trait Seq[+A] extends Iterable[A] with SeqLike[A, Seq] with ArrayLike[A]

/** Base trait for linearly accessed sequences that have efficient `head` and
* `tail` operations.
* Known subclasses: List, LazyList
Expand Down Expand Up @@ -157,6 +160,8 @@ object CollectionStrawMan6 extends LowPriority {
}
}

type IndexedSeq[+A] = Seq[A] { def view: IndexedView[A] }

/** Base trait for strict collections that can be built using a builder.
* @param A the element type of the collection
* @param Repr the type of the underlying collection
Expand Down Expand Up @@ -594,6 +599,7 @@ object CollectionStrawMan6 extends LowPriority {
}

class ArrayBufferView[A](val elems: Array[AnyRef], val start: Int, val end: Int) extends IndexedView[A] {
def length = end - start
def apply(n: Int) = elems(start + n).asInstanceOf[A]
override def className = "ArrayBufferView"
}
Expand Down Expand Up @@ -657,7 +663,8 @@ object CollectionStrawMan6 extends LowPriority {
extends AnyVal with IterableOps[Char]
with SeqMonoTransforms[Char, String]
with IterablePolyTransforms[Char, List]
with Buildable[Char, String] {
with Buildable[Char, String]
with ArrayLike[Char] {

protected def coll = new StringView(s)
def iterator = coll.iterator
Expand All @@ -672,6 +679,9 @@ object CollectionStrawMan6 extends LowPriority {

protected[this] def newBuilder = new StringBuilder

def length = s.length
def apply(i: Int) = s.charAt(i)

override def knownSize = s.length

override def className = "String"
Expand Down Expand Up @@ -721,8 +731,7 @@ object CollectionStrawMan6 extends LowPriority {
}

case class StringView(s: String) extends IndexedView[Char] {
val start = 0
val end = s.length
def length = s.length
def apply(n: Int) = s.charAt(n)
override def className = "StringView"
}
Expand All @@ -732,11 +741,15 @@ object CollectionStrawMan6 extends LowPriority {
implicit class ArrayOps[A](val xs: Array[A])
extends AnyVal with IterableOps[A]
with SeqMonoTransforms[A, Array[A]]
with Buildable[A, Array[A]] {
with Buildable[A, Array[A]]
with ArrayLike[A] {

protected def coll = new ArrayView(xs)
def iterator = coll.iterator

def length = xs.length
def apply(i: Int) = xs.apply(i)

override def view = new ArrayView(xs)

def elemTag: ClassTag[A] = ClassTag(xs.getClass.getComponentType)
Expand All @@ -758,8 +771,7 @@ object CollectionStrawMan6 extends LowPriority {
}

case class ArrayView[A](xs: Array[A]) extends IndexedView[A] {
val start = 0
val end = xs.length
def length = xs.length
def apply(n: Int) = xs(n)
override def className = "ArrayView"
}
Expand Down Expand Up @@ -823,15 +835,17 @@ object CollectionStrawMan6 extends LowPriority {
/** A view that drops leading elements of the underlying collection. */
case class Drop[A](underlying: Iterable[A], n: Int) extends View[A] {
def iterator = underlying.iterator.drop(n)
protected val normN = n max 0
override def knownSize =
if (underlying.knownSize >= 0) underlying.knownSize - n max 0 else -1
if (underlying.knownSize >= 0) (underlying.knownSize - normN) max 0 else -1
}

/** A view that takes leading elements of the underlying collection. */
case class Take[A](underlying: Iterable[A], n: Int) extends View[A] {
def iterator = underlying.iterator.take(n)
protected val normN = n max 0
override def knownSize =
if (underlying.knownSize >= 0) underlying.knownSize min n else -1
if (underlying.knownSize >= 0) underlying.knownSize min normN else -1
}

/** A view that maps elements of the underlying collection. */
Expand Down Expand Up @@ -873,29 +887,51 @@ object CollectionStrawMan6 extends LowPriority {
}

/** View defined in terms of indexing a range */
trait IndexedView[+A] extends View[A] { self =>
def start: Int
def end: Int
def apply(i: Int): A
trait IndexedView[+A] extends View[A] with ArrayLike[A] {

def iterator: Iterator[A] = new Iterator[A] {
private var current = start
def hasNext = current < end
private var current = 0
def hasNext = current < length
def next: A = {
val r = apply(current)
current += 1
r
}
}

def reverse = new IndexedView[A] {
def start = self.start
def end = self.end
def apply(i: Int) = self.apply(end - 1 - i)
override def take(n: Int): IndexedView[A] = new IndexedView.Take(this, n)
override def drop(n: Int): IndexedView[A] = new IndexedView.Drop(this, n)
override def map[B](f: A => B): IndexedView[B] = new IndexedView.Map(this, f)
def reverse: IndexedView[A] = new IndexedView.Reverse(this)
}

object IndexedView {

class Take[A](underlying: IndexedView[A], n: Int)
extends View.Take(underlying, n) with IndexedView[A] {
override def iterator = super.iterator
def length = underlying.length min normN
def apply(i: Int) = underlying.apply(i)
}

def length = end - start max 0
override def knownSize = length
class Drop[A](underlying: IndexedView[A], n: Int)
extends View.Take(underlying, n) with IndexedView[A] {
override def iterator = super.iterator
def length = (underlying.length - normN) max 0
def apply(i: Int) = underlying.apply(i + normN)
}

class Map[A, B](underlying: IndexedView[A], f: A => B)
extends View.Map(underlying, f) with IndexedView[B] {
override def iterator = super.iterator
def length = underlying.length
def apply(n: Int) = f(underlying.apply(n))
}

case class Reverse[A](underlying: IndexedView[A]) extends IndexedView[A] {
def length = underlying.length
def apply(i: Int) = underlying.apply(length - 1 - i)
}
}

/* ---------- Iterators ---------------------------------------------------*/
Expand Down Expand Up @@ -1003,8 +1039,7 @@ object CollectionStrawMan6 extends LowPriority {
def next = throw new NoSuchElementException("next on empty iterator")
}
def apply[A](xs: A*): Iterator[A] = new IndexedView[A] {
val start = 0
val end = xs.length
val length = xs.length
def apply(n: Int) = xs(n)
}.iterator
}
Expand Down