
    ˀh                    r    d dl mZ d dlmZ d dlmZmZmZmZ d dl	m
Z
 erd dlmZ d dlmZ  G d de      Zy	)
    )annotations)TYPE_CHECKING)BufferCopyNotAllowedErrorDlpackDeviceType	DtypeKind)polars_dtype_to_dtype)NoReturn)Seriesc                  Z    e Zd ZdZddddZedd       Zedd       ZddZddZ	dd	Z
y
)PolarsBufferaQ  
    A buffer object backed by a Polars Series consisting of a single chunk.

    Parameters
    ----------
    data
        The Polars Series backing the buffer object.
    allow_copy
        Allow data to be copied during operations on this column. If set to `False`,
        a RuntimeError will be raised if data would be copied.
    T)
allow_copyc               v    |j                         dkD  r|sd}t        |      |j                         }|| _        y )N   z-non-contiguous buffer must be made contiguous)n_chunksr   rechunk_data)selfdatar   msgs       u/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/polars/interchange/buffer.py__init__zPolarsBuffer.__init__    s5    ==?QE)#..<<>D
    c                .   t        | j                  j                        }|d   t        j                  k(  r>| j                  j                         \  }}}||z   }t        |d      \  }}|dk(  r|S |dz   S | j                  j                         |d   dz  z  S )zBuffer size in bytes.r      r   )r	   r   dtyper   BOOL_get_buffer_infodivmodlen)r   r   _offsetlengthn_bitsn_bytesrests           r   bufsizezPolarsBuffer.bufsize)   s     &djj&6&678y~~% $

 ; ; =Avvf_F"61-MGTqy{"zz~~58q=11r   c                B    | j                   j                         \  }}}|S )z-Pointer to start of the buffer as an integer.)r   r   )r   pointerr!   s      r   ptrzPolarsBuffer.ptr:   s!     

335Ar   c                    d}t        |      )z-Represent this structure as DLPack interface.
__dlpack__)NotImplementedError)r   r   s     r   r,   zPolarsBuffer.__dlpack__@   s    !#&&r   c                &    t         j                  dfS )zCDevice type and device ID for where the data in the buffer resides.N)r   CPU)r   s    r   __dlpack_device__zPolarsBuffer.__dlpack_device__E   s     $$d++r   c                    | j                   }| j                  }| j                         d   j                  }d| d| d|dS )Nr   zPolarsBuffer(bufsize=z, ptr=z	, device=))r'   r*   r0   name)r   r'   r*   devices       r   __repr__zPolarsBuffer.__repr__I   sH    ,,hh'')!,11&wivcU)F:QOOr   N)r   r   r   boolreturnNone)r7   int)r7   r
   )r7   ztuple[DlpackDeviceType, None])r7   str)__name__
__module____qualname____doc__r   propertyr'   r*   r,   r0   r5    r   r   r   r      sJ    
 <@  2 2   
'
,Pr   r   N)
__future__r   typingr   polars.interchange.protocolr   r   r   r   polars.interchange.utilsr	   r
   polarsr   r   r@   r   r   <module>rF      s1    "    ;:P6 :Pr   