Skip to content

Commit 5244c5c

Browse files
committed
Abstract over LegthType and make it be Long.
1 parent b77ab7c commit 5244c5c

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/strawman/collections/CollectionStrawMan2.scala

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import scala.reflect.ClassTag
1313
*/
1414
object CollectionStrawMan1 {
1515

16+
type LengthType = Long
17+
1618
/* ------------ Base Traits -------------------------------- */
1719

1820
/** Replaces TraversableOnce */
@@ -36,8 +38,8 @@ object CollectionStrawMan1 {
3638

3739
/** Base trait for sequence collections */
3840
trait Seq[+A] extends Iterable[A] with FromIterator[Seq] {
39-
def apply(i: Int): A
40-
def length: Int
41+
def apply(i: LengthType): A
42+
def length: LengthType
4143
}
4244

4345
type View[A] = () => Iterator[A]
@@ -50,7 +52,7 @@ object CollectionStrawMan1 {
5052
def foreach(f: A => Unit): Unit = iterator.foreach(f)
5153
def foldLeft[B](z: B)(op: (B, A) => B): B = iterator.foldLeft(z)(op)
5254
def foldRight[B](z: B)(op: (A, B) => B): B = iterator.foldRight(z)(op)
53-
def indexWhere(p: A => Boolean): Int = iterator.indexWhere(p)
55+
def indexWhere(p: A => Boolean): LengthType = iterator.indexWhere(p)
5456
def isEmpty: Boolean = !iterator.hasNext
5557
def head: A = iterator.next
5658
def collectAs[C[X] <: Iterable[X]](fi: FromIterator[C]): C[A] = fi.fromIterator(iterator)
@@ -70,7 +72,7 @@ object CollectionStrawMan1 {
7072
val ys = Iterator.Partition[A](this, !p(_), lookaheadFalse, lookaheadTrue)
7173
(fromIter(xs), fromIter(ys))
7274
}
73-
def drop(n: Int): Repr = fromIter(Iterator.Drop(iter, n))
75+
def drop(n: LengthType): Repr = fromIter(Iterator.Drop(iter, n))
7476
}
7577

7678
/** Transforms returning same collection type constructor */
@@ -129,11 +131,11 @@ object CollectionStrawMan1 {
129131
def tail: List[A]
130132
def iterator = new ListIterator[A](this)
131133
def fromIterator[B](it: Iterator[B]): List[B] = List.fromIterator(it)
132-
def apply(i: Int): A = {
134+
def apply(i: LengthType): A = {
133135
require(!isEmpty)
134136
if (i == 0) head else tail.apply(i - 1)
135137
}
136-
def length: Int =
138+
def length: LengthType =
137139
if (isEmpty) 0 else 1 + tail.length
138140
}
139141

@@ -164,12 +166,12 @@ object CollectionStrawMan1 {
164166
}
165167

166168
/** Concrete collection type: ArrayBuffer */
167-
class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: Int) extends Seq[A] with FromIterator[ArrayBuffer] {
169+
class ArrayBuffer[A] private (initElems: Array[AnyRef], initLength: LengthType) extends Seq[A] with FromIterator[ArrayBuffer] {
168170
def this() = this(new Array[AnyRef](16), 0)
169171
private var elems: Array[AnyRef] = initElems
170-
private var start = 0
171-
private var limit = initLength
172-
def apply(i: Int) = elems(start + i).asInstanceOf[A]
172+
private var start: LengthType = 0
173+
private var limit: LengthType = initLength
174+
def apply(i: LengthType) = elems(start + i).asInstanceOf[A]
173175
def length = limit - start
174176
def iterator = new ArrayBufferIterator[A](elems, start, length)
175177
def fromIterator[B](it: Iterator[B]): ArrayBuffer[B] =
@@ -191,7 +193,7 @@ object CollectionStrawMan1 {
191193
limit += 1
192194
this
193195
}
194-
def trimStart(n: Int): Unit = start += (n max 0)
196+
def trimStart(n: LengthType): Unit = start += (n max 0)
195197
override def toString = s"ArrayBuffer(${elems.slice(start, limit).mkString(", ")})"
196198
}
197199

@@ -216,9 +218,9 @@ object CollectionStrawMan1 {
216218
}
217219
}
218220

219-
class ArrayBufferIterator[A](val elems: Array[AnyRef], initStart: Int, length: Int) extends RandomAccessIterator[A] {
220-
val limit = length
221-
def apply(n: Int) = elems(initStart + n).asInstanceOf[A]
221+
class ArrayBufferIterator[A](val elems: Array[AnyRef], initStart: LengthType, length: LengthType) extends RandomAccessIterator[A] {
222+
val limit: LengthType = length
223+
def apply(n: LengthType) = elems(initStart + n).asInstanceOf[A]
222224
}
223225

224226
/** Concrete collection type: View */
@@ -244,7 +246,7 @@ object CollectionStrawMan1 {
244246
implicit class StringOps(val s: String) extends AnyVal with Ops[Char] {
245247
def iterator: Iterator[Char] = new RandomAccessIterator[Char] {
246248
override val limit = s.length
247-
def apply(n: Int) = s.charAt(n)
249+
def apply(n: LengthType) = s.charAt(n)
248250
}
249251
}
250252

@@ -312,7 +314,7 @@ object CollectionStrawMan1 {
312314
}
313315
def apply[A](xs: A*): Iterator[A] = new RandomAccessIterator[A] {
314316
override val limit = xs.length
315-
def apply(n: Int) = xs(n)
317+
def apply(n: LengthType) = xs(n)
316318
}
317319
def nextOnEmpty = throw new NoSuchElementException("next on empty iterator")
318320

@@ -358,7 +360,7 @@ object CollectionStrawMan1 {
358360
r
359361
} else Iterator.nextOnEmpty
360362
}
361-
case class Drop[A](override val underlying: Iterator[A], n: Int) extends Iterator[A] {
363+
case class Drop[A](override val underlying: Iterator[A], n: LengthType) extends Iterator[A] {
362364
var toSkip = n
363365
def hasNext: Boolean = underlying.hasNext && (
364366
toSkip == 0 || { underlying.next; toSkip -= 1; hasNext })
@@ -371,19 +373,19 @@ object CollectionStrawMan1 {
371373
override def remaining = underlying.remaining min other.remaining
372374
}
373375
case class Reverse[A](override val underlying: RandomAccessIterator[A]) extends RandomAccessIterator[A] {
374-
def apply(n: Int) = underlying.apply(underlying.limit - 1 - n)
376+
def apply(n: LengthType) = underlying.apply(underlying.limit - 1 - n)
375377
def limit = underlying.remaining
376378
}
377379
}
378380

379381
trait RandomAccessIterator[+A] extends Iterator[A] { self =>
380-
def apply(n: Int): A
381-
def limit: Int
382+
def apply(n: LengthType): A
383+
def limit: LengthType
382384
var start = 0
383385
override def remaining = (limit - start) max 0
384386
def hasNext = start < limit
385387
def next: A = { val r = this(start); start += 1; r }
386-
override def drop(n: Int): Iterator[A] = { start += (n max 0); this }
388+
override def drop(n: LengthType): Iterator[A] = { start += (n max 0); this }
387389
override def reverse: Iterator[A] = new Iterator.Reverse(this)
388390
}
389391
}

0 commit comments

Comments
 (0)