Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For utmost performance you will probably want to avoid the garbage collector (GC) as much as possible. This means not creating tuple objects (i, j) and not boxing primitives. If your table will be fully populated (not sparse), then your idea of an Array of Arrays is reasonable. For best performance, however, I'd go with didierd's idea and pack everything into a single array, with accessors. Here's a possible implementation:</p> <pre><code>// specialize A to avoid boxing primitives such as Int, Double, ... class Table[@specialized A: Manifest](numRows: Int, numCols: Int) { val data = new Array[A](numRows*numCols) def checkIndices(i: Int, j: Int) { require (i &gt;= 0 &amp;&amp; i &lt; numRows &amp;&amp; j &gt;= 0 &amp;&amp; j &lt; numCols) } def apply(i: Int, j: Int): A = { checkIndices(i, j) data(i*numCols + j) } def update(i: Int, j: Int, x: A) { checkIndices(i, j) data(i*numCols + j) = x } } </code></pre> <p>You can use it naturally like this (note the nice syntax you get from the special <code>apply</code> and <code>update</code> methods):</p> <pre><code>scala&gt; val table = new Table[Double](2, 2) table: Table[Double] = Table$mcD$sp@4d22366e scala&gt; table(0, 0) = 3.0 scala&gt; table(1, 1) = table(0, 0) + 2.0 scala&gt; table(2, 1) java.lang.IllegalArgumentException: requirement failed ... </code></pre> <p>On the other hand, if the table is sparse, then it might better to go with a <code>Map</code> to save memory for all the empty entries. Note that although <code>Map</code> has fast <code>update</code> and <code>apply</code> methods (nearly constant) they're still quite a bit slower than array accesses (mostly due to pressure on the GC; <code>Map</code> is not specialized, both the keys and values must be heap allocated).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload