Skip to content

Commit

Permalink
Inlined 'Array.tabulate' into 'DenseVector.tabulate'
Browse files Browse the repository at this point in the history
This allows to avoid boxing in the argument and return type of 'f'.
You  might be thinking "Boxing? but it is specialized!". Turns out
'Array.tabulate' is not, therefore it would call a non-specialized
version of the 'Function1' trait, which would effectively box both
the index and the produced value.
  • Loading branch information
Sergei Lebedev committed Apr 12, 2017
1 parent f0a5025 commit c3ff07a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion math/src/main/scala/breeze/linalg/Vector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,15 @@ trait VectorConstructors[Vec[T]<:Vector[T]] {
* @return
*/
def tabulate[@spec(Double, Int, Float, Long) V:ClassTag](size: Int)(f: Int=>V): Vec[V] = {
apply(Array.tabulate(size)(f))
val b = ArrayBuilder.make[V]()
b.sizeHint(size)
var i = 0
while (i < size) {
b += f(i)
i += 1
}

apply(b.result)
}

/**
Expand Down

0 comments on commit c3ff07a

Please sign in to comment.