o
    {if                     @   s  d dl Z d dlZd dlZejddddd Zejddddd	 Zejd
dddd Zejdej	ejj
ejj	ddddgejjejjdddddd Zejdddd Zejdddd ZeddZejdddd Zejddddd Zejddejjidd d!d" Zejd#ddd$d% Zejd&ddd'd( Zejd#ddd)d* Zejd+dejjejjejjejjejjd,dd-d.d/ Zejd+dejjejjejjejjejjd,dd-d0d1 Zejd2dejjejjejjejjejjd,dd-d3d4 Zejdej	ej	ejejej	ejej	d5ddd6d7d8 Zejddejejej	ejejejejd9d:d;d< Zejdddej	ej	ejejej	ejej	d5d=d>d? Z ejddd@dA Z!ejdddBdC Z"ejddddDdE Z#dFdG Z$dS )H    Nzvoid(i8[:], i8)T)cachec                 C   s   |  |d  dS )z3Seed the random number generator with a given seed.i  N)fill)	rng_stateseed r   E/home/ubuntu/.local/lib/python3.10/site-packages/pynndescent/utils.pyr      s   r   z	i4(i8[:])c                 C   s   | d d@ d> d@ | d d> d@ | d A d? A | d< | d d@ d	> d@ | d d
> d@ | d A d? A | d< | d
 d@ d> d@ | d
 d> d@ | d
 A d? A | d
< | d | d A | d
 A S )zA fast (pseudo)-random number generator.

    Parameters
    ----------
    state: array of int64, shape (3,)
        The internal state of the rng

    Returns
    -------
    A (pseudo)-random int32 value
    r   l       l             l             l             r   )stater   r   r   tau_rand_int   s   r   z	f4(i8[:])c                 C   s   t | }tt|d S )a  A fast (pseudo)-random number generator for floats in the range [0,1]

    Parameters
    ----------
    state: array of int64, shape (3,)
        The internal state of the rng

    Returns
    -------
    A (pseudo)-random float32 in the interval [0, 1]
    i)r   absfloat)r   integerr   r   r   tau_rand+   s   r   zf4(f4[::1])r   C)readonly)dimi)localsfastmathr   c                 C   s:   d}| j d }t|D ]}|| | | |  7 }qt|S )zCompute the (standard l2) norm of a vector.

    Parameters
    ----------
    vec: array of shape (dim,)

    Returns
    -------
    The l2 norm of vec.
    g        r   )shaperangenpsqrt)vecresultr   r   r   r   r   norm<   s
   

r$   c                 C   sh   t j| t jd}t| D ]%}d}d}|r-t|| }t|D ]
}||| kr( nqd}|s|||< q|S )aP  Generate n_samples many integers from 0 to pool_size such that no
    integer is selected twice. The duplication constraint is achieved via
    rejection sampling.

    Parameters
    ----------
    n_samples: int
        The number of random samples to select from the pool

    pool_size: int
        The size of the total pool of candidates to sample from

    rng_state: array of int64, shape (3,)
        Internal state of the random number generator

    Returns
    -------
    sample: array of shape(n_samples,)
        The ``n_samples`` randomly selected elements from the pool.
    dtypeTr   F)r    emptyint64r   r   )	n_samples	pool_sizer   r#   r   reject_samplejkr   r   r   rejection_sample]   s   
r.   c                 C   sh   t jt| t|fdt jd}t jt| t|ft jt jd}t jt| t|ft jd}|||f}|S )a^  Constructor for the numba enabled heap objects. The heaps are used
    for approximate nearest neighbor search, maintaining a list of potential
    neighbors sorted by their distance. We also flag if potential neighbors
    are newly added to the list or not. Internally this is stored as
    a single ndarray; the first axis determines whether we are looking at the
    array of candidate graph_indices, the array of distances, or the flag array for
    whether elements are new or not. Each of these arrays are of shape
    (``n_points``, ``size``)

    Parameters
    ----------
    n_points: int
        The number of graph_data points to track in the heap.

    size: int
        The number of items to keep on the heap for each graph_data point.

    Returns
    -------
    heap: An ndarray suitable for passing to other numba enabled heap functions.
    r%   )r    fullintint32inffloat32zerosuint8)n_pointssizeindices	distancesflagsr#   r   r   r   	make_heap   s
    
r<   c                 C   s   |d d | j d k ra|d d }|d }|}| | | | k r!|}|| j d k r2| | | | k r2|}||kr8dS | | | | | |< | |< || || ||< ||< |}|d d | j d k sdS dS )zRestore the heap property for a heap with an out of place element
    at position ``elt``. This works with a heap pair where heap1 carries
    the weights and heap2 holds the corresponding elements.r   r   r   Nr   )heap1heap2elt
left_childright_childswapr   r   r   siftdown   s   rD   F)parallelr   c                 C   s   t | jd D ]L}t| jd d ddD ]>}| ||f | |df | |df< | ||f< |||f ||df ||df< |||f< t||d|f | |d|f d qq| |fS )a  Given two arrays representing a heap (indices and distances), reorder the
     arrays by increasing distance. This is effectively just the second half of
     heap sort (the first half not being required since we already have the
     graph_data in a heap).

     Note that this is done in-place.

    Parameters
    ----------
    indices : array of shape (n_samples, n_neighbors)
        The graph indices to sort by distance.
    distances : array of shape (n_samples, n_neighbors)
        The corresponding edge distance.

    Returns
    -------
    indices, distances: arrays of shape (n_samples, n_neighbors)
        The indices and distances sorted by increasing distance.
    r   r   r/   N)numbapranger   r   rD   )r9   r:   r   r,   r   r   r   deheap_sort   s   **&rH   idx)rE   r   r   c                 C   s.  | d }| d }|j d }|j d }tj||fdtjd}tj||ftjtjd}	tj||fdtjd}
tj||ftjtjd}|| d }t|D ]}|| }|| }t|| |}t	|D ]|}t	|D ]u}|||f }|dkr||krz||k s||kr||k r|||f }t
|}|r||kr||k rt|	| || || ||kr||k rt|	| || || qf||kr||k rt|| |
| || ||kr||k rt|| |
| || qfq`qK| d }| d }t|D ]'}t	|D ] }|||f }t	|D ]}|||f |krd|||f<  nqqq||
fS )a  Build a heap of candidate neighbors for nearest neighbor descent. For
    each vertex the candidate neighbors are any current neighbors, and any
    vertices that have the vertex as one of their nearest neighbors.

    Parameters
    ----------
    current_graph: heap
        The current state of the graph for nearest neighbor descent.

    max_candidates: int
        The maximum number of new candidate neighbors.

    rng_state: array of int64, shape (3,)
        The internal state of the rng

    Returns
    -------
    candidate_neighbors: A heap with an array of (randomly sorted) candidate
    neighbors for each vertex in the graph.
    r   r   r   r/   r%   )r   r    r0   r2   r3   r4   rF   rG   minr   r   checked_heap_push)current_graphmax_candidatesr   	n_threadscurrent_indicescurrent_flags
n_verticesn_neighborsnew_candidate_indicesnew_candidate_priorityold_candidate_indicesold_candidate_priority
block_sizenlocal_rng_stateblock_start	block_endr   r,   rI   isndr9   r;   r-   r   r   r   new_build_candidates   s   

*r^   zb1(u1[::1],i4)c                 C   s    |d? }d|d@ > }| | |@ S Nr   r      r   table	candidatelocmaskr   r   r   has_been_visitedC  s   rf   zvoid(u1[::1],i4)c                 C   s(   |d? }d|d@ > }| |  |O  < d S r_   r   ra   r   r   r   mark_visitedJ  s   rg   c                 C   s:   |d? }t d|d@ > }| | |@ }| |  |O  < |S )zCheck if candidate was visited and mark it as visited in one operation.
    
    Returns True if the candidate was already visited, False otherwise.
    More efficient than separate has_been_visited + mark_visited calls.
    r   r   r`   )rF   r6   )rb   rc   rd   re   was_visitedr   r   r   check_and_mark_visitedR  s
   ri   zi4(f4[::1],i4[::1],f4,i4))r8   r   ic1ic2i_swap)r   r   r   c           	      C   s   || d krdS | j d }|| d< ||d< d}	 d| d }|d }||kr'n9||kr5| | |kr4|}nn+| | | | krG|| | k rF|}nn|| | k rP|}nn| | | |< || ||< |}q|| |< |||< dS Nr   Tr   r   r=   	
prioritiesr9   prX   r8   r   rj   rk   rl   r   r   r   simple_heap_push`  s:   
rq   c           	      C   s   || d krdS | j d }t|D ]}||| kr dS q|| d< ||d< d}	 d| d }|d }||kr7n9||krE| | |krD|}nn+| | | | krW|| | k rV|}nn|| | k r`|}nn| | | |< || ||< |}q(|| |< |||< dS rm   r   r   rn   r   r   r   rK     sB   
rK   z$i4(f4[::1],i4[::1],u1[::1],f4,i4,u1)c                 C   s  || d krdS | j d }t|D ]}||| kr dS q|| d< ||d< ||d< d}	 d| d }|d }	||kr;n?|	|krI| | |krH|}
nn1| | | |	 kr[|| | k rZ|}
nn|| |	 k rd|	}
nn| |
 | |< ||
 ||< ||
 ||< |
}q,|| |< |||< |||< dS rm   rr   )ro   r9   r;   rp   rX   fr8   r   rj   rk   rl   r   r   r   checked_flagged_heap_push  sH   
rt   )dist_thresh_pdist_thresh_qrp   qr]   max_updatesmax_threshold)rE   r   r   r   c              	   C   s  |j d }|j d }	|j d }
|| d }t|D ]}d}| j d }t|D ]}|| | }||ks7||kr9 nt|	D ]}||krE n|||f }|dk rPq=|| }|| }t||	D ]@}||kre n9|||f }|dk rpq]|||| }|| }t||}||kr|| ||df< || ||df< || ||df< |d7 }q]t|
D ]@}||kr n9|||f }|dk rq|||| }|| }t||}||kr|| ||df< || ||df< || ||df< |d7 }qq=q'|||< qdS )a  Generate graph updates into a pre-allocated array.

    This is more efficient than generating lists of tuples because:
    1. No dynamic memory allocation during the parallel loop
    2. Better cache locality with contiguous array storage
    3. Each thread writes to its own section of the array

    Parameters
    ----------
    update_array : ndarray of shape (n_threads, max_updates_per_thread, 3)
        Pre-allocated array to store updates. Each row stores (p, q, d).

    n_updates_per_thread : ndarray of shape (n_threads,)
        Output array to store the number of updates generated by each thread.

    new_candidate_block : ndarray of shape (block_size, max_candidates)
        New candidate indices for this block.

    old_candidate_block : ndarray of shape (block_size, max_candidates)
        Old candidate indices for this block.

    dist_thresholds : ndarray of shape (n_vertices,)
        Current distance thresholds (max heap distance) for each vertex.

    data : ndarray of shape (n_vertices, n_features)
        The data points.

    dist : callable
        Distance function.

    n_threads : int
        Number of threads to use.
    r   r   r   Nr   rF   rG   r   max)update_arrayn_updates_per_threadnew_candidate_blockold_candidate_blockdist_thresholdsdatadistrN   rW   max_new_candidatesmax_old_candidatesrows_per_threadtrI   rx   rr   r,   rp   data_pru   r-   rw   r]   rv   ry   r   r   r   generate_graph_update_array  sf   
9





r   )rp   rw   r]   addedrX   r   r,   )rE   r   r   c              
   C   s$  d}| d }| d }| d }|j d }|| d }	t|D ]q}
|
|	 }t||	 |}t|D ]_}t|| D ]V}t|||df }t|||df }t|||df }||krr||k rrt|| || || ||d}||7 }||kr||k rt|| || || ||d}||7 }q7q/q|S )a  Apply graph updates from a pre-allocated array.

    Uses block-based processing where each thread only updates vertices
    in its assigned block, avoiding the need for duplicate checking.

    Parameters
    ----------
    current_graph : tuple of (indices, distances, flags)
        The current nearest neighbor graph heap.

    update_array : ndarray of shape (n_threads, max_updates_per_thread, 3)
        Array of updates where each row is (p, q, d).

    n_updates_per_thread : ndarray of shape (n_threads,)
        Number of valid updates from each generating thread.

    n_threads : int
        Number of threads.

    Returns
    -------
    n_changes : int
        Total number of updates that modified the graph.
    r   r   r   )	r   rF   rG   rJ   r   r    r2   r4   rt   )rL   r|   r}   rN   	n_changesro   r9   r;   rQ   vertex_block_sizerX   rZ   r[   r   r,   rp   rw   r]   r   r   r   r   apply_graph_update_array  s8   (
r   )rE   r   r   r   c
              	   C   sf  |j d }
|j d }|j d }|
|	 d }t|	D ]}d}| j d }t|D ]}|| | }||
ks9||kr; nt|D ]}||krG n|||f }|dk rRq?||| ||d   }||| ||d   }|| }t||D ]X}||kr{ nQ|||f }|dk rqs||| ||d   }||| ||d   }|||||}|| }t||}||kr|| ||df< || ||df< || ||df< |d7 }qst|D ]Y}||kr nR|||f }|dk rq||| ||d   }||| ||d   }|||||}|| }t||}||kr)|| ||df< || ||df< || ||df< |d7 }qq?q(|||< qdS )zBGenerate graph updates for sparse data into a pre-allocated array.r   r   r   Nrz   )r|   r}   r~   r   r   indsindptrr   r   rN   rW   r   r   r   r   rI   rx   r   r   r,   rp   	from_inds	from_dataru   r-   rw   to_indsto_datar]   rv   ry   r   r   r   "sparse_generate_graph_update_array  sp   







r   c              	   C   s|   t |jd D ]4}t |jd D ]*}|||f }|dkr:||| || }t| d | | d | | d | ||d qq| S Nr   r   r   r   r   rt   )heapgraph_indicesr   metricr   rI   r,   r]   r   r   r   !initalize_heap_from_graph_indicesD  s   *r   c              	   C   sv   t |jd D ]1}t |jd D ]'}|||f }|dkr7|||f }t| d | | d | | d | ||d qq| S r   r   )r   r   graph_distancesr   rI   r,   r]   r   r   r   /initalize_heap_from_graph_indices_and_distancesQ  s   *r   c              	   C   s   t |jd D ]^}t|jd D ]T}|||f }||| ||d   }	||| ||d   }
||| ||d   }||| ||d   }||	|
||}t| d | | d | | d | ||d qq| S r   )rF   rG   r   r   rt   )r   r   data_indptrdata_indices	data_valsr   r   rI   r,   ind1data1ind2data2r]   r   r   r   (sparse_initalize_heap_from_graph_indices_  s   ,	r   c                   C   s   t t   S )N)timectimer   r   r   r   tsr  s   r   )%r   rF   numpyr    njitr   r   r   typesr4   Arrayintpuint32r$   r.   r<   EMPTY_GRAPHrD   rH   r(   r^   rf   rg   ri   uint16rq   rK   rt   r2   r   r6   r   r   r   r   r   r   r   r   r   r   <module>   s   






$





e



-
2
5	
o
>

V



