ó
„¾^Yc           @   sE   d  Z  d Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s*   Independent implementation of a Trie tree.t   Triet   TrieNodec         c   s1   x* t  t |  ƒ ƒ D] } |  | | d !Vq Wd  S(   Ni   (   t   ranget   len(   t   stringt   i(    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyt   _iterate_stringlike_objects   s    c           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s'   The object that manages the trie nodes.c         C   s   t  d d ƒ |  _ d S(   s   Initialize an empty trie.N(   R   t   Nonet   root(   t   self(    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyt   __init__   s    c         C   sj   |  j  } xJ t | ƒ D]< } | j | ƒ } | d k rL | j | g  ƒ } n  | } q W| j j | ƒ d S(   s(   Add the node data to the path described.N(   R   R   t   find_prefixR   t	   add_childt   datat   append(   R	   t   patht	   node_datat   nodet   prefixt   child(    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyt   add   s    	
c         C   sI   |  j  } x9 t | ƒ D]+ } | j | ƒ } | d k r; d S| } q W| S(   s'   Find a node based on the path provided.N(   R   R   R   R   (   R	   R   R   R   R   (    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyt   find   s    	
c         C   s   |  j  j ƒ  S(   s  Traverse this tree.

        This performs a depth-first pre-order traversal of children in this
        tree. It returns the results consistently by first sorting the
        children based on their prefix and then traversing them in
        alphabetical order.
        (   R   t   traverse(   R	   (    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyR   &   s    (   t   __name__t
   __module__t   __doc__R
   R   R   R   (    (    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyR       s
   		
	
c           B   sA   e  Z d  Z d d „ Z d „  Z d „  Z d d „ Z d „  Z RS(   s5   The majority of the implementation details of a Trie.c         C   s%   | p	 i  |  _  | |  _ | |  _ d S(   s-   Initialize a TrieNode with data and children.N(   t   childrenR   R   (   R	   R   R   R   (    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyR
   4   s    	c         C   s   d j  |  j |  j ƒ S(   s4   Generate an easy to read representation of the node.s   TrieNode(prefix={0}, data={1})(   t   formatR   R   (   R	   (    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyt   __repr__:   s    c         C   s   |  j  j | d ƒ S(   s™   Find the prefix in the children of this node.

        :returns: A child matching the prefix or None.
        :rtype: :class:`~TrieNode` or None
        N(   R   t   getR   (   R	   R   (    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyR   @   s    c         C   s#   t  | | | ƒ } | |  j | <| S(   sv   Create and add a new child node.

        :returns: The newly created node
        :rtype: :class:`~TrieNode`
        (   R   R   (   R	   R   R   R   t   new_node(    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyR   H   s    c         c   s_   |  j  s d SxK t |  j  j ƒ  ƒ D]4 } |  j  | } | Vx | j ƒ  D] } | VqH Wq# Wd S(   s-  Traverse children of this node.

        This performs a depth-first pre-order traversal of the remaining
        children in this sub-tree. It returns the results consistently by
        first sorting the children based on their prefix and then traversing
        them in alphabetical order.
        N(   R   t   sortedt   keysR   (   R	   R   R   (    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyR   R   s    	N(	   R   R   R   R   R
   R   R   R   R   (    (    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyR   1   s   		
N(   s   Tries   TrieNode(   R   t   __all__R   t   objectR    R   (    (    (    s4   /tmp/pip-build-EndXZ2/flake8/flake8/plugins/_trie.pyt   <module>   s   	&