
    shj                        S SK rS SKrS SKJrJr  S SKJr  S SKJ	r	J
r
  S SKJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S S	KJrJr  / S
Qr\" SSS9r\" S5       " S S\5      5       r\" S5       " S S\5      5       r " S S\5      rS r " S S\\5      r  " S S\5      r!\" S5       " S S\5      5       r" " S S\\5      r#\" S 5       " S! S"\5      5       r$\" S#5       " S$ S%\\%\      5      5       r&g)&    N)ABCabstractmethod)deque)IteratorSized)AnyCallableLiteralOptionalTypeVar)functional_datapipe)_SnapshotState)IterDataPipe)_check_unpickable_fnStreamWrapper)ConcaterIterDataPipeDemultiplexerIterDataPipeForkerIterDataPipeMultiplexerIterDataPipeZipperIterDataPipe_T_coT)	covariantconcatc                   T    \ rS rSr% Sr\\   \S'   S\4S jrS\	4S jr
S\4S jrSrg	)
r      a  
Concatenates multiple Iterable DataPipes (functional name: ``concat``).

The resulting DataPipe will yield all the elements from the first input DataPipe, before yielding from the subsequent ones.

Args:
    datapipes: Iterable DataPipes being concatenated

Example:
    >>> # xdoctest: +REQUIRES(module:torchdata)
    >>> import random
    >>> from torchdata.datapipes.iter import IterableWrapper
    >>> dp1 = IterableWrapper(range(3))
    >>> dp2 = IterableWrapper(range(5))
    >>> list(dp1.concat(dp2))
    [0, 1, 2, 0, 1, 2, 3, 4]
	datapipesc                     [        U5      S:X  a  [        S5      e[        S U 5       5      (       d  [        S5      eXl        g )Nr   z/Expected at least one DataPipe, but got nothingc              3   B   #    U  H  n[        U[        5      v   M     g 7fN
isinstancer   .0dps     }/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/utils/data/datapipes/iter/combining.py	<genexpr>0ConcaterIterDataPipe.__init__.<locals>.<genexpr>4        D)B:b,//)   z(Expected all inputs to be `IterDataPipe`)len
ValueErrorall	TypeErrorr   selfr   s     r%   __init__ConcaterIterDataPipe.__init__1   s<    y>QNOOD)DDDFGG"    returnc              #   J   #    U R                    H  nU S h  vN   M     g  N	7fr   r   )r/   r$   s     r%   __iter__ConcaterIterDataPipe.__iter__8   s     ..BMM !s   #!
#c                     [        S U R                   5       5      (       a  [        S U R                   5       5      $ [        [	        U 5      R
                   S35      e)Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   r!   r   r"   s     r%   r&   /ConcaterIterDataPipe.__len__.<locals>.<genexpr>=        >~z"e$$~r)   c              3   8   #    U  H  n[        U5      v   M     g 7fr   r*   r"   s     r%   r&   r;   >        82s2ww   # instance doesn't have valid length)r,   r   sumr-   type__name__r/   s    r%   __len__ConcaterIterDataPipe.__len__<   J    >t~~>>>8888tDz2233VWXXr2   r5   N)rD   
__module____qualname____firstlineno____doc__tupler   __annotations__r0   r   r6   intrF   __static_attributes__ r2   r%   r   r      s:    $ \""#< #( Y Yr2   r   forkc                   F    \ rS rSrSr  SS\S\S\S\\S      4S	 jjr	S
r
g)r   C   a}  
Creates multiple instances of the same Iterable DataPipe (functional name: ``fork``).

Args:
    datapipe: Iterable DataPipe being copied
    num_instances: number of instances of the datapipe to create
    buffer_size: this restricts how far ahead the leading child DataPipe
       can read relative to the slowest child DataPipe.
       Defaults to ``1000``. Use ``-1`` for the unlimited buffer.
    copy: copy strategy to use for items yielded by each branch. Supported
        options are ``None`` for no copying, ``"shallow"`` for shallow object
        copies, and ``"deep"`` for deep object copies. Defaults to ``None``.

Note:
    All branches of the forked pipeline return the identical object unless
    the copy parameter is supplied. If the object is mutable or contains
    mutable objects, changing them in one branch will affect all others.

Example:
    >>> # xdoctest: +REQUIRES(module:torchdata)
    >>> from torchdata.datapipes.iter import IterableWrapper
    >>> source_dp = IterableWrapper(range(5))
    >>> dp1, dp2 = source_dp.fork(num_instances=2)
    >>> list(dp1)
    [0, 1, 2, 3, 4]
    >>> list(dp2)
    [0, 1, 2, 3, 4]
Ndatapipenum_instancesbuffer_sizecopyshallowdeepc                     US:  a  [        SU S35      eUS:X  a  U$ [        XX45      n[        U5       Vs/ s H  n[        XV5      PM     sn$ s  snf N   z,Expected `num_instances` larger than 0, but z	 is found)r+   _ForkerIterDataPiperange_ChildDataPipe)clsrU   rV   rW   rX   	containeris          r%   __new__ForkerIterDataPipe.__new__b   sd     1>}oYW  AO'S	6;M6JK6Jy,6JKKKs   ArQ     N)rD   rI   rJ   rK   rL   r   rO   r   r
   re   rP   rQ   r2   r%   r   r   C   sQ    B  59LL L 	L
 w012L Lr2   r   c                   t    \ rS rSrSr\S\4S j5       r\S\4S j5       r	\SS j5       r
\S\4S	 j5       rS
rg)_ContainerTemplates   zVAbstract class for container ``DataPipes``. The followings are three required methods.instance_idc                     g r   rQ   r/   rl   s     r%   get_next_element_by_instance/_ContainerTemplate.get_next_element_by_instancev       r2   r3   c                     g r   rQ   rE   s    r%   is_every_instance_exhausted._ContainerTemplate.is_every_instance_exhaustedz   rq   r2   Nc                     g r   rQ   rE   s    r%   reset_ContainerTemplate.reset~   rq   r2   c                     g)zSRaise TypeError if it's not supposed to be implemented to support `list(datapipe)`.NrQ   rn   s     r%   get_length_by_instance)_ContainerTemplate.get_length_by_instance   s    r2   rQ   r3   N)rD   rI   rJ   rK   rL   r   rO   ro   boolrs   rv   ry   rP   rQ   r2   r%   rj   rj   s   sk    a   T     c# c cr2   rj   c                     U $ r   rQ   )xs    r%   _no_opr      s    Hr2   c                       \ rS rSrSr  SS\S\S\S\\S      4S	 jjr	S
 r
S\4S jrS\4S jrS\S\4S jrSS jrS rS rS rS rSrg)r_      z
Container to hold instance-specific information on behalf of ForkerIterDataPipe.

It tracks the state of its child DataPipes, maintains the buffer, and yields the next value
as requested by the child DataPipes.
NrU   rV   rW   rX   rY   c                    Xl         S U l        X l        [        5       U l        X0l        U R
                  S:  a  [        R                  " S[        5        Uc  [        U l
        OGUS:X  a  [        R                  U l
        O+US:X  a  [        R                  U l
        O[        SU S35      eS/U-  U l        SU l        SU l        S U l        ['        U5       Vs/ s H  nSPM     snU l        g s  snf )Nr   zPUnlimited buffer size is set for `fork`, please be aware of OOM at random placesrZ   r[   zUnknown copy method `z5` requested, choose one of None, `shallow` or `deep`.T)main_datapipe_datapipe_iteratorrV   r   bufferrW   warningswarnUserWarningr   copy_fn
copymodulerX   deepcopyr+   child_pointersslowest_ptrleading_ptrend_ptrr`   _child_stop)r/   rU   rV   rW   rX   _s         r%   r0   _ForkerIterDataPipe.__init__   s     &;?*"W&aMM:
 <!DLY%??DLV^%..DL'v-bc 
 *
* &*6;M6J'K6J6J'K'Ks   C,c                 ,    [        U R                  5      $ r   r*   r   rE   s    r%   rF   _ForkerIterDataPipe.__len__       4%%&&r2   rl   c              #   Z  #    U R                   cn  U R                  U   (       aZ  [        U R                  5      U l         [        R
                  U l        [        U R                  5       H  nSU R                  U'   M      U R                  U   (       Gd  U R                  U==   S-  ss'   U R                  b.  U R                  U   U R                  :X  a  SU R                  U'   GOU R                  (       aL  U R                  U   U R                  ::  a/  U R                  U   U R                  -
  S-
  nU R                  U   nOEU R                  U   U l         [        U R                   5      nU R                  R                  U5        U R                  U   U R                  S-   :X  aE  [#        U R                  5      nU R                  U:  a   XPl        U R                  R%                  5         U R&                  S:  aC  U R                  U R&                  U R                  -   :  a  [)        SSU R&                   S3-   5      eU R+                  U5      v   U R                  U   (       d  GM  SU R                  U'   [-        U R                  5      (       a  S U l         U R/                  5         g g ! [          a,    SU R                  U'   S U l         U R                  U l
         GMb  f = f! SU R                  U'   [-        U R                  5      (       a  S U l         U R/                  5         f f = f7f)NFr^   Tr   z#ForkerIterDataPipe buffer overflow,zbuffer size  is insufficient.)r   r   iterr   r   	Iterating_snapshot_stater`   rV   r   r   r   r   r   nextappendStopIterationminpopleftrW   BufferErrorr   r,   _cleanup)r/   rl   rd   idx
return_valnew_mins         r%   ro   0_ForkerIterDataPipe.get_next_element_by_instance   s    ""*t/?/?/L&*4+=+=&>D##1#;#;D 4--.&+  # /-	 &&{33##K0A50LL,++K8DLLH48D$$[1;;4#6#6{#CtGWGW#W--k:T=M=MMPQQC!%S!1J'+':':;'GD$!%)$*A*A%B
**:6 &&{3t7G7G!7KK!++G '''1+2(++-$$)((4+;+;d>N>N+NN%=()9)9(::KLM 
 ll:..M &&{33P -1D[)4##$$*.' %3 ) !8<((526/'+'7'7 	!. -1D[)4##$$*.' %sF   A<L+?CK% 0J, CK% *AL+,1K"K% !K""K% %AL((L+r3   c                 V    U R                   S L=(       a    [        U R                  5      $ r   )r   r,   r   rE   s    r%   rs   /_ForkerIterDataPipe.is_every_instance_exhausted   s!    ||4'AC0@0@,AAr2   c                 ,    [        U R                  5      $ r   r   rn   s     r%   ry   *_ForkerIterDataPipe.get_length_by_instance   r   r2   c                     S U l         [        5       U l        S/U R                  -  U l        SU l        SU l        S U l        [        U R                  5       Vs/ s H  nSPM     snU l	        g s  snf Nr   T)
r   r   r   rV   r   r   r   r   r`   r   r/   r   s     r%   rv   _ForkerIterDataPipe.reset   sf    "&g cD$6$66*/0B0B*CD*CQD*CDDs   A-c                     U R                   U R                  U R                  U R                  U R                  U R
                  4n[        R                  b  [        R                  " U5      $ U$ r   )r   rV   rW   r   _valid_iterator_id_number_of_samples_yieldedr   getstate_hookr/   states     r%   __getstate__ _ForkerIterDataPipe.__getstate__   sa    LL##++
 %%1--e44r2   c                 2   Uu  U l         U l        U l        U l        U l        U l        S U l        [        5       U l        S/U R                  -  U l	        SU l
        SU l        S U l        [        U R                  5       Vs/ s H  nSPM     snU l        g s  snf r   )r   rV   rW   r   r   r   r   r   r   r   r   r   r   r`   r   r/   r   r   s      r%   __setstate__ _ForkerIterDataPipe.__setstate__  s     	
L#+"&g cD$6$66*/0B0B*CD*CQD*CDDs   ?Bc                     U R                   (       aD  U R                   R                  5       n[        R                  " U5        U R                   (       a  MC  g g r   )r   r   r   close_streams)r/   ds     r%   r   _ForkerIterDataPipe._cleanup  s4    kk##%A''* kkkr2   c                 $    U R                  5         g r   r   rE   s    r%   __del___ForkerIterDataPipe.__del__"      r2   )r   r   r   r   r   r   rW   r   r   r   r   r   rV   r   rg   r{   )rD   rI   rJ   rK   rL   r   rO   r   r
   r0   rF   ro   r|   rs   ry   rv   r   r   r   r   rP   rQ   r2   r%   r_   r_      s      59#L#L #L 	#L
 w012#LJ'3  3 jBT B'# '# 'EE"+
r2   r_   c                   b    \ rS rSr% SrSr\\S'   S\S\	4S jr
S rS	 rS
\	4S jrS
\4S jrSrg)ra   i&  a1  
Iterable Datapipe that is a child of a main DataPipe.

The instance of this class will pass its instance_id to get the next value from its main DataPipe.

Note:
    ChildDataPipe, like all other IterDataPipe, follows the single iterator per IterDataPipe constraint.
    Since ChildDataPipes share a common buffer, when an iterator is created for one of the ChildDataPipes,
    the previous iterators  for all ChildDataPipes must be invalidated, with the exception when a ChildDataPipe
    hasn't had an iterator created from it since the last invalidation. See the example below.

Example:
    >>> # xdoctest: +REQUIRES(module:torchdata)
    >>> # Singler Iterator per IteraDataPipe Invalidation
    >>> from torchdata.datapipes.iter import IterableWrapper
    >>> source_dp = IterableWrapper(range(10))
    >>> cdp1, cdp2 = source_dp.fork(num_instances=2)
    >>> it1, it2 = iter(cdp1), iter(cdp2)
    >>> it3 = iter(cdp1)
    >>> # The line above invalidates `it1` and `it2`, and resets `ForkerIterDataPipe`.
    >>> it4 = iter(cdp2)
    >>> # The line above doesn't invalidate `it3`, because an iterator for `cdp2` hasn't been created since
    >>> # the last invalidation.

Args:
    main_datapipe: Main DataPipe with a method 'get_next_element_by_instance(instance_id)'
    instance_id: integer identifier of this instance
T_is_child_datapiper   rl   c                 J    [        U[        5      (       d   eXl        X l        g r   )r!   rj   r   rl   )r/   r   rl   s      r%   r0   _ChildDataPipe.__init__F  s"    -);<<<<+8&r2   c                 L    U R                   R                  U R                  5      $ r   )r   ro   rl   rE   s    r%   r6   _ChildDataPipe.__iter__L  s!     !!>>t?O?OPPr2   c                 L    U R                   R                  U R                  5      $ r   )r   ry   rl   rE   s    r%   rF   _ChildDataPipe.__len__Q  s    !!889I9IJJr2   r3   c                    U R                   R                  c  SU R                   l        OU R                   R                  U R                  :X  as  U R                   =R                  S-  sl        U R                   R                  5       (       d  [        R                  " S[
        5        U R                   R                  5         U R                   R                  U l        U R                  $ )z
Update the valid iterator ID for both this DataPipe object and `main_datapipe`.

`main_datapipe.reset()` is called when the ID is incremented to a new generation.
r   r^   zSome child DataPipes are not exhausted when __iter__ is called. We are resetting the buffer and each child DataPipe will read from the start again.)r   r   rs   r   r   r   rv   rE   s    r%   $_set_main_datapipe_valid_iterator_id3_ChildDataPipe._set_main_datapipe_valid_iterator_idU  s     00845D1 22d6M6MM11Q61%%AACCY
 $$& #'"4"4"G"G&&&r2   c                 \    XR                   :H  =(       a    XR                  R                   :H  $ )zXCheck the valid iterator ID against that of DataPipe object and that of `main_datapipe`.)r   r   )r/   iterator_ids     r%   _check_valid_iterator_id'_ChildDataPipe._check_valid_iterator_idp  s-     222 E11DDD	
r2   )r   rl   r   N)rD   rI   rJ   rK   rL   r   r|   rN   r   rO   r0   r6   rF   r   r   rP   rQ   r2   r%   ra   ra   &  sK    :  $#'l ' 'Q
K'c '6
t 
r2   ra   demuxc                   P    \ rS rSrSr  SS\S\S\\/\	\   4   S\
S\4
S jjrS	rg
)r   ix  a  
Splits the input DataPipe into multiple child DataPipes, using the given classification function (functional name: ``demux``).

A list of the child DataPipes is returned from this operation.

Args:
    datapipe: Iterable DataPipe being filtered
    num_instances: number of instances of the DataPipe to create
    classifier_fn: a function that maps values to an integer within the range ``[0, num_instances - 1]`` or ``None``
    drop_none: defaults to ``False``, if ``True``, the function will skip over elements classified as ``None``
    buffer_size: this defines the maximum number of inputs that the buffer can hold across all child
        DataPipes while waiting for their values to be yielded.
        Defaults to ``1000``. Use ``-1`` for the unlimited buffer.

Examples:
    >>> # xdoctest: +REQUIRES(module:torchdata)
    >>> from torchdata.datapipes.iter import IterableWrapper
    >>> def odd_or_even(n):
    ...     return n % 2
    >>> source_dp = IterableWrapper(range(5))
    >>> dp1, dp2 = source_dp.demux(num_instances=2, classifier_fn=odd_or_even)
    >>> list(dp1)
    [0, 2, 4]
    >>> list(dp2)
    [1, 3]
    >>> # It can also filter out any element that gets `None` from the `classifier_fn`
    >>> def odd_or_even_no_zero(n):
    ...     return n % 2 if n != 0 else None
    >>> dp1, dp2 = source_dp.demux(num_instances=2, classifier_fn=odd_or_even_no_zero, drop_none=True)
    >>> list(dp1)
    [2, 4]
    >>> list(dp2)
    [1, 3]
rU   rV   classifier_fn	drop_nonerW   c                     US:  a  [        SU S35      e[        U5        [        XX4U5      n[        U5       Vs/ s H  n[	        Xg5      PM     sn$ s  snf r]   )r+   r   _DemultiplexerIterDataPiper`   ra   )rb   rU   rV   r   r   rW   rc   rd   s           r%   re   !DemultiplexerIterDataPipe.__new__  sd     1>}oYW  	]+
 /xbmn	6;M6JK6Jy,6JKKKs   ArQ   N)Frh   )rD   rI   rJ   rK   rL   r   rO   r	   r   r   r|   re   rP   rQ   r2   r%   r   r   x  sa    !P  LL L  # 67	L
 L L Lr2   r   c            
           \ rS rSrSrS\\   S\S\\/\	\   4   S\
S\4
S jrS	\S
\4S jrS	\4S jrS
\
4S jrS	\S
\4S jrSS jrS rS rSS	\	\   4S jjrS rSrg)r   i  z
Container to hold instance-specific information on behalf of DemultiplexerIterDataPipe.

It tracks the state of its child DataPipes, maintains the buffer, classifies and yields the next correct value
as requested by the child DataPipes.
rU   rV   r   r   rW   c                 h   Xl         S U l        X l        XPl        U R                  S:  a  [        R
                  " S[        5        SU l        [        U5       Vs/ s H  n[        5       PM     snU l
        X0l        X@l        SU l        [        U5       Vs/ s H  nSPM     snU l        g s  snf s  snf )Nr   zQUnlimited buffer size is set for `demux`, please be aware of OOM at random placesFT)r   r   rV   rW   r   r   r   current_buffer_usager`   r   child_buffersr   r   main_datapipe_exhaustedr   )r/   rU   rV   r   r   rW   r   s          r%   r0   #_DemultiplexerIterDataPipe.__init__  s     &;?*&aMM:
 %&!CHCW1XCWa%'CW1X*"',$6;M6J'K6J6J'K	 2Y (Ls   B*B/rl   r3   c                     U R                   (       d  U R                  U   (       a  [        eU R                  c  [	        S5      e[        U R                  5      nU R                  U5      nUc)  U R                  (       a  [        R                  " U5        M  Ub  X0R                  :  d  US:  a"  [	        SU R                  S-
   S3U S3-   5      eX1:X  a  U$ U R                  U   R                  U5        U =R                  S-  sl        U R                  S:  a3  U R                  U R                  :  a  [        SU R                   S35      eGMO  )	Nz_datapipe_iterator has not been set, likely because this private method is called directly without invoking get_next_element_by_instance() first.r   z8Output of the classification fn should be between 0 and r^   z. z is returned.z7DemultiplexerIterDataPipe buffer overflow, buffer size r   )r   r   r   r   r+   r   r   r   r   r   rV   r   r   r   rW   r   )r/   rl   valueclassifications       r%   
_find_next%_DemultiplexerIterDataPipe._find_next  sN   ++t/?/?/L##&&. M  001E!//6N%$..++E2&!%7%77!A% NtOaOadeOeNffhi'(67  ,~.55e<%%*%1$)B)BTEUEU)U!MdN^N^M__pq 7 r2   c              #     #    U R                   cu  U R                  U   (       aa  [        U R                  5      U l         [        R
                  U l        SU l        [        U R                  5       H  nSU R                  U'   M      U R                  U   (       ds  U R                  U   (       a5  U =R                  S-  sl        U R                  U   R                  5       v   O U R                  U5      v   U R                  U   (       d  Ms  SU R                  U'   [        U R                  5      (       a  S U l         U R                  U   (       a  U R!                  U5        g g ! [         a     SU R                  U'   SU l        S U l          Nf = f! SU R                  U'   [        U R                  5      (       a  S U l         U R                  U   (       a  U R!                  U5        f f = f7f)NFr^   T)r   r   r   r   r   r   r   r   r`   rV   r   r   r   r   r   r,   r   )r/   rl   rd   s      r%   ro   7_DemultiplexerIterDataPipe.get_next_element_by_instance  s    ""*t/?/?/L&*4+=+=&>D#((   ,1D(4--.&+  # /	+&&{3%%k2--2-,,[9AACC7"ook:: &&{33 -1D[)4##$$*.'!!+.k* / ) 78<((57;426/7
 -1D[)4##$$*.'!!+.k* /sE   BG,AF $E$ 7F AG,$'FF FF AG))G,c                 R    U R                   =(       a    [        U R                  5      $ r   )r   r,   r   rE   s    r%   rs   6_DemultiplexerIterDataPipe.is_every_instance_exhausted  s    ++ED4D4D0EEr2   c                     [         er   )r-   rn   s     r%   ry   1_DemultiplexerIterDataPipe.get_length_by_instance  s    r2   Nc                     S U l         SU l        [        U R                  5       Vs/ s H  n[	        5       PM     snU l        [        U R                  5       Vs/ s H  nSPM     snU l        SU l        g s  snf s  snf Nr   TF)r   r   r`   rV   r   r   r   r   r   s     r%   rv    _DemultiplexerIterDataPipe.reset  sm    "&$%!/4T5G5G/HI/H!eg/HI*/0B0B*CD*CQD*CD',$ JDs   A5A:c                     U R                   U R                  U R                  U R                  U R                  U R
                  U R                  4n[        R                  b  [        R                  " U5      $ U$ r   )	r   rV   rW   r   r   r   r   r   r   r   s     r%   r   '_DemultiplexerIterDataPipe.__getstate__  sj    NN##++
 %%1--e44r2   c                 X   Uu  U l         U l        U l        U l        U l        U l        U l        S U l        SU l        [        U R                  5       Vs/ s H  n[        5       PM     snU l        [        U R                  5       Vs/ s H  nSPM     snU l        SU l        g s  snf s  snf r   )r   rV   rW   r   r   r   r   r   r   r`   r   r   r   r   r   s      r%   r   '_DemultiplexerIterDataPipe.__setstate__-  s     	
N#+"&$%!/4T5G5G/HI/H!eg/HI*/0B0B*CD*CQD*CD',$ JDs   B"B'c                     Uc  [        U R                  5      OU/nU HJ  nU R                  U   nU(       d  M  UR                  5       n[        R
                  " U5        U(       a  M/  ML     g r   )r`   rV   r   r   r   r   )r/   rl   idsrd   qr   s         r%   r   #_DemultiplexerIterDataPipe._cleanup=  sj     " $$$%  	 A""1%A!IIK++A. ! r2   c                 $    U R                  5         g r   r   rE   s    r%   r   "_DemultiplexerIterDataPipe.__del__K  r   r2   )r   r   r   r   r   rW   r   r   r   r   r   r   rV   r{   r   )rD   rI   rJ   rK   rL   r   r   rO   r	   r   r|   r0   r   ro   rs   ry   rv   r   r   r   r   rP   rQ   r2   r%   r   r     s    Lu%L L  # 67	L
 L L2c e @+ +<FT F# # -- /HSM /r2   r   muxc                   F    \ rS rSrSrS rS rS rSS jrS r	S	 r
S
 rSrg)r   iO  a  
Yields one element at a time from each of the input Iterable DataPipes (functional name: ``mux``).

As in, one element from the 1st input DataPipe, then one element from the 2nd DataPipe in the next iteration,
and so on. It ends when the shortest input DataPipe is exhausted.

Args:
    datapipes: Iterable DataPipes that will take turn to yield their elements, until the shortest DataPipe is exhausted

Example:
    >>> # xdoctest: +REQUIRES(module:torchdata)
    >>> from torchdata.datapipes.iter import IterableWrapper
    >>> dp1, dp2, dp3 = IterableWrapper(range(3)), IterableWrapper(range(10, 15)), IterableWrapper(range(20, 25))
    >>> list(dp1.mux(dp2, dp3))
    [0, 10, 20, 1, 11, 21, 2, 12, 22]
c                     Xl         / U l        g r   )r   r   r.   s     r%   r0    MultiplexerIterDataPipe.__init__b  s    " 	r2   c              #     #    U R                    Vs/ s H  n[        U5      PM     nn[        U5      (       aq  U H*  n [        U5      nU R                  R                  U5        M,     U R                   S h  vN   U R                  R                  5         [        U5      (       a  Mp  g g s  snf ! [         a    U R                  R                  5            g f = f Nb7fr   )r   r   r*   r   r   r   r   clear)r/   r~   	iteratorsitr   s        r%   r6    MultiplexerIterDataPipe.__iter__h  s     &*nn5nT!Wn	5)nn HEKK&&u-   {{""KK )nn 6 % KK%%' #sD   CB*C&B/$C7C8.C(C/$CCCCc                     [        S U R                   5       5      (       a2  [        S U R                   5       5      [        U R                  5      -  $ [	        [        U 5      R                   S35      e)Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   r:   r"   s     r%   r&   2MultiplexerIterDataPipe.__len__.<locals>.<genexpr>v  r<   r)   c              3   8   #    U  H  n[        U5      v   M     g 7fr   r>   r"   s     r%   r&   r  w  r?   r@   rA   )r,   r   r   r*   r-   rC   rD   rE   s    r%   rF   MultiplexerIterDataPipe.__len__u  sX    >t~~>>>8883t~~;NNNtDz2233VWXXr2   Nc                     / U l         g r   )r   rE   s    r%   rv   MultiplexerIterDataPipe.reset{  s	    r2   c                     U R                   U R                  U R                  4n[        R                  b  [        R                  " U5      $ U$ r   )r   r   r   r   r   r   s     r%   r   $MultiplexerIterDataPipe.__getstate__~  sF    NN##++

 %%1--e44r2   c                 <    Uu  U l         U l        U l        / U l        g r   )r   r   r   r   r   s     r%   r   $MultiplexerIterDataPipe.__setstate__  s$    
 		
N#+r2   c                 8    U R                   R                  5         g r   )r   r   rE   s    r%   r   MultiplexerIterDataPipe.__del__  s    r2   )r   r   r   r   r{   )rD   rI   rJ   rK   rL   r0   r6   rF   rv   r   r   r   rP   rQ   r2   r%   r   r   O  s+    "
 Yr2   r   zipc                   n   ^  \ rS rSr% Sr\\   \S'   S\4U 4S jjrS\	\\
      4S jrS\4S jrSrU =r$ )	r   i  a5  
Aggregates elements into a tuple from each of the input DataPipes (functional name: ``zip``).

The output is stopped as soon as the shortest input DataPipe is exhausted.

Args:
    *datapipes: Iterable DataPipes being aggregated

Example:
    >>> # xdoctest: +REQUIRES(module:torchdata)
    >>> from torchdata.datapipes.iter import IterableWrapper
    >>> dp1, dp2, dp3 = IterableWrapper(range(5)), IterableWrapper(range(10, 15)), IterableWrapper(range(20, 25))
    >>> list(dp1.zip(dp2, dp3))
    [(0, 10, 20), (1, 11, 21), (2, 12, 22), (3, 13, 23), (4, 14, 24)]
r   c                 r   > [        S U 5       5      (       d  [        S5      e[        TU ]  5         Xl        g )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   r    r"   s     r%   r&   .ZipperIterDataPipe.__init__.<locals>.<genexpr>  r(   r)   zCAll inputs are required to be `IterDataPipe` for `ZipIterDataPipe`.)r,   r-   superr0   r   )r/   r   	__class__s     r%   r0   ZipperIterDataPipe.__init__  s6    D)DDDU  	"r2   r3   c              #      #    U R                    Vs/ s H  n[        U5      PM     nn[        U6  S h  vN   g s  snf  N
7fr   )r   r   r  )r/   rU   r   s      r%   r6   ZipperIterDataPipe.__iter__  s7     48NNCNT(^N	C	?"" D"s   A 9A >A c                     [        S U R                   5       5      (       a  [        S U R                   5       5      $ [        [	        U 5      R
                   S35      e)Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   r:   r"   s     r%   r&   -ZipperIterDataPipe.__len__.<locals>.<genexpr>  r<   r)   c              3   8   #    U  H  n[        U5      v   M     g 7fr   r>   r"   s     r%   r&   r    r?   r@   rA   )r,   r   r   r-   rC   rD   rE   s    r%   rF   ZipperIterDataPipe.__len__  rH   r2   r5   )rD   rI   rJ   rK   rL   rM   r   rN   r0   r   r   r6   rO   rF   rP   __classcell__)r  s   @r%   r   r     sJ      \""#< ##(5<0 #Y Y Yr2   r   )'rX   r   r   abcr   r   collectionsr   collections.abcr   r   typingr   r	   r
   r   r   %torch.utils.data.datapipes._decoratorr   )torch.utils.data.datapipes._hook_iteratorr   #torch.utils.data.datapipes.datapiper   'torch.utils.data.datapipes.utils.commonr   r   __all__r   r   r   rj   r   r_   ra   r   r   r   rM   r   rQ   r2   r%   <module>r'     s=     #  + < < E D < W 	4( X$Y< $Y $YN V,L ,L ,L^c c(X,(: XvO
\ O
d W7L 7L 7LtY/A Yx UAl A AH U#YeEl3 #Y #Yr2   