
    rh
                     J    d dl Zd dlZd dlmZ d	dZd	dZd	dZd Zd Z	d Z
y)
    N)get_invlist_sizesc                 l   |j                   \  }}|j                   |fk(  sJ t        | t        j                        r|dz  }|| j                  k(  sJ |'|j                   |fk(  sJ t        j
                  |      }| j                  |t        j
                  |      |t        j
                  |             y)zP
    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         j/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/faiss/contrib/ivf_tools.pyadd_preassignedr      s     77DAq77qe)U112	Q	
yyQE!!!nnS!	5>>!c5>>!#4    c                    t        | t        j                        r| j                  j	                         dk(  sJ d       t        j
                  | j                  j                  d            }|j                  |      }t        j                  | j                        } |j                  \  }}t        | t        j                        r|dz  }d}nd}|| j                  k(  sJ |j                  || j                  fk(  sJ |$t        j                  || j                  f|      }n|j                  || j                  fk(  sJ | j!                  ||||      S )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##%*P,PP*229??3E3Ea3HI	__R ((9	88DAq)U112	Q	>>a!1!12222 XXq)"2"238D
Ay'7'7#8888''AxDDr   c           	      $   |j                   \  }}t        | t        j                        r|dz  }d}nd}|$t	        j
                  || j                  f|      }n|j                   || j                  fk(  sJ || j                  k(  sJ |j                   || j                  fk(  sJ t        j                  |      }t        j                  }	| j                  | |	|      | |	|       |	|      |       t        j                  |j                  |dz         j                         }
t        |
d         }t        j                  |j                  |      j                         }t        j                  |j                   |      j                         }|
||fS )z[
    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>   <   sd   
 77DAq)U112	Q XXq)"2"238D
Ay'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                    |j                   dk(  r=| j                  j                         }|j                  |       |j	                  |       n|j                   | j
                  k(  sJ | j                  }d| _        t        j                  | j                        }|j                  j                  |       || _        t        | d      r| j                  j                  |       |S |g| _        |S )zQ replace 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#'Iy./$$++M:  )6	$r   c                     |j                   \  }| j                  |k(  sJ t        j                  | j                        }|j
                  | j                  k(  sJ t        j                  |d      }t        j                  ||      }t        j                  |t        j                  |t              k(        sJ |j                  |       t        j                  | j                        }|j                  t        j                   |             y)z Apply 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 
TU	+B66"S11222 d# ++I,>,>?HennT23r   c                 p    t        | j                        }t        j                  |      }t	        | |       y 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   