Skip to content

Commit

Permalink
fixup! [bug#3088][bug#12121] Fix adding/subtracting ListBuffer to/fro…
Browse files Browse the repository at this point in the history
…m itself

if instead of match, for Jason
  • Loading branch information
NthPortal committed Oct 5, 2020
1 parent 0b00869 commit 7215b43
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
13 changes: 6 additions & 7 deletions src/library/scala/collection/mutable/Growable.scala
Expand Up @@ -58,13 +58,12 @@ trait Growable[-A] extends Clearable {
*/
@nowarn("msg=will most likely never compare equal")
def addAll(xs: IterableOnce[A]): this.type = {
xs match {
case xs: AnyRef if xs eq this => addAll(Buffer.from(xs)) // avoid mutating under our own iterator
case xs =>
val it = xs.iterator
while (it.hasNext) {
addOne(it.next())
}
if (xs.asInstanceOf[AnyRef] eq this) addAll(Buffer.from(xs)) // avoid mutating under our own iterator
else {
val it = xs.iterator
while (it.hasNext) {
addOne(it.next())
}
}
this
}
Expand Down
39 changes: 19 additions & 20 deletions src/library/scala/collection/mutable/ListBuffer.scala
Expand Up @@ -120,30 +120,29 @@ class ListBuffer[A]

// Overridden for performance
override final def addAll(xs: IterableOnce[A]): this.type = {
xs match {
case xs: AnyRef if xs eq this => // avoid mutating under our own iterator
if (len > 0) {
ensureUnaliased()
val copy = ListBuffer.from(this)
last0.next = copy.first
last0 = copy.last0
len *= 2
}
case xs =>
val it = xs.iterator
if (it.hasNext) {
ensureUnaliased()
if (xs.asInstanceOf[AnyRef] eq this) { // avoid mutating under our own iterator
if (len > 0) {
ensureUnaliased()
val copy = ListBuffer.from(this)
last0.next = copy.first
last0 = copy.last0
len *= 2
}
} else {
val it = xs.iterator
if (it.hasNext) {
ensureUnaliased()
val last1 = new ::[A](it.next(), Nil)
if (len == 0) first = last1 else last0.next = last1
last0 = last1
len += 1
while (it.hasNext) {
val last1 = new ::[A](it.next(), Nil)
if (len == 0) first = last1 else last0.next = last1
last0.next = last1
last0 = last1
len += 1
while (it.hasNext) {
val last1 = new ::[A](it.next(), Nil)
last0.next = last1
last0 = last1
len += 1
}
}
}
}
this
}
Expand Down
18 changes: 10 additions & 8 deletions src/library/scala/collection/mutable/Shrinkable.scala
Expand Up @@ -60,14 +60,16 @@ trait Shrinkable[-A] {
loop(xs.tail)
}
}
xs match {
case xs: AnyRef if xs eq this =>
xs match {
case xs: Clearable => xs.clear()
case xs => subtractAll(Buffer.from(xs))
}
case xs: collection.LinearSeq[A] => loop(xs)
case xs => xs.iterator.foreach(subtractOne)
if (xs.asInstanceOf[AnyRef] eq this) { // avoid mutating under our own iterator
xs match {
case xs: Clearable => xs.clear()
case xs => subtractAll(Buffer.from(xs))
}
} else {
xs match {
case xs: collection.LinearSeq[A] => loop(xs)
case xs => xs.iterator.foreach(subtractOne)
}
}
this
}
Expand Down

0 comments on commit 7215b43

Please sign in to comment.