三维静态常量数组的初始化

发布于 2024-10-01 19:27:30 字数 482 浏览 3 评论 0原文

我有一个三维静态常量数组,用作查找表。数组的每个单元格最多可以返回 8 个单独的数字(每个数字是立方体的角 - 准确地说是体素)。例如,MyStaticArray[0][1][1] 可能返回 1,2,5,7。它可以返回单个数字或最多八个数字,如下所示:1,2,3,4,5,6,7,8。

我已经准备好了查找表(在纸上),我的问题是存储它的最佳方式是什么?最初我打算使用 static const unsigned char[][][] 但这显然在这种情况下不起作用,因为表可以返回多个数字(同样,从 1-8 )。

我的下一个解决方案是返回一个无符号字符数组。即static const unsigned char* [][][]。我如何初始化它(也就是说,如何初始化静态 const 数组以返回给我也是静态的无符号 char 数组,以便我可以在不初始化类的情况下访问它们)?有更好的办法吗?

我希望我能够正确解释这个问题。如果没有,请告诉我,我会尝试重新措辞和/或提供额外信息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的背包 2024-10-08 19:27:30

您可以使用虚拟元素将每个元素填充到最大长度,并使用 4D 数组:

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 },
                                             ...

或者您可以使用各个位作为标志,例如:

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

如果您发现自己需要超过 8 个可能性,请升级到例如 uint16_t< /code> 或 uint32_t

使用哪种方法取决于您打算如何处理数据。

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.

晌融 2024-10-08 19:27:30

由于您只返回 8 个可能数字中的 0-8,因此我认为您应该坚持使用 static const unsigned char[][][] 并将“数字”存储为单字节位字段,其中位 <如果 n 是此单元格使用的数字之一,则设置 code>n,因此 fe 1,2,5,7 可能会编码为 01010011 二进制或 0x53 十六进制。

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.

毁我热情 2024-10-08 19:27:30

要使用静态初始化,您需要知道数组的每个维度。我想您已经知道这一点,但在您的问题中没有提及,因此我将使用维度 [2][2][2] 作为示例。

您还需要一种方法来确定数组中每个位置的元素数量。您可以将元素的数量作为列表的第一个值,或者创建一个哨兵值来标记列表的末尾。

要将每个列表填充到 8 个元素,它看起来像这样。数组中任何未初始化的值都将设置为 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}}},{{{...}}}};

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}}},{{{...}}}};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文