
    Pi-                         d dl mZmZmZmZmZ d dlmZmZ  G d deee                            Z	 G d dee                   Z
dS )    )AnyDictListOptionalSequence)BaseNodeTc                        e Zd ZdZdZddee         dedef fdZ	dde
eeef                  f fd	Zd
ee         fdZd
eeef         fdZ xZS )BatcheraY  Batcher node batches the data from the source node into batches of size batch_size.
    If the source node is exhausted, it will return the batch or raise StopIteration.
    If drop_last is True, the last batch will be dropped if it is smaller than batch_size.
    If drop_last is False, the last batch will be returned even if it is smaller than batch_size.

    Args:
        source (BaseNode[T]): The source node to batch the data from.
        batch_size (int): The size of the batch.
        drop_last (bool): Whether to drop the last batch if it is smaller than batch_size. Default is True.
    sourceT
batch_size	drop_lastc                 r    t                                                       || _        || _        || _        d S N)super__init__r   r   r   )selfr   r   r   	__class__s       i/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchdata/nodes/batch.pyr   zBatcher.__init__   s2    $"    Ninitial_statec                     t                                          |           |'| j                            || j                            d S | j                                         d S r   )r   resetr   
SOURCE_KEYr   r   r   s     r   r   zBatcher.reset    s\    m$$$$KmDO<=====Kr   returnc                    g }t          |          | j        k     rm	 t          | j                  }n# t          $ r Y nKw xY w|                    |           t          |          | j        k    r|S t          |          | j        k     mt          |          | j        k    r|S t          |          r	| j        s|S t	                      r   )lenr   nextr   StopIterationappendr   )r   batchitems      r   r   zBatcher.next'   s    %jj4?**DK((    LL5zzT_,, %jj4?** u::((LZZ 	" 	"L//!s   1 
>>c                 B    | j         | j                                        iS r   )r   r   
state_dictr   s    r   	get_statezBatcher.get_state9   s    !7!7!9!9::r   )Tr   )__name__
__module____qualname____doc__r   r   r	   intboolr   r   r   strr   r   r   r   r'   __classcell__r   s   @r   r   r      s        	 	 J# #x{ # # # # # # # #   8DcN#;            "d1g " " " "$;4S> ; ; ; ; ; ; ; ;r   r   c                        e Zd ZdZdZdZdeee                  f fdZ	dde
eeef                  f fdZdefd	Zdeeef         fd
Z xZS )	UnbatcherzUnbatcher will flatten batches pulled from source, and
    yields elements in sequential order when next() is called on it.

    Args:
        source (BaseNode[T]): The source node to pull batches from.
    r   	batch_idxc                 X    t                                          |            || _        d S r   )r   r   r   )r   r   r   s     r   r   zUnbatcher.__init__H   s&    r   Nr   c                    t                                          |           || j                            || j                            || j                 | _        	 t          | j                  | _        || j                 | _        d S # t          $ r g | _        d| _        Y d S w xY w| j                                         g | _        d | _        d| _        d S )Nr   )
r   r   r   r   _cached_state_dictr   _batchBATCH_IDX_KEY
_batch_idxr    r   s     r   r   zUnbatcher.resetL   s    m$$$$KmDO<===&3DO&DD#$"4;//"/0B"C  $ $ $ !"#	$ KDK&*D#DOOOs   +B
 
B&%B&r   c                 <   | j         t          | j                  k    r[| j                                        | _        t          | j                  | _        d| _         | j         t          | j                  k    [| xj         dz  c_         | j        | j         dz
           S )Nr      )r9   r   r7   r   r%   r6   r   r&   s    r   r   zUnbatcher.next_   s    oT[!1!111&*k&<&<&>&>D#t{++DKDO oT[!1!111
 	1{4?Q.//r   c                     | j         | j                                        | _         | j        | j         | j        | j        iS r   )r6   r   r%   r   r8   r9   r&   s    r   r'   zUnbatcher.get_stateh   sA    "*&*k&<&<&>&>D# OT4
 	
r   r   )r(   r)   r*   r+   r   r8   r   r   r	   r   r   r   r.   r   r   r   r'   r/   r0   s   @r   r2   r2   =   s          JMx4         8DcN#;            &0a 0 0 0 0
4S> 
 
 
 
 
 
 
 
r   r2   N)typingr   r   r   r   r   torchdata.nodes.base_noder   r	   r   r2    r   r   <module>r@      s    7 6 6 6 6 6 6 6 6 6 6 6 6 6 1 1 1 1 1 1 1 1.; .; .; .; .;htAw .; .; .;b2
 2
 2
 2
 2
 2
 2
 2
 2
 2
r   