
    i
                     P    S SK rS SKrS SKJr  S	S jrS	S jrS	S jrS rS r	S r
g)
    N)get_invlist_sizesc                    UR                   u  pEUR                   U4:X  d   e[        U [        R                  5      (       a  US-  nXPR                  :X  d   eUb)  UR                   U4:X  d   e[        R
                  " U5      nU R                  U[        R
                  " U5      U[        R
                  " U5      5        g)zH
Add elements to an IVF index, where the assignment is already computed
   N)shape
isinstancefaissIndexBinaryIVFdswig_ptradd_core)	index_ivfxaidsnr
   s         g/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/faiss/contrib/ivf_tools.pyadd_preassignedr      s     77DA77qe)U1122	Q
yyQE!!!nnS!	5>>!c5>>!#4    c                    [        U [        R                  5      (       a  U R                  R	                  5       S:X  d   S5       e[        R
                  " U R                  R                  S5      5      nUR                  U5      n[        R                  " U R                  5      n UR                  u  pg[        U [        R                  5      (       a  US-  nSnOSnXpR                  :X  d   eUR                  X`R                  4:X  d   eUc!  [        R                  " X`R                  4US9nOUR                  X`R                  4:X  d   eU R!                  XX45      $ )z
Perform a search in the IVF index, with predefined lists to search into.
Supports indexes with pretransforms (as opposed to the
IndexIVF.search_preassigned, that cannot be applied with pretransform).
   z"chain must have only one componentr   r   int32float32dtype)r   r   IndexPreTransformchainsizedowncast_VectorTransformatapplydowncast_indexindexr   r	   r
   nprobenpzerossearch_preassigned)	r   xqklist_nos
coarse_dis	transformr   r
   dis_types	            r   r&   r&      s&    )U4455##%*P,PP*229??3E3Ea3HI	__R ((9	88DA)U1122	Q>>a!1!12222 XXq"2"238D
A'7'7#8888''xDDr   c           	      ,   UR                   u  pV[        U [        R                  5      (       a  US-  nSnOSnUc!  [        R
                  " XPR                  4US9nOUR                   XPR                  4:X  d   eX`R                  :X  d   eUR                   XPR                  4:X  d   e[        R                  " U5      n[        R                  n	U R                  XY" U5      UU	" U5      U	" U5      U5        [        R                  " UR                  US-   5      R                  5       n
[        U
S   5      n[        R                  " UR                  U5      R                  5       n[        R                  " UR                   U5      R                  5       nXU4$ )zO
Perform a range search in the IVF index, with predefined lists to
search into
r   r   r   r   r   )r   r   r   r	   r$   emptyr#   r
   RangeSearchResultr   range_search_preassigned_crev_swig_ptrlimscopyint	distanceslabels)r   r   radiusr)   r*   r   r
   r,   resspr3   num_resultsdistindicess                 r   range_search_preassignedr>   <   s[   
 77DA)U1122	Q XXq"2"238D
A'7'7#8888>>a!1!12222

!
!!
$C	B((	2a5&
8bn chhA.335Dd2h-Kcmm[9>>@D  [9>>@Gwr   c                    UR                   S:X  a=  U R                  R                  5       nUR                  U5        UR	                  U5        OUR                   U R
                  :X  d   eU R                  nSU l        [        R                  " U R                  5      nUR                  R                  U5        Xl        [        U S5      (       a  U R                  R                  U5        U$ U/U l        U$ )zLreplace the IVF quantizer with a flat quantizer and return the
old quantizerr   Freferenced_objects)ntotal	quantizerreconstruct_ntrainaddnlist
own_fieldsr   r!   thisownhasattrr@   append)r   new_quantizer	centroidsold_ownold_quantizers        r   replace_ivf_quantizerrP   b   s     q ''557	I&)$##y666 ""G I(()<)<=M7#'y.//$$++M:  )6	$r   c                 (   UR                   u  nU R                  U:X  d   e[        R                  " U R                  5      nUR
                  U R                  :X  d   e[        R                  " USS9n[        R                  " XS9n[        R                  " U[        R                  " U[        S9:H  5      (       d   eUR                  U5        [        R                  " U R                  5      nUR                  [        R                   " U5      5        g)zApply some permutation to the inverted lists, and modify the quantizer
entries accordingly.
Perm is an array of size nlist, where old_index = perm[new_index]
int64r   )	minlengthN)r   rF   r   r!   rB   rA   r$   ascontiguousarraybincountallonesr5   permute_entriesdowncast_InvertedListsinvlistspermute_invlistsr   )r   permrF   rB   bcrZ   s         r   r[   r[   z   s    
 ZZFE??e###$$Y%8%89Iy...G4D 
T	+B66"S112222 d# ++I,>,>?HennT23r   c                 p    [        U R                  5      n[        R                  " U5      n[	        X5        g N)r   rZ   r$   argsortr[   )r   invlist_sizesr\   s      r   sort_invlists_by_sizerb      s(    %i&8&89M::m$DY%r   r_   )numpyr$   r   faiss.contrib.inspect_toolsr   r   r&   r>   rP   r[   rb    r   r   <module>rf      s0      9"E>#L04.&r   