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
278
279
280
281
282
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2006-2009, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id: Vector.scala 15437 2008-06-25 16:22:45Z stepancheg $
package scala.collection.generic
import mutable.ArrayBuffer
/** Sequences that support O(1) element access and O(1) length computation.
* This class does not add any methods to Sequence but overrides several
* methods with optimized implementations.
*
* @author Sean McDirmid
* @author Martin Odersky
* @version 2.8
*/
trait VectorTemplate[+A, +This <: VectorTemplate[A, This] with Vector[A]] extends SequenceTemplate[A, This] { self =>
// Overridden methods from IterableTemplate
/** The iterator returned by the elements method
*/
protected class Elements(start: Int, end: Int) extends scala.collection.BufferedIterator[A] {
private var i = start
def hasNext: Boolean = i < end
def next: A =
if (i < end) {
val x = self(i)
i += 1
x
} else Iterator.empty.next
def head =
if (i < end) self(i) else Iterator.empty.next
/** drop is overridden to enable fast searching in the middle of random access sequences */
override def drop(n: Int): Iterator[A] =
if (n > 0) new Elements(start + n, end) else this
/** take is overridden to be symmetric to drop */
override def take(n: Int): Iterator[A] =
if (n <= 0) Iterator.empty.buffered
else if (start + n < end) new Elements(start, start + n)
else this
}
override def elements: Iterator[A] = new Elements(0, length)
override def isEmpty: Boolean = { length == 0 }
override def foreach[U](f: A => U): Unit = {
var i = 0
val len = length
while (i < len) { f(this(i)); i += 1 }
}
override def forall(p: A => Boolean): Boolean = prefixLength(p(_)) == length
override def exists(p: A => Boolean): Boolean = prefixLength(!p(_)) != length
override def find(p: A => Boolean): Option[A] = {
val i = prefixLength(!p(_))
if (i < length) Some(this(i)) else None
}
private def foldl[B](start: Int, z: B, op: (B, A) => B): B = {
var i = start
val len = length
var result = z
while (i < len) {
result = op(result, this(i))
i += 1
}
result
}
private def foldr[B](start: Int, len: Int, z: B, op: (A, B) => B): B =
if (start == len) z
else op(this(start), foldr(start + 1, len, z, op))
override def foldLeft[B](z: B)(op: (B, A) => B): B =
foldl(0, z, op)
override def foldRight[B](z: B)(op: (A, B) => B): B =
foldr(0, length, z, op)
override def reduceLeft[B >: A](op: (B, A) => B): B =
if (length > 0) foldl(1, this(0), op) else super.reduceLeft(op)
override 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 def zip[A1 >: A, B, That](that: Sequence[B])(implicit bf: BuilderFactory[(A1, B), That, This]): That = that match {
case that: Vector[_] =>
val b = bf(thisCollection)
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 def zipWithIndex[A1 >: A, That](implicit bf: BuilderFactory[(A1, Int), That, This]): That = {
val b = bf(thisCollection)
val len = length
b.sizeHint(len)
var i = 0
while (i < len) {
b += ((this(i), i))
i += 1
}
b.result
}
override def slice(from: Int, until: Int): This = {
var i = from max 0
val end = until min length
val b = newBuilder
b.sizeHint(end - i)
while (i < end) {
b += this(i)
i += 1
}
b.result
}
override def head: A = if (isEmpty) super.head else this(0)
override def tail: This = slice(1, length)
override def last: A = if (length > 0) this(length - 1) else super.last
override def init: This = if (length > 0) slice(0, length - 1) else super.init
override def take(n: Int): This = slice(0, n)
override def drop(n: Int): This = slice(n, length)
override def takeRight(n: Int): This = slice(length - n, length)
override def dropRight(n: Int): This = slice(0, length - n)
override def splitAt(n: Int): (This, This) = (take(n), drop(n))
override def takeWhile(p: A => Boolean): This = take(prefixLength(p))
override def dropWhile(p: A => Boolean): This = drop(prefixLength(p))
override def span(p: A => Boolean): (This, This) = splitAt(prefixLength(p))
override def sameElements[B >: A](that: Iterable[B]): Boolean = that match {
case that: Vector[_] =>
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 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 Sequence
override def lengthCompare(len: Int): Int = length - len
override 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 def indexWhere(p: A => Boolean, from: Int): Int = {
val start = from max 0
negLength(start + segmentLength(!p(_), start))
}
override def lastIndexWhere(p: A => Boolean, end: Int): Int = {
var i = end
while (i >= 0 && !p(this(i))) i -= 1
i
}
override def reverse: This = {
val b = newBuilder
b.sizeHint(length)
var i = length
while (0 < i) {
i -= 1
b += this(i)
}
b.result
}
override def reversedElements: 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 def startsWith[B](that: Sequence[B], offset: Int): Boolean = that match {
case that: Vector[_] =>
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.elements
while (i < thisLen && thatElems.hasNext && this(i) == thatElems.next()) {
i += 1
}
!thatElems.hasNext
}
override def endsWith[B](that: Sequence[B]): Boolean = that match {
case that: Vector[_] =>
val thisLen = length
val thatLen = that.length
var i = thisLen - 1
var j = thatLen - 1
while (i >= 0 && j >= 0 && this(i) == that(j)) {
i -= 1
j -= 1
}
j == 0
case _ =>
super.endsWith(that)
}
override def indexOfSeq[B >: A](that: Sequence[B]): Int = {
var i = 0
val last = length - that.length
while (i <= last && !startsWith(that, i)) i += 1
negLength(i)
}
override def equals(that: Any): Boolean = that match {
case that1: Vector[a] =>
val len = this.length
len == that1.length && {
var i = 0
while (i < len && this(i) == that1(i)) i += 1
i == len
}
case _ =>
super.equals(that)
}
override def view = new VectorView[A, This] {
protected lazy val underlying = self.thisCollection
override def elements = self.elements
override def length = self.length
override def apply(idx: Int) = self.apply(idx)
}
override def view(from: Int, until: Int) = view.slice(from, until)
}
|