I would like to write a SparseVector[T]
class where T
can be a double, an int or a boolean.
The class will not be backed by an array (because I want a sparse data structure) but I have seen that when I build an empty array of an AnyVal
type, the elements are initialized to the default value. For instance:
scala> new Array[Int](10)
res0: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> new Array[Boolean](10)
res1: Array[Boolean] = Array(false, false, false, false, false, false, false, false, false, false)
scala> new Array[Double](10)
res2: Array[Double] = Array(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
How can I include this default value in my class ? The behaviour I would like to get is:
val v = new SparseVector[Double](100)
println( v(12) ) // should print '0.0'
val w = new SparseVector[Boolean](100)
println( v(85) ) // should print 'false'
Thanks