查找列表中哪些哈希与另一个哈希最快匹配的算法? (这在标题上解释起来很复杂)

标签 algorithm data-structures hash pattern-matching

用文字解释 2 个哈希值何时匹配是很复杂的,因此,请看示例: 哈希模式存储在如下列表中:(我使用 JavaScript 表示)

pattern:[
    0:{type:'circle', radius:function(n){ return n>10; }},
    1:{type:'circle', radius:function(n){ return n==2; }},
    2:{type:'circle', color:'blue', radius:5},
    ... etc]

var test = {type:'circle', radius:12};

test 应该与 pattern 0 匹配,因为 pattern[0].type==test.type && pattern.radius(test.radius)==true。

因此,尝试使用单词,如果散列的每个值都等于模式的值,或者在作为函数应用时返回 true,则散列匹配模式。

我的问题是:是否有一种算法可以在不测试所有模式的情况下找到与特定哈希匹配的所有模式?

最佳答案

考虑如下所示的动态递归决策树结构。

decision:[
    field:'type',
    values:[
        'circle': <another decision structure>,
        'square': 0, // meaning it matched, return this value
        'triangle': <another decision structure>
    ],
    functions:[
        function(n){ return n<12;}: <another decision structure>,
        function(n){ return n>12;}: <another decision structure>
    ],
    missing: <another decision structure>
]

d 上的算法(决策结构):

if test has field d.field
    if test[d.field] in d.values
        if d.values[test[d.field]] is a decision structure
            recurse using the new decision structure
        else
            yield d.values[test[d.field]]
    foreach f => v in d.functions
        if f(test[d.field])
            if v is a decision structure
                recurse using the new decision structure
            else
                yield v
else if d.missing is present
    if d.missing is a decision structure
        recurse using the new decision structure
    else
        yield d.missing
else
    No match

关于查找列表中哪些哈希与另一个哈希最快匹配的算法? (这在标题上解释起来很复杂),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12238689/

相关文章:

sprintf 的 Python 等价物

algorithm - 特定情况下的子集和

Ruby 算法语法

c - 在 C 中声明一个动态数组?

hash - 加盐您的密码 : Best Practices?

algorithm - PHP中有双向哈希算法吗?

仅查找权重为 1 和 2 的生成树的算法

algorithm - 如何将一组集合减少到没有元素是另一个元素的子集?

c - 我怎样才能构建这棵树?

algorithm - 最小堆到最大堆,比较