
    "fq                         d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ  G d dee          Zeeee         ef                  Z G d de          ZdS )0Integer to bytes and bytes to integer transform.    )Integral)ListOptionalTupleTypeUnion   )getbytes)ByteOrderHint)Data)Record)InsufficientMemoryError)	Transform)
NumberViewc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdddZdS )IntViewzInteger type view.c                 0    |                                  |z  S Nunpackselfothers     L/mounts/lovelace/software/anaconda3/lib/python3.11/site-packages/plum/int.py__and__zIntView.__and__       {{}}u$$    c                     | j         j        }|t          | j         d          |                                 ||j        z  z   }|                    | j        |          S )Nz* type does not support pointer dereference)__type____dref__RuntimeErrorr   
__nbytes__view
__buffer__)r   itemdrefoffsets       r   __getitem__zIntView.__getitem__   sb    }%<=NNN   $"89yy&111r   c                 Z    |                      |                                 |z             | S r   setr   r   s     r   __iand__zIntView.__iand__%   &    &'''r   c                 Z    |                      |                                 |z             | S r   r+   r   s     r   __ilshift__zIntView.__ilshift__)   &    %'(((r   c                 *    t          j        |           S r   )r   r   r   s    r   __int__zIntView.__int__-   s     &&&r   c                 ,    |                                   S r   r   r3   s    r   
__invert__zIntView.__invert__0   s    ~r   c                 Z    |                      |                                 |z             | S r   r+   r   s     r   __ior__zIntView.__ior__3   r.   r   c                 Z    |                      |                                 |z	             | S r   r+   r   s     r   __irshift__zIntView.__irshift__7   r1   r   c                 Z    |                      |                                 |z             | S r   r+   r   s     r   __ixor__zIntView.__ixor__;   r.   r   c                 0    |                                  |z  S r   r   r   s     r   
__lshift__zIntView.__lshift__?       {{}}%%r   c                 0    |                                  |z  S r   r   r   s     r   __or__zIntView.__or__B   r   r   c                 0    ||                                  z  S r   r   r   s     r   __rand__zIntView.__rand__E       t{{}}$$r   c                 0    ||                                  z  S r   r   r   s     r   __rlshift__zIntView.__rlshift__H       %%r   c                 0    ||                                  z  S r   r   r   s     r   __ror__zIntView.__ror__K   rD   r   c                 0    ||                                  z	  S r   r   r   s     r   __rrshift__zIntView.__rrshift__N   rG   r   c                 0    |                                  |z	  S r   r   r   s     r   
__rshift__zIntView.__rshift__Q   r?   r   c                 0    ||                                  z  S r   r   r   s     r   __rxor__zIntView.__rxor__T   rD   r   c                 Z    |                      |          }|                    |           d S r   )r)   r,   )r   r&   value	dref_views       r   __setitem__zIntView.__setitem__W   s-    $$T**	er   c                 0    |                                  |z  S r   r   r   s     r   __xor__zIntView.__xor__[   r   r   Fsignedc                V    |                                                      |||          S )a  Return an array of bytes representing an integer.

        :param int length:
            Length of bytes object to use.  An OverflowError is raised if the
            integer is not representable with the given number of bytes.

        :param str byteorder:
            The byte order used to represent the integer.  If byteorder is 'big',
            the most significant byte is at the beginning of the byte array.  If
            byteorder is 'little', the most significant byte is at the end of the
            byte array.  To request the native byte order of the host system, use
            ``sys.byteorder`` as the byte order value.

        :param bool signed:
            Determines whether two's complement is used to represent the integer.
            If signed is False and a negative integer is given, an OverflowError
            is raised.

        :returns: array of bytes
        :rtype: bytes

        rV   )r   to_bytes)r   length	byteorderrW   s       r   rY   zIntView.to_bytes^   s'    0 {{}}%%fi%GGGr   N)__name__
__module____qualname____doc__r   r)   r-   r0   r4   r6   r8   r:   r<   r>   rA   rC   rF   rI   rK   rM   rO   rS   rU   rY    r   r   r   r      s\       % % %
2 
2 
2    ' ' '        & & &% % %% % %& & &% % %& & && & &% % %  % % % 5: H H H H H H Hr   r   c                   4    e Zd ZU dZeed<   eed<   eeee	f         ed<   	 dddddd	ed
ede	dede
e         ddf fdZedefd            Zede	fd            Z	 ddedee         de
e         ddfdZ	 ddedede
e         deeef         fdZddZ xZS )IntXr   r!   r#   __bns__littleFN)rW   r'   namenbytesr[   rW   r'   re   returnc                    ||rdnd d|dz   }t                                          |d           |dk    sJ |dv sJ || _        || _        |||f| _        d S )Nsuint   )hintr   >   bigrd   )super__init__r!   r#   rc   )r   rf   r[   rW   r'   re   	__class__s         r   rp   zIntX.__init__   s     <#,cc==!==DE***zzzz-----   &&0r   c                     | j         d         S )zByte order ("little" or "big").r   rc   r3   s    r   r[   zIntX.byteorder        |Ar   c                     | j         d         S )zSigned integer.   rs   r3   s    r   rW   zIntX.signed   rt   r   rQ   piecesdumpc                    | j         \  }}}|-|                    |                    |||                     d S t          |          |_        	 |                    |||          }n/# t
          $ r" t          dt          |          d          d w xY wt          |          |_        ||_	        |                    |           d S )NrV   zvalue type z$ not int-like (no to_bytes() method))
rc   appendrY   reprrQ   AttributeError	TypeErrortyperk   memory)r   rQ   rw   rx   r[   rf   rW   pieces           r   __pack__zIntX.__pack__   s     %)L!	66<MM%..6.JJKKKKK eDJvyHH!   .$u++ . . .   UDJDKMM%     s   A) ),Bbufferr(   c                 2   | j         \  }}}|N||z   }t          |          |k     rt          d          t                              |||         ||          |fS t          ||||          \  }}t                              |||          x}	|_        |	|fS )Nztoo few bytes to unpackrV   )rc   lenr   rk   
from_bytesr   rQ   )
r   r   r(   rx   r[   rf   rW   endchunkrQ   s
             r   
__unpack__zIntX.__unpack__   s     %)L!	66<6/C6{{S  -.GHHH>>&"4i>OOQTTTffdF;;
s ^^E9V^LLL
czr   r   c                 $    t          | ||          S )zCreate integer view of bytes in buffer.

        :param buffer: bytes buffer
        :type buffer: bytes-like (e.g. bytes, bytearray, memoryview)
        :param int offset: byte offset

        )r   )r   r   r(   s      r   __view__zIntX.__view__   s     tVV,,,r   )rd   r   )r   )r\   r]   r^   r_   DREF__annotations__rk   r   r   boolr   strrp   propertyr[   rW   r   bytesr   r   r   r   __classcell__)rq   s   @r   rb   rb   |   s        ::NNNOOO=#t+,,,,
 $,1
 "1 1 11 !1
 1 1 sm1 
1 1 1 1 1 14 =    X     X
 IM! !!"&u+!5=f5E!	! ! ! !2 DH %(080@	sCx   &- - - - - - - -r   rb   N)r_   numbersr   typingr   r   r   r   r	   	_getbytesr   _typingr   datar   rx   r   
exceptionsr   	transformr   r$   r   r   r   rb   r`   r   r   <module>r      s\   7 6       5 5 5 5 5 5 5 5 5 5 5 5 5 5       " " " " " "             / / / / / /                  dH dH dH dH dHj( dH dH dHN d4j)+,-_- _- _- _- _-9 _- _- _- _- _-r   