三维静态常量数组的初始化
我有一个三维静态常量数组,用作查找表。数组的每个单元格最多可以返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用虚拟元素将每个元素填充到最大长度,并使用 4D 数组:
或者您可以使用各个位作为标志,例如:
如果您发现自己需要超过 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:
or you could use individual bits as flags, e.g.:
If you find yourself needing more than 8 possibilities, upgrade to e.g.
uint16_t
oruint32_t
.Which method you go with depends on what you intend to do with the data.
由于您只返回 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 bitn
is set ifn
is one of the used numbers for this cell, so f.e. 1,2,5,7 might be encoded as01010011
binary or0x53
hexadecimal.要使用静态初始化,您需要知道数组的每个维度。我想您已经知道这一点,但在您的问题中没有提及,因此我将使用维度 [2][2][2] 作为示例。
您还需要一种方法来确定数组中每个位置的元素数量。您可以将元素的数量作为列表的第一个值,或者创建一个哨兵值来标记列表的末尾。
要将每个列表填充到 8 个元素,它看起来像这样。数组中任何未初始化的值都将设置为 0。
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.