nicolasstucki/scala-rrb-vector

exception when prepending

Opened this issue · 2 comments

Hi,

I'm trying to step through a test I'm writing, but it seems the issue might be in RRBVector

In the simplest case I can reproduce with:

import org.scalatest._
import scala.collection.immutable.rrbvector._

class MyTest extends FlatSpec {

  "A Vector" should "prepend in a range up to 2048 items" in {
    var vec = RRBVector.empty[Int]

    for (i <- 0 to 2048)
    {
      vec =  i +: vec
    }

    for (i <- 0 to 2048) 
    {
      assert(vec(i).equals(i))
    }
  }
}

I get the following error:

java.lang.NullPointerException was thrown.
java.lang.NullPointerException
	at scala.collection.immutable.rrbvector.RRBVectorPointer$class.treeSizeRec$1(RRBVector.scala:2936)
	at scala.collection.immutable.rrbvector.RRBVectorPointer$class.treeSize(RRBVector.scala:2947)
	at scala.collection.immutable.rrbvector.RRBVectorPointer$class.withRecomputeSizes(RRBVector.scala:2858)
	at scala.collection.immutable.rrbvector.RRBVector.withRecomputeSizes(RRBVector.scala:53)
	at scala.collection.immutable.rrbvector.RRBVectorPointer$class.setupNewBlockInInitBranch(RRBVector.scala:2146)
	at scala.collection.immutable.rrbvector.RRBVector.setupNewBlockInInitBranch(RRBVector.scala:53)
	at scala.collection.immutable.rrbvector.RRBVector.prependFrontNewBlock(RRBVector.scala:381)
	at scala.collection.immutable.rrbvector.RRBVector.prepend(RRBVector.scala:324)
	at scala.collection.immutable.rrbvector.RRBVector.$plus$colon(RRBVector.scala:306)

I think the problem is with the sizes (relaxed indices?) here
since we're prepending we can't reuse the existing cache, and must always rebuild it

It might be. I will need some time to debug this one. Thanks for the input.