Skip to content

Commit d030172

Browse files
committed
Merge pull request scala#4415 from Ichoran/issue/9254
SI-9254 UnrolledBuffer appends in wrong position
2 parents d7aacbb + 302562f commit d030172

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/library/scala/collection/mutable/UnrolledBuffer.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ object UnrolledBuffer extends ClassTagTraversableFactory[UnrolledBuffer] {
208208
def newBuilder[T](implicit t: ClassTag[T]): Builder[T, UnrolledBuffer[T]] = new UnrolledBuffer[T]
209209

210210
val waterline = 50
211-
val waterlineDelim = 100
211+
val waterlineDelim = 100 // TODO -- fix this name! It's a denominator, not a delimiter. (But it's part of the API so we can't just change it.)
212212
private[collection] val unrolledlength = 32
213213

214214
/** Unrolled buffer node.
@@ -319,13 +319,15 @@ object UnrolledBuffer extends ClassTagTraversableFactory[UnrolledBuffer] {
319319
for (elem <- t) curr = curr append elem
320320
curr.next = newnextnode
321321

322-
// try to merge the last node of this with the newnextnode
322+
// try to merge the last node of this with the newnextnode and fix tail pointer if needed
323323
if (curr.tryMergeWithNext()) buffer.lastPtr = curr
324+
else if (newnextnode.next eq null) buffer.lastPtr = newnextnode
324325
}
325-
else if (idx == size) {
326+
else if (idx == size || (next eq null)) {
326327
var curr = this
327328
for (elem <- t) curr = curr append elem
328-
} else insertAll(idx - size, t, buffer)
329+
}
330+
else next.insertAll(idx - size, t, buffer)
329331
}
330332
private def nullout(from: Int, until: Int) {
331333
var idx = from
@@ -344,7 +346,7 @@ object UnrolledBuffer extends ClassTagTraversableFactory[UnrolledBuffer] {
344346
tryMergeWithNext()
345347
}
346348

347-
override def toString = array.take(size).mkString("Unrolled[" + array.length + "](", ", ", ")") + " -> " + (if (next ne null) next.toString else "")
349+
override def toString = array.take(size).mkString("Unrolled@%08x".format(System.identityHashCode(this)) + "[" + size + "/" + array.length + "](", ", ", ")") + " -> " + (if (next ne null) next.toString else "")
348350
}
349351

350352
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scala.collection.mutable
2+
3+
import org.junit.runner.RunWith
4+
import org.junit.runners.JUnit4
5+
import org.junit.Test
6+
7+
@RunWith(classOf[JUnit4])
8+
class UnrolledBufferTestTest {
9+
@Test
10+
def test_SI9254_original() {
11+
val b = new UnrolledBuffer[Int]()
12+
(1 to 16).foreach(i => b append i)
13+
b.insert(0,-1)
14+
b append 17
15+
assert(b sameElements (Seq(-1) ++ (1 to 16) ++ Seq(17)))
16+
}
17+
18+
@Test
19+
def test_SI9254_additional() {
20+
val b = new UnrolledBuffer[Int]()
21+
(1 to 100).foreach(i => b append i)
22+
b.insert(40, -1)
23+
assert(b sameElements((1 to 40) ++ Seq(-1) ++ (41 to 100)))
24+
}
25+
}

0 commit comments

Comments
 (0)