o
    'ѹg$                     @   s8  d Z 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mZmZmZmZmZmZmZ dd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zeeeejdd Zeejddd Zeejddd Z eejddd Z!edejej"d d! Z#d"d# Z$eejd$d% Z%eej&j'ejd&d' Z(d(S ))z2
Implement slices and various slice computations.
    )zip_longest)ir)cgutilstypestypingutils)impl_ret_borrowedimpl_ret_new_refimpl_ret_untrackediternext_impllower_builtin
lower_castlower_constantlower_getattrc                 C   s2   |  d|t|jd}| ||}| |||S )z^
    Fix negative index by adding *size* to it.  Positive
    indices are left untouched.
    <r   N)icmp_signedr   Constanttypeaddselect)builderidxsizeZis_negativeZwrapped_index r   Y/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/numba/cpython/slicing.py	fix_index   s   r   c           	   	      s  t jdt jd} fdd} t jW\}}| |d |d W d   n1 s=w   Y  | |} |}|d|| |d|| W d   n1 scw   Y  W d   dS W d   dS 1 s{w   Y  dS )z
    Fix *slice* start and stop to be valid (inclusive and exclusive, resp)
    indexing bounds for a sequence of the given *size*.
    r   c                    s   t | }t |}t| |  d|} j|dd t| | W d    n1 s0w   Y   d|} j|dd t| | W d    d S 1 sUw   Y  d S )Nr   FZlikely>=)getattrr   setattrr   if_then)Z
bound_nameZ
lower_replZ
upper_replboundZ	underflowZoverflowr   r   slicezeror   r   	fix_bound"   s   
"zfix_slice.<locals>.fix_boundstartstopN)r   r   r   Zif_elser   
is_neg_intstepr   )	r   r$   r   Z	minus_oner&   Zif_neg_stepZif_pos_steplowerupperr   r#   r   	fix_slice   s"   "r-   c              	   C   s   |j }|j}|j}t|jd}t|jd}t| |}| ||}| ||}	| 	||}
| 
||
|	}| 	|| ||}| 
|| d||| d||}| 
|||S )a   
    Given a slice, compute the number of indices it spans, i.e. the
    number of iterations that for_range_slice() will execute.

    Pseudo-code:
        assert step != 0
        if step > 0:
            if stop <= start:
                return 0
            else:
                return (stop - start - 1) // step + 1
        else:
            if stop >= start:
                return 0
            else:
                return (stop - start + 1) // step + 1

    (see PySlice_GetIndicesEx() in CPython)
       r   r   z<=N)r'   r(   r*   r   r   r   r   r)   subr   r   Zsdivr   )r   slicestructr'   r(   r*   oner%   is_step_negativedeltaZpos_dividendZneg_dividendZdividendZnominal_lengthZis_zero_lengthr   r   r   get_slice_length=   s    r4   c           	      C   sd   |j }|j}|d}|d}| d|j|}| || |||}| || |||}||fS )z?
    Return the [lower, upper) indexing bounds of a slice.
    r   r.   r   N)r'   r(   r   r   r*   r   r   )	r   r0   r'   r(   r%   r1   r2   r+   r,   r   r   r   get_slice_boundsi   s   

r5   c                 C   s   |  |j|S )z4
    Fix the given stride for the slice's step.
    N)mulr*   )r   r$   Zstrider   r   r   
fix_stride{   s   r7   c                 C   s$   |j rt| ||jtdf dS dS )zP
    Guard against *slicestruct* having a zero step (and raise ValueError).
    slice step cannot be zeroN)has_stepr   Z
guard_nullr*   
ValueError)contextr   typr0   r   r   r   guard_invalid_slice   s
   r=   c                 C   s&   d| j d > d }d||| d dfS )z
    Get the default values for a slice's members:
    (start for positive step, start for negative step,
     stop for positive step, stop for negative step, step)
    r.   r   N)Zaddress_size)r;   Zmaxintr   r   r   get_defaults   s   r>   c                    s    fddt  D \}}}}}d gd t|dkr*|jd tjur*|d d< ntt|j|D ]\}	\}
}|
tjurA||	< q2fdd}|d|}|d	| tj	d}|
|||}|
|||}|d|}|d|}|j}
 ||j}||_||_||_| }t ||j|S )
Nc                       g | ]	}  tj|qS r   get_constantr   intp.0xr;   r   r   
<listcomp>       z*slice_constructor_impl.<locals>.<listcomp>   r.   r   c                    s    |  }|d u r
|S |S Nr   )idefaultval)
slice_argsr   r   get_arg_value   s   z-slice_constructor_impl.<locals>.get_arg_value   r   )r>   lenargsr   none	enumeratezipr   rA   rB   r   return_typemake_helperr'   r(   r*   	_getvaluer
   )r;   r   sigrR   default_start_posdefault_start_negdefault_stop_posdefault_stop_negdefault_steprK   tyrM   rO   r*   r2   Zdefault_stopZdefault_startr(   r'   sliresr   )r;   rN   r   slice_constructor_impl   sD   




rb   r'   c                 C      |  |||}|jS rJ   )rW   r'   r;   r   r<   valuer`   r   r   r   slice_start_impl      rf   r(   c                 C   rc   rJ   )rW   r(   rd   r   r   r   slice_stop_impl   rg   rh   r*   c                 C   s(   |j r| |||}|jS | tjdS )Nr.   )r9   rW   r*   rA   r   rB   rd   r   r   r   slice_step_impl   s   ri   zslice.indicesc                 C   s   |d }|  ||jd |d }|jt||dd | j|td W d    n1 s.w   Y  |jt||j	dd | j|td W d    n1 sRw   Y  t
||| | ||j|j|j|j	fS )Nr.   r   Fr   )zlength should not be negative)r8   )rW   rR   r!   r   r)   Z	call_convZreturn_user_excr:   Zis_scalar_zeror*   r-   Z
make_tuplerV   r'   r(   )r;   r   rY   rR   lengthr`   r   r   r   slice_indices   s(   rk   c                    s     ||} tj} fddt D \}}}}	}
|j}|d u r(d}|
}n|dk }||}|j}|d u r?|r<|}n|}n||}|j}|d u rR|rO|	}n|}n||}||_||_||_| S )Nc                    r?   r   r@   rC   rF   r   r   rG      rH   z,make_slice_from_constant.<locals>.<listcomp>Fr   )	rW   Zget_value_typer   rB   r>   r*   r'   r(   rX   )r;   r   r_   pyvalr`   ZltyrZ   r[   r\   r]   r^   r*   Zstep_is_negr'   r(   r   rF   r   make_slice_from_constant   s>   rm   c                 C   s&   t |tjr
|j}n|}t| |||S rJ   )
isinstancer   LiteralZliteral_typerm   )r;   r   r_   rl   r<   r   r   r   constant_slice   s   rp   c                 C   s   t | |||jS rJ   )rm   Zliteral_value)r;   r   ZfromtyZtotyrM   r   r   r   cast_from_literal*  s   
rq   N))__doc__	itertoolsr   Zllvmliter   Z
numba.corer   r   r   r   Znumba.core.imputilsr   r	   r
   r   r   r   r   r   r   r-   r4   r5   r7   r=   r>   r$   ZVarArgAnyrb   Z	SliceTyperf   rh   ri   ZIntegerrk   rm   rp   miscZSliceLiteralrq   r   r   r   r   <module>   s6    (
$,	

/





-
	