o
    8ήc                     @   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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 dd	lmZ dd
l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+e,dd Z-G d!d" d"e.Z/G d#d$ d$e.Z0G d%d& d&e.Z1d'd( Z2ee3d)d* Z4ee3ej5d+d, Z6ee7ej8d-d. Z9eej:ej8ej;d/d0 Z<ed1ej8d2d3 Z=ed4ej>eej?d5d6 Z@ed7ej8ej;d8d9 ZAed:ej8ej;d;d< ZBed=ej8d>d? ZCed@ej8ej;dAdB ZDedCej8dDdE ZEedFej8dGdH ZFedIej8ej5dJdK ZGedLej8ej8dMdN ZHedOej8ej8dPdQ ZIedRej8ej5dSdT ZJejKeHfejLeJfejMeGfejNeIffD ]\ZOZPeeOej8ej8ePfdUdVZQqheejRej8ej8edWej8ej8dXdY ZSeejTej8ej8edZej8ej8d[d\ ZUeejVej8ej8ed]ej8ej8d^d_ ZWeejXej8ej8ed`ej8ej8dadb ZYedcej8ej8ddde ZZeej[ej8ej8edfej8ej8dgdh Z\eej]ej8ej8ediej8ej8djdk Z^eej_ej8ej8dlde ZZeej`ej8ej8dmdn Zaeejbej8ej8dodp Zceejdej8ej8dqdr Zeeejfej8ej8dsdt Zgeej8ej8dudv ZhdS )wz&
Support for native homogeneous sets.
    N)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)cached_property)	quicksort)slicing)	intrinsic)NumbaValueErrorc                 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"   ;/tmp/pip-target-vg8gfxp4/lib/python/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   
r@   c                    s.    fdd}|  t}||  fi }||fS )Nc                    s   t | | |d S Nr   )r@   )r   r   r7   argsr8   r"   r#   implL   s   z'_get_hash_value_intrinsic.<locals>.impl)r.   r/   r0   )r9   r8   rD   r:   r;   r"   rC   r#   _get_hash_value_intrinsicJ   s   
rE   c                 C      t |jt}|d||S )z8
    Whether the hash value denotes an empty entry.
    ==)r   r3   r4   EMPTYicmp_unsigned)r   r   r=   emptyr"   r"   r#   is_hash_emptyS      rK   c                 C   rF   )z9
    Whether the hash value denotes a deleted entry.
    rG   r   r3   r4   DELETEDrI   r   r   r=   deletedr"   r"   r#   is_hash_deletedZ   rL   rQ   c                 C   rF   )z9
    Whether the hash value denotes an active entry.
    <rM   rO   r"   r"   r#   r2   a   s   r2   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__o   s   
z_SetPayload.__init__c                 C      | j jS Nr]   maskra   r"   r"   r#   rf   x      z_SetPayload.maskc                 C      || j _d S rd   re   ra   r8   r"   r"   r#   rf   |   s   c                 C   rc   rd   r]   usedrg   r"   r"   r#   rl      rh   z_SetPayload.usedc                 C   ri   rd   rk   rj   r"   r"   r#   rl         c                 C   rc   rd   r]   fillrg   r"   r"   r#   ro      rh   z_SetPayload.fillc                 C   ri   rd   rn   rj   r"   r"   r#   ro      rm   c                 C   rc   rd   r]   fingerrg   r"   r"   r#   rq      rh   z_SetPayload.fingerc                 C   ri   rd   rp   rj   r"   r"   r#   rq      rm   c                 C   rc   rd   r]   dirtyrg   r"   r"   r#   rs      rh   z_SetPayload.dirtyc                 C   ri   rd   rr   rj   r"   r"   r#   rs      rm   c                 C      | j S )z>
        A pointer to the start of the entries array.
        )r_   rg   r"   r"   r#   rY         z_SetPayload.entriesc                 C   rt   )zC
        A pointer to the start of the NRT-allocated area.
        )r`   rg   r"   r"   r#   r      ru   z_SetPayload.ptrc                 C   s2   t | j| j|}| jj| jt| j|d}|S )z)
        Get entry number *idx*.
        r   )	r   gepr[   r_   rZ   r   r   r%   r\   )ra   idx	entry_ptrrV   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.
            rG   N)ry   r/   if_thenrI   keybranchrK   rQ   loadr6   store)irV   
entry_hasheqjbb_foundbb_not_foundr   r   eqfn
for_insert
free_indexfree_index_sentinelr=   itemra   r"   r#   check_entry   s*   

"z(_SetPayload._lookup.<locals>.check_entryNrG   found)#rZ   r[   r4   rf   r\   dtyper-   r.   operatorr   r0   r1   r   r3   r   alloca_once_valueand_append_basic_block	for_rangeLINEAR_PROBESr   addr   r~   
goto_blocklshrmulr6   rI   position_at_endphiIntTypeadd_incomingtrue_bit	false_bit)ra   r   r=   r   intp_trf   r   tyctxr:   r;   onefiveperturbrU   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.
        rz   startrT   N)rZ   r[   get_value_typer   intpr   r3   r   rf   r   r   ry   rU   r2   r/   r|   rS   rW   )ra   r   r   r   r   r   size
range_looprV   is_usedloopr"   r"   r#   _iterate/  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   rz   next_entry_bodynext_entry_endN)rZ   r[   r   r   r   r   r3   rf   r   r   r   rq   r~   r   r   r   r   r   ry   r2   r/   cbranchr   )ra   r   r   r   zeror   rf   r   r   rU   r   rV   r   r"   r"   r#   _next_entryC  s.   





	
z_SetPayload._next_entryFrd   )__name__
__module____qualname__rb   propertyrf   setterrl   ro   rq   rs   rY   r   ry   r   
contextlibcontextmanagerr   r   r"   r"   r"   r#   rX   m   s@    	













vrX   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 rd   )rZ   r[   r\   r(   
_entrysizemake_helper_set)ra   r   r   r   set_valr"   r"   r#   rb   n  s
   zSetInstance.__init__c                 C   rc   rd   )r\   r   rg   r"   r"   r#   r   u  rh   zSetInstance.dtypec                 C   s.   | j }| j}| j j|| j}t||| j|S )z/
        The _SetPayload for this set.
        )rZ   r[   nrtmeminfo_datameminforX   r\   )ra   r   r   r   r"   r"   r#   r!   y  s   zSetInstance.payloadc                 C   
   | j  S rd   )r   	_getvaluerg   r"   r"   r#   r8        
zSetInstance.valuec                 C   rc   rd   )r   r   rg   r"   r"   r#   r     rh   zSetInstance.meminfoc                 C   rc   rd   r   parentrg   r"   r"   r#   r     rh   zSetInstance.parentc                 C   ri   rd   r   rj   r"   r"   r#   r     rm   c                 C   rc   )z<
        Return the number of elements in the size.
        )r!   rl   rg   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 rd   )r\   	reflectedr   r   r   r!   rs   ra   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 )Nrz   Tlikely)rZ   r[   r/   incref_valuer}   rl   r   r3   r4   r   r|   rK   ro   upsizer   )ra   r!   rV   r   r=   	do_resizer   r   old_hashrl   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   rz   r   )rZ   r[   r   not_r|   ry   r/   r   r}   rl   r   r3   r4   r   rK   ro   r   r   )ra   r!   r   r=   r   	do_increfr   r   r   r   	not_foundrV   r   rl   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 )Nrz   T)r   r3   r/   r4   rN   decref_valuer}   rl   r[   subdownsizer   )ra   r!   rV   r   	do_decrefrl   r   r"   r"   r#   _remove_entry  s   
zSetInstance._remove_entryc           
      C   sd   | j }| j}|||\}}|| ||}	| ||	| W d    |S 1 s+w   Y  |S rd   )rZ   r[   r   r|   ry   r   )
ra   r!   r   r=   r   r   r   r   r   rV   r"   r"   r#   _remove_key  s   

zSetInstance._remove_keyc                 C   s8   | j }| j}| j}t||| jj|}| |||| d S rd   )rZ   r[   r!   r@   r\   r   r   )ra   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)rZ   r[   r!   _pyapi_get_hash_valuer   )ra   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   s   t | S rd   )rE   )r   r"   r"   r#   wrapper     z2SetInstance._pyapi_get_hash_value.<locals>.wrapperFr   N)
r\   r   r   r   r   	signaturecall_jit_coder|   retget_null_object)ra   r   r   r   r   argtypesrestyr   rB   r;   is_errorretvalr"   r"   r#   r     s   

z!SetInstance._pyapi_get_hash_valuec                 C   s8   | j }| j}| j}t||| jj|}|||\}}|S rd   )rZ   r[   r!   r@   r\   r   r   )ra   r   r   r   r!   r=   r   r   r"   r"   r#   contains  s   zSetInstance.containsc                 C   s6   | j }| j}| j}t||| jj|}| |||}|S rd   )rZ   r[   r!   r@   r\   r   r   )ra   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   )rZ   r[   r   r\   r   r   alloca_oncer!   r   r   r}   r   r   )ra   r   r   ltyr}   r!   rV   r"   r"   r#   pop!  s   

zSetInstance.popc                 C   s<   | j }| j}|tj}t|t}| | | 	d d S )NT)
rZ   r[   r   r   r   r   r3   MINSIZE_replace_payloadr   )ra   r   r   r   minsizer"   r"   r#   clear0  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.
        NrG   Tr   F)zcannot copy setr   )rZ   r[   r!   rl   ro   r4   r\   rI   if_else_copy_payloadr|   r   	call_convreturn_user_excMemoryErrorchoose_alloc_size_allocate_payloadr   rV   r   r}   r/   )ra   r   r   r!   rl   ro   otherno_deleted_entriesif_no_deleted
if_deletedoknentriesother_payloadr   rV   r"   r"   r#   copy9  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)rZ   r[   r!   r   rV   r   r}   r/   r|   r   r   r   rl   )
ra   r  r   r   r!   r  r   rV   r   _r"   r"   r#   	intersect`  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)
rZ   r[   r!   r   rV   r   r}   r/   r   rl   )ra   r  r   r   r!   r  r   rV   r"   r"   r#   
differencer  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)rZ   r[   r!   r   rV   r}   r/   r   ry   r   r   r   r   rl   )ra   r  r   r   r  r   r}   r=   r!   r   r   rV   	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 )NrR   <=)rZ   r[   r!   r   r   r   r   rI   rl   r   r   r   rV   r   r}   r/   r|   r   rW   r   )ra   r  strictr   r   r!   r  cmp_opres
if_smaller	if_largerr   rV   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 rd   )
r   rV   r   r}   r/   r|   r   r   r   rW   )smallerlargerr   rV   r   r	  r   r  r"   r#   check  s   

"z%SetInstance.isdisjoint.<locals>.check>)
rZ   r[   r!   r   r   r   r   rI   rl   r   )ra   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 )NrG   )rZ   r[   r!   r   r   r   r   rI   rl   r   rV   r   r}   r/   r|   r   r   r   rW   r   )ra   r  r   r   r!   r  r  if_same_sizer  r   rV   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   r3   r   
isinstanceintr   r   )	clsr   r   r   nitemsr   r  ra   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  ra   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   increfr8   )r!  r   r   r   r   ra   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.
        rz   calcsize.bodycalcsize.end>=Fr   N)r4   r   r3   r   shlr   r   r   r~   r   r   rI   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.
        rz      r+  Fr   r)  r*  NzKupsize to %zd items: current size = %zd, min entries = %zd, new size = %zd
zcannot grow set)rZ   r[   r4   r   r3   r!   r,  r   rf   rI   r|   r   r   r   r~   r   r   r   r   r   DEBUG_ALLOCSprintf_resize)ra   r"  r   r   r   r   twor!   r-  r   need_resize
new_size_pr   r   new_sizeis_too_smallr"   r"   r#   r   ;  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.
        rz   r1  r+  r  rR   Fr   r)  r*  r  NzMdownsize to %zd items: current size = %zd, min entries = %zd, new size = %zd
zcannot shrink set)rZ   r[   r4   r   r3   r   r!   r,  r6   rI   r   rf   r   r|   r   r   r   r~   r   r   r   r   r   r2  r3  r4  )ra   r"  r   r   r   r   r5  r   r!   r-  max_sizer   r6  r7  r   r   r8  r9  r"   r"   r#   r   i  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   )rZ   r[   r   r|   r   r   r   r   r!   r   rV   r   r}   r/   _free_payloadr   )
ra   r!   r  errmsgr   r   old_payloadr  r   rV   r"   r"   r#   r4    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!
        NTr;  Fr   )zcannot reallocate set)rZ   r[   r!   r   rV   r   r}   r=  r   r   r|   r   r   r   r   )ra   r  r   r   r   rV   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   rz   Fr   NT)r      .allocated %zd bytes for set at %p: mask = %zd
),rZ   r[   r   r   r   r   r   r   r   r3   r   r   r\   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   r%  r&  r   r!   memsetr   rl   ro   rq   r   rf   r2  r3  )ra   r  r<  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)rZ   r   meminfo_varsize_freer[   r   )ra   r   r"   r"   r#   r=    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   rz   Tr   FNrA  )-rZ   r[   r   r   r   r   r   r   r   r3   r   r   r\   r&   r   rf   r   r   r|   r   rE  rF  r   rG  r   rH  rD  r   r   r   r   r   r!   rl   ro   rq   
raw_memcpyrY   r   r   rV   r}   r2  r3  r   )ra   src_payloadr   r   r  r   r   r   r   rJ  rK  rf   r  rL  rO  r   rN  rP  rQ  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   rH  r   r   size_tr   FunctionTypeVoidTyper\   r   get_or_insert_functionis_declarationlinkage	IRBuilderr   rX   rB   r   rV   r   decrefr}   ret_void)ra   r   rF  	llvoidptrllsize_tr:   fnamer<   r   r!   r   rV   r"   r"   r#   rE  a  s$   
zSetInstance._imp_dtorc                 C      | j j| j| jj| dS )z Incref an element value
        N)rZ   r   r'  r[   r\   r   r   r"   r"   r#   r   }     zSetInstance.incref_valuec                 C   rd  )z Decref an element value
        N)rZ   r   r_  r[   r\   r   r   r"   r"   r#   r     re  zSetInstance.decref_value)T)TTr   rd   ).r   r   r   rb   r   r   r!   r8   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r
  r  r  r  r  r  classmethodr#  r$  r(  r   r   r   r4  r   r   r=  r   rE  r   r   r"   r"   r"   r#   r   l  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 rd   )rZ   r[   r\   r   _iterr   r   r   rX   	containerr]   )ra   r   r   	iter_typeiter_valr   r"   r"   r#   rb     s   zSetIterInstance.__init__c                 C   sJ   t |||j|}| |||d }|tjd}t|||j_|j	|j_	|S rA   )
r   ri  get_constantr   r   r   r   rh  rU   r   )r!  r   r   rj  r   set_instra   rU   r"   r"   r#   from_set  s   
zSetIterInstance.from_setc                 C   r   rd   )rh  r   rg   r"   r"   r#   r8     r   zSetIterInstance.valuec                 C   rc   rd   )rh  r   rg   r"   r"   r#   r     rh   zSetIterInstance.meminfoc                 C   s   | j | jjS rd   )r[   r   rh  rU   rg   r"   r"   r#   rU     s   zSetIterInstance.indexc                 C   s   | j || jj d S rd   )r[   r   rh  rU   rj   r"   r"   r#   rU     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 )Nrz   r   )rU   r]   r   r3   r4   set_exhaustedr   rV   	set_validyield_r}   r[   r   rW   )ra   resultrU   r!   r   r   rV   r"   r"   r#   iternext  s   
"zSetIterInstance.iternextN)r   r   r   rb   rf  rn  r   r8   r   rU   r   rs  r"   r"   r"   r#   rg    s    




rg  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   r$  r   
pack_arrayr   rl  r   r   r   r   rv   rU   r   r
   r8   )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 rd   )return_typer   r$  r
   r8   )r   r   r;   rB   r   rw  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 rd   )r|  rB   r   r   r$  r   r   r8   r   r_  r   r
   )
r   r   r;   rB   r   
items_typerv  nrw  r   r"   r"   r#   set_constructor  s   r  c                 C   s    t | ||jd |d }| S rA   )r   rB   r   r   r   r;   rB   rw  r"   r"   r#   set_len  s   r  c                 C   s&   t | ||jd |d }||d S Nr   rz   )r   rB   r   r  r"   r"   r#   in_set  s   r  getiterc                 C   s(   t | ||j|d }t| ||j|jS rA   )rg  rn  r|  r	   r8   r  r"   r"   r#   getiter_set  s   r  rs  c                 C   s&   t | ||jd |d }|| d S rA   )rg  rB   rs  )r   r   r;   rB   rr  rw  r"   r"   r#   iternext_listiter  s   r  zset.addc                 C   2   t | ||jd |d }|d }|| |  S r  )r   rB   r   get_dummy_valuer   r   r;   rB   rw  r   r"   r"   r#   set_add     
r  zset.discardc                 C   r  r  )r   rB   r   r  r  r"   r"   r#   set_discard  r  r  zset.popc                 C   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   rB   r!   rl   r|   r   rD  r   r   KeyErrorr   )r   r   r;   rB   rw  rl   r"   r"   r#   set_pop  s   

r  z
set.removec                 C   s|   t | ||jd |d }|d }||}|j||dd | j|td W d    |  S 1 s5w   Y  |  S )Nr   rz   Fr   )zset.remove(): key not in set)	r   rB   r   r|   r   r   r   r  r  )r   r   r;   rB   rw  r   r   r"   r"   r#   
set_remove'  s   


r  z	set.clearc                 C   s(   t | ||jd |d }|  |  S rA   )r   rB   r   r  r  r"   r"   r#   	set_clear5  s   r  zset.copyc                 C   s2   t | ||jd |d }| }t| ||j|jS rA   )r   rB   r  r
   r|  r8   r   r   r;   rB   rw  r  r"   r"   r#   set_copy;  s   r  zset.difference_updatec                 C   B   t | ||jd |d }t | ||jd |d }|| |  S r  )r   rB   r  r  r  r"   r"   r#   set_difference_updateA     
r  zset.intersection_updatec                 C   r  r  )r   rB   r
  r  r  r"   r"   r#   set_intersection_updateJ  r  r  zset.symmetric_difference_updatec                 C   r  r  )r   rB   r  r  r  r"   r"   r#   set_symmetric_difference_updateS  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   rB   r   r   r!   rl   r   r   castr8   r   r   r_  r   r  )r   r   r;   rB   rw  r~  rv  r  r8  r   castedr"   r"   r#   
set_update\  s   


r  c                 C   s:   |j |jd ks
J || ||| t| ||jd |d S rA   )r|  rB   r	   )r   r   r;   rB   op_implr"   r"   r#   set_inplace  s   r  zset.differencec                 C      dd }|  ||||S )Nc                 S   s   |   }|| |S rd   )r  difference_updateabsr"   r"   r#   difference_impl  s   
z'set_difference.<locals>.difference_implcompile_internal)r   r   r;   rB   r  r"   r"   r#   set_difference  s   r  zset.intersectionc                 C   r  )Nc                 S   s<   t | t |k r|  }|| |S | }||  |S rd   )rt  r  intersection_updater  r"   r"   r#   intersection_impl     

z+set_intersection.<locals>.intersection_implr  )r   r   r;   rB   r  r"   r"   r#   set_intersection     
r  zset.symmetric_differencec                 C   r  )Nc                 S   <   t | t |kr|  }|| |S | }||  |S rd   )rt  r  symmetric_difference_updater  r"   r"   r#   symmetric_difference_impl  r  z;set_symmetric_difference.<locals>.symmetric_difference_implr  )r   r   r;   rB   r  r"   r"   r#   set_symmetric_difference  s   
r  z	set.unionc                 C   r  )Nc                 S   r  rd   )rt  r  updater  r"   r"   r#   
union_impl  r  zset_union.<locals>.union_implr  )r   r   r;   rB   r  r"   r"   r#   	set_union  r  r  zset.isdisjointc                 C   :   t | ||jd |d }t | ||jd |d }||S r  )r   rB   r  r  r"   r"   r#   set_isdisjoint     
r  zset.issubsetc                 C   r  r  r   rB   r  r  r"   r"   r#   set_issubset  s   
r  zset.issupersetc                 C   r  )Nc                 S   s
   | | S rd   )r  r  r  r"   r"   r#   superset_impl     
z%set_issuperset.<locals>.superset_implr  )r   r   r;   rB   r  r"   r"   r#   set_issuperset  s   r  c                 C   r  r  )r   rB   r  r  r"   r"   r#   r    r  c                 C   r  )Nc                 S   s
   | |k S rd   r"   r  r"   r"   r#   ne_impl  r  zset_ne.<locals>.ne_implr  )r   r   r;   rB   r  r"   r"   r#   set_ne     r  c                 C   s>   t | ||jd |d }t | ||jd |d }|j|ddS )Nr   rz   T)r  r  r  r"   r"   r#   set_lt  s   r  c                 C   r  )Nc                 S   s   || k S rd   r"   r  r"   r"   r#   gt_impl  r   zset_gt.<locals>.gt_implr  )r   r   r;   rB   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   rz   rG   )r   rB   ptrtointr   r   r   icmp_signed)r   r   r;   rB   r  r  mambr"   r"   r#   set_is  s
   r  c                 C   s   |j |j ksJ |S rd   )r   )r   r   fromtytotyr   r"   r"   r#   
set_to_set  s   r  )i__doc__collectionsr   mathr   llvmliter   
numba.corer   r   r   numba.core.imputilsr   r   r   r	   r
   r   r   r   r   numba.core.utilsr   
numba.miscr   numba.cpythonr   numba.extendingr   numba.core.errorsr   r$   r(   rH   rN   r5   r   r   r2  r@   rE   rK   rQ   r2   
namedtuplerS   objectrX   r   rg  r{  setr}  IterableTyper  rt  Setr  r   Anyr  r  SetIterBORROWEDr  r  r  r  r  r  r  r  r  r  r  iandiorisubixorop_r  r  r   r  r   r  xorr  or_r  r  ler  ger  r   ner  ltr  gtr  is_r  r  r"   r"   r"   r#   <module>   s    ,
	        "6













	














