代码之家  ›  专栏  ›  技术社区  ›  BCS

如何处理非整数的离散集?

  •  1
  • BCS  · 技术社区  · 16 年前

    我有一个程序需要 从一组已知值映射到另一组已知值:

    in      out
    ------------
    8       37
    10      61
    12      92
    13 1/4  109
    15 1/4  151
    etc
    

    如果输入是整数或等距的,这将很容易实现。我将遍历行,但也希望能够在可读的manor中进行查找。

    enum Size
    {
       _8,
       _10,
       _12,
       _13_25,
       _15_25,
       // etc
    }
    

    有更好的主意吗?

    编辑: 我最关心的是限制我的能力 尝试 抬起头来。我想要一些东西 如果代码可能尝试查找无效的内容。

    集合很小,迭代次数几乎完全不相关。

    * 注意:我不担心用指针捕捉问题,而不是像for循环和变量分配这样的直接代码。


    细枝末节 :为了清晰和通用,我对上述内容进行了过度简化。实际上,我有一个表,它有3个非整数、非均匀轴和一个非数值轴。在这一点上,我不确定我需要什么方向来列举它。

    有几个链接可以让您了解我正在寻找的产品:

    Boost::SI 还有我的 D version 属于 the 相同的 idea

    6 回复  |  直到 8 年前
        1
  •  1
  •   Draemon    16 年前

    你不能用散列映射吗?

        2
  •  1
  •   AShelly    16 年前

    如果你的输入分数被限制在2分母的幂次范围内,你可以使用定点数字作为键。对于您的示例,使用1位=0.25(将每个输入乘以4),如下所示:

    IN maps to Key
    --         ---   
    8          32   
    10         40
    12         48 
    13 1/4     53
    15 1/4     61 
    
    KeyMin= 32
    

        3
  •  1
  •   RossFabricant    16 年前

    使用枚举会丢失数值,除非对变量名进行丑陋的分析。我会这样做:

    class Size
    {
        public decimal Val{get;set;}
        private Size(decimal val){this.val = val;}
        public static Size _8 = new Size(8.0);   
        //...
        public Dictionary<Size, Size> sizeMap = new Dictionary<Size, Size>
        {
            {_8, _37}, 
            //...
        };
    }
    
        4
  •  0
  •   David Norman    16 年前

    听起来你想用一个排序的二叉树。查找和迭代都很快,树不关心条目的间距。

    如果多个轴是独立的,可以为每个轴创建一个轴。

        5
  •  0
  •   jmucchiello    16 年前

    枚举的想法并不可怕,但我会动态地做。您有一个有效字符串的数组/列表。字符串列表的索引是映射的关键。

    // this could be loaded from a file potentially
    // notice that the keys have been sorted.
    const char* keys[] = { "10", "12", "13 1/4", "15 1/4", "8", 0 };
    float values[] = { 61, 92, 109, 151, 37, 0 };
    int key_count = 0;
    while (keys[key_count]) ++key_count;
    
    bool find(const char* key, float* val) {
       int idx = bsearch(key, keys, sizeof(const char*), key_count, strcmp);
       if (idx < 0) return false;
       *val = values[idx];
       return true;
    }
    

        6
  •  0
  •   eaanon01    16 年前

    这里有一个建议,你可以如何解决它。使用结构和数组。

    typedef struct{
        float input;
        int   output;
    }m_lookup;
    m_lookup in_out[] = 
    {   
        (float) 8   , 37,
        (float)10   , 61,
        (float)12   , 92,
        (float)13.25,109,
        (float)15.25,151,
    };
    
    int get_Var(float input)
    {
        int i=0;
        for(i=0;i<sizeof(in_out);i++)
            if(in_out[i].input == input)
                return in_out[i].output;
        // Here you could make some special code for your compiler
        return 0;
    }
    int main(void)
    {
        printf("Input 15.25 : Output %d\n",get_Var(15.25));
        printf("Input 13,25 : Output %d\n",get_Var(13.25));
        printf("Illegal input:\n");
        printf("Input 5 : Output %d\n",get_Var(5));
        system( "pause" );
        return 0;
    }
    

    如果你再多解释一些细节的话,我可能会做些调整。

    enum Size
    {
       i_8=37,
       i_10=61,
       i_12=92,
       i_13_25=109,
       i_15_25=151,
       // etc
    }