U
    /e                     @  s   d dl mZ d dlZd dlZd dlmZ d dlZd dlmZ ej	
ejdZdd dD Zd	ed
< d	ed< efddddddZdd Zdd Zdd ZdddddZejd Zddddd ZdS )!    )annotationsN)Sequence)nbyteszdistributed.comm.shardc                 C  s   i | ]}d | dqS )z
max_%s_leni ).0xr   r   >/tmp/pip-unpacked-wheel-g426oqom/distributed/protocol/utils.py
<dictcomp>   s     r	   )strbinarraymapextFstrict_map_keyrawzbytes | memoryviewintzlist[memoryview])framenreturnc                   sR   |pt }t   j|kr  gS  j j }| j  fddtd|D S )z
    Split a frame into a list of frames of maximum size

    This helps us to avoid passing around very large bytestrings.

    Examples
    --------
    >>> frame_split_size([b'12345', b'678'], n=3)  # doctest: +SKIP
    [b'123', b'45', b'678']
    c                   s   g | ]} ||  qS r   r   )r   ir   Zitems_per_shardr   r   
<listcomp>+   s     z$frame_split_size.<locals>.<listcomp>r   )BIG_BYTES_SHARD_SIZE
memoryviewr   itemsizerange)r   r   Znitemsr   r   r   frame_split_size   s    

r   c                 C  s,   t | }tt| }tjd| d|f| S )NQ)lenr   r   structpack)framesZnframesZnbytes_framesr   r   r   pack_frames_prelude.   s    
r"   c                 C  s   d t| f| S )zPack frames into a byte-like object

    This prepends length information to the front of the bytes-like object

    See Also
    --------
    unpack_frames
        )joinr"   )r!   r   r   r   pack_frames4   s    	r%   c           	      C  sv   t | } d}t|}t|| \}t| | | |}g }|d|  }|D ]"}|| }|| ||  |}qN|S )zUnpack bytes into a sequence of frames

    This assumes that length information is at the front of the bytestring,
    as performed by pack_frames

    See Also
    --------
    pack_frames
    r      )r   r   calcsizeunpack_fromappend)	bfmtZfmt_sizeZn_frameslengthsr!   startlengthendr   r   r   unpack_frames@   s    

r0   zSequence[memoryview]r   )mvsr   c              	   C  s  | st t S t| dkr"| d S | d }t|t sFtdt| |j}|j}d}d}t| D ]\}}t|t st| dt| |j	dkrqb|j|k	rt
| d|jd||jst
| d|jdkrt
| d|j d	|j|krt
| d
|j d| t|}|dkr.|}n4|| }	||	krbt
d| d|	dd||	  d||j	7 }qb|dkrt|dkst|S |dkstdt |d}
t|
}|| }|
|||  |S )a  
    Zero-copy "concatenate" a sequence of contiguous memoryviews.

    Returns a new memoryview which slices into the underlying buffer
    to extract out the portion equivalent to all of ``mvs`` being concatenated.

    All the memoryviews must:
    * Share the same underlying buffer (``.obj``)
    * When merged, cover a continuous portion of that buffer with no gaps
    * Have the same strides
    * Be 1-dimensional
    * Have the same format
    * Be contiguous

    Raises ValueError if these conditions are not met.
    r&   r   zExpected memoryview; got z: expected memoryview; got z#: memoryview has different buffer: z vs z: memoryview non-contiguousz: memoryview has z dimensions, not 1z: inconsistent format: zmemoryview z2 does not start where the previous ends. Expected r   z	, starts z byte(s) away.z#Underlying buffer is null pointer?!B)r   	bytearrayr   
isinstance	TypeErrortypeobjformat	enumerater   
ValueError
contiguousndimaddress_of_memoryviewAssertionErrorcast)r1   firstr7   r8   Zfirst_start_addrr   r   mvZ
start_addrZexpected_addrZbase_mvZbase_start_addrstart_indexr   r   r   merge_memoryviews\   sV    








rC   r&   )rA   r   c                 C  st   zt | }W n tk
r"   Y nX t|S zddl}W n$ tk
r^   td|  dY nX || j	d d S )z
    Get the pointer to the first byte of a memoryview's data.

    If the memoryview is read-only, NumPy must be installed.
    r   Nz+Cannot get address of read-only memoryview z since NumPy is not installed.data)
one_byte_carrfrom_bufferr5   ctypes	addressofZnumpyImportErrorr:   ZasarrayZ__array_interface__)rA   Zcarrnpr   r   r   r=      s    


r=   )
__future__r   rG   r   collections.abcr   ZdaskZdistributed.utilsr   utilsZparse_bytesconfiggetr   Zmsgpack_optsr   r"   r%   r0   rC   c_byterE   r=   r   r   r   r   <module>   s&   J
