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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.collection
package mutable
import generic._
import script._
/** Buffers are used to create sequences of elements incrementally by
* appending, prepending, or inserting new elements. It is also
* possible to access and modify elements in a random access fashion
* via the index of the element in the current sequence.
*
* @author Matthias Zenger
* @author Martin Odersky
* @version 2.8
* @since 2.8
*/
@cloneable
trait BufferLike[A, +This <: BufferLike[A, This] with Buffer[A]]
extends Growable[A]
with Shrinkable[A]
with Scriptable[A]
with Addable[A, This]
with Subtractable[A, This]
with Cloneable[This]
with SeqLike[A, This]
{ self : This =>
import scala.collection.{Iterable, Traversable}
// Abstract methods from IndexedSeq:
/** Return element at index `n`
* @throws IndexOutofBoundsException if the index is not valid
*/
def apply(n: Int): A
/** Replace element at index <code>n</code> with the new element
* <code>newelem</code>.
*
* @param n the index of the element to replace.
* @param newelem the new element.
* @throws IndexOutofBoundsException if the index is not valid
*/
def update(n: Int, newelem: A)
/** Return number of elements in the buffer
*/
def length: Int
// Abstract methods from Appendabl
/** Append a single element to this buffer.
*
* @param elem the element to append.
*/
def +=(elem: A): this.type
/** Clears the buffer contents.
*/
def clear()
// Abstract methods new in this class
/** Prepend a single element to this buffer and return
* the identity of the buffer.
* @param elem the element to prepend.
*/
def +=:(elem: A): this.type
@deprecated("use `+=:' instead")
final def +:(elem: A): This = +=:(elem)
/** Inserts new elements at the index <code>n</code>. Opposed to method
* <code>update</code>, this method will not replace an element with a
* one. Instead, it will insert a new element at index <code>n</code>.
*
* @param n the index where a new element will be inserted.
* @param iter the iterable object providing all elements to insert.
* @throws IndexOutofBoundsException if the index is not valid
*/
def insertAll(n: Int, iter: Traversable[A])
/** Removes the element on a given index position.
*
* @param n the index which refers to the element to delete.
* @return the previous element
*/
def remove(n: Int): A
/** Removes a number of elements from a given index position.
*
* @param n the index which refers to the element to delete.
* @param count the number of elements to delete
* @throws IndexOutofBoundsException if the index is not valid
*/
def remove(n: Int, count: Int) {
for (i <- 0 until count) remove(n)
}
/** Removes a single element from this buffer, at its first occurrence.
* If the buffer does not contain that element, it is unchanged.
*
* @param x the element to remove.
*/
def -= (x: A): this.type = {
val i = indexOf(x)
if (i != -1) remove(i)
this
}
/** Prepends a number of elements provided by an iterable object
* via its <code>iterator</code> method. The identity of the
* buffer is returned.(repr /: elems) (_ plus _)
*
* @param iter the iterable object.
*/
def ++=:(iter: Traversable[A]): this.type = { insertAll(0, iter); this }
@deprecated("use ++=: instead")
final def ++:(iter: Traversable[A]): This = ++=:(iter)
/** Prepends a number of elements provided by an iterator
* The identity of the buffer is returned.
*
* @param iter the iterator
* @return the updated buffer.
*/
def ++=:(iter: Iterator[A]): This = { insertAll(0, iter.toSeq); this }
/** Appends elements to this buffer.
*
* @param elems the elements to append.
*/
def append(elems: A*) { this ++= elems }
/** Appends a number of elements provided by an iterable object
* via its <code>iterator</code> method.
*
* @param iter the iterable object.
*/
def appendAll(iter: Traversable[A]) { this ++= iter }
/** Prepend given elements to this list.
*
* @param elem the element to prepend.
*/
def prepend(elems: A*) { elems ++=: this }
/** Prepends a number of elements provided by an iterable object
* via its <code>iterator</code> method. The identity of the
* buffer is returned.
*
* @param iter the iterable object.
*/
def prependAll(iter: Traversable[A]) { iter ++=: this }
/** Prepends a number of elements provided by an iterable object
* via its <code>iterator</code> method. The identity of the
* buffer is returned.
*
* @param iter the iterable object.
*/
def prependAll(iter: Iterator[A]) { iter ++=: this }
/** Inserts new elements at the index <code>n</code>. Opposed to method
* <code>update</code>, this method will not replace an element with a
* one. Instead, it will insert the new elements at index <code>n</code>.
*
* @param n the index where a new element will be inserted.
* @param elems the new elements to insert.
*/
def insert(n: Int, elems: A*) { insertAll(n, elems) }
/** Removes the first <code>n</code> elements.
*
* @param n the number of elements to remove from the beginning
* of this buffer.
*/
def trimStart(n: Int) { remove(0, n) }
/** Removes the last <code>n</code> elements.
*
* @param n the number of elements to remove from the end
* of this buffer.
*/
def trimEnd(n: Int) { remove(length - n max 0, n) }
/** Send a message to this scriptable object.
*
* @param cmd the message to send.
*/
def <<(cmd: Message[A]): Unit = cmd match {
case Include(Start, x) => prepend(x)
case Include(End, x) => append(x)
case Include(Index(n), x) => insert(n, x)
case Include(NoLo, x) => this += x
case Update(Start, x) => update(0, x)
case Update(End, x) => update(length - 1, x)
case Update(Index(n), x) => update(n, x)
case Remove(Start, x) => if (this(0) == x) remove(0)
case Remove(End, x) => if (this(length - 1) == x) remove(length - 1)
case Remove(Index(n), x) => if (this(n) == x) remove(n)
case Remove(NoLo, x) => this -= x
case Reset() => clear
case s: Script[_] => s.iterator foreach <<
case _ => throw new UnsupportedOperationException("message " + cmd + " not understood")
}
/** Defines the prefix of the string representation.
*/
override def stringPrefix: String = "Buffer"
/** Adds a number of elements in an array
*
* @param src the array
* @param start the first element to append
* @param len the number of elements to append
*/
@deprecated("replace by: <code>buf ++= src.view(start, end)</code>")
def ++=(src: Array[A], start: Int, len: Int) {
var i = start
val end = i + len
while (i < end) {
this += src(i)
i += 1
}
}
/** Adds a single element to this collection and returns
* the collection itself.
*
* @param elem the element to add.
*/
@deprecated("Use += instead if you intend to add by side effect to an existing collection.\n"+
"Use `clone() ++=' if you intend to create a new collection.")
override def + (elem: A): This = { +=(elem); repr }
/** Adds two or more elements to this collection and returns
* the collection itself.
*
* @param elem1 the first element to add.
* @param elem2 the second element to add.
* @param elems the remaining elements to add.
*/
@deprecated("Use += instead if you intend to add by side effect to an existing collection.\n"+
"Use `clone() ++=' if you intend to create a new collection.")
override def + (elem1: A, elem2: A, elems: A*): This = {
this += elem1 += elem2 ++= elems
repr
}
/** Adds a number of elements provided by a traversable object and returns
* either the collection itself.
*
* @param iter the iterable object.
*/
@deprecated("Use ++= instead if you intend to add by side effect to an existing collection.\n"+
"Use `clone() ++=` if you intend to create a new collection.")
override def ++(iter: Traversable[A]): This = {
for (elem <- iter) +=(elem)
repr
}
/** Adds a number of elements provided by an iterator and returns
* the collection itself.
*
* @param iter the iterator
*/
@deprecated("Use ++= instead if you intend to add by side effect to an existing collection.\n"+
"Use `clone() ++=` if you intend to create a new collection.")
override def ++ (iter: Iterator[A]): This = {
for (elem <- iter) +=(elem)
repr
}
/** Removes a single element from this collection and returns
* the collection itself.
*
* @param elem the element to remove.
*/
@deprecated("Use -= instead if you intend to remove by side effect from an existing collection.\n"+
"Use `clone() -=` if you intend to create a new collection.")
override def -(elem: A): This = { -=(elem); repr }
/** Removes two or more elements from this collection and returns
* the collection itself.
*
* @param elem1 the first element to remove.
* @param elem2 the second element to remove.
* @param elems the remaining elements to remove.
*/
@deprecated("Use -= instead if you intend to remove by side effect from an existing collection.\n"+
"Use `clone() -=` if you intend to create a new collection.")
override def -(elem1: A, elem2: A, elems: A*): This = {
this -= elem1 -= elem2 --= elems
repr
}
/** Removes a number of elements provided by a Traversable object and returns
* the collection itself.
*
* @param iter the Traversable object.
*/
@deprecated("Use --= instead if you intend to remove by side effect from an existing collection.\n"+
"Use `clone() --=` if you intend to create a new collection.")
override def --(iter: Traversable[A]): This = {
for (elem <- iter) -=(elem)
repr
}
/** Removes a number of elements provided by an iterator and returns
* the collection itself.
*
* @param iter the iterator
*/
@deprecated("Use --= instead if you intend to remove by side effect from an existing collection.\n"+
"Use `clone() --=` if you intend to create a new collection.")
override def --(iter: Iterator[A]): This = {
for (elem <- iter) -=(elem)
repr
}
def readOnly: scala.collection.Seq[A] = toSeq
}
|