@@ -13,6 +13,8 @@ import scala.reflect.ClassTag
13
13
*/
14
14
object CollectionStrawMan1 {
15
15
16
+ type LengthType = Long
17
+
16
18
/* ------------ Base Traits -------------------------------- */
17
19
18
20
/** Replaces TraversableOnce */
@@ -36,8 +38,8 @@ object CollectionStrawMan1 {
36
38
37
39
/** Base trait for sequence collections */
38
40
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
41
43
}
42
44
43
45
type View [A ] = () => Iterator [A ]
@@ -50,7 +52,7 @@ object CollectionStrawMan1 {
50
52
def foreach (f : A => Unit ): Unit = iterator.foreach(f)
51
53
def foldLeft [B ](z : B )(op : (B , A ) => B ): B = iterator.foldLeft(z)(op)
52
54
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)
54
56
def isEmpty : Boolean = ! iterator.hasNext
55
57
def head : A = iterator.next
56
58
def collectAs [C [X ] <: Iterable [X ]](fi : FromIterator [C ]): C [A ] = fi.fromIterator(iterator)
@@ -70,7 +72,7 @@ object CollectionStrawMan1 {
70
72
val ys = Iterator .Partition [A ](this , ! p(_), lookaheadFalse, lookaheadTrue)
71
73
(fromIter(xs), fromIter(ys))
72
74
}
73
- def drop (n : Int ): Repr = fromIter(Iterator .Drop (iter, n))
75
+ def drop (n : LengthType ): Repr = fromIter(Iterator .Drop (iter, n))
74
76
}
75
77
76
78
/** Transforms returning same collection type constructor */
@@ -129,11 +131,11 @@ object CollectionStrawMan1 {
129
131
def tail : List [A ]
130
132
def iterator = new ListIterator [A ](this )
131
133
def fromIterator [B ](it : Iterator [B ]): List [B ] = List .fromIterator(it)
132
- def apply (i : Int ): A = {
134
+ def apply (i : LengthType ): A = {
133
135
require(! isEmpty)
134
136
if (i == 0 ) head else tail.apply(i - 1 )
135
137
}
136
- def length : Int =
138
+ def length : LengthType =
137
139
if (isEmpty) 0 else 1 + tail.length
138
140
}
139
141
@@ -164,12 +166,12 @@ object CollectionStrawMan1 {
164
166
}
165
167
166
168
/** 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 ] {
168
170
def this () = this (new Array [AnyRef ](16 ), 0 )
169
171
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 ]
173
175
def length = limit - start
174
176
def iterator = new ArrayBufferIterator [A ](elems, start, length)
175
177
def fromIterator [B ](it : Iterator [B ]): ArrayBuffer [B ] =
@@ -191,7 +193,7 @@ object CollectionStrawMan1 {
191
193
limit += 1
192
194
this
193
195
}
194
- def trimStart (n : Int ): Unit = start += (n max 0 )
196
+ def trimStart (n : LengthType ): Unit = start += (n max 0 )
195
197
override def toString = s " ArrayBuffer( ${elems.slice(start, limit).mkString(" , " )}) "
196
198
}
197
199
@@ -216,9 +218,9 @@ object CollectionStrawMan1 {
216
218
}
217
219
}
218
220
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 ]
222
224
}
223
225
224
226
/** Concrete collection type: View */
@@ -244,7 +246,7 @@ object CollectionStrawMan1 {
244
246
implicit class StringOps (val s : String ) extends AnyVal with Ops [Char ] {
245
247
def iterator : Iterator [Char ] = new RandomAccessIterator [Char ] {
246
248
override val limit = s.length
247
- def apply (n : Int ) = s.charAt(n)
249
+ def apply (n : LengthType ) = s.charAt(n)
248
250
}
249
251
}
250
252
@@ -312,7 +314,7 @@ object CollectionStrawMan1 {
312
314
}
313
315
def apply [A ](xs : A * ): Iterator [A ] = new RandomAccessIterator [A ] {
314
316
override val limit = xs.length
315
- def apply (n : Int ) = xs(n)
317
+ def apply (n : LengthType ) = xs(n)
316
318
}
317
319
def nextOnEmpty = throw new NoSuchElementException (" next on empty iterator" )
318
320
@@ -358,7 +360,7 @@ object CollectionStrawMan1 {
358
360
r
359
361
} else Iterator .nextOnEmpty
360
362
}
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 ] {
362
364
var toSkip = n
363
365
def hasNext : Boolean = underlying.hasNext && (
364
366
toSkip == 0 || { underlying.next; toSkip -= 1 ; hasNext })
@@ -371,19 +373,19 @@ object CollectionStrawMan1 {
371
373
override def remaining = underlying.remaining min other.remaining
372
374
}
373
375
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)
375
377
def limit = underlying.remaining
376
378
}
377
379
}
378
380
379
381
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
382
384
var start = 0
383
385
override def remaining = (limit - start) max 0
384
386
def hasNext = start < limit
385
387
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 }
387
389
override def reverse : Iterator [A ] = new Iterator .Reverse (this )
388
390
}
389
391
}
0 commit comments