o
    wZŽhm  ã                   @   sp   U d dl mZ d dlmZmZmZ edƒZdZee	d< dgZ
G dd„ dƒZG d	d
„ d
ƒZG dd„ dee ƒZdS )é    )ÚIterable)ÚCallableÚGenericÚTypeVarÚTé   Ú_ENCODING_VERSIONÚAppendingByteSerializerc                   @   sb   e Zd Zdeddfdd„Zdeddfdd„Zd	eddfd
d„Zdeddfdd„Z	defdd„Z
dS )ÚBytesWriterÚpreallocate_sizeÚreturnNc                 C   s   t |ƒ| _d S ©N)Ú	bytearrayÚ_data)Úselfr   © r   úU/var/www/auris/lib/python3.10/site-packages/torch/utils/_appending_byte_serializer.pyÚ__init__   ó   zBytesWriter.__init__Úic                 C   s   | j  |jdddd¡ d S ©Né   ÚbigF)Ú	byteorderÚsigned)r   ÚextendÚto_bytes)r   r   r   r   r   Úwrite_uint64   s   zBytesWriter.write_uint64Úsc                 C   s   |  d¡}|  |¡ d S ©Nzutf-8)ÚencodeÚwrite_bytes)r   r   Úpayloadr   r   r   Ú	write_str   s   
zBytesWriter.write_strÚbc                 C   s   |   t|ƒ¡ | j |¡ d S r   )r   Úlenr   r   )r   r$   r   r   r   r!      s   zBytesWriter.write_bytesc                 C   s
   t | jƒS r   )Úbytesr   ©r   r   r   r   r       ó   
zBytesWriter.to_bytes)Ú__name__Ú
__module__Ú__qualname__Úintr   r   Ústrr#   r&   r!   r   r   r   r   r   r
      s    r
   c                   @   sV   e Zd Zdeddfdd„Zdefdd„Zdefdd	„Zde	fd
d„Z
defdd„ZdS )ÚBytesReaderÚdatar   Nc                 C   s   || _ d| _d S )Nr   )r   Ú_i©r   r/   r   r   r   r   %   s   
zBytesReader.__init__c                 C   s   t | jƒ| jkS r   )r%   r   r0   r'   r   r   r   Úis_finished)   s   zBytesReader.is_finishedc                 C   s4   t j| j| j| jd … ddd}|  jd7  _|S r   )r,   Ú
from_bytesr   r0   )r   Úresultr   r   r   Úread_uint64,   s
   ÿzBytesReader.read_uint64c                 C   s   |   ¡  d¡S r   )Ú
read_bytesÚdecoder'   r   r   r   Úread_str3   r   zBytesReader.read_strc                 C   s0   |   ¡ }| j| j| j| … }|  j|7  _|S r   )r5   r   r0   )r   Úsizer4   r   r   r   r6   6   s   zBytesReader.read_bytes)r)   r*   r+   r&   r   Úboolr2   r,   r5   r-   r8   r6   r   r   r   r   r.   $   s    r.   c                   @   sÆ   e Zd ZU dZeeegdf ed< eed< eed< ddœdeeegdf d	ed
dfdd„Z	ddd„Z
ded
dfdd„Zdee d
dfdd„Zd
efdd„Zededeegef d
ee fdd„ƒZdS )r	   z”
    Provides efficient serialization and deserialization of list of bytes
    Note that this does not provide any guarantees around byte order
    NÚ_serialize_fnÚ_writerÚ_preallocate_sizer   ©r   Úserialize_fnr   r   c                C   s   || _ || _|  ¡  d S r   )r;   r=   Úclear)r   r?   r   r   r   r   r   L   s   z AppendingByteSerializer.__init__c                 C   s   t | jd| _| j t¡ d S )Nr>   )r
   r=   r<   r   r   r'   r   r   r   r@   V   s   zAppendingByteSerializer.clearr/   c                 C   s   |   | j|¡ d S r   )r;   r<   r1   r   r   r   Úappend[   s   zAppendingByteSerializer.appendÚelemsc                 C   s   |D ]}|   |¡ qd S r   )rA   )r   rB   Úelemr   r   r   r   ^   s   ÿzAppendingByteSerializer.extendc                 C   s
   | j  ¡ S r   )r<   r   r'   r   r   r   r   b   r(   z AppendingByteSerializer.to_bytesÚdeserialize_fnc                C   s>   t | ƒ}| ¡ tksJ ‚g }| ¡ s| ||ƒ¡ | ¡ r|S r   )r.   r5   r   r2   rA   )r/   rD   Úreaderr4   r   r   r   Úto_liste   s   ÿzAppendingByteSerializer.to_list)r   N)r)   r*   r+   Ú__doc__r   r
   r   Ú__annotations__r,   r   r@   rA   r   r   r&   r   Ústaticmethodr.   ÚlistrF   r   r   r   r   r	   B   s&   
 üýü
û

*N)Úcollections.abcr   Útypingr   r   r   r   r   r,   rH   Ú__all__r
   r.   r	   r   r   r   r   Ú<module>   s    