o
    Td                     @   s  d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZmZmZmZ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mZ dd
lmZm Z m!Z! dd Z"dd Z#dZ$dZ%dZ&dZ'dZ(dZ)dd Z*e!dd Z+dd Z,dd Z-dd Z.dd  Z/e0d!d"Z1G d#d$ d$e2Z3G d%d& d&e2Z4G d'd( d(e2Z5d)d* Z6ee7d+d, Z8ee7e
j9d-d. Z:ee;e
j<d/d0 Z=eej>e
j<e
j?d1d2 Z@ed3e
j<d4d5 ZAed6e
jBeejCd7d8 ZDed9e
j<e
j?d:d; ZEe!d<d= ZFe e
j<d>d?d@ ZGe!dAdB ZHe e
j<dCdDdE ZIe!dFdG ZJe e
j<dHdIdJ ZKe!dKdL ZLe e
j<dMdNdO ZMe!dPdQ ZNe e
j<dRdSdT ZOdUdV ZPe!dWdX ZQe e
j<dYdZd[ ZRd\d] ZSe!d^d_ ZTe e
j<d`dadb ZUdcdd ZVe!dedf ZWe e
j<dgdhdi ZXedje
j<e
j9dkdl ZYdmdn ZZej[eSfej\eYfej]ePfej^eVffD ]
\Z_Z`eZe_e` qeejae e
j<dodpdq Zbeejce e
j<drdsdt Zdeejee e
j<dudvdw Zfeejge e
j<dxdydz Zhe!d{d| Zie e
j<d}d~d Zje!dd Zkeejle e
j<ddd Zmeejne e
j<ddd Zoe!dd Zpeejqdd Zreejsdd Zte!dd Zueejvdd Zweejxdd Zyeejze
j<e
j<dd Z{ee
j<e
j<dd Z|dS )z&
Support for native homogeneous sets.
    N)cached_property)ir)typestypingcgutils)	lower_builtin
lower_castiternext_implimpl_ret_borrowedimpl_ret_new_refimpl_ret_untrackedfor_itercall_lenRefType)	quicksort)slicing)NumbaValueErrorTypingError)overloadoverload_method	intrinsicc                 C   s4   t |}| | }|||}| j|||dS )zx
    Given a set value and type, get its payload structure (as a
    reference, so that mutations are seen by all).
    ref)r   
SetPayloadget_data_type
as_pointerbitcastmake_data_helper)contextbuilderset_typeptrpayload_typeptrtypayload r%   g/home/ncw/WWW/www-new/content/articles/pi-bbp/venv/lib/python3.10/site-packages/numba/cpython/setobj.pyget_payload_struct   s   
r'   c                 C   s   |  t|}| |S )z7
    Return the entry size for the given set type.
    )r   r   SetEntryget_abi_sizeof)r   r    lltyr%   r%   r&   get_entry_size#   s   
r+   i      Fc                 C   s`   | j }|t}|||fi }| ||}|||f}t| ||}	t|jt	}
|
|	||
S )z.
    Compute the hash of the given value.
    )typing_contextresolve_value_typehashget_call_typeget_functionis_hash_usedr   ConstanttypeFALLBACKselect)r   r   typvalue	typingctxfntysigfnhis_okfallbackr%   r%   r&   get_hash_value;   s   
rC   c                    s.    fdd}|  t}||  fi }||fS )Nc                    s   t | | |d S Nr   )rC   )r   r   r:   argsr;   r%   r&   implL   s   z'_get_hash_value_intrinsic.<locals>.impl)r1   r2   r3   )r<   r;   rG   r=   r>   r%   rF   r&   _get_hash_value_intrinsicJ   s   
rH   c                 C      t |jt}|d||S )z8
    Whether the hash value denotes an empty entry.
    ==)r   r6   r7   EMPTYicmp_unsigned)r   r   r@   emptyr%   r%   r&   is_hash_emptyS      rN   c                 C   rI   )z9
    Whether the hash value denotes a deleted entry.
    rJ   r   r6   r7   DELETEDrL   r   r   r@   deletedr%   r%   r&   is_hash_deletedZ   rO   rT   c                 C   rI   )z9
    Whether the hash value denotes an active entry.
    <rP   rR   r%   r%   r&   r5   a   s   r5   c                     sH   t dd  D std  t  fdd D s"td  d S )Nc                 S   s   g | ]}t |tjqS r%   )
isinstancer   Set).0r:   r%   r%   r&   
<listcomp>k   s    z!check_all_set.<locals>.<listcomp>z All arguments must be Sets, got c                    s   g | ]
} d  j |j kqS )r   dtype)rX   srE   r%   r&   rY   n   s    z'All Sets must be of the same type, got )allr   r]   r%   r]   r&   check_all_setj   s
   r_   SetLoopindexentrydo_breakc                   @   s   e Zd Zdd Zedd Zejdd Zedd Zejdd Zed	d
 Zejdd
 Zedd Z	e	jdd Z	edd Z
e
jdd Z
edd Zedd Zdd Zd ddZejd!ddZejdd ZdS )"_SetPayloadc                 C   s<   t ||||}|| _|| _|| _|| _|d| _|| _d S )Nentries)r'   _context_builder_ty_payload_get_ptr_by_name_entries_ptr)selfr   r   r    r!   r$   r%   r%   r&   __init__w   s   
z_SetPayload.__init__c                 C      | j jS Nrj   maskrn   r%   r%   r&   rs         z_SetPayload.maskc                 C      || j _d S rq   rr   rn   r;   r%   r%   r&   rs      s   c                 C   rp   rq   rj   usedrt   r%   r%   r&   ry      ru   z_SetPayload.usedc                 C   rv   rq   rx   rw   r%   r%   r&   ry         c                 C   rp   rq   rj   fillrt   r%   r%   r&   r|      ru   z_SetPayload.fillc                 C   rv   rq   r{   rw   r%   r%   r&   r|      rz   c                 C   rp   rq   rj   fingerrt   r%   r%   r&   r~      ru   z_SetPayload.fingerc                 C   rv   rq   r}   rw   r%   r%   r&   r~      rz   c                 C   rp   rq   rj   dirtyrt   r%   r%   r&   r      ru   z_SetPayload.dirtyc                 C   rv   rq   r   rw   r%   r%   r&   r      rz   c                 C      | j S )z>
        A pointer to the start of the entries array.
        )rl   rt   r%   r%   r&   rf         z_SetPayload.entriesc                 C   r   )zC
        A pointer to the start of the NRT-allocated area.
        )rm   rt   r%   r%   r&   r!      r   z_SetPayload.ptrc                 C   s2   t | j| j|}| jj| jt| j|d}|S )z)
        Get entry number *idx*.
        r   )	r   geprh   rl   rg   r   r   r(   ri   )rn   idx	entry_ptrrc   r%   r%   r&   	get_entry   s   

z_SetPayload.get_entryFc                    s  
j 
jj}
j}
jj}j}|tj	}|
|||fi }	||	t|d}
t|d}t}t|}rQ|dtd}d dd} 	
fdd	}tt|t# |}|| ||
}||}|| W d
   n1 sw   Y  | |A |}|| |}||}|
||}|||}|| || | W d
   n1 sw   Y  , r"|}}d|||}|| | W d
   n	1 s2w   Y    | W d
   n	1 sMw   Y  | tdd}| tj!  | tj" ||fS )ag  
        Lookup the *item* with the given hash values in the entries.

        Return a (found, entry index) tuple:
        - If found is true, <entry index> points to the entry containing
          the item.
        - If found is false, <entry index> points to the empty entry that
          the item can be written to (only if *for_insert* is true)
              r,   zlookup.bodyzlookup.foundzlookup.not_foundz
lookup.endc              	      s2  
 | }|j}d|* 	|jf}|   W d   n1 s0w   Y  W d   n1 s?w   Y  t|  W d   n1 s]w   Y  rt|  }	d|| |}
| W d   dS 1 sw   Y  dS dS )zO
            Check entry *i* against the value being searched for.
            rJ   N)r   r2   if_thenrL   keybranchrN   rT   loadr9   store)irc   
entry_hasheqjbb_foundbb_not_foundr   r   eqfn
for_insert
free_indexfree_index_sentinelr@   itemrn   r%   r&   check_entry   s*   

"z(_SetPayload._lookup.<locals>.check_entryNrJ   found)#rg   rh   r7   rs   ri   r[   r0   r1   operatorr   r3   r4   r   r6   r   alloca_once_valueand_append_basic_block	for_rangeLINEAR_PROBESr   addr   r   
goto_blocklshrmulr9   rL   position_at_endphiIntTypeadd_incomingtrue_bit	false_bit)rn   r   r@   r   intp_trs   r[   tyctxr=   r>   onefiveperturbrb   bb_bodybb_endr   r   pr   r   r%   r   r&   _lookup   sx   






 







z_SetPayload._lookupNc              	   c   s    | j }| j}|tj}t|d}|| j|}t	j
|||d?}| |j}t|||j}	||	 t|j||jd}
|
V  W d   n1 sLw   Y  W d   dS W d   dS 1 sdw   Y  dS )zG
        Iterate over the payload's entries.  Yield a SetLoop.
        r   startra   N)rg   rh   get_value_typer   intpr   r6   r   rs   r   r   r   rb   r5   r2   r   r`   rd   )rn   r   r   r   r   r   size
range_looprc   is_usedloopr%   r%   r&   _iterate7  s$   "z_SetPayload._iteratec                 c   s   | j }| j}|tj}t|d}t|d}| j}|d}|d}t	
|| j}	|| ||0 ||	}
||||
|}
||
|	 | |
}t|||j}|||| W d   n1 sjw   Y  || ||	}
|
| _| |
V  dS )z
        Yield a random entry from the payload.  Caller must ensure the
        set isn't empty, otherwise the function won't end.
        r   r   next_entry_bodynext_entry_endN)rg   rh   r   r   r   r   r6   rs   r   r   r   r~   r   r   r   r   r   r   r   r5   r2   cbranchr   )rn   r   r   r   zeror   rs   r   r   rb   r   rc   r   r%   r%   r&   _next_entryK  s.   





	
z_SetPayload._next_entryFrq   )__name__
__module____qualname__ro   propertyrs   setterry   r|   r~   r   rf   r!   r   r   
contextlibcontextmanagerr   r   r%   r%   r%   r&   re   u   s@    	













vre   c                   @   s  e Zd Zdd Zedd Zedd Zedd Zed	d
 Zedd Z	e	j
dd Z	dd Zdd ZdUddZdVddZdVddZdUddZdUddZdUd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/d0 ZdWd2d3Zd4d5 Zd6d7 ZedXd9d:Z edXd;d<Z!ed=d> Z"ed?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dWdIdJZ(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-d8S )YSetInstancec                 C   s2   || _ || _|| _t||| _||||| _d S rq   )rg   rh   ri   r+   
_entrysizemake_helper_set)rn   r   r   r    set_valr%   r%   r&   ro   v  s
   zSetInstance.__init__c                 C   rp   rq   )ri   r[   rt   r%   r%   r&   r[   }  ru   zSetInstance.dtypec                 C   s.   | j }| j}| j j|| j}t||| j|S )z/
        The _SetPayload for this set.
        )rg   rh   nrtmeminfo_datameminfore   ri   )rn   r   r   r!   r%   r%   r&   r$     s   zSetInstance.payloadc                 C   
   | j  S rq   )r   	_getvaluert   r%   r%   r&   r;        
zSetInstance.valuec                 C   rp   rq   )r   r   rt   r%   r%   r&   r     ru   zSetInstance.meminfoc                 C   rp   rq   r   parentrt   r%   r%   r&   r     ru   zSetInstance.parentc                 C   rv   rq   r   rw   r%   r%   r&   r     rz   c                 C   rp   )z<
        Return the number of elements in the size.
        )r$   ry   rt   r%   r%   r&   get_size  s   zSetInstance.get_sizec                 C   s$   | j jr|r	tjntj| j_d S d S rq   )ri   	reflectedr   r   r   r$   r   rn   valr%   r%   r&   	set_dirty  s   zSetInstance.set_dirtyTc                 C   s   | j }| j}|j}||_| | ||_|j}	t|	jd}
|	|	|
 }	|_|j
t|||dd |	|j|
|_W d    n1 sEw   Y  |rQ| |	 | d d S )Nr   Tlikely)rg   rh   r2   incref_valuer   ry   r   r6   r7   r   r   rN   r|   upsizer   )rn   r$   rc   r   r@   	do_resizer   r   old_hashry   r   r%   r%   r&   
_add_entry  s"   

zSetInstance._add_entryc              	   C   s  | j }| j}|j||dd\}}	||}
||
` ||	}|j}||_|r-| | ||_|j	}t
|jd}||| }|_	|jt|||dd ||j||_W d    n1 saw   Y  |rm| | | d W d    d S 1 s}w   Y  d S )NTr   r   r   )rg   rh   r   not_r   r   r2   r   r   ry   r   r6   r7   r   rN   r|   r   r   )rn   r$   r   r@   r   	do_increfr   r   r   r   	not_foundrc   r   ry   r   r%   r%   r&   _add_key  s.   



"zSetInstance._add_keyc                 C   sf   t |jjt|_|r| |j |j}t |jd}| j	|| }|_|r,| 
| | d d S )Nr   T)r   r6   r2   r7   rQ   decref_valuer   ry   rh   subdownsizer   )rn   r$   rc   r   	do_decrefry   r   r%   r%   r&   _remove_entry  s   
zSetInstance._remove_entryc           
      C   sd   | j }| j}|||\}}|| ||}	| ||	| W d    |S 1 s+w   Y  |S rq   )rg   rh   r   r   r   r   )
rn   r$   r   r@   r   r   r   r   r   rc   r%   r%   r&   _remove_key  s   

zSetInstance._remove_keyc                 C   s8   | j }| j}| j}t||| jj|}| |||| d S rq   )rg   rh   r$   rC   ri   r[   r   )rn   r   r   r   r   r$   r@   r%   r%   r&   r     s
   zSetInstance.addc                 C   s6   | j }| j}| j}| ||||}| |||| dS )z`A version of .add for use inside functions following Python calling
        convention.
        N)rg   rh   r$   _pyapi_get_hash_valuer   )rn   pyapir   r   r   r   r$   r@   r%   r%   r&   	add_pyapi  s
   zSetInstance.add_pyapic                 C   s   | j jg}tj}dd }|g}tj|g|R  }	|||	|\}
}|j|
dd ||	  W d   |S 1 s;w   Y  |S )z=Python API compatible version of `get_hash_value()`.
        c                 S      t | S rq   )rH   )r   r%   r%   r&   wrapper     z2SetInstance._pyapi_get_hash_value.<locals>.wrapperFr   N)
ri   r[   r   r   r   	signaturecall_jit_coder   retget_null_object)rn   r   r   r   r   argtypesrestyr   rE   r>   is_errorretvalr%   r%   r&   r     s   

z!SetInstance._pyapi_get_hash_valuec                 C   s8   | j }| j}| j}t||| jj|}|||\}}|S rq   )rg   rh   r$   rC   ri   r[   r   )rn   r   r   r   r$   r@   r   r   r%   r%   r&   contains  s   zSetInstance.containsc                 C   s6   | j }| j}| j}t||| jj|}| |||}|S rq   )rg   rh   r$   rC   ri   r[   r   )rn   r   r   r   r$   r@   r   r%   r%   r&   discard   s   zSetInstance.discardc                 C   s|   | j }| j}|| jj}t||}| j}| }|	|j
| | j||dd W d    n1 s4w   Y  ||S )NF)r   )rg   rh   r   ri   r[   r   alloca_oncer$   r   r   r   r   r   )rn   r   r   ltyr   r$   rc   r%   r%   r&   pop)  s   

zSetInstance.popc                 C   s<   | j }| j}|tj}t|t}| | | 	d d S )NT)
rg   rh   r   r   r   r   r6   MINSIZE_replace_payloadr   )rn   r   r   r   minsizer%   r%   r&   clear8  s   
zSetInstance.clearc              
   C   s  | j }| j}| j}|j}|j}t| ||| jd}|d||}|j|dd\}}	|/ |	|}
|j
||
dd |j|td W d   n1 sOw   Y  W d   n1 s^w   Y  |	[ | |||}||}
|j
||
dd |j|td W d   n1 sw   Y  |j}| }|j}|j||j|jdd W d   n1 sw   Y  W d   n1 sw   Y  W d   |S W d   |S 1 sw   Y  |S )z,
        Return a copy of this set.
        NrJ   Tr   F)zcannot copy setr   )rg   rh   r$   ry   r|   r7   ri   rL   if_else_copy_payloadr   r   	call_convreturn_user_excMemoryErrorchoose_alloc_size_allocate_payloadr   rc   r   r   r2   )rn   r   r   r$   ry   r|   otherno_deleted_entriesif_no_deleted
if_deletedoknentriesother_payloadr   rc   r%   r%   r&   copyA  sT   






zSetInstance.copyc           
   	   C   s   | j }| j}| j}|j}| 5}|j}||j|j\}}	||	| | j
||dd W d   n1 s9w   Y  W d   n1 sHw   Y  | |j dS )z9
        In-place intersection with *other* set.
        Fr  N)rg   rh   r$   r   rc   r   r   r2   r   r   r   r   ry   )
rn   r  r   r   r$   r  r   rc   r   _r%   r%   r&   	intersecth  s   
zSetInstance.intersectc                 C   sl   | j }| j}| j}|j}| }|j}| j||j|jdd W d   n1 s)w   Y  | |j	 dS )z7
        In-place difference with *other* set.
        Fr  N)
rg   rh   r$   r   rc   r   r   r2   r   ry   )rn   r  r   r   r$   r  r   rc   r%   r%   r&   
differencez  s   
zSetInstance.differencec              
   C   s  | j }| j}|j}| m}|jj}|jj}| j}|j||dd\}	}
||
}|	|	>\}}| | j
||dd W d   n1 sEw   Y  | | |||| W d   n1 s_w   Y  W d   n1 snw   Y  W d   n1 s}w   Y  | | jj dS )zA
        In-place symmetric difference with *other* set.
        Tr   Fr  N)rg   rh   r$   r   rc   r   r2   r   r   r  r   r   r   ry   )rn   r  r   r   r  r   r   r@   r$   r   r   rc   	if_commonif_not_commonr%   r%   r&   symmetric_difference  s*   

z SetInstance.symmetric_differenceFc                 C   sV  | j }| j}| j}|j}|rdnd}t|tj}||||j|jy\}	}
|
 |	tj
| W d    n1 s<w   Y  |	L | 8}|j}||j|j\}}||| |	tj
| |  W d    n1 stw   Y  W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  ||S )NrU   <=)rg   rh   r$   r   r   r   r  rL   ry   r   r   r   rc   r   r   r2   r   r   rd   r   )rn   r  strictr   r   r$   r  cmp_opres
if_smaller	if_largerr   rc   r   r  r%   r%   r&   issubset  s8   


zSetInstance.issubsetc              	      s   | j }| j | j}|j}t tj fdd}  d|j|j8\}}| ||| W d    n1 s;w   Y  | ||| W d    n1 sRw   Y  W d    n1 saw   Y   	S )Nc              	      s   |   >}|j}||j|j\}} |  tj |	  W d    n1 s-w   Y  W d    d S W d    d S 1 sEw   Y  d S rq   )
r   rc   r   r   r2   r   r   r   r   rd   )smallerlargerr   rc   r   r  r   r  r%   r&   check  s   

"z%SetInstance.isdisjoint.<locals>.check>)
rg   rh   r$   r   r   r   r  rL   ry   r   )rn   r  r   r$   r  r&  r!  	otherwiser%   r%  r&   
isdisjoint  s&   	
zSetInstance.isdisjointc                 C   sJ  | j }| j}| j}|j}t|tj}||d|j|jy\}}|L |	 8}	|	j
}
||
j|
j\}}||| |tj| |	  W d    n1 sUw   Y  W d    n1 sdw   Y  W d    n1 ssw   Y  | |tj| W d    n1 sw   Y  W d    n1 sw   Y  ||S )NrJ   )rg   rh   r$   r   r   r   r  rL   ry   r   rc   r   r   r2   r   r   r   r   rd   r   )rn   r  r   r   r$   r  r  if_same_sizer(  r   rc   r   r  r%   r%   r&   equals  s6   


zSetInstance.equalsNc           	      C   sf   | tj}|du rt|t}nt|trt||}| |||}| |||d}|	|}||fS )z
        Allocate a SetInstance with its storage.
        Return a (ok, instance) tuple where *ok* is a LLVM boolean and
        *instance* is a SetInstance object (the object's contents are
        only valid when *ok* is true).
        N)
r   r   r   r   r6   r  rV   intr  r  )	clsr   r   r    nitemsr   r  rn   r  r%   r%   r&   allocate_ex  s   

zSetInstance.allocate_exc                 C   s^   |  ||||\}}|j||dd |j|td W d   |S 1 s(w   Y  |S )z
        Allocate a SetInstance with its storage.  Same as allocate_ex(),
        but return an initialized *instance*.  If allocation failed,
        control is transferred to the caller using the target's current
        call convention.
        Fr   )zcannot allocate setN)r/  r   r   r	  r
  r  )r-  r   r   r    r.  r  rn   r%   r%   r&   allocate  s   

zSetInstance.allocatec                 C   s<   | |||d}||j _|tj|j _|j|||j |S )z
        Allocate a new set instance pointing to an existing payload
        (a meminfo pointer).
        Note the parent field has to be filled by the caller.
        N)	r   r   get_constant_nullr   pyobjectr   r   increfr;   )r-  r   r   r    r   rn   r%   r%   r&   from_meminfo  s
   zSetInstance.from_meminfoc              	   C   s  |j }t|d}t|t}|||}t||}|d}	|d}
||	 |	|	A |
|}|d||}|j|dd ||
 W d   n1 sSw   Y  |||}||| ||	 W d   n1 ssw   Y  ||
 |
|S )zT
        Choose a suitable number of entries for the given number of items.
        r   calcsize.bodycalcsize.end>=Fr   N)r7   r   r6   r  shlr   r   r   r   r   r   rL   r   r   r   )r-  r   r   r.  r   r   r  min_entriessize_pr   r   r   is_large_enough	next_sizer%   r%   r&   r  $  s(   




	
zSetInstance.choose_alloc_sizec              	   C   sX  | j }| j}|j}t|d}t|d}| j}|||}||j|}	|	d||	}
|j
|
ddo t||	}|d}|d}|| ||' ||}|||}||| |	d||}|||| W d   n1 sxw   Y  || ||}tr||d	||	|| | ||d
 W d   dS 1 sw   Y  dS )zs
        When adding to the set, ensure it is properly sized for the given
        number of used entries.
        r      r7  Fr   r5  r6  NzKupsize to %zd items: current size = %zd, min entries = %zd, new size = %zd
zcannot grow set)rg   rh   r7   r   r6   r$   r8  r   rs   rL   r   r   r   r   r   r   r   r   r   r   DEBUG_ALLOCSprintf_resize)rn   r.  r   r   r   r   twor$   r9  r   need_resize
new_size_pr   r   new_sizeis_too_smallr%   r%   r&   r   C  s<   





"zSetInstance.upsizec              
   C   s  | j }| j}|j}t|d}t|d}t|t}| j}|||}	||	d|	||	|}	||	|}
|
|j|}||	d|
||	d||}|j|dd t||}|d}|d	}|| ||? ||}|||}|	d
|	|}|| || W d   n1 sw   Y  ||| || W d   n1 sw   Y  || ||}tr||d|||	| | ||d W d   dS 1 sw   Y  dS )zw
        When removing from the set, ensure it is properly sized for the given
        number of used entries.
        r   r=  r7  r  rU   Fr   r5  r6  r'  NzMdownsize to %zd items: current size = %zd, min entries = %zd, new size = %zd
zcannot shrink set)rg   rh   r7   r   r6   r  r$   r8  r9   rL   r   rs   r   r   r   r   r   r   r   r   r   r   r   r>  r?  r@  )rn   r.  r   r   r   r   rA  r  r$   r9  max_sizer   rB  rC  r   r   rD  rE  r%   r%   r&   r   q  sR   





"zSetInstance.downsizec           
      C   s   | j }| j}|}| j|dd}|j||dd |j|t|f W d   n1 s-w   Y  | j}|	 }|j
}	| j||	j|	jddd W d   n1 sSw   Y  | |j dS )zw
        Resize the payload to the given number of entries.

        CAUTION: *nentries* must be a power of 2!
        TreallocFr   N)r   r   )rg   rh   r  r   r   r	  r
  r  r$   r   rc   r   r   r2   _free_payloadr!   )
rn   r$   r  errmsgr   r   old_payloadr  r   rc   r%   r%   r&   r@    s"   

zSetInstance._resizec                 C   s   | j }| j}| j }|j}| |j W d   n1 sw   Y  | | jj | j	|dd}|j
||dd |j|td W d   dS 1 sPw   Y  dS )z
        Replace the payload with a new empty payload with the given number
        of entries.

        CAUTION: *nentries* must be a power of 2!
        NTrG  Fr   )zcannot reallocate set)rg   rh   r$   r   rc   r   r   rI  r!   r  r   r   r	  r
  r  )rn   r  r   r   r   rc   r  r%   r%   r&   r    s   
"zSetInstance._replace_payloadc              
   C   sd  | j }| j}t|tj}|tj}t	|d}t	|d}|
t| j}	||	}
| j}|
|8 }
t||t	||t	||
\}}|j|dd |tj| W d   n1 s_w   Y  |j||dd |r| jj}|jj|||d}t||}n| ||j}|j||||tj}t||}|j|ddn\}}| |tj| W d   n1 sw   Y  |@ |s|| j_|tj | j_!| j"}t#||j$|d ||_%||_&||_'|(||}||_)t*r|+|d	||j$| W d   n	1 sw   Y  W d   n	1 sw   Y  W d   n	1 s(w   Y  ||S )
z
        Allocate and initialize payload for the given number of entries.
        If *realloc* is True, the existing meminfo is reused.

        CAUTION: *nentries* must be a power of 2!
        r   r   Fr   NT)r      .allocated %zd bytes for set at %p: mask = %zd
),rg   rh   r   r   r   r   r   r   r   r6   r   r   ri   r)   r   muladd_with_overflowr   r   r   r   r   r   r   meminfo_varsize_alloc_uncheckedis_null	_imp_dtormodule"meminfo_new_varsize_dtor_uncheckedr   	voidptr_tr  r1  r2  r   r$   memsetr!   ry   r|   r~   r   rs   r>  r?  )rn   r  rH  r   r   r  r   r   r   r"   payload_size
entry_size	allocsizeovfr   r!   alloc_okdtorif_errorif_okr$   new_maskr%   r%   r&   r    sx   



$zSetInstance._allocate_payloadc                 C   s   | j j| j| j| dS )z9
        Free an allocated old payload at *ptr*.
        N)rg   r   meminfo_varsize_freerh   r   )rn   r!   r%   r%   r&   rI  "  s   zSetInstance._free_payloadc                 C   s(  | j }| j}t|tj}|tj}t	|d}t	|d}|
t| j}||}	| j}
|	|
8 }	|j}|||}|t	||	|t	||
|}|j||dd | ||j}|j||||tj}t||}|j|dd|\}}| |tj| W d   n1 sw   Y  |O || j_| j }|j!|_!|j"|_"||_#||_t$||j%|j%||
 |& }| '|j(j) W d   n1 sw   Y  t*r|+|d||j,| W d   n1 sw   Y  W d   n1 sw   Y  W d   n	1 s
w   Y  ||S )z7
        Raw-copy the given payload into self.
        r   r   Tr   FNrM  )-rg   rh   r   r   r   r   r   r   r   r6   r   r   ri   r)   r   rs   r   r   r   r   rQ  rR  r   rS  r   rT  rP  r  r   r   r   r   r$   ry   r|   r~   
raw_memcpyrf   r   r   rc   r   r>  r?  r!   )rn   src_payloadr   r   r  r   r   r   r"   rV  rW  rs   r  rX  r[  r   rZ  r\  r]  r$   r   r%   r%   r&   r  (  sj   



"zSetInstance._copy_payloadc                 C   s   t j}|tj}tt |||g}d| jj	 }t j
|||d}|jrad|_t| }t||| j|jd }	|	 }
|
j}|j|| jj	|j W d   n1 sXw   Y  |  |S )z Define the dtor for set
        z
.dtor.set.)namelinkonce_odrr   N)r   rT  r   r   size_tr   FunctionTypeVoidTyperi   r[   get_or_insert_functionis_declarationlinkage	IRBuilderr   re   rE   r   rc   r   decrefr   ret_void)rn   r   rR  	llvoidptrllsize_tr=   fnamer?   r   r$   r   rc   r%   r%   r&   rQ  i  s$   
zSetInstance._imp_dtorc                 C      | j j| j| jj| dS )z Incref an element value
        N)rg   r   r3  rh   ri   r[   r   r%   r%   r&   r        zSetInstance.incref_valuec                 C   rp  )z Decref an element value
        N)rg   r   rk  rh   ri   r[   r   r%   r%   r&   r     rq  zSetInstance.decref_value)T)TTr   rq   ).r   r   r   ro   r   r[   r$   r;   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r"  r)  r+  classmethodr/  r0  r4  r  r   r   r@  r  r  rI  r  rQ  r   r   r%   r%   r%   r&   r   t  sh    











			'


.<
CAr   c                   @   sZ   e Zd Zdd Zedd Zedd Zedd Zed	d
 Z	e	j
dd
 Z	dd ZdS )SetIterInstancec                 C   sL   || _ || _|| _||||| _| j j|| j}t||| jj	|| _
d S rq   )rg   rh   ri   r   _iterr   r   r   re   	containerrj   )rn   r   r   	iter_typeiter_valr!   r%   r%   r&   ro     s   zSetIterInstance.__init__c                 C   sJ   t |||j|}| |||d }|tjd}t|||j_|j	|j_	|S rD   )
r   ru  get_constantr   r   r   r   rt  rb   r   )r-  r   r   rv  r   set_instrn   rb   r%   r%   r&   from_set  s   
zSetIterInstance.from_setc                 C   r   rq   )rt  r   rt   r%   r%   r&   r;     r   zSetIterInstance.valuec                 C   rp   rq   )rt  r   rt   r%   r%   r&   r     ru   zSetIterInstance.meminfoc                 C   s   | j | jjS rq   )rh   r   rt  rb   rt   r%   r%   r&   rb     s   zSetIterInstance.indexc                 C   s   | j || jj d S rq   )rh   r   rt  rb   rw   r%   r%   r&   rb     s   c                 C   s   | j }| j}t|jd}|  |j|d#}|j}|  |	|j
 | j|j || _ |  W d    d S 1 s=w   Y  d S )Nr   r   )rb   rj   r   r6   r7   set_exhaustedr   rc   	set_validyield_r   rh   r   rd   )rn   resultrb   r$   r   r   rc   r%   r%   r&   iternext  s   
"zSetIterInstance.iternextN)r   r   r   ro   rr  rz  r   r;   r   rb   r   r  r%   r%   r%   r&   rs    s    




rs  c              	   C   s   t |}t| |||}|dkrJt||}t||}| tj|}t	||}	|
t||d|	j}
||
 W d   n1 sEw   Y  t| |||jS )zD
    Build a set of the given type, containing the given items.
    r   N)lenr   r0  r   
pack_arrayr   rx  r   r   r   r   r   rb   r   r   r;   )r   r   r    itemsr.  instarray	array_ptrcountr   r   r%   r%   r&   	build_set  s   r  c                 C   s$   |j }t| ||}t| |||jS rq   )return_typer   r0  r   r;   )r   r   r>   rE   r    r  r%   r%   r&   set_empty_constructor  s   r  c           
      C   s   |j }|j\}|\}t| |||}t| |||}t| |||}	||	j | j	||j
|	j W d    n1 s;w   Y  t| |||jS rq   )r  rE   r   r   r0  r   r   r;   r   rk  r[   r   )
r   r   r>   rE   r    
items_typer  nr  r   r%   r%   r&   set_constructor  s   r  c                 C   s    t | ||jd |d }| S rD   )r   rE   r   r   r   r>   rE   r  r%   r%   r&   set_len  s   r  c                 C   s&   t | ||jd |d }||d S Nr   r   )r   rE   r   r  r%   r%   r&   in_set  s   r  getiterc                 C   s(   t | ||j|d }t| ||j|jS rD   )rs  rz  r  r
   r;   r  r%   r%   r&   getiter_set  s   r  r  c                 C   s&   t | ||jd |d }|| d S rD   )rs  rE   r  )r   r   r>   rE   r~  r  r%   r%   r&   iternext_listiter	  s   r  zset.addc                 C   2   t | ||jd |d }|d }|| |  S r  )r   rE   r   get_dummy_valuer   r   r>   rE   r  r   r%   r%   r&   set_add  s   
r  c                 C      t ||}dd }||fS )Nc                 S   r  r  )r   rE   r   r  r  r%   r%   r&   set_discard"  s   
z!_set_discard.<locals>.set_discardr   none)r<   r\   r   r>   r  r%   r%   r&   _set_discard  s   r  r   c                 C      dd S )Nc                 S   
   t | |S rq   )r  r\   r   r%   r%   r&   <lambda>.     
 z ol_set_discard.<locals>.<lambda>r%   r  r%   r%   r&   ol_set_discard,  ru   r  c                 C   s   | |}dd }||fS )Nc                 S   st   t | ||jd |d }|jj}|jt||dd | j|t	d W d    |
 S 1 s1w   Y  |
 S )Nr   Fr   )zset.pop(): empty set)r   rE   r$   ry   r   r   rP  r	  r
  KeyErrorr  )r   r   r>   rE   r  ry   r%   r%   r&   set_pop5  s   

z_set_pop.<locals>.set_poprZ   )r<   r\   r>   r  r%   r%   r&   _set_pop1  s   
	r  r  c                 C   r  )Nc                 S   r   rq   )r  r\   r%   r%   r&   r  C      zol_set_pop.<locals>.<lambda>r%   r  r%   r%   r&   
ol_set_popA  ru   r  c                 C   r  )Nc                 S   s|   t | ||jd |d }|d }||}|j||dd | j|td W d    |  S 1 s5w   Y  |  S )Nr   r   Fr   )zset.remove(): key not in set)	r   rE   r   r   r   r	  r
  r  r  )r   r   r>   rE   r  r   r   r%   r%   r&   
set_removeJ  s   


z_set_remove.<locals>.set_remover  )r<   r\   r   r>   r  r%   r%   r&   _set_removeF  s   
r  removec                 C   s   | j |kr	dd S d S )Nc                 S   r  rq   )r  r  r%   r%   r&   r  Z  r  zol_set_remove.<locals>.<lambda>rZ   r  r%   r%   r&   ol_set_removeW  s   
r  c                 C   s   t |}dd }||fS )Nc                 S   s(   t | ||jd |d }|  |  S rD   )r   rE   r  r  r  r%   r%   r&   	set_clearc  s   z_set_clear.<locals>.set_clearr  )r<   r\   r>   r  r%   r%   r&   
_set_clear_  s   
r  r  c                 C   r  )Nc                 S   r   rq   )r  r  r%   r%   r&   r  m  r  zol_set_clear.<locals>.<lambda>r%   r  r%   r%   r&   ol_set_cleark  ru   r  c                 C   s   ||}dd }||fS )Nc                 S   s2   t | ||jd |d }| }t| ||j|jS rD   )r   rE   r  r   r  r;   r   r   r>   rE   r  r  r%   r%   r&   set_copyt  s   z_set_copy.<locals>.set_copyr%   )r<   r\   r>   r  r%   r%   r&   	_set_copyp  s   r  r  c                 C   r  )Nc                 S   r   rq   )r  r  r%   r%   r&   r  ~  r  zol_set_copy.<locals>.<lambda>r%   r  r%   r%   r&   ol_set_copy|  ru   r  c                 C   B   t | ||jd |d }t | ||jd |d }|| |  S r  )r   rE   r  r  r  r%   r%   r&   set_difference_update  s   
r  c                 C      t ||}|tfS rq   )r   r  r  r<   abr>   r%   r%   r&   _set_difference_update     r  difference_updatec                 C      t | | dd S )Nc                 S   r  rq   )r  r  r  r%   r%   r&   r    r  z,set_difference_update_impl.<locals>.<lambda>r_   r  r%   r%   r&   set_difference_update_impl     
r  c                 C   r  r  )r   rE   r  r  r  r%   r%   r&   set_intersection_update     
r  c                 C   r  rq   )r   r  r  r  r%   r%   r&   _set_intersection_update  r  r  intersection_updatec                 C   r  )Nc                 S   r  rq   )r  r  r%   r%   r&   r    r  z.set_intersection_update_impl.<locals>.<lambda>r  r  r%   r%   r&   set_intersection_update_impl  r  r  c                 C   r  r  )r   rE   r  r  r  r%   r%   r&   set_symmetric_difference_update  r  r  c                 C   r  rq   )r   r  r  r  r%   r%   r&    _set_symmetric_difference_update  r  r  symmetric_difference_updatec                 C   r  )Nc                 S   r  rq   )r  r  r%   r%   r&   r    r  z6set_symmetric_difference_update_impl.<locals>.<lambda>r  r  r%   r%   r&   $set_symmetric_difference_update_impl  r  r  z
set.updatec                 C   s   t | ||jd |d }|jd }|d }t| |||}|d ur-||jj|}|| t| |||"}	| ||	j	|j
|j
}
||
 | j||j
|	j	 W d    n1 sYw   Y  |d uri||jj |  S r  )r   rE   r   r   r$   ry   r   r   castr;   r[   r   rk  r   r  )r   r   r>   rE   r  r  r  r  rD  r   castedr%   r%   r&   
set_update  s   


r  c                    s(   t fdd t|  fdd}d S )Nc                    s   |||} fdd}||fS )Nc                    s:   |j |jd ks
J  | ||| t| ||jd |d S rD   )r  rE   r
   )r   r   r>   rE   rG   r%   r&   codegen  s   z>gen_operator_impl.<locals>._set_operator_intr.<locals>.codegenr%   r<   r  r  r>   r  r  r%   r&   _set_operator_intr  s   
z-gen_operator_impl.<locals>._set_operator_intrc                    s   t | |  fddS )Nc                    s
    | |S rq   r%   r  r  r%   r&   r    r  z=gen_operator_impl.<locals>._ol_set_operator.<locals>.<lambda>r  r  r  r%   r&   _ol_set_operator  s   
z+gen_operator_impl.<locals>._ol_set_operator)r   r   )oprG   r  r%   )r  rG   r&   gen_operator_impl  s   r  r  c                 C      t | | dd }|S )Nc                 S   s   |   }|| |S rq   )r  r  r  r  r\   r%   r%   r&   difference_impl  s   
z,impl_set_difference.<locals>.difference_implr  )r  r  r  r%   r%   r&   impl_set_difference  s   
r  intersectionc                 C   r  )Nc                 S   s<   t | t |k r|  }|| |S | }||  |S rq   )r  r  r  r  r%   r%   r&   intersection_impl     

z+set_intersection.<locals>.intersection_implr  )r  r  r  r%   r%   r&   set_intersection     

r  r  c                 C   r  )Nc                 S   <   t | t |kr|  }|| |S | }||  |S rq   )r  r  r  r  r%   r%   r&   symmetric_difference_impl  r  z;set_symmetric_difference.<locals>.symmetric_difference_implr  )r  r  r  r%   r%   r&   set_symmetric_difference  r  r  unionc                 C   r  )Nc                 S   r  rq   )r  r  updater  r%   r%   r&   
union_impl(  r  zset_union.<locals>.union_implr  )r  r  r  r%   r%   r&   	set_union#  r  r  c                 C   r  )Nc                 S   :   t | ||jd |d }t | ||jd |d }||S r  )r   rE   r)  r  r%   r%   r&   r  ;     
z _set_isdisjoint.<locals>.codegenr   booleanr  r%   r%   r&   _set_isdisjoint7     r  r)  c                 C   r  )Nc                 S   r  rq   )r  r  r%   r%   r&   r  H  r  z set_isdisjoint.<locals>.<lambda>r  r  r%   r%   r&   set_isdisjointD     
r  c                 C   r  )Nc                 S   r  r  r   rE   r"  r  r%   r%   r&   r  O  r  z_set_issubset.<locals>.codegenr  r  r%   r%   r&   _set_issubsetK  r  r  r"  c                 C   r  )Nc                 S   r  rq   )r  r  r%   r%   r&   r  \  r  zset_issubset.<locals>.<lambda>r  r  r%   r%   r&   set_issubsetW  s   
r  
issupersetc                 C   r  )Nc                 S   s
   | | S rq   )r"  r  r%   r%   r&   superset_impld     
z%set_issuperset.<locals>.superset_implr  )r  r  r  r%   r%   r&   set_issuperset_  s   
r  c                 C   r  )Nc                 S   r  r  )r   rE   r+  r  r%   r%   r&   r  m  r  z_set_eq.<locals>.codegenr  r  r%   r%   r&   _set_eqi  r  r  c                 C   r  )Nc                 S   r  rq   )r  r  r%   r%   r&   r  y  r  zset_eq.<locals>.<lambda>r  r  r%   r%   r&   set_equ  r  r  c                 C   r  )Nc                 S   s
   | |k S rq   r%   r  r%   r%   r&   ne_impl  r  zset_ne.<locals>.ne_implr  )r  r  r  r%   r%   r&   set_ne{     
r  c                 C   r  )Nc                 S   s>   t | ||jd |d }t | ||jd |d }|j|ddS )Nr   r   T)r  r  r  r%   r%   r&   r    s   z_set_lt.<locals>.codegenr  r  r%   r%   r&   _set_lt  r  r  c                 C   r  )Nc                 S   r  rq   )r  r  r%   r%   r&   r    r  zset_lt.<locals>.<lambda>r  r  r%   r%   r&   set_lt  r  r  c                 C   r  )Nc                 S   s   || k S rq   r%   r  r%   r%   r&   gt_impl  r   zset_gt.<locals>.gt_implr  )r  r  r  r%   r%   r&   set_gt  r  r  c                 C   s^   t | ||jd |d }t | ||jd |d }||jtj}||jtj}|d||S )Nr   r   rJ   )r   rE   ptrtointr   r   r   icmp_signed)r   r   r>   rE   r  r  mambr%   r%   r&   set_is  s
   r  c                 C   s   |j |j ksJ |S rq   rZ   )r   r   fromtytotyr   r%   r%   r&   
set_to_set  s   r  )}__doc__collectionsr   mathr   	functoolsr   llvmliter   
numba.corer   r   r   numba.core.imputilsr   r   r	   r
   r   r   r   r   r   
numba.miscr   numba.cpythonr   numba.core.errorsr   r   numba.core.extendingr   r   r   r'   r+   rK   rQ   r8   r  r   r>  rC   rH   rN   rT   r5   r_   
namedtupler`   objectre   r   rs  r  setr  IterableTyper  r  rW   r  r   Anyr  r  SetIterBORROWEDr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  iandiorisubixorop_op_implr   r  r   r  xorr  or_r  r  r  r  ler  ger  r  r   r  ner  r  ltr  gtr  is_r  r  r%   r%   r%   r&   <module>   s   ,
	        "6

























	



























