U
    Mh0                     @   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                   @   sH   e Zd ZU dZe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)r   c                 G   sR   t |dkrtdtdd |D s.tdtdd |D sHt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   Z/var/www/html/venv/lib/python3.8/site-packages/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   selfr   r   r   r   __init__%   s    zConcaterMapDataPipe.__init__returnc                 C   sP   d}| j D ]0}|| t|k r.|||    S |t|7 }q
td| dd S )Nr   Index z is out of range.)r   r   
IndexError)r    indexoffsetr   r   r   r   __getitem__.   s    
zConcaterMapDataPipe.__getitem__c                 C   s   t dd | jD S )Nc                 s   s   | ]}t |V  qd S r   r   r   r   r   r   r   8   s     z.ConcaterMapDataPipe.__len__.<locals>.<genexpr>)sumr   r    r   r   r   __len__7   s    zConcaterMapDataPipe.__len__N)__name__
__module____qualname____doc__r   r   __annotations__r!   r	   r(   intr,   r   r   r   r   r      s
   
		zipc                   @   s^   e Zd ZU dZeee df 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   N)r   r#   c                 G   sR   t |dkrtdtdd |D s.tdtdd |D sHt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!   Q   s    zZipperMapDataPipe.__init__r"   c                 C   sf   g }| j D ]R}z|||  W q
 tk
rZ } ztd| d| d|W 5 d }~X Y q
X q
t|S )Nr$   z3 is out of range for one of the input MapDataPipes .)r   appendr%   tuple)r    r&   resr   er   r   r   r(   Z   s    
,zZipperMapDataPipe.__getitem__c                 C   s   t dd | jD S )Nc                 s   s   | ]}t |V  qd S r   r)   r   r   r   r   r   d   s     z,ZipperMapDataPipe.__len__.<locals>.<genexpr>)minr   r+   r   r   r   r,   c   s    zZipperMapDataPipe.__len__)r-   r.   r/   r0   r   r   r	   r1   r!   r(   r2   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   /