1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2006-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.collection
import generic._
import mutable.ArrayBuffer
import scala.annotation.tailrec
/** A template trait for indexed sequences of type `IndexedSeq[A]` which optimizes
* the implementation of several methods under the assumption of fast random access.
*
* $indexedSeqInfo
*
* @define willNotTerminateInf
* @define mayNotTerminateInf
*/
trait IndexedSeqOptimized[+A, +Repr] extends IndexedSeqLike[A, Repr] { self =>
override /*IterableLike*/
def isEmpty: Boolean = { length == 0 }
override /*IterableLike*/
def foreach[U](f: A => U): Unit = {
var i = 0
val len = length
while (i < len) { f(this(i)); i += 1 }
}
override /*IterableLike*/
def forall(p: A => Boolean): Boolean = prefixLength(p(_)) == length
override /*IterableLike*/
def exists(p: A => Boolean): Boolean = prefixLength(!p(_)) != length
override /*IterableLike*/
def find(p: A => Boolean): Option[A] = {
val i = prefixLength(!p(_))
if (i < length) Some(this(i)) else None
}
@tailrec
private def foldl[B](start: Int, end: Int, z: B, op: (B, A) => B): B =
if (start == end) z
else foldl(start + 1, end, op(z, this(start)), op)
@tailrec
private def foldr[B](start: Int, end: Int, z: B, op: (A, B) => B): B =
if (start == end) z
else foldr(start, end - 1, op(this(end - 1), z), op)
override /*TraversableLike*/
def foldLeft[B](z: B)(op: (B, A) => B): B =
foldl(0, length, z, op)
override /*IterableLike*/
def foldRight[B](z: B)(op: (A, B) => B): B =
foldr(0, length, z, op)
override /*TraversableLike*/
def reduceLeft[B >: A](op: (B, A) => B): B =
if (length > 0) foldl(1, length, this(0), op) else super.reduceLeft(op)
override /*IterableLike*/
def reduceRight[B >: A](op: (A, B) => B): B =
if (length > 0) foldr(0, length - 1, this(length - 1), op) else super.reduceRight(op)
override /*IterableLike*/
def zip[A1 >: A, B, That](that: GenIterable[B])(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = that match {
case that: IndexedSeq[_] =>
val b = bf(repr)
var i = 0
val len = this.length min that.length
b.sizeHint(len)
while (i < len) {
b += ((this(i), that(i).asInstanceOf[B]))
i += 1
}
b.result
case _ =>
super.zip[A1, B, That](that)(bf)
}
override /*IterableLike*/
def zipWithIndex[A1 >: A, That](implicit bf: CanBuildFrom[Repr, (A1, Int), That]): That = {
val b = bf(repr)
val len = length
b.sizeHint(len)
var i = 0
while (i < len) {
b += ((this(i), i))
i += 1
}
b.result
}
override /*IterableLike*/
def slice(from: Int, until: Int): Repr = {
val lo = from max 0
val hi = until min length
val elems = hi - lo
val b = newBuilder
b.sizeHint(elems max 0)
var i = lo
while (i < hi) {
b += self(i)
i += 1
}
b.result
}
override /*IterableLike*/
def head: A = if (isEmpty) super.head else this(0)
override /*TraversableLike*/
def tail: Repr = if (isEmpty) super.tail else slice(1, length)
override /*TraversableLike*/
def last: A = if (length > 0) this(length - 1) else super.last
override /*IterableLike*/
def init: Repr = if (length > 0) slice(0, length - 1) else super.init
override /*TraversableLike*/
def take(n: Int): Repr = slice(0, n)
override /*TraversableLike*/
def drop(n: Int): Repr = slice(n, length)
override /*IterableLike*/
def takeRight(n: Int): Repr = slice(length - n, length)
override /*IterableLike*/
def dropRight(n: Int): Repr = slice(0, length - n)
override /*TraversableLike*/
def splitAt(n: Int): (Repr, Repr) = (take(n), drop(n))
override /*IterableLike*/
def takeWhile(p: A => Boolean): Repr = take(prefixLength(p))
override /*TraversableLike*/
def dropWhile(p: A => Boolean): Repr = drop(prefixLength(p))
override /*TraversableLike*/
def span(p: A => Boolean): (Repr, Repr) = splitAt(prefixLength(p))
override /*IterableLike*/
def sameElements[B >: A](that: GenIterable[B]): Boolean = that match {
case that: IndexedSeq[_] =>
val len = length
len == that.length && {
var i = 0
while (i < len && this(i) == that(i)) i += 1
i == len
}
case _ =>
super.sameElements(that)
}
override /*IterableLike*/
def copyToArray[B >: A](xs: Array[B], start: Int, len: Int) {
var i = 0
var j = start
val end = length min len min (xs.length - start)
while (i < end) {
xs(j) = this(i)
i += 1
j += 1
}
}
// Overridden methods from Seq
override /*SeqLike*/
def lengthCompare(len: Int): Int = length - len
override /*SeqLike*/
def segmentLength(p: A => Boolean, from: Int): Int = {
val start = from
val len = length
var i = start
while (i < len && p(this(i))) i += 1
i - start
}
private def negLength(n: Int) = if (n >= length) -1 else n
override /*SeqLike*/
def indexWhere(p: A => Boolean, from: Int): Int = {
val start = from max 0
negLength(start + segmentLength(!p(_), start))
}
override /*SeqLike*/
def lastIndexWhere(p: A => Boolean, end: Int): Int = {
var i = end
while (i >= 0 && !p(this(i))) i -= 1
i
}
override /*SeqLike*/
def reverse: Repr = {
val b = newBuilder
b.sizeHint(length)
var i = length
while (0 < i) {
i -= 1
b += this(i)
}
b.result
}
override /*SeqLike*/
def reverseIterator: Iterator[A] = new Iterator[A] {
private var i = self.length
def hasNext: Boolean = 0 < i
def next: A =
if (0 < i) {
i -= 1
self(i)
} else Iterator.empty.next
}
override /*SeqLike*/
def startsWith[B](that: GenSeq[B], offset: Int): Boolean = that match {
case that: IndexedSeq[_] =>
var i = offset
var j = 0
val thisLen = length
val thatLen = that.length
while (i < thisLen && j < thatLen && this(i) == that(j)) {
i += 1
j += 1
}
j == thatLen
case _ =>
var i = offset
val thisLen = length
val thatElems = that.iterator
while (i < thisLen && thatElems.hasNext) {
if (this(i) != thatElems.next())
return false
i += 1
}
!thatElems.hasNext
}
override /*SeqLike*/
def endsWith[B](that: GenSeq[B]): Boolean = that match {
case that: IndexedSeq[_] =>
var i = length - 1
var j = that.length - 1
(j <= i) && {
while (j >= 0) {
if (this(i) != that(j))
return false
i -= 1
j -= 1
}
true
}
case _ =>
super.endsWith(that)
}
}
|