This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Vector filter issue

No replies
calathus
Joined: 2010-03-22,
User offline. Last seen 2 years 28 weeks ago.

Hello,
I found a problem for the filter(filterNot) method of Vector class(I
used them to remove element).
I'm using Scala version 2.8.0.Beta1-prerelease.

it will crash while executing code using filter.

The test program will create Vector then remove all the elements.
(This was created to compare the perofrmance with other collection
construct, like List, etc)

while it run without crash with 200, but it will crash with 2000.

comparison(200) // OK
comparison(2000) // this will crach

I'll attach the whole test program.
I found Vector has very good performance for append operation, but
unfortunately, remove operation were crashed.

Is this know problem?
regards,
nc

P.S.
This program is using M[_], I tried to use generic type for the
parameter type(like (updateM[M[_], T] ..), but with no success. Are
there any good way to achieve this?

----

package scalax.collection.immutable

object vectorTest {

class BenchmarkA(num: Int) {
def ctime = System.currentTimeMillis()
def duration(startTime: Long) = ctime - startTime
def exec[T](test: => T): (T, Long) = { val t = ctime; (test, duration(t))}

//
type A = Int
def A(i: Int): A = i
/*
type A = List[Int]
def A(i: Int): A = List(i)
*/
def updateM[M[_]](ms: M[A], update: (M[A], A)=>M[A]): M[A] = {
var is = ms
for (i <- 0 until num) is = update(is, A(i))
is
}

//
def vectorAppend: Vector[A] = updateM[Vector](Vector(), (as, a)=>{
val v = (as :+ a)
//println("==>append: i: "+i1+", v: "+v)
v
})
// this will crash, Vector bug!
def vectorRemove(vec: Vector[A]): Vector[A] = updateM[Vector](vec,
(as, a)=>{
val v = (as filterNot{ _ == a})
//val v = (is filter{ _ != i})
//println("==>remove: i: "+i+", v: "+v)
v
})

//
type CT = Vector[A]
def appendTest: CT = {
val (v, t) = exec(vectorAppend)
println(" append [num: "+num+"] vec: "+t)
//println(" v: "+v)
v
}

def removeTest(ct: CT): Unit = {
val (v, t) = exec(vectorRemove(ct))
println(" remove [num: "+num+"] vec: "+t)
//println(" v: "+v)
}

def testRun: Unit = {
val ct = appendTest
removeTest(ct)
}
} // BenchmarkA

def createBenchmarkA(num: Int): BenchmarkA = new BenchmarkA(num)

def comparison(num: Int): Unit = {
for (i <- 1 until 5) createBenchmarkA(num*i).testRun
println(">> comparison done, num: "+num);
}

def main(args: Array[String]): Unit = {
try {
//createBenchmarkA(23).testRun

comparison(200) // OK
comparison(2000) // this will crach

} catch {
case e: Exception => e.printStackTrace()
}
}
}
/*
run:
append [num: 200] vec: 31
remove [num: 200] vec: 16
append [num: 400] vec: 15
remove [num: 400] vec: 0
append [num: 600] vec: 0
remove [num: 600] vec: 16
append [num: 800] vec: 0
remove [num: 800] vec: 31
>> comparison done, num: 200
append [num: 2000] vec: 0
java.lang.NullPointerException
at scala.collection.immutable.VectorPointer$class.gotoPos(Vector.scala:727)
at scala.collection.immutable.Vector.gotoPos(Vector.scala:36)
at scala.collection.immutable.VectorBuilder.result(Vector.scala:631)
at scala.collection.immutable.VectorBuilder.result(Vector.scala:603)
at scala.collection.TraversableLike$class.filter(TraversableLike.scala:273)
at scala.collection.immutable.Vector.filter(Vector.scala:36)
at scala.collection.TraversableLike$class.filterNot(TraversableLike.scala:282)
at scala.collection.immutable.Vector.filterNot(Vector.scala:36)
at scalax.collection.immutable.vectorTest$BenchmarkA$$anonfun$vectorRemove$1.apply(testVector.scala:25)
at scalax.collection.immutable.vectorTest$BenchmarkA$$anonfun$vectorRemove$1.apply(testVector.scala:24)
at scalax.collection.immutable.vectorTest$BenchmarkA$$anonfun$updateM$1.apply(testVector.scala:13)
at scalax.collection.immutable.vectorTest$BenchmarkA$$anonfun$updateM$1.apply(testVector.scala:13)
at scala.collection.immutable.Range$ByOne$class.foreach(Range.scala:171)
at scala.collection.immutable.Range$$anon$2.foreach(Range.scala:153)
at scalax.collection.immutable.vectorTest$BenchmarkA.updateM(testVector.scala:13)
at scalax.collection.immutable.vectorTest$BenchmarkA.vectorRemove(testVector.scala:24)
at scalax.collection.immutable.vectorTest$BenchmarkA$$anonfun$3.apply(testVector.scala:40)
at scalax.collection.immutable.vectorTest$BenchmarkA$$anonfun$3.apply(testVector.scala:40)
at scalax.collection.immutable.vectorTest$BenchmarkA.exec(testVector.scala:8)
at scalax.collection.immutable.vectorTest$BenchmarkA.removeCompariosn(testVector.scala:40)
at scalax.collection.immutable.vectorTest$BenchmarkA.testRun(testVector.scala:47)
at scalax.collection.immutable.vectorTest$$anonfun$comparison$1.apply(testVector.scala:54)
at scalax.collection.immutable.vectorTest$$anonfun$comparison$1.apply(testVector.scala:54)
at scala.collection.immutable.Range$ByOne$class.foreach(Range.scala:171)
at scala.collection.immutable.Range$$anon$2.foreach(Range.scala:153)
at scalax.collection.immutable.vectorTest$.comparison(testVector.scala:54)
at scalax.collection.immutable.vectorTest$.main(testVector.scala:63)
at scalax.collection.immutable.vectorTest.main(testVector.scala)
*/

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland