o
    8ήc$                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZmZ ddlmZmZmZmZmZmZ ddlmZ dd	lmZ ed
d Zedd Zedd Zedd Zedd Zedd Zedd Z edd Z!edd Z"edd Z#edd Z$d d! Z%G d"d# d#eZ&eejd$d%d& Z'eejd'd( Z(eejd)d* Z)eed+d, Z*eed-d. Z+d/S )0zK
Python wrapper that connects CPython interpreter to the numba dictobject.
    )MutableMapping)DictType)numba_typeref_ctor)njittypeof)typeserrorsconfigcgutils)overloadboxunboxNativeValuetype_callableoverload_classmethod)
dictobject)	signaturec                 C   s   t t | |S N)r   _as_meminfonew_dict)keytyvalty r   </tmp/pip-target-vg8gfxp4/lib/python/numba/typed/typeddict.py
_make_dict   s   r   c                 C      t | S r   )lendr   r   r   _length      r   c                 C   s   || |< d S r   r   )r   keyvaluer   r   r   _setitem      r#   c                 C   s   | | S r   r   r   r!   r   r   r   _getitem$   r    r&   c                 C   s
   | |= d S r   r   r%   r   r   r   _delitem)   s   
r'   c                 C   s   || v S r   r   r%   r   r   r   	_contains.   r    r(   c                 C      |  ||S r   )getr   r!   defaultr   r   r   _get3   r$   r-   c                 C   r)   r   )
setdefaultr+   r   r   r   _setdefault8   r$   r/   c                 C   s   t |  S r   )listkeysr   r   r   r   _iter=   r$   r2   c                 C      |   S r   )popitemr   r   r   r   _popitemB   r    r5   c                 C   r3   r   )copyr   r   r   r   _copyG   r    r7   c                 C   s   t | |d}|S )N)meminfodcttype)Dict)ptrdicttyper   r   r   r   _from_meminfo_ptrL   s   r=   c                   @   s   e Zd ZdZd)ddZedd Zdd Zd*d	d
Ze	dd Z
e	dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd*d!d"Zd*d#d$Zd%d& Zd'd( ZdS )+r:   ziA typed-dictionary usable in Numba compiled functions.

    Implements the MutableMapping interface.
    Nc                 C   s   t jrttS t| S r   )r	   DISABLE_JITdict__new__object)clsr9   r8   r   r   r   r@   W   s   

zDict.__new__c                 C   s   t jrt S | t||dS )zCreate a new empty Dict with *key_type* and *value_type*
        as the types for the keys and values of the dictionary respectively.
        )r9   )r	   r>   r?   r   rB   key_type
value_typer   r   r   empty]   s   z
Dict.emptyc                 K   s*   |r| j di |\| _| _dS d| _dS )a  
        For users, the constructor does not take any parameters.
        The keyword arguments are for internal use only.

        Parameters
        ----------
        dcttype : numba.core.types.DictType; keyword-only
            Used internally for the dictionary type.
        meminfo : MemInfo; keyword-only
            Used internally to pass the MemInfo object when boxing.
        Nr   )
_parse_arg
_dict_type_opaque)selfkwargsr   r   r   __init__g   s   
zDict.__init__c                 C   s<   t |ts	td|d ur|}||fS t|j|j}||fS )Nz*dcttype* must be a DictType)
isinstancer   	TypeErrorr   rD   rE   )rJ   r9   r8   opaquer   r   r   rG   x   s   
zDict._parse_argc                 C   s   | j d u r	td| j S )Nz'invalid operation on untyped dictionary)rH   rN   rJ   r   r   r   _numba_type_   s   
zDict._numba_type_c                 C   s
   | j duS )z1Returns True if the dictionary is typed.
        N)rH   rP   r   r   r   _typed   s   
zDict._typedc                 C   s*   t t|t|}| |\| _| _d S r   )r   r   r   rG   rH   rI   )rJ   r!   r"   r9   r   r   r   _initialise_dict   s   zDict._initialise_dictc                 C   s   | j st|t| |S r   )rR   KeyErrorr&   rJ   r!   r   r   r   __getitem__   s   
zDict.__getitem__c                 C   s   | j s	| || t| ||S r   )rR   rS   r#   )rJ   r!   r"   r   r   r   __setitem__   s   zDict.__setitem__c                 C   s   | j st|t| | d S r   )rR   rT   r'   rU   r   r   r   __delitem__   s   zDict.__delitem__c                 C   s   | j stdS tt| S )Nr   )rR   iterr2   rP   r   r   r   __iter__   s   zDict.__iter__c                 C   s   | j sdS t| S )Nr   )rR   r   rP   r   r   r   __len__   s   zDict.__len__c                 C   s   t | dkrdS t| |S )Nr   F)r   r(   rU   r   r   r   __contains__   s   
zDict.__contains__c                 C   s8   g }|   D ]\}}|d|| qdd|S )Nz{}: {}z{{{0}}}z, )itemsappendformatjoin)rJ   bufkvr   r   r   __str__   s   zDict.__str__c                 C   s    t | }t | j}dj||dS )Nz{prefix}({body}))prefixbody)strrH   r_   )rJ   rf   re   r   r   r   __repr__   s   
zDict.__repr__c                 C   s   | j s|S t| ||S r   )rR   r-   rJ   r!   r,   r   r   r   r*      s   zDict.getc                 C   s&   | j s|d ur| || t| ||S r   )rR   rS   r/   ri   r   r   r   r.      s   zDict.setdefaultc                 C   s   t | dkr
tdt| S )Nr   zdictionary is empty)r   rT   r5   rP   r   r   r   r4      s   zDict.popitemc                 C   r   r   )r7   rP   r   r   r   r6      s   z	Dict.copy)NNr   )__name__
__module____qualname____doc__r@   classmethodrF   rL   rG   propertyrQ   rR   rS   rV   rW   rX   rZ   r[   r\   rd   rh   r*   r.   r4   r6   r   r   r   r   r:   Q   s0    

	





r:   rF   c                 C   s   | j turd S dd }|S )Nc                 S   s   t ||S r   )r   r   rC   r   r   r   impl      ztypeddict_empty.<locals>.impl)instance_typer   )rB   rD   rE   rp   r   r   r   typeddict_empty   s   
rs   c                 C   s  |j }|j}t| }||||d}|ttj|j}|j 	|jj
d}|j|}	|j|	d}
|j|j| }||jj}|t|jj| |t||) |j|
||f}|j|
 |j|	 |j| ||| W d    n1 sw   Y  ||S )N)r"   znumba.typed.typeddictr=   )contextbuilderr
   create_struct_proxyr   r   MemInfoPointervoidptrr8   insert_const_stringmodulepyapiimport_module_noblockobject_getattr_stringunserializeserialize_objectallocapyobjstoreget_null_valueif_thenis_not_nullcall_function_objargsdecrefload)typvalcrt   ru   ctordstructboxed_meminfomodnametypeddict_modfmp_fndicttype_obj
result_varresr   r   r   box_dicttype   s4   


r   c              	   C   s  |j }|j|jt}|j|}|jd||}|j|\}}|Y |j	|d}	t
t
j}
||
|	}|j}|
t| f}dd }t| g|R  }||d }||f}|j|||\}}|j j|j| | |j|	 |jj}W d    n1 sw   Y  | |jdd|| |jj}W d    n1 sw   Y  W d    n1 sw   Y  |j|j}|j|j}||| ||d | ||| |tj| |j| |j| t||dS )	Nz==rI   c                 S   s   t | |S r   )r   _from_meminfo)mir   r   r   r   convert  rq   zunbox_dicttype.<locals>.convert   PyExc_TypeErrorzcan't unbox a %S as a %S)is_error)rt   r{   r~   r   r:   object_typeru   icmp_unsignedif_elser}   r   rw   rx   r   r"   r   r   get_constant_nullcall_jit_codenrtr   basic_block
err_formatphitypeadd_incomingr
   true_bitr   )r   r   r   rt   	dict_typevaltype	same_typethenorelsemiptrmip_typenativer   argtypesr   signil_typerefargsr   dctobj
bb_unboxedbb_else
dctobj_resis_error_resr   r   r   unbox_dicttype  sN   

"r   c                 C   s   dd }|S )zV
    Defines typing logic for ``Dict()``.
    Produces Dict[undefined, undefined]
    c                   S   s   t t jt jS r   )r   r   	undefinedr   r   r   r   typerB  s   ztypeddict_call.<locals>.typerr   )rt   r   r   r   r   typeddict_call<  s   r   c                    sf   | j }t|tjsd|}dS | sd|}t|t|j	 t|j
 fdd}|S )z
    Defines ``Dict()``, the type-inferred version of the dictionary ctor.

    Parameters
    ----------
    cls : TypeRef
        Expecting a TypeRef of a precise DictType.

    See also: `redirect_type_ctor` in numba/cpython/bulitins.py
    zexpecting a DictType but got {}Nz'expecting a precise DictType but got {}c                    s   t  S r   )r:   rF   )rB   rD   rE   r   r   rp   _  r$   z%impl_numba_typeref_ctor.<locals>.impl)rr   rM   r   r   r_   
is_preciser   LoweringErrorTypeRefrD   rE   )rB   dict_tymsgrp   r   r   r   impl_numba_typeref_ctorG  s   


r   N),rm   collections.abcr   numba.core.typesr   numba.core.imputilsr   numbar   r   
numba.corer   r   r	   r
   numba.core.extendingr   r   r   r   r   r   numba.typedr   numba.core.typingr   r   r   r#   r&   r'   r(   r-   r/   r2   r5   r7   r=   r:   rs   r   r   r   r   r   r   r   r   <module>   sX     










 



"
:

