o
    i/                     @   s^   d dl Z d dlZd dlmZmZ e ddZe ddZdZ	dZ
dd
dZdd Zdd ZdS )    N)typesconfigQuicksortImplementation)compile	partition
partition3insertion_sortrun_quicksort	Partition)startstop   d   Fc           	         s  t jrtj}ntj}|d|r&|r| dd n| dd | dd  n| dd | dd  d	d
 }| |d ur=|n||  fdd|  fdd| fdd| fdd|rp| fdd}n| fdd}| fdd}t| |S )Nr   c                 S   s   dd t t| D S )Nc                 S   s   g | ]}|qS  r   ).0xr   r   Q/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/numba/misc/quicksort.py
<listcomp>*   s    z9make_quicksort_impl.<locals>.make_res.<locals>.<listcomp>)rangelenAr   r   r   make_res(   s   z%make_quicksort_impl.<locals>.make_resc                 S   s   t | jS N)nparangesizer   r   r   r   r   ,   s   c                 S   s   | | S r   r   r   
idx_or_valr   r   r   GET0      z make_quicksort_impl.<locals>.GETc                 S      | S r   r   r   r   r   r   r   5      c                 S   s   |S r   r   r   r   r   r   r   9   r"   c                 S   s   | |k S )z?
        Trivial comparison function between two keys.
        r   )abr   r   r   
default_lt=   s   z'make_quicksort_impl.<locals>.default_ltc                    s   |dksJ ||krdS t |d |d D ]=}|| } | |}|}||krN| | ||d  rN||d  ||< |d8 }||krN| | ||d  s2|||< qdS )zL
        Insertion sort A[low:high + 1]. Note the inclusive bounds.
        r   N   )r   )r   Rlowhighikvjr   LTr   r   r   E   s   
  
z+make_quicksort_impl.<locals>.insertion_sortc                    s  |dksJ ||ksJ || d? } | ||  | || r.|| || ||< ||<  | ||  | || rJ|| || ||< ||<  | ||  | || rf|| || ||< ||<  | || }|| || ||< ||< |}|d }	 ||k r | || |r|d7 }||k r | || |s||kr| | || r|d8 }||kr| | || s||krn|| || ||< ||< |d7 }|d8 }q|| || ||< ||< |S )zj
        Partition A[low:high + 1] around a chosen pivot.  The pivot's index
        is returned.
        r   r&   r   )r   r'   r(   r)   midpivotr*   r-   r.   r   r   r   Y   s8   z&make_quicksort_impl.<locals>.partitionc                    sN  || d? } | | | | r| | | | | |< | |<  | | | | r2| | | | | |< | |<  | | | | rH| | | | | |< | |< | | }| | | | | |< | |< |}|}|d }||kr | | |r| | | | | |< | |< |d7 }|d7 }n || | r| | | | | |< | |< |d8 }n|d7 }||kse||fS )a  
        Three-way partition [low, high) around a chosen pivot.
        A tuple (lt, gt) is returned such that:
            - all elements in [low, lt) are < pivot
            - all elements in [lt, gt] are == pivot
            - all elements in (gt, high] are > pivot
        r&   r   )r   r(   r)   r0   r1   ltgtr*   )r/   r   r   r      s.   	


z'make_quicksort_impl.<locals>.partition3c                    s  | }t | dk r|S tgt }tt | d |d< d}|dkr|d8 }|| \}}|| tkr{|tk s;J | |||}|| || kr`||kr[t|d |||< |d7 }|d }n||krqt||d ||< |d7 }|d }|| tks5 | ||| |dks%|S )N   r&   r   )r   r
   	MAX_STACKSMALL_QUICKSORT)r   r'   stacknr(   r)   r*   )r   r   r   zeror   r   run_quicksort1   s2   
z+make_quicksort_impl.<locals>.run_quicksort1c                    s<   | j dkr	 | S t| jd d D ]} | |  q| S )Nr&   )ndimr   ndindexshape)r   idxr:   r   r   r	      s
   
z*make_quicksort_impl.<locals>.run_quicksortc                        | S r   r   r   r@   r   r   r	      r    c                    s  t gd }t t| d |d< d}|dkr|d8 }|| \}}|| tkrz|tk s/J | ||\}}||kr@|d }n4||krI|d }n+|| || krct |d |||< |d7 }|d }nt ||d ||< |d7 }|d }|| tks) | || |dksd S d S )Nr   r&   r   )r
   r   r6   r5   )r   r7   r8   r(   r)   lr)r   r   r9   r   r   _run_quicksort   s.   


z+make_quicksort_impl.<locals>._run_quicksort)r   USE_LEGACY_TYPE_SYSTEMr   intppy_intr   )	wrapr2   
is_argsortis_listis_np_arrayrF   r%   r	   rD   r   )r   r/   r   r   r   r   r:   r9   r   make_quicksort_impl   sJ   


*""rL   c                  O   s   t dd g| R i |S )Nc                 S   r!   r   r   fr   r   r   <lambda>   s    z#make_py_quicksort.<locals>.<lambda>)rL   argskwargsr   r   r   make_py_quicksort   s   rS   c                     s*   ddl m  t fddg| R i |S )Nr   register_jitablec                    rA   r   r   rM   rT   r   r   rO     s    z$make_jit_quicksort.<locals>.<lambda>)numba.core.extendingrU   rL   rP   r   rT   r   make_jit_quicksort  s   rW   )NFFF)collectionsnumpyr   
numba.corer   r   
namedtupler   r
   r6   r5   rL   rS   rW   r   r   r   r   <module>   s    
 e