3

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};

pattern[0].type==test.type && pattern.radius(test.radius)==true であるため、test はパターン 0 と一致する必要があります。

したがって、単語で試してみると、すべての値がパターンの値と等しいか、関数として適用されたときに true を返す場合、ハッシュはパターンに一致します。

私の質問は: 特定のハッシュに一致するすべてのパターンをすべてテストせずに見つけるアルゴリズムはありますか?

4

1 に答える 1

1

次のような動的で再帰的な決定木の構造を考えてみましょう。

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
于 2012-09-02T19:39:00.023 に答える