我有一个程序需要执行从一组已知值到另一组已知值的编译时可检查映射:
in out
------------
8 37
10 61
12 92
13 1/4 109
15 1/4 151
etc
如果输入是整数或均匀分布,这将很容易。 我将迭代行,但也希望能够在可读的庄园中进行查找。
我目前的想法(我不喜欢)是定义一个类似的枚举
enum Size
{
_8,
_10,
_12,
_13_25,
_15_25,
// etc
}
,然后将其设置为两次查找。
还有更好的想法吗?
编辑:我主要关心的是限制我尝试查找的内容。 如果代码可能尝试查找无效的内容,我希望某些内容甚至无法编译。
该集合很小,迭代时间几乎完全无关。
我还没有看到任何比枚举能给我带来任何好处的东西,所以现在我就这样做。 OTOH我会继续关注这个问题。
*
注意:我并不担心捕获指针问题以及其他问题,只是简单的代码,例如 for 循环和变量赋值。
本质:为了清晰和普遍性,我对上面的内容进行了过度简化。 我实际上有一个表,其中有 3 个非整数、非均匀轴和一个非数字轴。 此时,我不确定我需要在哪些方向中枚举它。
一些链接可以让您了解我正在寻找的内容:
Boost::SI 和我的 D 版本 相同
I have a program that needs to do a compile time checkable map from one known set of values to another known set of values:
in out
------------
8 37
10 61
12 92
13 1/4 109
15 1/4 151
etc
This would be easy if the inputs were either integers or evenly spaced. I'm going to be iterating over the rows but also want to be able to do lookups in a readable manor.
My current thought (that I'm not liking) is to define an enum like
enum Size
{
_8,
_10,
_12,
_13_25,
_15_25,
// etc
}
and then set it up for 2 lookups.
Any better ideas?
Edit: My primary concern is limiting what I can try to look up. I'd like stuff to not even compile if the code might try and look up something that is invalid.
The set is small and iteration times are almost totally irrelevant.
I haven't seen anything that gains me anything over the enum so for now I'm going with that. OTOH I'll keep watching this question.
*
Note: I'm not worried about catching issues with pointers and what not, just straight forward code like for loops and variable assignments.
The nitty grity: I over simplified the above for clarity and generality. I actually have a table that has 3 non-integer, non-uniform axes and one non-numeric axis. And at this point I'm not sure what directions I'm going to need to enumerate it in.
a few links to give a flavor of what I'm looking for:
Boost::SI and my D version of the same idea
发布评论
评论(6)
不能使用哈希映射吗?
Can't you use a hash map?
如果您的输入分数仅限于某个 2 的幂分母,您可以使用定点数作为键。 对于您的示例情况,使用 1 位 = 0.25(将每个输入乘以 4),如下所示:
然后您可以使用 Key-KeyMin 作为稀疏数组的索引,该数组包含用于无效条目的标志值,例如 -1。 优点是,如果您的密钥发生变化,它可以使您不必重新编码。 缺点是浪费内存。
If your input fractions are limited to some power-of-2 denominator, you could use fixed point numbers as Keys. For your example case, use 1 bit = 0.25, (multiply each input by 4) like so:
Then you can use Key-KeyMin as the index into a sparse array which contains a flag value like -1 for the invalid entries. The advantage is that it saves you from having to recode if your keys change. The disadvantage is wasted memory.
使用枚举会丢失数值,除非对变量名进行丑陋的解析。 我会这样做:
Using enums you lose the numeric value unless you do an ugly parse of the variable name. I would do this:
听起来你想要使用类似排序二叉树的东西。 查找和迭代都很快,并且树不会关心条目的间距。
如果您的多个轴是独立的,您可以为每个轴创建一个。
Sounds like you want to want to use something like a sorted binary tree. Both lookup and iteration are fast and the tree won't care about the spacing of the entries.
If your multiple axes are independent, you could create one for each axis.
枚举的想法并不太糟糕,但我会动态地进行。 您有一个有效字符串的数组/列表。 字符串列表的索引是地图的关键。
现在,你说过这里有不止一个维度。 这仅仅意味着您需要多个键数组。
The enum idea wasn't too terrible but I would do it dynamically. You have an array/list of valid strings. The index into the list of strings is your key to your map.
Now, you said something about there being more than one dimension here. That just means you need multiple keys arrays.
以下是有关如何解决该问题的建议。 使用结构体和数组。
如果你能多解释一下实质内容,我可能会做出一些调整。
如果您决定在编译时检查它,那么您可以像这样使用枚举:
Here is a suggestion to how you could solve it. Using structs and arrays.
I could probably make some sdjustments if you explain a little more about the The nitty grity.
If you are determend to get it checked at compile time then you can use the enum like this: