Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create an efficient data structure for two-dimensional LINQ operations?
    primarykey
    data
    text
    <p>Problem: I have 2 kinds of objects, lets call them <code>Building</code> and <code>Improvement</code>. There are roughly 30 <code>Improvement</code> instances, while there can be 1-1000 <code>Building</code>s. For each combination of <code>Building</code> and <code>Improvement</code>, I have to perform some heavy calculation, and store the result in a <code>Result</code> object.</p> <p>Both <code>Building</code>s and <code>Improvement</code>s can be represented by an integer ID.</p> <p>I then need to be able to:</p> <ul> <li><strike> Access the <code>Result</code> for a given <code>Building</code> and <code>Improvement</code> efficiently</strike> (EDIT: see comment further down)</li> <li>Perform aggregations on the <code>Result</code>s for all <code>Improvement</code>s for a given <code>Building</code>, like .Sum() and .Average()</li> <li>Perform the same aggregations on the <code>Result</code>s for all <code>Building</code>s for a given <code>Improvement</code></li> </ul> <p>This will happen on a web-server back-end, so memory may be a concern, but speed is most important.</p> <p>Thoughts so far:</p> <ol> <li>Use a <code>Dictionary&lt;Tuple&lt;int, int&gt;, Result&gt;</code> with <code>&lt;BuildingID, ImprovementID&gt;</code> as key. This should give me speedy inserts and single lookups, but I am concerned about <code>.Where()</code> and <code>.Sum()</code> performance.</li> <li>Use a two-dimensional array, with one dimension for <code>BuildingID</code>s and one for <code>ImprovementID</code>s, and the <code>Result</code> as value. In addition, build two <code>Dictionary&lt;int, int&gt;</code> that map <code>BuildingID</code>s and <code>ImprovementID</code>s to their respective array row/column indexes. This could potentially mean max 1000+ <code>Dictionary</code>s, will this be a problem?</li> <li>Use a <code>List&lt;Tuple&lt;int, int, Result&gt;&gt;</code>. I think this may be the least efficient, with O(n) inserts, though I could be wrong.</li> </ol> <p>Am I missing an obvious better option here?</p> <p>EDIT: Turns out it is only the aggregated values (per <code>Building</code> and per <code>Improvement</code>) I am interested in; see my answer.</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.
 

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