Search code examples
cmultidimensional-arrayhashtableheap-memoryspace-complexity

Sparse Multidimensional Array taking huge space - HashTable better?


Is there a better approach to use of Multidimensional Arrays to compute values to be displayed in a table. Please note each of the dimension of the array is huge but is sparse. Can something like a HashTable be considered?

Output Table after the computation looks like this

Output Table after the computations


Solution

  • This answer is outdated, because the OP added the information, that the data is a sparse matrix


    Not really. Maybe a one dimensional array (would save the pointers to the dimensions - but that's err... pointless).

    An array is the data structure with the fewest metadata (because there is no metadata at all). So your approach can't be optimized much, if you really need to store all that data in memory.

    Any other data structure (tree, linked lists, etc.) would contain extra metadata and would therefore consume more memory.

    The only way for you to use less memory is to actually use less memory (by only loading the data into memory you really need and leaving the rest on your hard drive or whatever).

    You want to display a table, so maybe you can limit the rows you save in memory to an area slightly bigger than the viewport of your table (so you can scroll through the table fluently). Then you can dynamically compute and overwrite the rows according to the scroll state of your table.