U
    <c                     @   s   d dl mZ d dlmZ d dlmZmZmZ ddgZedddZ	ed	G d
d deZ
edG dd deee	df  ZdS )    )functional_datapipe)MapDataPipe)SizedTupleTypeVarConcaterMapDataPipeZipperMapDataPipeT_coT)	covariantconcatc                   @   sP   e Zd ZU dZee ed< eed< edddZe	ddd	Z
edd
dZdS )r   a  
    Concatenate multiple Map DataPipes (functional name: ``concat``).
    The new index of is the cumulative sum of source DataPipes.
    For example, if there are 2 source DataPipes both with length 5,
    index 0 to 4 of the resulting `ConcatMapDataPipe` would refer to
    elements of the first DataPipe, and 5 to 9 would refer to elements
    of the second DataPipe.

    Args:
        datapipes: Map DataPipes being concatenated

    Example:
        >>> # xdoctest: +SKIP
        >>> from torchdata.datapipes.map import SequenceWrapper
        >>> dp1 = SequenceWrapper(range(3))
        >>> dp2 = SequenceWrapper(range(3))
        >>> concat_dp = dp1.concat(dp2)
        >>> list(concat_dp)
        [0, 1, 2, 0, 1, 2]
    	datapipeslength)r   c                 G   sX   t |dkrtdtdd |D s.tdtdd |D sHtd|| _d| _d S )	Nr   /Expected at least one DataPipe, but got nothingc                 s   s   | ]}t |tV  qd S N
isinstancer   .0dp r   L/tmp/pip-unpacked-wheel-gikjz4vx/torch/utils/data/datapipes/map/combining.py	<genexpr>&   s     z/ConcaterMapDataPipe.__init__.<locals>.<genexpr>'Expected all inputs to be `MapDataPipe`c                 s   s   | ]}t |tV  qd S r   r   r   r   r   r   r   r   (   s     !Expected all inputs to be `Sized`len
ValueErrorall	TypeErrorr   r   selfr   r   r   r   __init__#   s    zConcaterMapDataPipe.__init__returnc                 C   sN   d}| j D ]0}|| t|k r.|||    S |t|7 }q
td|d S )Nr   zIndex {} is out of range.)r   r   
IndexErrorformat)r"   indexoffsetr   r   r   r   __getitem__-   s    
zConcaterMapDataPipe.__getitem__c                 C   s&   | j dkr tdd | jD | _ | j S )Nr   c                 s   s   | ]}t |V  qd S r   r   r   r   r   r   r   8   s     z.ConcaterMapDataPipe.__len__.<locals>.<genexpr>)r   sumr   r"   r   r   r   __len__6   s    
zConcaterMapDataPipe.__len__N)__name__
__module____qualname____doc__r   r   __annotations__intr#   r	   r*   r.   r   r   r   r   r   
   s   

	zipc                   @   sf   e Zd ZU dZeee df ed< eed< ee ddddZ	eedf d	d
dZ
ed	ddZdS )r   a$  
    Aggregates elements into a tuple from each of the input DataPipes (functional name: ``zip``).
    This MataPipe is out of bound as soon as the shortest input DataPipe is exhausted.

    Args:
        *datapipes: Map DataPipes being aggregated

    Example:
        >>> # xdoctest: +SKIP
        >>> from torchdata.datapipes.map import SequenceWrapper
        >>> dp1 = SequenceWrapper(range(3))
        >>> dp2 = SequenceWrapper(range(10, 13))
        >>> zip_dp = dp1.zip(dp2)
        >>> list(zip_dp)
        [(0, 10), (1, 11), (2, 12)]
    .r   r   N)r   r%   c                 G   sX   t |dkrtdtdd |D s.tdtdd |D sHtd|| _d| _d S )	Nr   r   c                 s   s   | ]}t |tV  qd S r   r   r   r   r   r   r   T   s     z-ZipperMapDataPipe.__init__.<locals>.<genexpr>r   c                 s   s   | ]}t |tV  qd S r   r   r   r   r   r   r   V   s     r   r   r   r!   r   r   r   r#   Q   s    zZipperMapDataPipe.__init__r$   c              
   C   sV   g }| j D ]B}z|||  W q
 tk
rJ   td| d| dY q
X q
t|S )NzIndex z3 is out of range for one of the input MapDataPipes .)r   appendr&   tuple)r"   r(   resr   r   r   r   r*   [   s    
zZipperMapDataPipe.__getitem__c                 C   s&   | j dkr tdd | jD | _ | j S )Nr   c                 s   s   | ]}t |V  qd S r   r+   r   r   r   r   r   f   s     z,ZipperMapDataPipe.__len__.<locals>.<genexpr>)r   minr   r-   r   r   r   r.   d   s    
zZipperMapDataPipe.__len__)r/   r0   r1   r2   r   r   r	   r3   r4   r#   r*   r.   r   r   r   r   r   <   s   

	.N)Z%torch.utils.data.datapipes._decoratorr   Z#torch.utils.data.datapipes.datapiper   typingr   r   r   __all__r	   r   r   r   r   r   r   <module>   s   1