Answer the question
In order to leave comments, you need to log in
Which data structure to choose for a multidimensional sparse matrix?
Omitting some minor details, there is a large sparse 2- or 3-dimensional matrix. How big? Well, the range of indices (for each dimension) is up to several million. The total number of actually occupied elements (for the entire matrix) is thousands, tens of thousands.
On the one hand, it is required to be able to quickly access an element by index (a[i][j][k]). On the other hand, we need the ability to quickly bypass all non-empty elements and do something with them (relatively speaking, a.forEach(...)). Speed is very important.
The matrix element is an object, so typed arrays are obviously excluded.
The question is in the data structure. At the moment I have the following ideas:
1. An array of arrays. The general principle is simplistically demonstrated by the code:
var a = [];
function set (i, j, k, value) {
if (a[i] === undefined) a[i] = [];
if (a[i][j] === undefined) a[i][j] = [];
a[i][j][k] = value;
return a[i][j][k];
}
function get (i, j, k) {
return (a[i] === undefined || a[i][j] === undefined) ? undefined : a[i][j][k];
}
var a = {};
function set (i, j, k, value) {
var key = i + '-' + j + '-' + k;
return (a[key] = value);
}
function get (i, j, k) {
var key = i + '-' + j + '-' + k;
return a[key];
}
Answer the question
In order to leave comments, you need to log in
> Array of arrays. Bold minus: it is not clear how to search for and bypass busy elements? Organize a full cycle with stopizziot indexes and checks for undefined? It will search until the global flood.
In fact, the pass
does not produce any undefined, only real-life keys and values will be traversed. No map or hashes are explicitly required here - it's all implemented in the language itself.
Without changes, you can create a matrix from nested sorted arrays, and they store an index and a reference to an array or value. The value can be an object or an index of an object in a separate list of all objects. I read something about asm js for a long time. Might be useful here. Given the changes, you can have a second matrix in which changes will occur. In the background, you can glue them. It seems there are some background workers. There are two matrices to consider when searching and traversing. Most likely there are ready-made implementations for your task in other languages.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question