summaryrefslogtreecommitdiffstats
path: root/sqlglot/trie.py
blob: a2341072281e6b6ba61106032bddcb9cf5fffde8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def new_trie(keywords):
    trie = {}

    for key in keywords:
        current = trie

        for char in key:
            current = current.setdefault(char, {})
        current[0] = True

    return trie


def in_trie(trie, key):
    if not key:
        return (0, trie)

    current = trie

    for char in key:
        if char not in current:
            return (0, current)
        current = current[char]

    if 0 in current:
        return (2, current)
    return (1, current)