
    Pi:                     ~    d dl mZmZmZmZ d dlmZmZ  G d dee                   Z G d dee                   Z	dS )    )AnyDictGenericOptional)BaseNodeTc                   x     e Zd ZdZddee         def fdZd Zde	e
ef         fdZd	e	e
ef         fd
Z xZS )Loadera  Wraps the root BaseNode (an iterator) and provides a stateful iterable interface.

    The state of the last-returned iterator is returned by the state_dict() method, and can be
    loaded using the load_state_dict() method.

    Args:
        root (BaseNode[T]): The root node of the data pipeline.
        restart_on_stop_iteration (bool): Whether to restart the iterator when it reaches the end. Default is True
    Trootrestart_on_stop_iterationc                     t                                                       || _        || _        d | _        d | _        d| _        d S )NF)super__init__r   r   _next_iter_state_dict_it_iter_for_state_dict)selfr   r   	__class__s      j/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchdata/nodes/loader.pyr   zLoader.__init__   sG    	)B&?C"04 +0!!!    c                 x   | j         t          |           | _         n| j        rd| _        | j         S | j        b| j                             | j                   d | _        | j        r3| j                                         s| j                             d            n| j                             d            | j         S )NF)initial_state)r   LoaderIteratorr   r   resetr   has_nextr   s    r   __iter__zLoader.__iter__"   s    8%d++DHH& 	(-D%8O%1HNN)CNDDD)-D&- %dh6G6G6I6I %t$$$HNN4   xr   
state_dictc                     || _         dS )zLoads a state_dict which will be used to initialize the next iter() requested
        from this loader.

        Args:
            state_dict (Dict[str, Any]): The state_dict to load. Should be generated from a call to state_dict().
        N)r   )r   r   s     r   load_state_dictzLoader.load_state_dict3   s     &0"""r   returnc                 n    | j         t          |            d| _        | j                                         S )a>  Returns a state_dict which can be passed to load_state_dict() in the future to
        resume iteration.

        The state_dict will come from the iterator returned by the most recent call to iter().
        If no iterator has been created, a new iterator will be created and the state_dict returned from it.
        NT)r   iterr   r   r   s    r   r   zLoader.state_dict<   s3     8JJJ(,D%x""$$$r   )T)__name__
__module____qualname____doc__r   r   boolr   r   r   strr   r    r   __classcell__r   s   @r   r
   r
      s         0 0Xa[ 0T 0 0 0 0 0 0"  "0$sCx. 0 0 0 0
%DcN 
% 
% 
% 
% 
% 
% 
% 
%r   r
   c                        e Zd ZdZdZdZdee         f fdZdde	e
eef                  f fdZd	efd
Zd Zd	e
eef         fdZ xZS )r   a  An iterator class that wraps a root node and works with the Loader class.

    The LoaderIterator object saves state of the underlying root node, and calls reset on the root node when
    the iterator is exhausted or on a reset call. We look one step ahead to determine if the iterator is exhausted.
    The state of the iterator is saved in the state_dict() method, and can be loaded on reset calls.

    Args:
        loader (Loader[T]): The loader object that contains the root node.
    num_yieldedr   loaderc                     t                                                       || _        |j        | _        d | _        d | _        d| _        d S Nr   )r   r   r.   r   _cached_item_cached_state_dict_num_yielded)r   r.   r   s     r   r   zLoaderIterator.__init__W   sI     	K	 <@r   Nr   c                    t                                          |           |8| j                            || j                            || j                 | _        n!| j                            d            d| _        d | _        d S r0   )r   r   r   ROOT_KEYNUM_YIELDED_KEYr3   r1   )r   r   r   s     r   r   zLoaderIterator.resetb   sw    m$$$$IOOM$-8999 -d.B CDIOOD!!! !D r   r!   c                     | j         ?	 |                                 | _        t          |           | _         n# t          $ r Y nw xY w| j         d uS N)r1   r   r2   nextStopIterationr   s    r   r   zLoaderIterator.has_nextl   sa    $*.//*;*;'$(JJ!!     ,,s   -7 
AAc                     | j         | j         }d | _         d | _        nt          | j                  }| xj        dz  c_        |S )N   )r1   r2   r9   r   r3   )r   items     r   r9   zLoaderIterator.nextw   sL    ($D $D&*D##	??DQr   c                 v    | j         | j         S | j        | j                                        | j        | j        iS r8   )r2   r5   r   r   r6   r3   r   s    r   	get_statezLoaderIterator.get_state   s>    ".**M49//11 $"3
 	
r   r8   )r$   r%   r&   r'   r6   r5   r
   r   r   r   r   r)   r   r   r(   r   r9   r?   r*   r+   s   @r   r   r   I   s          $OH	q		 	 	 	 	 	! !8DcN#; ! ! ! ! ! !	-$ 	- 	- 	- 	-  
4S> 
 
 
 
 
 
 
 
r   r   N)
typingr   r   r   r   torchdata.nodes.base_noder   r   r
   r    r   r   <module>rC      s    / / / / / / / / / / / / 1 1 1 1 1 1 1 1@% @% @% @% @%WQZ @% @% @%F>
 >
 >
 >
 >
Xa[ >
 >
 >
 >
 >
r   