
    g
                     L    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
dS )
    N)get_invlist_sizesc                 N   |j         \  }}|j         |fk    sJ t          | t          j                  r|dz  }|| j        k    sJ |"|j         |fk    sJ t          j        |          }|                     |t          j        |          |t          j        |                     dS )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         U/var/www/html/mpstechhub/venv/lib/python3.11/site-packages/faiss/contrib/ivf_tools.pyadd_preassignedr      s     7DAq7qe)U122 	Q	
yQE!!!!nS!!	5>!c5>!#4#4        c                 n   t          | t          j                  r| j                                        dk    s
J d            t          j        | j                            d                    }|                    |          }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 |                     ||||          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float32N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&      sC    )U455 :##%%***,P***29?3E3Ea3H3HII	__R  (99	8DAq)U122 	Q	>a!122222 Xq)"238DDD

Ay'7#88888''AxD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	        }	| 
                    | |	|          | |	|           |	|          |           t          j        |j        |dz                                             }
t          |
d                   }t          j        |j        |                                          }t          j        |j        |                                          }|
||fS )z[
    Perform a range search in the IVF index, with predefined lists to
    search into
    r   r   r   Nr   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   
 7DAq)U122 	Q Xq)"238DDD

Ay'7#88888	>a!122222

!!
$
$C	B((	22a55&
8bbnn   chA..3355Dd2h--Kcm[99>>@@D [99>>@@Gwr   c                    |j         dk    rD| j                                        }|                    |           |                    |           n|j         | j        k    sJ | j        }d| _        t          j        | j                  }|j	        
                    |           || _        t          | d          r| j                            |           n|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7	I&&&)$$$$#y6666 "G I()<==M7###'Iy.// 7$++M::::(5	$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 |                    |           t          j        | j                  }|                    t          j        |                     dS )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    
 ZFE?e####$Y%899Iy....G444D 
TU	+	+	+B6"S111122222 d### +I,>??HenT2233333r   c                 v    t          | j                  }t          j        |          }t	          | |           d S N)r   rZ   r$   argsortr[   )r   invlist_sizesr\   s      r   sort_invlists_by_sizerb      s7    %i&899M:m$$DY%%%%%r   r_   )numpyr$   r   faiss.contrib.inspect_toolsr   r   r&   r>   rP   r[   rb    r   r   <module>rf      s         9 9 9 9 9 9   "E E E E># # # #L  04 4 4.& & & & &r   