o
    Xi                     @  s   d Z ddlmZ dgZddlmZmZ ddlmZm	Z	 ddl
mZ ddlmZmZ e	ejejejf ZG dd deej eej Zd	S )
z&Utilities for traversing the IR graph.    )annotationsRecursiveGraphIterator)Iterator
Reversible)CallableUnion)Self)_core_enumsc                   @  sT   e Zd Zdddddd ddZd!ddZd"ddZd#ddZd$ddZd%ddZdS )&r   NF	recursivereverseenter_graph
exit_graph
graph_like	GraphLiker   #Callable[[_core.Node], bool] | Noner   boolr   "Callable[[GraphLike], None] | Noner   c                C  s.   || _ || _|| _| || _|| _|| _dS )a  Iterate over the nodes in the graph, recursively visiting subgraphs.

        This iterator allows for traversing the nodes of a graph and its subgraphs
        in a depth-first manner. It supports optional callbacks for entering and exiting
        subgraphs, as well as a callback `recursive` to determine whether to visit subgraphs
        contained within nodes.

        .. versionadded:: 0.1.6
            Added the `enter_graph` and `exit_graph` callbacks.

        Args:
            graph_like: The graph to traverse.
            recursive: A callback that determines whether to recursively visit the subgraphs
                contained in a node. If not provided, all nodes in subgraphs are visited.
            reverse: Whether to iterate in reverse order.
            enter_graph: An optional callback that is called when entering a subgraph.
            exit_graph: An optional callback that is called when exiting a subgraph.
        N)_graph
_recursive_reverse_recursive_node_iter	_iterator_enter_graph_exit_graph)selfr   r   r   r   r    r   E/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/traversal.py__init__   s   
zRecursiveGraphIterator.__init__returnr   c                 C  s   |  | j| _| S N)r   r   r   r   r   r   r   __iter__8   s   zRecursiveGraphIterator.__iter__
_core.Nodec                 C  s
   t | jS r!   )nextr   r"   r   r   r   __next__<   s   
zRecursiveGraphIterator.__next__graph._core.Graph | _core.Function | _core.GraphViewIterator[_core.Node]c                 c  sz    | j rt|n|}| jd ur| | |D ]}|V  | jd ur&| |s&q| |E d H  q| jd ur;| | d S d S r!   )r   reversedr   r   _iterate_subgraphsr   )r   r'   iterablenoder   r   r   r   ?   s   


z+RecursiveGraphIterator._recursive_node_iterr-   c              	   c  s    |j  D ]s}t|tjsq|jtjjkr>| j	d ur!| 	|j
 t|j
| j| j| j	| jdE d H  | jd ur=| |j
 q|jtjjkry| jrMt|j
n|j
}|D ]&}| j	d ur^| 	| t|| j| j| j	| jdE d H  | jd urx| | qRqd S Nr   )
attributesvalues
isinstancer	   Attrtyper
   AttributeTypeGRAPHr   valuer   r   r   r   GRAPHSr*   )r   r-   attrgraphsr'   r   r   r   r+   P   sB   





z)RecursiveGraphIterator._iterate_subgraphsc                 C  s   t | j| j| j | j| jdS r.   )r   r   r   r   r   r   r"   r   r   r   __reversed__o   s   z#RecursiveGraphIterator.__reversed__)
r   r   r   r   r   r   r   r   r   r   )r    r   )r    r$   )r'   r(   r    r)   )r-   r$   )r    r)   )	__name__
__module____qualname__r   r#   r&   r   r+   r:   r   r   r   r   r      s    
"


N)__doc__
__future__r   __all__collections.abcr   r   typingr   r   typing_extensionsr   onnx_irr	   r
   GraphFunction	GraphViewr   Noder   r   r   r   r   <module>   s   "