o
    8D©iÅ  ã                   @   s„   d dl Zd dlmZmZ ejej ZG dd„ deƒZddej	e dej
ej dejfdd	„Zdejd
edejeejf fdd„ZdS )é    N)ÚEnumÚautoc                   @   s   e Zd Zeƒ Zeƒ Zeƒ ZdS )Ú
TrieResultN)Ú__name__Ú
__module__Ú__qualname__r   ÚFAILEDÚPREFIXÚEXISTS© r   r   úE/var/www/Datamplify/venv/lib/python3.10/site-packages/sqlglot/trie.pyr      s    
r   ÚkeywordsÚtrieÚreturnc                 C   s@   |du ri n|}| D ]}|}|D ]}|  |i ¡}qd|d< q
|S )a<  
    Creates a new trie out of a collection of keywords.

    The trie is represented as a sequence of nested dictionaries keyed by either single
    character strings, or by 0, which is used to designate that a keyword is in the trie.

    Example:
        >>> new_trie(["bla", "foo", "blab"])
        {'b': {'l': {'a': {0: True, 'b': {0: True}}}}, 'f': {'o': {'o': {0: True}}}}

    Args:
        keywords: the keywords to create the trie from.
        trie: a trie to mutate instead of creating a new one

    Returns:
        The trie corresponding to `keywords`.
    NTr   )Ú
setdefault)r   r   ÚkeyÚcurrentÚcharr   r   r   Únew_trie   s   
r   r   c                 C   sV   |st j| fS | }|D ]}||vrt j|f  S || }qd|v r&t j|fS t j|fS )a&  
    Checks whether a key is in a trie.

    Examples:
        >>> in_trie(new_trie(["cat"]), "bob")
        (<TrieResult.FAILED: 1>, {'c': {'a': {'t': {0: True}}}})

        >>> in_trie(new_trie(["cat"]), "ca")
        (<TrieResult.PREFIX: 2>, {'t': {0: True}})

        >>> in_trie(new_trie(["cat"]), "cat")
        (<TrieResult.EXISTS: 3>, {0: True})

    Args:
        trie: The trie to be searched.
        key: The target key.

    Returns:
        A pair `(value, subtrie)`, where `subtrie` is the sub-trie we get at the point
        where the search stops, and `value` is a TrieResult value that can be one of:

        - TrieResult.FAILED: the search was unsuccessful
        - TrieResult.PREFIX: `value` is a prefix of a keyword in `trie`
        - TrieResult.EXISTS: `key` exists in `trie`
    r   )r   r   r
   r	   )r   r   r   r   r   r   r   Úin_trie+   s   



r   )N)ÚtypingÚtÚenumr   r   ÚSequenceÚHashabler   r   ÚIterableÚOptionalÚDictr   ÚTupler   r   r   r   r   Ú<module>   s    ((