How to store a vector of N dimensions in a datatable in C #?

How would you store a vector of N dimensions in a datatable in C #?

+1


a source to share


2 answers


For truly n-dimensional stuff, you probably have to ditch the simpler concepts - a multidimensional array ( T[,...,]

) perhaps .

Things like jagged arrays ( T[]...[]

) or wrappers using List<T>

etc. are possible if the number of dimensions is known and constant (but> 1).

An example using Array

unknown size:

    int[] dimensions = { 3, 2, 5 }; // etc
    Array arr = Array.CreateInstance(
        typeof(int), dimensions);
    int[] index = {0,0,0}; // etc
    arr.SetValue(3, index);

      

But it's obviously easier to know the dimensions:



    int[, ,] arr = new int[3, 2, 5];
    arr[0, 0, 0] = 3;

      

The problem with multi-sized arrays is that they can quickly become too large for the CLR to touch ... which can use jagged arrays or other wrappers (splitting them into multiple smaller objects), but making the construction much more complicated:

    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 within a class, which is probably a sane approach.

+3


a source


If the number of dimensions is known in advance, you can simply create one "column" for each dimension.



+1


a source







All Articles