How would you store a vector of N dimensions in a datatable in C#?
For truly n-dimensional stuff, you're probably going to have to drop to simpler concepts - maybe a multi-dimensional array (T[,...,]
).
Things like jagged arrays (T[]...[]
) or wrappers using List<T>
etc are feasible if the number of dimensions is known and constant (but > 1).
An example using an Array
of unknown dimension:
int[] dimensions = { 3, 2, 5 }; // etc
Array arr = Array.CreateInstance(
typeof(int), dimensions);
int[] index = {0,0,0}; // etc
arr.SetValue(3, index);
But obviously it is easier if the dimensions are known:
int[, ,] arr = new int[3, 2, 5];
arr[0, 0, 0] = 3;
The problem with multi-dimension arrays is that they can quickly get too big for the CLR to touch... which is where jagged arrays or other wrappers come in handy (by splitting it into multiple smaller objects) - but making construction much harder:
int[][][] arr = new int[3][][];
for(int i = 0 ; i < 3 ; i++) {
arr[i] = new int[2][];
for(int j = 0 ; j < 2 ; j++) {
arr[i][j] = new int[5];
}
}
arr[0][0][0] = 3;
Any of these can usually be wrapped inside a class, which is probably the sensible approach.
If the number of dimensions is known in advance, you could just create one "column" per dimension.