@@ -66,8 +66,8 @@ final class ListBuffer[A]
66
66
*/
67
67
private var start : List [A ] = Nil
68
68
private var last0 : :: [A ] = _
69
- private var exported : Boolean = false
70
- private var len = 0
69
+ private [ this ] var exported : Boolean = false
70
+ private [ this ] var len = 0
71
71
72
72
protected def underlying : List [A ] = start
73
73
@@ -143,7 +143,7 @@ final class ListBuffer[A]
143
143
def update (n : Int , x : A ) {
144
144
// We check the bounds early, so that we don't trigger copying.
145
145
if (n < 0 || n >= len) throw new IndexOutOfBoundsException (n.toString)
146
- if (exported) copy ()
146
+ ensureUnaliased ()
147
147
if (n == 0 ) {
148
148
val newElem = new :: (x, start.tail)
149
149
if (last0 eq start) {
@@ -171,15 +171,10 @@ final class ListBuffer[A]
171
171
* @return this $coll.
172
172
*/
173
173
def += (x : A ): this .type = {
174
- if (exported) copy()
175
- if (isEmpty) {
176
- last0 = new :: (x, Nil )
177
- start = last0
178
- } else {
179
- val last1 = last0
180
- last0 = new :: (x, Nil )
181
- last1.tl = last0
182
- }
174
+ ensureUnaliased()
175
+ val last1 = new :: [A ](x, Nil )
176
+ if (len == 0 ) start = last1 else last0.tl = last1
177
+ last0 = last1
183
178
len += 1
184
179
this
185
180
}
@@ -209,7 +204,7 @@ final class ListBuffer[A]
209
204
* @return this $coll.
210
205
*/
211
206
def +=: (x : A ): this .type = {
212
- if (exported) copy ()
207
+ ensureUnaliased ()
213
208
val newElem = new :: (x, start)
214
209
if (isEmpty) last0 = newElem
215
210
start = newElem
@@ -228,7 +223,7 @@ final class ListBuffer[A]
228
223
def insertAll (n : Int , seq : Traversable [A ]) {
229
224
// We check the bounds early, so that we don't trigger copying.
230
225
if (n < 0 || n > len) throw new IndexOutOfBoundsException (n.toString)
231
- if (exported) copy ()
226
+ ensureUnaliased ()
232
227
var elems = seq.toList.reverse
233
228
len += elems.length
234
229
if (n == 0 ) {
@@ -276,7 +271,7 @@ final class ListBuffer[A]
276
271
if (count < 0 ) throw new IllegalArgumentException (" removing negative number of elements: " + count.toString)
277
272
else if (count == 0 ) return // Nothing to do
278
273
if (n < 0 || n > len - count) throw new IndexOutOfBoundsException (" at " + n.toString + " deleting " + count.toString)
279
- if (exported) copy ()
274
+ ensureUnaliased ()
280
275
val n1 = n max 0
281
276
val count1 = count min (len - n1)
282
277
if (n1 == 0 ) {
@@ -327,7 +322,7 @@ final class ListBuffer[A]
327
322
def prependToList (xs : List [A ]): List [A ] = {
328
323
if (isEmpty) xs
329
324
else {
330
- if (exported) copy ()
325
+ ensureUnaliased ()
331
326
last0.tl = xs
332
327
toList
333
328
}
@@ -345,7 +340,7 @@ final class ListBuffer[A]
345
340
*/
346
341
def remove (n : Int ): A = {
347
342
if (n < 0 || n >= len) throw new IndexOutOfBoundsException (n.toString())
348
- if (exported) copy ()
343
+ ensureUnaliased ()
349
344
var old = start.head
350
345
if (n == 0 ) {
351
346
start = start.tail
@@ -371,7 +366,7 @@ final class ListBuffer[A]
371
366
* @return this $coll.
372
367
*/
373
368
override def -= (elem : A ): this .type = {
374
- if (exported) copy ()
369
+ ensureUnaliased ()
375
370
if (isEmpty) {}
376
371
else if (start.head == elem) {
377
372
start = start.tail
@@ -439,6 +434,9 @@ final class ListBuffer[A]
439
434
}
440
435
441
436
// Private methods
437
+ private def ensureUnaliased () = {
438
+ if (exported) copy()
439
+ }
442
440
443
441
/** Copy contents of this buffer */
444
442
private def copy () {
0 commit comments