Question

I have a three dimensional static const array that is acting as a lookup table. Each cell of the array can return up to 8 separate numbers (each number is the corner of a cube - a voxel to be exact). So for example, MyStaticArray[0][1][1] might return 1,2,5,7. It can return a single number or a maximum of eight numbers, like so: 1,2,3,4,5,6,7,8.

I already have the lookup table prepared (on paper), my question is what the best way to store it? Initially I was going with a static const unsigned char[][][] but that is obviously not going to work in this case as the table can return more than one number (again, from 1-8).

My next solution was to return an unsigned char array. That is, static const unsigned char* [][][]. How do I initialize this (that is, how do I initialize the static const array to return to me unsigned char arrays that are also static so that I can access them without initializing a class)? Is there a better way?

I hope I was able to explain the question properly. If not, let me know and I will try to reword it and/or provide extra info.

Was it helpful?

Solution

You could pad each element up to the maximum length with dummy elements, and go with a 4D array:

static const unsigned char x[M][N][P][8] = { { 1, 2, 5, 7, -1, -1, -1, -1 },
                                             { 1, 2, 3, 4,  5,  6,  7,  8 },
                                             ...

or you could use individual bits as flags, e.g.:

static const unsigned char x[M][N][P] = { 0x53, // Bits 0, 1, 4, 6 are set
                                          0xFF,
                                          ...

If you find yourself needing more than 8 possibilities, upgrade to e.g. uint16_t or uint32_t.

Which method you go with depends on what you intend to do with the data.

OTHER TIPS

As you only return 0-8 out of 8 possible numbers, I think you should stick with static const unsigned char[][][] and store the "numbers" as an one byte bitfield where bit n is set if n is one of the used numbers for this cell, so f.e. 1,2,5,7 might be encoded as 01010011 binary or 0x53 hexadecimal.

To use static initialization you'll need to know each dimension of your array. I presume you already know this but did not mention it in your question, so I'll use the dimensions [2][2][2] for my examples.

You'll also need a way to determine the number of elements at each position in the array. You can put the number of elements as the first value of the list, or create a sentinel value to mark the end of the list.

To pad each list to 8 elements, it would look like this. Any uninitialized values in the array will be set to 0.

static const unsigned char array[2][2][2][8] = {{{{1,2,3,4,5},{1,2,3,4,5,6,7,8}},{{4},{5}}},{{{...}}}};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top