a
    ^g                    @   s0  d dl mZ esdev r&ddlmZ nd dlZzd dlZW n e	yT   d dlZY n0 dd Z
dd	 Zd
d Zdd ZG dd deZG dd deZee ejZG dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZ e!e  G d d! d!eZ"e#e" G d"d# d#eZ$e%e$ G d$d% d%eZ&e'e& G d&d' d'eZ(e)e( G d(d) d)eZ*e+e* G d*d+ d+eZ,e-e, G d,d- d-eZ.e/e. G d.d/ d/eZ0e1e0 G d0d1 d1eZ2e3e2 G d2d3 d3eZ4e5e4 G d4d5 d5eZ6e7e6 G d6d7 d7eZ8e9e8 G d8d9 d9eZ:e;e: G d:d; d;eZ<e=e< d<d= Z>d>d? Z?G d@dA dAeZ@eAe@ dBdC ZBdDdE ZCdFdG ZDdHdI ZEdJdK ZFG dLdM dMeZGeHeG ejIZIG dNdO dOeZJeKeJ dPdQ ZLdRdS ZMdTdU ZNdVdW ZOdXdY ZPdZd[ ZQd\d] ZRd^d_ ZSddadbZTdcdd ZUdedf ZVdgdh ZWdidj ZXdkdl ZYdmdn ZZdodp Z[dqdr Z\dsdt Z]dudv Z^dwdx Z_dydz Z`d{d| Zad}d~ Zbdd Zcdd Zddd ZedddZfdd Zgdd Zhdd Zidd Zjdd Zkdd ZldddZmdd Zndd Zodd Zpdd ZqG dd deZreser eIjtZtG dd deZueveu G dd deZwexew dd Zydd Zzdd Z{dd Z|dd Z}dd Z~dddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZddÄ Zddń ZddǄ ZddɄ Zdd˄ Zdd̈́ Zddτ Zddф Zddӄ ZddՄ Zddׄ ZdddلZdddۄZddd݄Zddd߄Zdd Zdd Zdd ZG dd deZee G dd deZee dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdddZd d Zdd Zdd Zdd ZejZejZejZejZejZejZejZejZejZejZejZdd	 ZejZejZejZejZG d
d deZee G dd deZee G dd deZee G dd deZee¡ G dd deZeeġ G dd deZeeơ G dd deƃZeeȡ G dd deȃZeeʡ G dd deȃZee̡ G dd dẽZeeΡ G dd deZeeС G d d! d!eZeeҡ G d"d# d#eЃZeeԡ G d$d% d%eԃZee֡ G d&d' d'eЃZeeء G d(d) d)eZeeڡ G d*d+ d+e؃Zeeܡ d,d- Zސdd.d/Zߐd0d1 Zd2d3 ZG d4d5 d5eZee G d6d7 d7eZee G d8d9 d9eZee G d:d; d;eZee G d<d= d=eZee G d>d? d?eZee G d@dA dAeZee G dBdC dCeZee G dDdE dEeZee dFdG ZdHdI ZG dJdK dKeZee dLdM ZG dNdO dOeZee dPdQ ZG dRdS dSeZee dTdU ZG dVdW dWeZe e dXdY ZG dZd[ d[eZee G d\d] d]eZee G d^d_ d_eZee G d`da daeZe	e G dbdc dceZ
ee
 G ddde deeZee G dfdg dgeZee G dhdi dieZee G djdk dkeZee G dldm dmeZee G dndo doeZee G dpdq dqeZee G drds dseZee G dtdu dueZee G dvdw dweZee G dxdy dyeZ e!e  G dzd{ d{eZ"e#e" G d|d} d}eZ$e%e$ G d~d deZ&e'e& G dd deZ(e)e( G dd deZ*e+e* G dd deZ,e-e, G dd deZ.e/e. G dd deZ0e1e0 G dd deZ2e3e2 G dd de2Z4e5e4 G dd deƃZ6e7e6 G dd deZ8e9e8 G dd deZ:e;e: G dd de:Z<e=e< G dd de8Z>e?e> G dd de8Z@eAe@ G dd deƃZBeCeB G dd deZDeEeD G dd deZFeGeF G dd deZHeIeH G dd deHZJeKeJ G dd deƃZLeMeL G dd deLZNeOeN G dd deLZPeQeP G dd deLZReSeR G dd deLZTeUeT G dd deZVeWeV G dd deZXeYeX G dd deVZZe[eZ G dd deVZ\e]e\ G dd deZ^e_e^ G dd deZ`eae` G dd de^Zbeceb G dd de`Zdeeed G dd de^Zfegef G dd de`Zheieh G ddÄ dÐe^Zjekej G dĐdń dŐe`Zlemel dƐdǄ ZndȐdɄ Zodʐd˄ ZpG d̐d̈́ deZqereq G dΐdτ deZsetes G dАdф dѐesZueveu G dҐdӄ dӐesZwexew G dԐdՄ dՐewZyezey G d֐dׄ dאewZ{e|e{ G dؐdل dِewZ}e~e} G dڐdۄ dېewZee G dܐd݄ dݐewZee G dސd߄ deZee G dd desZee dd Zdd Zdd ZG dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deeZee G dd deZee G dd deZee dd Zdd Zdd Zdd Zd d Zdd ZG dd deZee dd Zdd	 Zdd
dZdddZdd ZdddZG dd deZee G dd deƃZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G d d! d!eZee G d"d# d#eZee G d$d% d%eZee G d&d' d'eZee G d(d) d)eZee dd*d+Zd,d- Zd.d/ Zdd0d1ZG d2d3 d3eZeŐeġ G d4d5 d5eăZeǐeơ G d6d7 d7eăZeɐeȡ G d8d9 d9eăZeːeʡ G d:d; d;eăZe͐e̡ G d<d= d=eăZeϐeΡ d>d? ZАd@dA ZG dBdC dCeZeӐeҡ G dDdE dEeZeՐeԡ G dFdG dGeZeאe֡ G dHdI dIeZeِeء G dJdK dKe؃Zeېeڡ G dLdM dMeZeݐeܡ G dNdO dOe܃Zeߐeޡ dPdQ ZG dRdS dSeZee G dTdU dUeZee G dVdW dWeZee G dXdY dYeZee G dZd[ d[eZee G d\d] d]eZee G d^d_ d_eZee G d`da daesZee G dbdc dceZee G ddde deeZee G dfdg dgeZee G dhdi dieZee G djdk dkeeZee G dldm dmeZee G dndo doeZee G dpdq dqeƃZe e G drds dseZee G dtdu dueZee dvdw ZG dxdy dyeZee G dzd{ d{eZe	e G d|d} d}eƃZ
ee
 G d~d deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZ e!e  G dd deZ"e#e" G dd deZ$e%e$ G dd deZ&e'e& G dd deZ(e)e( G dd de&Z*e+e* G dd de*Z,e-e, G dd de*Z.e/e. G dd de*Z0e1e0 G dd de*Z2e3e2 G dd deZ4e5e4 G dd de&Z6e7e6 dddZ8dddZ9dddZ:dd Z;dd Z<G dd deZ=e>e= G dd de=Z?e@e? G dd de=ZAeBeA G dd deZCeDeC G dd de=ZEeFeE G dd de=ZGeHeG G dd de=ZIeJeI G ddÄ deZKeLeK G dĐdń dŐe=ZMeNeM G dƐdǄ deZOePeO G dȐdɄ dɐe=ZQeReQ G dʐd˄ dːeOZSeTeS G d̐d̈́ d͐eQZUeVeU G dΐdτ dϐeSeZWeXeW G dАdф dѐeOZYeZeY G dҐdӄ dӐeQZ[e\e[ G dԐdՄ deZ]e^e] G d֐dׄ deZ_e`e_ G dؐdل deZaebea G dڐdۄ dېeaZcedec G dܐd݄ dݐeaZeefee G dސd߄ deZgeheg G dd deZiejei G dd deZkelek G dd deZmenem G dd deZoepeo G dd deoZqereq G dd deZsetes G dd deZueveu G dd deƃZwexew G dd dewZyezey G dd deZ{e|e{ G dd deZ}e~e} G dd deZee G dd de}Zee G dd deZee G dd deZee G dd deZee G d d deZee G dd deZee G dd deZee G dd deZee G dd	 d	eZee G d
d deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd deZee G dd de=Zee G dd deZee G dd deZee ejZejZejZejZejZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zdd<d=Zd>d? ZG d@dA dAeZee eIjZeIjZeIjZeIjZeIjÐZeIjĐZeIjŐZŐdBdC ZƐdDdE ZG dFdG dGeZeɐeȡ G dHdI dIeȃZeːeʡ G dJdK dKeȃZe͐e̡ G dLdM dMeZeϐeΡ G dNdO dOeZeѐeС G dPdQ dQeZeӐeҡ G dRdS dSeZeՐeԡ dTdU Z֐dVdW ZG dXdY dYeZeِeء G dZd[ d[e`Zeېeڡ G d\d] d]e^Zeݐeܡ G d^d_ d_eZeߐeޡ G d`da daeZee G dbdc dceZee G ddde deeZee G dfdg dgeZee dhdi Zdjdk ZG dldm dmeZee G dndo doeZee G dpdq dqeZee drds Zdtdu Zdvdw Zdxdy ZG dzd{ d{eZee d|d} Zd~d Zdd ZG dd deZee dd Zdd Zdd Zdd Zdd Zdd Z dd Zdd ZdS (      )version_info.   )
_swigfaissNc                 C   sB   zd| j   }W n tjy*   d}Y n0 d| jj| jj|f S )Nz	proxy of  z<%s.%s; %s >)this__repr____builtin__	Exception	__class__
__module____name__)selfZstrthis r   ]/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/faiss/swigfaiss.py
_swig_repr   s
    
r   c                    s    fdd}|S )Nc                    sh   |dkr | || nN|dkr,| j | n8t| |rXttt| |trX | || ntd|  d S )Nr   thisownz(You cannot add instance attributes to %s)r   ownhasattr
isinstancegetattrtypepropertyAttributeError)r   namevaluesetr   r   set_instance_attr   s    zE_swig_setattr_nondynamic_instance_variable.<locals>.set_instance_attrr   )r   r   r   r   r   *_swig_setattr_nondynamic_instance_variable   s    	r   c                    s    fdd}|S )Nc                    s8   t | |r(tt| |ts( | || ntd|  d S )Nz%You cannot add class attributes to %s)r   r   r   r   r   )clsr   r   r   r   r   set_class_attr)   s    z?_swig_setattr_nondynamic_class_variable.<locals>.set_class_attrr   )r   r!   r   r   r   '_swig_setattr_nondynamic_class_variable(   s    r"   c                    s    fdd}|S )zlClass decorator for adding a metaclass to a SWIG wrapped class - a slimmed down version of six.add_metaclassc                    s    | j | j| j S N)r   	__bases____dict__copy)r    	metaclassr   r   wrapper3   s    z$_swig_add_metaclass.<locals>.wrapperr   )r(   r)   r   r'   r   _swig_add_metaclass1   s    r*   c                   @   s   e Zd ZdZeejZdS )_SwigNonDynamicMetazKMeta class to enforce nondynamic attributes (no new attributes) for a classN)r   r   __qualname____doc__r"   r   __setattr__r   r   r   r   r+   8   s   r+   c                   @   s   e Zd Zedd dd ddZdd ZeZej	Z
dd	 Zd,ddZd-ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+S ).SwigPyIteratorc                 C   s
   | j  S r#   r   r   xr   r   r   <lambda>>       zSwigPyIterator.<lambda>c                 C   s   | j |S r#   r0   r2   vr   r   r   r3   >   r4   The membership flagdocc                 O   s   t dd S Nz*No constructor defined - class is abstractr   r   argskwargsr   r   r   __init__@   s    zSwigPyIterator.__init__c                 C   s
   t | S r#   )r   ZSwigPyIterator_valuer   r   r   r   r   E   s    zSwigPyIterator.valuer   c                 C   s   t | |S r#   )r   ZSwigPyIterator_incrr   nr   r   r   incrH   s    zSwigPyIterator.incrc                 C   s   t | |S r#   )r   ZSwigPyIterator_decrrA   r   r   r   decrK   s    zSwigPyIterator.decrc                 C   s   t | |S r#   )r   ZSwigPyIterator_distancer   r2   r   r   r   distanceN   s    zSwigPyIterator.distancec                 C   s   t | |S r#   )r   ZSwigPyIterator_equalrE   r   r   r   equalQ   s    zSwigPyIterator.equalc                 C   s
   t | S r#   )r   ZSwigPyIterator_copyr@   r   r   r   r&   T   s    zSwigPyIterator.copyc                 C   s
   t | S r#   )r   ZSwigPyIterator_nextr@   r   r   r   nextW   s    zSwigPyIterator.nextc                 C   s
   t | S r#   )r   ZSwigPyIterator___next__r@   r   r   r   __next__Z   s    zSwigPyIterator.__next__c                 C   s
   t | S r#   )r   ZSwigPyIterator_previousr@   r   r   r   previous]   s    zSwigPyIterator.previousc                 C   s   t | |S r#   )r   ZSwigPyIterator_advancerA   r   r   r   advance`   s    zSwigPyIterator.advancec                 C   s   t | |S r#   )r   ZSwigPyIterator___eq__rE   r   r   r   __eq__c   s    zSwigPyIterator.__eq__c                 C   s   t | |S r#   )r   ZSwigPyIterator___ne__rE   r   r   r   __ne__f   s    zSwigPyIterator.__ne__c                 C   s   t | |S r#   )r   ZSwigPyIterator___iadd__rA   r   r   r   __iadd__i   s    zSwigPyIterator.__iadd__c                 C   s   t | |S r#   )r   ZSwigPyIterator___isub__rA   r   r   r   __isub__l   s    zSwigPyIterator.__isub__c                 C   s   t | |S r#   )r   ZSwigPyIterator___add__rA   r   r   r   __add__o   s    zSwigPyIterator.__add__c                 G   s   t j| g|R  S r#   )r   ZSwigPyIterator___sub__r   r=   r   r   r   __sub__r   s    zSwigPyIterator.__sub__c                 C   s   | S r#   r   r@   r   r   r   __iter__t   s    zSwigPyIterator.__iter__N)r   )r   )r   r   r,   r   r   r?   r   r   r   Zdelete_SwigPyIterator__swig_destroy__r   rC   rD   rF   rG   r&   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rR   rS   r   r   r   r   r/   =   s*   

r/   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )Float32Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   {   r4   zFloat32Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   {   r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZFloat32Vector_swiginitZnew_Float32Vectorr@   r   r   r   r?   ~   s    zFloat32Vector.__init__c                 C   s   t | |S r#   )r   ZFloat32Vector_push_backr   arg2r   r   r   	push_back   s    zFloat32Vector.push_backc                 C   s
   t | S r#   )r   ZFloat32Vector_clearr@   r   r   r   clear   s    zFloat32Vector.clearc                 C   s
   t | S r#   )r   ZFloat32Vector_datar@   r   r   r   data   s    zFloat32Vector.datac                 C   s
   t | S r#   )r   ZFloat32Vector_sizer@   r   r   r   size   s    zFloat32Vector.sizec                 C   s   t | |S r#   )r   ZFloat32Vector_atrA   r   r   r   at   s    zFloat32Vector.atc                 C   s   t | |S r#   )r   ZFloat32Vector_resizerA   r   r   r   resize   s    zFloat32Vector.resizec                 C   s   t | |S r#   )r   ZFloat32Vector_swapr   otherr   r   r   swap   s    zFloat32Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_Float32VectorrT   r   r   r   r   rU   z   s   rU   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )Float64Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zFloat64Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZFloat64Vector_swiginitZnew_Float64Vectorr@   r   r   r   r?      s    zFloat64Vector.__init__c                 C   s   t | |S r#   )r   ZFloat64Vector_push_backrV   r   r   r   rX      s    zFloat64Vector.push_backc                 C   s
   t | S r#   )r   ZFloat64Vector_clearr@   r   r   r   rY      s    zFloat64Vector.clearc                 C   s
   t | S r#   )r   ZFloat64Vector_datar@   r   r   r   rZ      s    zFloat64Vector.datac                 C   s
   t | S r#   )r   ZFloat64Vector_sizer@   r   r   r   r[      s    zFloat64Vector.sizec                 C   s   t | |S r#   )r   ZFloat64Vector_atrA   r   r   r   r\      s    zFloat64Vector.atc                 C   s   t | |S r#   )r   ZFloat64Vector_resizerA   r   r   r   r]      s    zFloat64Vector.resizec                 C   s   t | |S r#   )r   ZFloat64Vector_swapr^   r   r   r   r`      s    zFloat64Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_Float64VectorrT   r   r   r   r   ra      s   ra   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )
Int8Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zInt8Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZInt8Vector_swiginitZnew_Int8Vectorr@   r   r   r   r?      s    zInt8Vector.__init__c                 C   s   t | |S r#   )r   ZInt8Vector_push_backrV   r   r   r   rX      s    zInt8Vector.push_backc                 C   s
   t | S r#   )r   ZInt8Vector_clearr@   r   r   r   rY      s    zInt8Vector.clearc                 C   s
   t | S r#   )r   ZInt8Vector_datar@   r   r   r   rZ      s    zInt8Vector.datac                 C   s
   t | S r#   )r   ZInt8Vector_sizer@   r   r   r   r[      s    zInt8Vector.sizec                 C   s   t | |S r#   )r   ZInt8Vector_atrA   r   r   r   r\      s    zInt8Vector.atc                 C   s   t | |S r#   )r   ZInt8Vector_resizerA   r   r   r   r]      s    zInt8Vector.resizec                 C   s   t | |S r#   )r   ZInt8Vector_swapr^   r   r   r   r`      s    zInt8Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_Int8VectorrT   r   r   r   r   rb      s   rb   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )Int16Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zInt16Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZInt16Vector_swiginitZnew_Int16Vectorr@   r   r   r   r?      s    zInt16Vector.__init__c                 C   s   t | |S r#   )r   ZInt16Vector_push_backrV   r   r   r   rX      s    zInt16Vector.push_backc                 C   s
   t | S r#   )r   ZInt16Vector_clearr@   r   r   r   rY      s    zInt16Vector.clearc                 C   s
   t | S r#   )r   ZInt16Vector_datar@   r   r   r   rZ      s    zInt16Vector.datac                 C   s
   t | S r#   )r   ZInt16Vector_sizer@   r   r   r   r[      s    zInt16Vector.sizec                 C   s   t | |S r#   )r   ZInt16Vector_atrA   r   r   r   r\      s    zInt16Vector.atc                 C   s   t | |S r#   )r   ZInt16Vector_resizerA   r   r   r   r]      s    zInt16Vector.resizec                 C   s   t | |S r#   )r   ZInt16Vector_swapr^   r   r   r   r`      s    zInt16Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_Int16VectorrT   r   r   r   r   rc      s   rc   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )Int32Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zInt32Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZInt32Vector_swiginitZnew_Int32Vectorr@   r   r   r   r?      s    zInt32Vector.__init__c                 C   s   t | |S r#   )r   ZInt32Vector_push_backrV   r   r   r   rX      s    zInt32Vector.push_backc                 C   s
   t | S r#   )r   ZInt32Vector_clearr@   r   r   r   rY      s    zInt32Vector.clearc                 C   s
   t | S r#   )r   ZInt32Vector_datar@   r   r   r   rZ     s    zInt32Vector.datac                 C   s
   t | S r#   )r   ZInt32Vector_sizer@   r   r   r   r[     s    zInt32Vector.sizec                 C   s   t | |S r#   )r   ZInt32Vector_atrA   r   r   r   r\   	  s    zInt32Vector.atc                 C   s   t | |S r#   )r   ZInt32Vector_resizerA   r   r   r   r]     s    zInt32Vector.resizec                 C   s   t | |S r#   )r   ZInt32Vector_swapr^   r   r   r   r`     s    zInt32Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_Int32VectorrT   r   r   r   r   rd      s   rd   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )Int64Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zInt64Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZInt64Vector_swiginitZnew_Int64Vectorr@   r   r   r   r?     s    zInt64Vector.__init__c                 C   s   t | |S r#   )r   ZInt64Vector_push_backrV   r   r   r   rX     s    zInt64Vector.push_backc                 C   s
   t | S r#   )r   ZInt64Vector_clearr@   r   r   r   rY     s    zInt64Vector.clearc                 C   s
   t | S r#   )r   ZInt64Vector_datar@   r   r   r   rZ   "  s    zInt64Vector.datac                 C   s
   t | S r#   )r   ZInt64Vector_sizer@   r   r   r   r[   %  s    zInt64Vector.sizec                 C   s   t | |S r#   )r   ZInt64Vector_atrA   r   r   r   r\   (  s    zInt64Vector.atc                 C   s   t | |S r#   )r   ZInt64Vector_resizerA   r   r   r   r]   +  s    zInt64Vector.resizec                 C   s   t | |S r#   )r   ZInt64Vector_swapr^   r   r   r   r`   .  s    zInt64Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_Int64VectorrT   r   r   r   r   re     s   re   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )UInt8Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   5  r4   zUInt8Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   5  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZUInt8Vector_swiginitZnew_UInt8Vectorr@   r   r   r   r?   8  s    zUInt8Vector.__init__c                 C   s   t | |S r#   )r   ZUInt8Vector_push_backrV   r   r   r   rX   ;  s    zUInt8Vector.push_backc                 C   s
   t | S r#   )r   ZUInt8Vector_clearr@   r   r   r   rY   >  s    zUInt8Vector.clearc                 C   s
   t | S r#   )r   ZUInt8Vector_datar@   r   r   r   rZ   A  s    zUInt8Vector.datac                 C   s
   t | S r#   )r   ZUInt8Vector_sizer@   r   r   r   r[   D  s    zUInt8Vector.sizec                 C   s   t | |S r#   )r   ZUInt8Vector_atrA   r   r   r   r\   G  s    zUInt8Vector.atc                 C   s   t | |S r#   )r   ZUInt8Vector_resizerA   r   r   r   r]   J  s    zUInt8Vector.resizec                 C   s   t | |S r#   )r   ZUInt8Vector_swapr^   r   r   r   r`   M  s    zUInt8Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_UInt8VectorrT   r   r   r   r   rf   4  s   rf   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )UInt16Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   T  r4   zUInt16Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   T  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZUInt16Vector_swiginitZnew_UInt16Vectorr@   r   r   r   r?   W  s    zUInt16Vector.__init__c                 C   s   t | |S r#   )r   ZUInt16Vector_push_backrV   r   r   r   rX   Z  s    zUInt16Vector.push_backc                 C   s
   t | S r#   )r   ZUInt16Vector_clearr@   r   r   r   rY   ]  s    zUInt16Vector.clearc                 C   s
   t | S r#   )r   ZUInt16Vector_datar@   r   r   r   rZ   `  s    zUInt16Vector.datac                 C   s
   t | S r#   )r   ZUInt16Vector_sizer@   r   r   r   r[   c  s    zUInt16Vector.sizec                 C   s   t | |S r#   )r   ZUInt16Vector_atrA   r   r   r   r\   f  s    zUInt16Vector.atc                 C   s   t | |S r#   )r   ZUInt16Vector_resizerA   r   r   r   r]   i  s    zUInt16Vector.resizec                 C   s   t | |S r#   )r   ZUInt16Vector_swapr^   r   r   r   r`   l  s    zUInt16Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_UInt16VectorrT   r   r   r   r   rg   S  s   rg   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )UInt32Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   s  r4   zUInt32Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   s  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZUInt32Vector_swiginitZnew_UInt32Vectorr@   r   r   r   r?   v  s    zUInt32Vector.__init__c                 C   s   t | |S r#   )r   ZUInt32Vector_push_backrV   r   r   r   rX   y  s    zUInt32Vector.push_backc                 C   s
   t | S r#   )r   ZUInt32Vector_clearr@   r   r   r   rY   |  s    zUInt32Vector.clearc                 C   s
   t | S r#   )r   ZUInt32Vector_datar@   r   r   r   rZ     s    zUInt32Vector.datac                 C   s
   t | S r#   )r   ZUInt32Vector_sizer@   r   r   r   r[     s    zUInt32Vector.sizec                 C   s   t | |S r#   )r   ZUInt32Vector_atrA   r   r   r   r\     s    zUInt32Vector.atc                 C   s   t | |S r#   )r   ZUInt32Vector_resizerA   r   r   r   r]     s    zUInt32Vector.resizec                 C   s   t | |S r#   )r   ZUInt32Vector_swapr^   r   r   r   r`     s    zUInt32Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_UInt32VectorrT   r   r   r   r   rh   r  s   rh   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )UInt64Vectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zUInt64Vector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZUInt64Vector_swiginitZnew_UInt64Vectorr@   r   r   r   r?     s    zUInt64Vector.__init__c                 C   s   t | |S r#   )r   ZUInt64Vector_push_backrV   r   r   r   rX     s    zUInt64Vector.push_backc                 C   s
   t | S r#   )r   ZUInt64Vector_clearr@   r   r   r   rY     s    zUInt64Vector.clearc                 C   s
   t | S r#   )r   ZUInt64Vector_datar@   r   r   r   rZ     s    zUInt64Vector.datac                 C   s
   t | S r#   )r   ZUInt64Vector_sizer@   r   r   r   r[     s    zUInt64Vector.sizec                 C   s   t | |S r#   )r   ZUInt64Vector_atrA   r   r   r   r\     s    zUInt64Vector.atc                 C   s   t | |S r#   )r   ZUInt64Vector_resizerA   r   r   r   r]     s    zUInt64Vector.resizec                 C   s   t | |S r#   )r   ZUInt64Vector_swapr^   r   r   r   r`     s    zUInt64Vector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_UInt64VectorrT   r   r   r   r   ri     s   ri   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )Float32VectorVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zFloat32VectorVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZFloat32VectorVector_swiginitZnew_Float32VectorVectorr@   r   r   r   r?     s    zFloat32VectorVector.__init__c                 C   s   t | |S r#   )r   ZFloat32VectorVector_push_backrV   r   r   r   rX     s    zFloat32VectorVector.push_backc                 C   s
   t | S r#   )r   ZFloat32VectorVector_clearr@   r   r   r   rY     s    zFloat32VectorVector.clearc                 C   s
   t | S r#   )r   ZFloat32VectorVector_datar@   r   r   r   rZ     s    zFloat32VectorVector.datac                 C   s
   t | S r#   )r   ZFloat32VectorVector_sizer@   r   r   r   r[     s    zFloat32VectorVector.sizec                 C   s   t | |S r#   )r   ZFloat32VectorVector_atrA   r   r   r   r\     s    zFloat32VectorVector.atc                 C   s   t | |S r#   )r   ZFloat32VectorVector_resizerA   r   r   r   r]     s    zFloat32VectorVector.resizec                 C   s   t | |S r#   )r   ZFloat32VectorVector_swapr^   r   r   r   r`     s    zFloat32VectorVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_Float32VectorVectorrT   r   r   r   r   rj     s   rj   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )UInt8VectorVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zUInt8VectorVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZUInt8VectorVector_swiginitZnew_UInt8VectorVectorr@   r   r   r   r?     s    zUInt8VectorVector.__init__c                 C   s   t | |S r#   )r   ZUInt8VectorVector_push_backrV   r   r   r   rX     s    zUInt8VectorVector.push_backc                 C   s
   t | S r#   )r   ZUInt8VectorVector_clearr@   r   r   r   rY     s    zUInt8VectorVector.clearc                 C   s
   t | S r#   )r   ZUInt8VectorVector_datar@   r   r   r   rZ     s    zUInt8VectorVector.datac                 C   s
   t | S r#   )r   ZUInt8VectorVector_sizer@   r   r   r   r[     s    zUInt8VectorVector.sizec                 C   s   t | |S r#   )r   ZUInt8VectorVector_atrA   r   r   r   r\     s    zUInt8VectorVector.atc                 C   s   t | |S r#   )r   ZUInt8VectorVector_resizerA   r   r   r   r]     s    zUInt8VectorVector.resizec                 C   s   t | |S r#   )r   ZUInt8VectorVector_swapr^   r   r   r   r`     s    zUInt8VectorVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_UInt8VectorVectorrT   r   r   r   r   rk     s   rk   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )Int32VectorVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zInt32VectorVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZInt32VectorVector_swiginitZnew_Int32VectorVectorr@   r   r   r   r?     s    zInt32VectorVector.__init__c                 C   s   t | |S r#   )r   ZInt32VectorVector_push_backrV   r   r   r   rX     s    zInt32VectorVector.push_backc                 C   s
   t | S r#   )r   ZInt32VectorVector_clearr@   r   r   r   rY     s    zInt32VectorVector.clearc                 C   s
   t | S r#   )r   ZInt32VectorVector_datar@   r   r   r   rZ     s    zInt32VectorVector.datac                 C   s
   t | S r#   )r   ZInt32VectorVector_sizer@   r   r   r   r[     s    zInt32VectorVector.sizec                 C   s   t | |S r#   )r   ZInt32VectorVector_atrA   r   r   r   r\     s    zInt32VectorVector.atc                 C   s   t | |S r#   )r   ZInt32VectorVector_resizerA   r   r   r   r]     s    zInt32VectorVector.resizec                 C   s   t | |S r#   )r   ZInt32VectorVector_swapr^   r   r   r   r`     s    zInt32VectorVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_Int32VectorVectorrT   r   r   r   r   rl     s   rl   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )Int64VectorVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zInt64VectorVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZInt64VectorVector_swiginitZnew_Int64VectorVectorr@   r   r   r   r?     s    zInt64VectorVector.__init__c                 C   s   t | |S r#   )r   ZInt64VectorVector_push_backrV   r   r   r   rX     s    zInt64VectorVector.push_backc                 C   s
   t | S r#   )r   ZInt64VectorVector_clearr@   r   r   r   rY     s    zInt64VectorVector.clearc                 C   s
   t | S r#   )r   ZInt64VectorVector_datar@   r   r   r   rZ     s    zInt64VectorVector.datac                 C   s
   t | S r#   )r   ZInt64VectorVector_sizer@   r   r   r   r[     s    zInt64VectorVector.sizec                 C   s   t | |S r#   )r   ZInt64VectorVector_atrA   r   r   r   r\      s    zInt64VectorVector.atc                 C   s   t | |S r#   )r   ZInt64VectorVector_resizerA   r   r   r   r]   #  s    zInt64VectorVector.resizec                 C   s   t | |S r#   )r   ZInt64VectorVector_swapr^   r   r   r   r`   &  s    zInt64VectorVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_Int64VectorVectorrT   r   r   r   r   rm     s   rm   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )VectorTransformVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   -  r4   zVectorTransformVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   -  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZVectorTransformVector_swiginitZnew_VectorTransformVectorr@   r   r   r   r?   0  s    zVectorTransformVector.__init__c                 C   s   t | |S r#   )r   ZVectorTransformVector_push_backrV   r   r   r   rX   3  s    zVectorTransformVector.push_backc                 C   s
   t | S r#   )r   ZVectorTransformVector_clearr@   r   r   r   rY   6  s    zVectorTransformVector.clearc                 C   s
   t | S r#   )r   ZVectorTransformVector_datar@   r   r   r   rZ   9  s    zVectorTransformVector.datac                 C   s
   t | S r#   )r   ZVectorTransformVector_sizer@   r   r   r   r[   <  s    zVectorTransformVector.sizec                 C   s   t | |S r#   )r   ZVectorTransformVector_atrA   r   r   r   r\   ?  s    zVectorTransformVector.atc                 C   s   t | |S r#   )r   ZVectorTransformVector_resizerA   r   r   r   r]   B  s    zVectorTransformVector.resizec                 C   s   t | |S r#   )r   ZVectorTransformVector_swapr^   r   r   r   r`   E  s    zVectorTransformVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_VectorTransformVectorrT   r   r   r   r   rn   ,  s   rn   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )OperatingPointVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   L  r4   zOperatingPointVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   L  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZOperatingPointVector_swiginitZnew_OperatingPointVectorr@   r   r   r   r?   O  s    zOperatingPointVector.__init__c                 C   s   t | |S r#   )r   ZOperatingPointVector_push_backrV   r   r   r   rX   R  s    zOperatingPointVector.push_backc                 C   s
   t | S r#   )r   ZOperatingPointVector_clearr@   r   r   r   rY   U  s    zOperatingPointVector.clearc                 C   s
   t | S r#   )r   ZOperatingPointVector_datar@   r   r   r   rZ   X  s    zOperatingPointVector.datac                 C   s
   t | S r#   )r   ZOperatingPointVector_sizer@   r   r   r   r[   [  s    zOperatingPointVector.sizec                 C   s   t | |S r#   )r   ZOperatingPointVector_atrA   r   r   r   r\   ^  s    zOperatingPointVector.atc                 C   s   t | |S r#   )r   ZOperatingPointVector_resizerA   r   r   r   r]   a  s    zOperatingPointVector.resizec                 C   s   t | |S r#   )r   ZOperatingPointVector_swapr^   r   r   r   r`   d  s    zOperatingPointVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_OperatingPointVectorrT   r   r   r   r   ro   K  s   ro   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )InvertedListsPtrVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   k  r4   zInvertedListsPtrVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   k  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZInvertedListsPtrVector_swiginitZnew_InvertedListsPtrVectorr@   r   r   r   r?   n  s    zInvertedListsPtrVector.__init__c                 C   s   t | |S r#   )r   Z InvertedListsPtrVector_push_backrV   r   r   r   rX   q  s    z InvertedListsPtrVector.push_backc                 C   s
   t | S r#   )r   ZInvertedListsPtrVector_clearr@   r   r   r   rY   t  s    zInvertedListsPtrVector.clearc                 C   s
   t | S r#   )r   ZInvertedListsPtrVector_datar@   r   r   r   rZ   w  s    zInvertedListsPtrVector.datac                 C   s
   t | S r#   )r   ZInvertedListsPtrVector_sizer@   r   r   r   r[   z  s    zInvertedListsPtrVector.sizec                 C   s   t | |S r#   )r   ZInvertedListsPtrVector_atrA   r   r   r   r\   }  s    zInvertedListsPtrVector.atc                 C   s   t | |S r#   )r   ZInvertedListsPtrVector_resizerA   r   r   r   r]     s    zInvertedListsPtrVector.resizec                 C   s   t | |S r#   )r   ZInvertedListsPtrVector_swapr^   r   r   r   r`     s    zInvertedListsPtrVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_InvertedListsPtrVectorrT   r   r   r   r   rp   j  s   rp   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )RepeatVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zRepeatVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZRepeatVector_swiginitZnew_RepeatVectorr@   r   r   r   r?     s    zRepeatVector.__init__c                 C   s   t | |S r#   )r   ZRepeatVector_push_backrV   r   r   r   rX     s    zRepeatVector.push_backc                 C   s
   t | S r#   )r   ZRepeatVector_clearr@   r   r   r   rY     s    zRepeatVector.clearc                 C   s
   t | S r#   )r   ZRepeatVector_datar@   r   r   r   rZ     s    zRepeatVector.datac                 C   s
   t | S r#   )r   ZRepeatVector_sizer@   r   r   r   r[     s    zRepeatVector.sizec                 C   s   t | |S r#   )r   ZRepeatVector_atrA   r   r   r   r\     s    zRepeatVector.atc                 C   s   t | |S r#   )r   ZRepeatVector_resizerA   r   r   r   r]     s    zRepeatVector.resizec                 C   s   t | |S r#   )r   ZRepeatVector_swapr^   r   r   r   r`     s    zRepeatVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_RepeatVectorrT   r   r   r   r   rq     s   rq   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )ClusteringIterationStatsVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z'ClusteringIterationStatsVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   Z'ClusteringIterationStatsVector_swiginitZ"new_ClusteringIterationStatsVectorr@   r   r   r   r?     s    z'ClusteringIterationStatsVector.__init__c                 C   s   t | |S r#   )r   Z(ClusteringIterationStatsVector_push_backrV   r   r   r   rX     s    z(ClusteringIterationStatsVector.push_backc                 C   s
   t | S r#   )r   Z$ClusteringIterationStatsVector_clearr@   r   r   r   rY     s    z$ClusteringIterationStatsVector.clearc                 C   s
   t | S r#   )r   Z#ClusteringIterationStatsVector_datar@   r   r   r   rZ     s    z#ClusteringIterationStatsVector.datac                 C   s
   t | S r#   )r   Z#ClusteringIterationStatsVector_sizer@   r   r   r   r[     s    z#ClusteringIterationStatsVector.sizec                 C   s   t | |S r#   )r   Z!ClusteringIterationStatsVector_atrA   r   r   r   r\     s    z!ClusteringIterationStatsVector.atc                 C   s   t | |S r#   )r   Z%ClusteringIterationStatsVector_resizerA   r   r   r   r]     s    z%ClusteringIterationStatsVector.resizec                 C   s   t | |S r#   )r   Z#ClusteringIterationStatsVector_swapr^   r   r   r   r`     s    z#ClusteringIterationStatsVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Z%delete_ClusteringIterationStatsVectorrT   r   r   r   r   rr     s   rr   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )ParameterRangeVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zParameterRangeVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZParameterRangeVector_swiginitZnew_ParameterRangeVectorr@   r   r   r   r?     s    zParameterRangeVector.__init__c                 C   s   t | |S r#   )r   ZParameterRangeVector_push_backrV   r   r   r   rX     s    zParameterRangeVector.push_backc                 C   s
   t | S r#   )r   ZParameterRangeVector_clearr@   r   r   r   rY     s    zParameterRangeVector.clearc                 C   s
   t | S r#   )r   ZParameterRangeVector_datar@   r   r   r   rZ     s    zParameterRangeVector.datac                 C   s
   t | S r#   )r   ZParameterRangeVector_sizer@   r   r   r   r[     s    zParameterRangeVector.sizec                 C   s   t | |S r#   )r   ZParameterRangeVector_atrA   r   r   r   r\     s    zParameterRangeVector.atc                 C   s   t | |S r#   )r   ZParameterRangeVector_resizerA   r   r   r   r]     s    zParameterRangeVector.resizec                 C   s   t | |S r#   )r   ZParameterRangeVector_swapr^   r   r   r   r`     s    zParameterRangeVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_ParameterRangeVectorrT   r   r   r   r   rs     s   rs   c                   @   sl   e Zd Zedd dd ddZeZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZejZdS )OnDiskOneListVectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zOnDiskOneListVector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZOnDiskOneListVector_swiginitZnew_OnDiskOneListVectorr@   r   r   r   r?     s    zOnDiskOneListVector.__init__c                 C   s   t | |S r#   )r   ZOnDiskOneListVector_push_backrV   r   r   r   rX     s    zOnDiskOneListVector.push_backc                 C   s
   t | S r#   )r   ZOnDiskOneListVector_clearr@   r   r   r   rY     s    zOnDiskOneListVector.clearc                 C   s
   t | S r#   )r   ZOnDiskOneListVector_datar@   r   r   r   rZ     s    zOnDiskOneListVector.datac                 C   s
   t | S r#   )r   ZOnDiskOneListVector_sizer@   r   r   r   r[     s    zOnDiskOneListVector.sizec                 C   s   t | |S r#   )r   ZOnDiskOneListVector_atrA   r   r   r   r\     s    zOnDiskOneListVector.atc                 C   s   t | |S r#   )r   ZOnDiskOneListVector_resizerA   r   r   r   r]     s    zOnDiskOneListVector.resizec                 C   s   t | |S r#   )r   ZOnDiskOneListVector_swapr^   r   r   r   r`     s    zOnDiskOneListVector.swapN)r   r   r,   r   r   r   r   r?   rX   rY   rZ   r[   r\   r]   r`   r   Zdelete_OnDiskOneListVectorrT   r   r   r   r   rt     s   rt   c                 C   s   t | ||||S )z
     low level SIMD histogramming functions  8-bin histogram of (x - min) >> shift
    values outside the range are ignored.
    the data table should be aligned on 32 bytes
    )r   simd_histogram_8rZ   rB   minshifthistr   r   r   ru     s    ru   c                 C   s   t | ||||S )z same for 16-bin histogram)r   simd_histogram_16rv   r   r   r   rz     s    rz   c                   @   sX   e Zd Zedd dd ddZeZeejej	Z
eejejZdd Zdd	 ZejZd
S )PartitionStatsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zPartitionStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZPartitionStats_swiginitZnew_PartitionStatsr@   r   r   r   r?     s    zPartitionStats.__init__c                 C   s
   t | S r#   )r   ZPartitionStats_resetr@   r   r   r   reset  s    zPartitionStats.resetN)r   r   r,   r   r   r   r   r   Z!PartitionStats_bissect_cycles_getZ!PartitionStats_bissect_cycles_setZbissect_cyclesZ"PartitionStats_compress_cycles_getZ"PartitionStats_compress_cycles_setZcompress_cyclesr?   r|   Zdelete_PartitionStatsrT   r   r   r   r   r{     s   r{   c                 C   s   t | |S r#   )r   bitvec_print)bdr   r   r   r}   !  s    r}   c                 C   s   t | |||S r#   )r   fvecs2bitvecs)r2   r~   r   rB   r   r   r   r   $  s    r   c                 C   s   t | |||S r#   )r   bitvecs2fvecs)r~   r2   r   rB   r   r   r   r   '  s    r   c                 C   s   t | ||S r#   )r   fvec2bitvec)r2   r~   r   r   r   r   r   *  s    r   c                 C   s   t | |||||S )z0 Shuffle the bits from b(i, j) := a(i, order[j]))r   bitvec_shuffle)rB   dadborderar~   r   r   r   r   -  s    r   c                   @   sf   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZdd Zdd	 ZejZd
S )BitstringWriterc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   1  r4   zBitstringWriter.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   1  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZBitstringWriter_swiginitZnew_BitstringWriterr   code	code_sizer   r   r   r?   7  s    zBitstringWriter.__init__c                 C   s   t | ||S r#   )r   ZBitstringWriter_write)r   r2   nbitr   r   r   write:  s    zBitstringWriter.writeN)r   r   r,   r   r   r   r   r   ZBitstringWriter_code_getZBitstringWriter_code_setr   ZBitstringWriter_code_size_getZBitstringWriter_code_size_setr   ZBitstringWriter_i_getZBitstringWriter_i_setir?   r   Zdelete_BitstringWriterrT   r   r   r   r   r   0  s   r   c                   @   sf   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZdd Zdd	 ZejZd
S )BitstringReaderc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   C  r4   zBitstringReader.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   C  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZBitstringReader_swiginitZnew_BitstringReaderr   r   r   r   r?   I  s    zBitstringReader.__init__c                 C   s   t | |S r#   )r   ZBitstringReader_read)r   r   r   r   r   readL  s    zBitstringReader.readN)r   r   r,   r   r   r   r   r   ZBitstringReader_code_getZBitstringReader_code_setr   ZBitstringReader_code_size_getZBitstringReader_code_size_setr   ZBitstringReader_i_getZBitstringReader_i_setr   r?   r   Zdelete_BitstringReaderrT   r   r   r   r   r   B  s   r   c                 C   s   t | |||||S )ax  
     Compute a set of Hamming distances between na and nb binary vectors

    :type a: uint8_t
    :param a:             size na * nbytespercode
    :type b: uint8_t
    :param b:             size nb * nbytespercode
    :type nbytespercode: int
    :param nbytespercode: should be multiple of 8
    :type dis: int
    :param dis:           output distances, size na * nb
    )r   hammings)r   r~   nanbZnbytespercodedisr   r   r   r   S  s    r   c                  G   s
   t j|  S )a  
     Return the k smallest Hamming distances for a set of binary query vectors,
    using a max heap.
    :type a: uint8_t
    :param a:       queries, size ha->nh * ncodes
    :type b: uint8_t
    :param b:       database, size nb * ncodes
    :type nb: int
    :param nb:      number of database vectors
    :type ncodes: int
    :param ncodes:  size of the binary codes (bytes)
    :type ordered: int
    :param ordered: if != 0: order the results by decreasing distance
                       (may be bottleneck for k/n > 0.01)
    :type approx_topk_mode: int, optional
    :param approx_topk_mode: allows to use approximate top-k facilities
                                to speedup heap
    )r   hammings_knn_hcr=   r   r   r   r   b  s    r   c                 C   s   t | |||||S r#   )r   hammings_knn)har   r~   r   ncodesorderedr   r   r   r   w  s    r   c              
   C   s   t | |||||||S )a  
     Return the k smallest Hamming distances for a set of binary query vectors,
    using counting max.
    :type a: uint8_t
    :param a:       queries, size na * ncodes
    :type b: uint8_t
    :param b:       database, size nb * ncodes
    :type na: int
    :param na:      number of query vectors
    :type nb: int
    :param nb:      number of database vectors
    :type k: int
    :param k:       number of vectors/distances to return
    :type ncodes: int
    :param ncodes:  size of the binary codes (bytes)
    :type distances: int
    :param distances: output distances from each query vector to its k nearest
                       neighbors
    :type labels: int
    :param labels:  output ids of the k nearest neighbors to each query vector
    )r   hammings_knn_mc)r   r~   r   r   kr   	distanceslabelsr   r   r   r   z  s    r   c              	   C   s   t | ||||||S )zD same as hammings_knn except we are doing a range search with radius)r   hamming_range_search)r   r~   r   r   radiusr   resultr   r   r   r     s    r   c              	   C   s   t | ||||||S r#   )r   hamming_count_thres)bs1bs2n1n2htr   nptrr   r   r   r     s    r   c              
   C   s   t | |||||||S r#   )r   match_hamming_thres)r   r   r   r   r   r   idxr   r   r   r   r     s    r   c                 C   s   t | ||||S r#   )r   crosshamming_count_thres)ZdbsrB   r   r   r   r   r   r   r     s    r   Tc                 C   s   t | |||||S )z_
    generalized Hamming distances (= count number of code bytes that
       are the same)
    )r   generalized_hammings_knn_hc)r   r   r~   r   r   r   r   r   r   r     s    r   c                  G   s
   t j|  S )a  
    *Overload 1:*
     Pack a set of n codes of size M * nbit

    :type n: int
    :param n:           number of codes to pack
    :type M: int
    :param M:           number of elementary codes per code
    :type nbit: int
    :param nbit:        number of bits per elementary code
    :type unpacked: int
    :param unpacked:    input unpacked codes, size (n, M)
    :type packed: uint8_t
    :param packed:      output packed codes, size (n, code_size)
    :type code_size: int
    :param code_size:   should be >= ceil(M * nbit / 8)

    |

    *Overload 2:*
     Pack a set of n codes of variable sizes

    :param nbit:       number of bits per entry (size M)
    )r   pack_bitstringsr   r   r   r   r     s    r   c                  G   s
   t j|  S )a  
    *Overload 1:*
     Unpack a set of n codes of size M * nbit

    :type n: int
    :param n:           number of codes to pack
    :type M: int
    :param M:           number of elementary codes per code
    :type nbit: int
    :param nbit:        number of bits per elementary code
    :type unpacked: int
    :param unpacked:    input unpacked codes, size (n, M)
    :type packed: uint8_t
    :param packed:      output packed codes, size (n, code_size)
    :type code_size: int
    :param code_size:   should be >= ceil(M * nbit / 8)

    |

    *Overload 2:*
     Unpack a set of n codes of variable sizes

    :param nbit:       number of bits per entry (size M)
    )r   unpack_bitstringsr   r   r   r   r     s    r   c                 C   s
   t | S r#   )r   
popcount32r1   r   r   r   r     s    r   c                 C   s
   t | S r#   )r   
popcount64r1   r   r   r   r     s    r   c                   C   s   t  S r#   )r   get_num_gpusr   r   r   r   r     s    r   c                   C   s   t  S r#   )r   gpu_profiler_startr   r   r   r   r     s    r   c                   C   s   t  S r#   )r   gpu_profiler_stopr   r   r   r   r     s    r   c                   C   s   t  S r#   )r   gpu_sync_all_devicesr   r   r   r   r     s    r   c                   C   s   t  S )z get compile options)r   get_compile_optionsr   r   r   r   r     s    r   c                   C   s   t  S r#   )r   get_versionr   r   r   r   r     s    r   c                   C   s   t  S )z& ms elapsed since some arbitrary epoch)r   getmillisecsr   r   r   r   r     s    r   c                   C   s   t  S )z get current RSS usage in kB)r   get_mem_usage_kbr   r   r   r   r     s    r   c                   C   s   t  S r#   )r   
get_cyclesr   r   r   r   r     s    r   c                 C   s   t | ||||S r#   )r   
reflection)ur2   rB   r   nur   r   r   r      s    r   c                 C   s   t | ||S )z
     compute the Q of the QR decomposition for m > n
    :type a: float
    :param a:   size n * m: input matrix and output Q
    )r   	matrix_qr)mrB   r   r   r   r   r     s    r   c                 C   s   t | ||S )zF distances are supposed to be sorted. Sorts indices with same distance)r   ranklist_handle_ties)r   r   r   r   r   r   r     s    r   c                 C   s   t | |||S )z
     count the number of common elements between v1 and v2
    algorithm = sorting + bissection to avoid double-counting duplicates
    )r   ranklist_intersection_size)Zk1Zv1Zk2Zv2r   r   r   r     s    r   c              
   C   s   t | |||||||S )a  
     merge a result table into another one

    :type I0: int
    :param I0:, D0       first result table, size (n, k)
    :type I1: int
    :param I1:, D1       second result table, size (n, k)
    :type keep_min: boolean, optional
    :param keep_min:     if true, keep min values, otherwise keep max
    :type translation: int, optional
    :param translation:  add this value to all I1's indexes
    :rtype: int
    :return: nb of values that were taken from the second table
    )r   merge_result_table_with)rB   r   ZI0ZD0ZI1ZD1Zkeep_mintranslationr   r   r   r     s    r   c                  G   s
   t j|  S )z
    *Overload 1:*
    a balanced assignment has a IF of 1, a completely unbalanced assignment has
    an IF = k.

    |

    *Overload 2:*
    same, takes a histogram as input
    )r   imbalance_factorr   r   r   r   r   '  s    r   c                 C   s   t | |||S )z compute histogram on v)r   	ivec_hist)rB   r6   Zvmaxry   r   r   r   r   4  s    r   c                 C   s   t | |||S )z
     Compute histogram of bits on a code array

    :type codes: uint8_t
    :param codes:   size(n, nbits / 8)
    :type hist: int
    :param hist:    size(nbits): nb of 1s in the array of codes
    )r   bincode_hist)rB   nbitscodesry   r   r   r   r   8  s    	r   c                 C   s   t | |S z compute a checksum on a table.)r   ivec_checksumrB   r   r   r   r   r   C  s    r   c                 C   s   t | |S r   )r   bvec_checksumr   r   r   r   r   G  s    r   c                 C   s   t | |||S )a  
     compute checksums for the rows of a matrix

    :type n: int
    :param n:   number of rows
    :type d: int
    :param d:   size per row
    :type a: uint8_t
    :param a:   matrix to handle, size n * d
    :type cs: int
    :param cs:  output checksums, size n
    )r   bvecs_checksum)rB   r   r   csr   r   r   r   K  s    r   F  c                 C   s   t | |||||S )a  
     random subsamples a set of vectors if there are too many of them

    :type d: int
    :param d:      dimension of the vectors
    :type n: int
    :param n:      on input: nb of input vectors, output: nb of output vectors
    :type nmax: int
    :param nmax:   max nb of vectors to keep
    :type x: float
    :param x:      input array, size *n-by-d
    :type seed: int, optional
    :param seed:   random seed to use for sampling
    :rtype: float
    :return: x or an array allocated with new [] with *n vectors
    )r   fvecs_maybe_subsample)r   rB   Znmaxr2   verboseseedr   r   r   r   Z  s    r   c                 C   s   t | ||S )a>  
     Convert binary vector to +1/-1 valued float vector.

    :type d: int
    :param d:      dimension of the vector (multiple of 8)
    :type x_in: uint8_t
    :param x_in:   input binary vector (uint8_t table of size d / 8)
    :type x_out: float
    :param x_out:  output float vector (float table of size d)
    )r   binary_to_realr   x_inZx_outr   r   r   r   m  s    r   c                 C   s   t | ||S )ae  
     Convert float vector to binary vector. Components > 0 are converted to 1,
    others to 0.

    :type d: int
    :param d:      dimension of the vector (multiple of 8)
    :type x_in: float
    :param x_in:   input float vector (float table of size d)
    :type x_out: uint8_t
    :param x_out:  output binary vector (uint8_t table of size d / 8)
    )r   real_to_binaryr   r   r   r   r   z  s    r   c                 C   s   t | |S )z A reasonable hashing function)r   
hash_bytes)bytesrB   r   r   r   r     s    r   c                   C   s   t  S )z+ Whether OpenMP annotations were respected.)r   check_openmpr   r   r   r   r     s    r   c                   @   sX   e Zd Zedd dd ddZeZeejej	Z
eejejZdd Zdd	 ZejZd
S )CodeSetc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zCodeSet.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t | d S r#   )r   ZCodeSet_swiginitZnew_CodeSetr   r   r   r   r   r?     s    zCodeSet.__init__c                 C   s   t | |||S r#   )r   ZCodeSet_insert)r   rB   r   Zinsertedr   r   r   insert  s    zCodeSet.insertN)r   r   r,   r   r   r   r   r   ZCodeSet_d_getZCodeSet_d_setr   ZCodeSet_s_getZCodeSet_s_setsr?   r   Zdelete_CodeSetrT   r   r   r   r   r     s   r   c                   @   s
  e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZeejejddZeejejd	dZd
d ZeejejddZeejejddZeejejddZeejej ddZ!eej"ej#ddZ$eej%ej&ddZ'eej(ej)ddZ*dd Z+dd Z,ej-Z.dS )CombinerRangeKNNfloatw
     This class is used to combine range and knn search results
    in contrib.exhaustive_search.range_search_gpu
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zCombinerRangeKNNfloat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8    nb of queries, number of neighbors for the knn search part range search radiusc              	   C   s   t | t |||| dS z+ whether to keep max values instead of min.N)r   ZCombinerRangeKNNfloat_swiginitZnew_CombinerRangeKNNfloatr   nqr   r2keep_maxr   r   r   r?     s    zCombinerRangeKNNfloat.__init__ Knn search results size nq * kR
    size nq * k
    optional: range search results (ignored if mask is NULL)
    . mask for where knn results are valid, size nq size nrange + 1 size lim_remain[nrange]c                 C   s   t | |S z size nq + 1)r   Z#CombinerRangeKNNfloat_compute_sizesr   L_resr   r   r   compute_sizes  s    z#CombinerRangeKNNfloat.compute_sizesc                 C   s   t | ||S zu
        Phase 2: caller allocates D_res and I_res (size L_res[nq])
        Phase 3: fill in D_res and I_res
        )r   Z"CombinerRangeKNNfloat_write_resultr   ZD_resZI_resr   r   r   write_result  s    z"CombinerRangeKNNfloat.write_resultN)/r   r   r,   r-   r   r   r   r   r   ZCombinerRangeKNNfloat_nq_getZCombinerRangeKNNfloat_nq_setr   ZCombinerRangeKNNfloat_k_getZCombinerRangeKNNfloat_k_setr   ZCombinerRangeKNNfloat_r2_getZCombinerRangeKNNfloat_r2_setr   Z"CombinerRangeKNNfloat_keep_max_getZ"CombinerRangeKNNfloat_keep_max_setr   r?   ZCombinerRangeKNNfloat_I_getZCombinerRangeKNNfloat_I_setIZCombinerRangeKNNfloat_D_getZCombinerRangeKNNfloat_D_setDZCombinerRangeKNNfloat_mask_getZCombinerRangeKNNfloat_mask_setmaskZ$CombinerRangeKNNfloat_lim_remain_getZ$CombinerRangeKNNfloat_lim_remain_set
lim_remainZ"CombinerRangeKNNfloat_D_remain_getZ"CombinerRangeKNNfloat_D_remain_setD_remainZ"CombinerRangeKNNfloat_I_remain_getZ"CombinerRangeKNNfloat_I_remain_setI_remainZCombinerRangeKNNfloat_L_res_getZCombinerRangeKNNfloat_L_res_setr   r   r   Zdelete_CombinerRangeKNNfloatrT   r   r   r   r   r     s$   r   c                   @   s
  e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZeejejddZeejejd	dZd
d ZeejejddZeejejddZeejejddZeejej ddZ!eej"ej#ddZ$eej%ej&ddZ'eej(ej)ddZ*dd Z+dd Z,ej-Z.dS )CombinerRangeKNNint16r   c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zCombinerRangeKNNint16.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   r   r   r   c              	   C   s   t | t |||| dS r   )r   ZCombinerRangeKNNint16_swiginitZnew_CombinerRangeKNNint16r   r   r   r   r?     s    zCombinerRangeKNNint16.__init__r   r   r   r   r   r   c                 C   s   t | |S r   )r   Z#CombinerRangeKNNint16_compute_sizesr   r   r   r   r     s    z#CombinerRangeKNNint16.compute_sizesc                 C   s   t | ||S r   )r   Z"CombinerRangeKNNint16_write_resultr   r   r   r   r     s    z"CombinerRangeKNNint16.write_resultN)/r   r   r,   r-   r   r   r   r   r   ZCombinerRangeKNNint16_nq_getZCombinerRangeKNNint16_nq_setr   ZCombinerRangeKNNint16_k_getZCombinerRangeKNNint16_k_setr   ZCombinerRangeKNNint16_r2_getZCombinerRangeKNNint16_r2_setr   Z"CombinerRangeKNNint16_keep_max_getZ"CombinerRangeKNNint16_keep_max_setr   r?   ZCombinerRangeKNNint16_I_getZCombinerRangeKNNint16_I_setr   ZCombinerRangeKNNint16_D_getZCombinerRangeKNNint16_D_setr   ZCombinerRangeKNNint16_mask_getZCombinerRangeKNNint16_mask_setr   Z$CombinerRangeKNNint16_lim_remain_getZ$CombinerRangeKNNint16_lim_remain_setr   Z"CombinerRangeKNNint16_D_remain_getZ"CombinerRangeKNNint16_D_remain_setr   Z"CombinerRangeKNNint16_I_remain_getZ"CombinerRangeKNNint16_I_remain_setr   ZCombinerRangeKNNint16_L_res_getZCombinerRangeKNNint16_L_res_setr   r   r   Zdelete_CombinerRangeKNNint16rT   r   r   r   r   r     s$   r   c                 C   s   t | ||S )z( Squared L2 distance between two vectors)r   
fvec_L2sqrr2   yr   r   r   r   r     s    r   c                 C   s   t | ||S )z inner product)r   fvec_inner_productr   r   r   r   r     s    r   c                 C   s   t | ||S )z L1 distance)r   fvec_L1r   r   r   r   r     s    r   c                 C   s   t | ||S )z infinity distance)r   	fvec_Linfr   r   r   r   r     s    r   c
           
      C   s   t | |||||||||	
S )zy
    Special version of inner product that computes 4 distances
    between x and yi, which is performance oriented.
    )r   fvec_inner_product_batch_4
r2   Zy0y1y2Zy3r   dis0dis1dis2dis3r   r   r   r    s    r  c
           
      C   s   t | |||||||||	
S )zq
    Special version of L2sqr that computes 4 distances
    between x and yi, which is performance oriented.
    )r   fvec_L2sqr_batch_4r  r   r   r   r	  
  s    r	  c	           	      C   s   t | ||||||||	S )a  
     Compute pairwise distances between sets of vectors

    :type d: int
    :param d:     dimension of the vectors
    :type nq: int
    :param nq:    nb of query vectors
    :type nb: int
    :param nb:    nb of database vectors
    :type xq: float
    :param xq:    query vectors (size nq * d)
    :type xb: float
    :param xb:    database vectors (size nb * d)
    :type dis: float
    :param dis:   output distances (size nq * nb)
    :param ldq,ldb:, ldd strides for the matrices
    )r   pairwise_L2sqr)	r   r   xqr   xbr   ldqldblddr   r   r   r    s    r  c                 C   s   t | ||||S r#   )r   fvec_inner_products_ny)ipr2   r   r   nyr   r   r   r  %  s    r  c                 C   s   t | ||||S r#   )r   fvec_L2sqr_ny)r   r2   r   r   r  r   r   r   r  (  s    r  c              	   C   s   t | ||||||S r#   )r   fvec_L2sqr_ny_transposed)r   r2   r   y_sqlenr   d_offsetr  r   r   r   r  +  s    r  c                 C   s   t | ||||S r#   )r   fvec_L2sqr_ny_nearest)distances_tmp_bufferr2   r   r   r  r   r   r   r  .  s    r  c              	   C   s   t | ||||||S r#   )r   "fvec_L2sqr_ny_nearest_y_transposed)r  r2   r   r  r   r  r  r   r   r   r  1  s    r  c                 C   s   t | |S )z squared norm of a vector)r   fvec_norm_L2sqr)r2   r   r   r   r   r  4  s    r  c                 C   s   t | |||S )z
     compute the L2 norms for a set of vectors

    :type norms: float
    :param norms:    output norms, size nx
    :type x: float
    :param x:        set of vectors, size nx * d
    )r   fvec_norms_L2normsr2   r   nxr   r   r   r  8  s    	r  c                 C   s   t | |||S )z2 same as fvec_norms_L2, but computes squared norms)r   fvec_norms_L2sqrr  r   r   r   r   C  s    r   c                 C   s   t | ||S r#   )r   fvec_renorm_L2)r   r  r2   r   r   r   r!  G  s    r!  c                 C   s   t | ||||S r#   )r   inner_product_to_L2sqr)r   Znr1Znr2r   r   r   r   r   r"  J  s    r"  c                  G   s
   t j|  S )a  
    *Overload 1:*
     compute c := a + b for vectors

    c and a can overlap, c and b can overlap

    :type a: float
    :param a: size d
    :type b: float
    :param b: size d
    :type c: float
    :param c: size d

    |

    *Overload 2:*
     compute c := a + b for a, c vectors and b a scalar

    c and a can overlap

    :type a: float
    :param a: size d
    :type c: float
    :param c: size d
    )r   fvec_addr   r   r   r   r#  M  s    r#  c                 C   s   t | |||S )z
     compute c := a - b for vectors

    c and a can overlap, c and b can overlap

    :type a: float
    :param a: size d
    :type b: float
    :param b: size d
    :type c: float
    :param c: size d
    )r   fvec_sub)r   r   r~   cr   r   r   r$  i  s    r$  c              	   C   s   t | ||||||S )a  
     compute the inner product between x and a subset y of ny vectors defined by
    ids

    ip(i, j) = inner_product(x(i, :), y(ids(i, j), :))

    :type ip: float
    :param ip:    output array, size nx * ny
    :type x: float
    :param x:     first-term vector, size nx * d
    :type y: float
    :param y:     second-term vector, size (max(ids) + 1) * d
    :type ids: int
    :param ids:   ids to sample from y, size nx * ny
    )r   fvec_inner_products_by_idx)r  r2   r   idsr   r  r  r   r   r   r&  x  s    r&  c              	   C   s   t | ||||||S )a  
     compute the squared L2 distances between x and a subset y of ny vectors
    defined by ids

    dis(i, j) = inner_product(x(i, :), y(ids(i, j), :))

    :type dis: float
    :param dis:   output array, size nx * ny
    :type x: float
    :param x:     first-term vector, size nx * d
    :type y: float
    :param y:     second-term vector, size (max(ids) + 1) * d
    :type ids: int
    :param ids:   ids to sample from y, size nx * ny
    )r   fvec_L2sqr_by_idx)r   r2   r   r'  r   r  r  r   r   r   r(    s    r(  c              	   C   s   t | ||||||S )a2  
     compute dis[j] = L2sqr(x[ix[j]], y[iy[j]]) forall j=0..n-1

    :type x: float
    :param x:  size (max(ix) + 1, d)
    :type y: float
    :param y:  size (max(iy) + 1, d)
    :type ix: int
    :param ix: size n
    :type iy: int
    :param iy: size n
    :type dis: float
    :param dis: size n
    )r   pairwise_indexed_L2sqrr   rB   r2   Zixr   Ziyr   r   r   r   r)    s    r)  c              	   C   s   t | ||||||S )a:  
     compute dis[j] = inner_product(x[ix[j]], y[iy[j]]) forall j=0..n-1

    :type x: float
    :param x:  size (max(ix) + 1, d)
    :type y: float
    :param y:  size (max(iy) + 1, d)
    :type ix: int
    :param ix: size n
    :type iy: int
    :param iy: size n
    :type dis: float
    :param dis: size n
    )r   pairwise_indexed_inner_productr*  r   r   r   r+    s    r+  c                  G   s
   t j|  S )a  
    *Overload 1:*
     Return the k nearest neighbors of each of the nx vectors x among the ny
     vector y, w.r.t to max inner product.

    :type x: float
    :param x:    query vectors, size nx * d
    :type y: float
    :param y:    database vectors, size ny * d
    :type res: :py:class:`float_minheap_array_t`
    :param res:  result heap structure, which also provides k. Sorted on output

    |

    *Overload 2:*
      Return the k nearest neighbors of each of the nx vectors x among the ny
     vector y, for the inner product metric.

    :type x: float
    :param x:    query vectors, size nx * d
    :type y: float
    :param y:    database vectors, size ny * d
    :type distances: float
    :param distances:  output distances, size nq * k
    :type indexes: int
    :param indexes:    output vector ids, size nq * k

    |

    *Overload 3:*
      Return the k nearest neighbors of each of the nx vectors x among the ny
     vector y, for the inner product metric.

    :type x: float
    :param x:    query vectors, size nx * d
    :type y: float
    :param y:    database vectors, size ny * d
    :type distances: float
    :param distances:  output distances, size nq * k
    :type indexes: int
    :param indexes:    output vector ids, size nq * k
    )r   knn_inner_productr   r   r   r   r,    s    +r,  c                  G   s
   t j|  S )aS	  
    *Overload 1:*
     Return the k nearest neighbors of each of the nx vectors x among the ny
     vector y, for the L2 distance
    :type x: float
    :param x:    query vectors, size nx * d
    :type y: float
    :param y:    database vectors, size ny * d
    :type res: :py:class:`float_maxheap_array_t`
    :param res:  result heap strcture, which also provides k. Sorted on output
    :type y_norm2: float, optional
    :param y_norm2:    (optional) norms for the y vectors (nullptr or size ny)
    :type sel: :py:class:`IDSelector`, optional
    :param sel:  search in this subset of vectors

    |

    *Overload 2:*
      Return the k nearest neighbors of each of the nx vectors x among the ny
     vector y, for the L2 distance

    :type x: float
    :param x:    query vectors, size nx * d
    :type y: float
    :param y:    database vectors, size ny * d
    :type distances: float
    :param distances:  output distances, size nq * k
    :type indexes: int
    :param indexes:    output vector ids, size nq * k
    :type y_norm2: float, optional
    :param y_norm2:    (optional) norms for the y vectors (nullptr or size ny)
    :type sel: :py:class:`IDSelector`, optional
    :param sel:  search in this subset of vectors

    |

    *Overload 3:*
      Return the k nearest neighbors of each of the nx vectors x among the ny
     vector y, for the L2 distance

    :type x: float
    :param x:    query vectors, size nx * d
    :type y: float
    :param y:    database vectors, size ny * d
    :type distances: float
    :param distances:  output distances, size nq * k
    :type indexes: int
    :param indexes:    output vector ids, size nq * k
    :type y_norm2: float, optional
    :param y_norm2:    (optional) norms for the y vectors (nullptr or size ny)
    :param sel:  search in this subset of vectors

    |

    *Overload 4:*
      Return the k nearest neighbors of each of the nx vectors x among the ny
     vector y, for the L2 distance

    :type x: float
    :param x:    query vectors, size nx * d
    :type y: float
    :param y:    database vectors, size ny * d
    :type distances: float
    :param distances:  output distances, size nq * k
    :type indexes: int
    :param indexes:    output vector ids, size nq * k
    :param y_norm2:    (optional) norms for the y vectors (nullptr or size ny)
    :param sel:  search in this subset of vectors
    )r   	knn_L2sqrr   r   r   r   r-    s    Fr-  c                 C   s   t | |||||||||	|
S )a=  
     Find the max inner product neighbors for nx queries in a set of ny vectors
    indexed by ids. May be useful for re-ranking a pre-selected vector list

    :type x: float
    :param x:    query vectors, size nx * d
    :type y: float
    :param y:    database vectors, size (max(ids) + 1) * d
    :type ids: int
    :param ids:  subset of database vectors to consider, size (nx, nsubset)
    :param res:  result structure
    :type ld_ids: int, optional
    :param ld_ids: stride for the ids array. -1: use nsubset, 0: all queries
        process the same subset
    )r   knn_inner_products_by_idx)r2   r   subsetr   r  r  nsubsetr   valsr'  Zld_idsr   r   r   r.  3  s    r.  c                 C   s   t | |||||||||	|
S )aC  
     Find the nearest neighbors for nx queries in a set of ny vectors
    indexed by ids. May be useful for re-ranking a pre-selected vector list

    :type x: float
    :param x:    query vectors, size nx * d
    :type y: float
    :param y:    database vectors, size (max(ids) + 1) * d
    :type subset: int
    :param subset: subset of database vectors to consider, size (nx, nsubset)
    :param res:  rIDesult structure
    :type ld_subset: int, optional
    :param ld_subset: stride for the subset array. -1: use nsubset, 0: all queries
        process the same subset
    )r   knn_L2sqr_by_idx)r2   r   r/  r   r  r  r0  r   r1  r'  Z	ld_subsetr   r   r   r2  E  s    r2  c              
   C   s   t | |||||||S )a  
     Return the k nearest neighbors of each of the nx vectors x among the ny
     vector y, w.r.t to max inner product

    :type x: float
    :param x:      query vectors, size nx * d
    :type y: float
    :param y:      database vectors, size ny * d
    :type radius: float
    :param radius: search radius around the x vectors
    :type result: :py:class:`RangeSearchResult`
    :param result: result structure
    )r   range_search_L2sqrr2   r   r   r  r  r   r   selr   r   r   r3  W  s    r3  c              
   C   s   t | |||||||S )z< same as range_search_L2sqr for the inner product similarity)r   range_search_inner_productr4  r   r   r   r6  g  s    r6  c              	   C   s   t | ||||||S )z specialized function for PQ2)r   compute_PQ_dis_tables_dsub2)r   ksub	centroidsr  r2   Zis_inner_product
dis_tablesr   r   r   r7  k  s    r7  c                 C   s   t | ||||S )z
     compute c := a + bf * b for a, b and c tables

    :type n: int
    :param n:   size of the tables
    :type a: float
    :param a:   size n
    :type b: float
    :param b:   size n
    :type c: float
    :param c:   result table, size n
    )r   	fvec_maddrB   r   bfr~   r%  r   r   r   r;  o  s    r;  c                 C   s   t | ||||S )z
     same as fvec_madd, also return index of the min of the result table
    :rtype: int
    :return: index of the min of table c
    )r   fvec_madd_and_argminr<  r   r   r   r>  ~  s    r>  c                   @   sh   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 Zdd Zdd ZdddZejZdS )RandomGeneratorz< random generator that can be used in multithreaded contextsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zRandomGenerator.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s
   t | S z random int64_t)r   ZRandomGenerator_rand_int64r@   r   r   r   
rand_int64  s    zRandomGenerator.rand_int64c                 G   s   t j| g|R  S z
        *Overload 1:*
        random positive integer

        |

        *Overload 2:*
        generate random integer between 0 and max-1
        )r   ZRandomGenerator_rand_intrQ   r   r   r   rand_int  s    
zRandomGenerator.rand_intc                 C   s
   t | S z between 0 and 1)r   ZRandomGenerator_rand_floatr@   r   r   r   
rand_float  s    zRandomGenerator.rand_floatc                 C   s
   t | S r#   )r   ZRandomGenerator_rand_doubler@   r   r   r   rand_double  s    zRandomGenerator.rand_doubler   c                 C   s   t | t | d S r#   )r   ZRandomGenerator_swiginitZnew_RandomGeneratorr   r   r   r   r   r?     s    zRandomGenerator.__init__N)r   )r   r   r,   r-   r   r   r   r   r   ZRandomGenerator_mt_getZRandomGenerator_mt_setmtrA  rC  rE  rF  r?   Zdelete_RandomGeneratorrT   r   r   r   r   r?    s   
r?  c                   @   sp   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 Zdd Zdd ZdddZdd ZejZdS )SplitMix64RandomGeneratorzu
    fast random generator that cannot be used in multithreaded contexts.
    based on https://prng.di.unimi.it/
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z"SplitMix64RandomGenerator.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s
   t | S r@  )r   Z$SplitMix64RandomGenerator_rand_int64r@   r   r   r   rA    s    z$SplitMix64RandomGenerator.rand_int64c                 G   s   t j| g|R  S rB  )r   Z"SplitMix64RandomGenerator_rand_intrQ   r   r   r   rC    s    
z"SplitMix64RandomGenerator.rand_intc                 C   s
   t | S rD  )r   Z$SplitMix64RandomGenerator_rand_floatr@   r   r   r   rE    s    z$SplitMix64RandomGenerator.rand_floatc                 C   s
   t | S r#   )r   Z%SplitMix64RandomGenerator_rand_doubler@   r   r   r   rF    s    z%SplitMix64RandomGenerator.rand_doubler   c                 C   s   t | t | d S r#   )r   Z"SplitMix64RandomGenerator_swiginitZnew_SplitMix64RandomGeneratorrG  r   r   r   r?     s    z"SplitMix64RandomGenerator.__init__c                 C   s
   t | S r#   )r   ZSplitMix64RandomGenerator_nextr@   r   r   r   rH     s    zSplitMix64RandomGenerator.nextN)r   )r   r   r,   r-   r   r   r   r   r   Z#SplitMix64RandomGenerator_state_getZ#SplitMix64RandomGenerator_state_setstaterA  rC  rE  rF  r?   rH   Z delete_SplitMix64RandomGeneratorrT   r   r   r   r   rI    s   
rI  c                 C   s   t | ||S r#   )r   
float_randr2   rB   r   r   r   r   rK    s    rK  c                 C   s   t | ||S r#   )r   float_randnrL  r   r   r   rM    s    rM  c                 C   s   t | ||S r#   )r   
int64_randrL  r   r   r   rN    s    rN  c                 C   s   t | ||S r#   )r   	byte_randrL  r   r   r   rO    s    rO  c                 C   s   t | |||S r#   )r   int64_rand_max)r2   rB   maxr   r   r   r   rP    s    rP  c                 C   s   t | ||S r#   )r   	rand_permpermrB   r   r   r   r   rR    s    rR  c                 C   s   t | ||S r#   )r   rand_perm_splitmix64rS  r   r   r   rU    s    rU  c                 C   s   t | |||S r#   )r   rand_smooth_vectors)rB   r   r2   r   r   r   r   rV    s    rV  c                 C   s   t | ||S )a)  
     Indirect sort of a floating-point array

    :type n: int
    :param n:     size of the array
    :type vals: float
    :param vals:  array to sort, size n
    :type perm: int
    :param perm:  output: permutation of [0..n-1], st.
                     vals[perm[i + 1]] >= vals[perm[i]]
    )r   fvec_argsortrB   r1  rT  r   r   r   rW    s    rW  c                 C   s   t | ||S )z# Same as fvec_argsort, parallelized)r   fvec_argsort_parallelrX  r   r   r   rY    s    rY  c                 C   s   t | |||||S )a  
     Bucket sort of a list of values

    :type vals: int
    :param vals:     values to sort, size nval, max value nbucket - 1
    :type lims: int
    :param lims:     output limits of buckets, size nbucket + 1
    :type perm: int
    :param perm:     output buckets, the elements of bucket
                        i are in perm[lims[i]:lims[i + 1]]
    :type nt: int, optional
    :param nt:       number of threads (0 = pure sequential code)
    )r   bucket_sort)Znvalr1  ZnbucketlimsrT  ntr   r   r   rZ    s    rZ  c                  G   s
   t j|  S )a_  
    *Overload 1:*
     in-place bucket sort (with attention to memory=>int32)
    on input the values are in a nrow * col matrix
    we want to store the row numbers in the output.

    :type vals: int
    :param vals:     positive values to sort, size nrow * ncol,
                        max value nbucket - 1
    :type lims: int
    :param lims:     output limits of buckets, size nbucket + 1
    :type nt: int, optional
    :param nt:       number of threads (0 = pure sequential code)

    |

    *Overload 2:*
     same with int64 elements

    |

    *Overload 3:*
     same with int64 elements
    )r   matrix_bucket_sort_inplacer   r   r   r   r]    s    r]  c                 C   s   t | |S )a  
     Hashtable implementation for int64 -> int64 with external storage
    implemented for fast batch add and lookup.

    tab is of size  2 * (1 << log2_capacity)
    n is the number of elements to add or search

    adding several values in a same batch: an arbitrary one gets added
    in different batches: the newer batch overwrites.
    raises an exception if capacity is exhausted.
    )r   hashtable_int64_to_int64_init)log2_capacitytabr   r   r   r^  )  s    r^  c                 C   s   t | ||||S r#   )r   hashtable_int64_to_int64_addr_  r`  rB   keysr1  r   r   r   ra  7  s    ra  c                 C   s   t | ||||S r#   )r   hashtable_int64_to_int64_lookuprb  r   r   r   rd  :  s    rd  c                 C   s
   t | S )z
    this function is used to distinguish between min and max indexes since
    we need to support similarity and dis-similarity metrics in a flexible way
    )r   is_similarity_metric)metric_typer   r   r   re  T  s    re  c                   @   sJ   e Zd ZdZedd dd ddZeZeej	ej
ddZejZdd	 Zd
S )SearchParametersz
     Parent class for the optional search paramenters.

    Sub-classes with additional search parameters should inherit this class.
    Ownership of the object fields is always to the caller.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   f  r4   zSearchParameters.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   f  r4   r7   r8   z> if non-null, only these IDs will be considered during search.c                 C   s   t | t   d S r#   )r   ZSearchParameters_swiginitZnew_SearchParametersr@   r   r   r   r?   k  s    zSearchParameters.__init__N)r   r   r,   r-   r   r   r   r   r   ZSearchParameters_sel_getZSearchParameters_sel_setr5  Zdelete_SearchParametersrT   r?   r   r   r   r   rg  ^  s   rg  c                   @   sV  e Zd ZdZedd dd ddZdd ZeZee	j
e	jd	dZee	je	jd
dZee	je	jddZee	je	jddZee	je	jddZee	je	jddZe	jZdd Zdd Zdd Z d<ddZ!d=ddZ"d>ddZ#dd Z$dd  Z%d!d" Z&d#d$ Z'd%d& Z(d?d'd(Z)d)d* Z*d+d, Z+d-d. Z,d/d0 Z-d1d2 Z.d3d4 Z/d@d6d7Z0d8d9 Z1d:d; Z2dS )AIndexz
     Abstract structure for an index, supports adding vectors and searching
    them.

    All vectors provided at add or search time are 32-bit float arrays,
    although the internal representation may vary.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   y  r4   zIndex.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   y  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   {  s    zIndex.__init__ vector dimension total nb of indexed vectors verbosity levelzX
    set if the Index does not require training, or if training is
    done already
    * type of metric this index uses for searchz argument of the metric typec                 C   s   t | ||S )z
         Perform training on a representative set of vectors

        :type n: int
        :param n:      nb of training vectors
        :type x: float
        :param x:      training vecors, size n * d
        )r   ZIndex_trainr   rB   r2   r   r   r   train  s    	zIndex.trainc                 C   s   t | ||S )a{  
         Add n vectors of dimension d to the index.

        Vectors are implicitly assigned labels ntotal .. ntotal + n - 1
        This function slices the input vectors in chunks smaller than
        blocksize_add and calls add_core.
        :type n: int
        :param n:      number of vectors
        :type x: float
        :param x:      input matrix, size n * d
        )r   Z	Index_addrm  r   r   r   add  s    z	Index.addc                 C   s   t | |||S )a  
         Same as add, but stores xids instead of sequential ids.

        The default implementation fails with an assertion, as it is
        not supported by all indexes.

        :type n: int
        :param n:         number of vectors
        :type x: float
        :param x:         input vectors, size n * d
        :type xids: int
        :param xids:      if non-null, ids to store for the vectors (size n)
        )r   ZIndex_add_with_idsr   rB   r2   xidsr   r   r   add_with_ids  s    zIndex.add_with_idsNc              	   C   s   t | ||||||S )a\  
         query n vectors of dimension d to the index.

        return at most k vectors. If there are not enough results for a
        query, the result array is padded with -1s.

        :type n: int
        :param n:           number of vectors
        :type x: float
        :param x:           input vectors to search, size n * d
        :type k: int
        :param k:           number of extracted vectors
        :type distances: float
        :param distances:   output pairwise distances, size n*k
        :type labels: int
        :param labels:      output labels of the NNs, size n*k
        )r   ZIndex_searchr   rB   r2   r   r   r   paramsr   r   r   search  s    zIndex.searchc                 C   s   t | |||||S )a  
         query n vectors of dimension d to the index.

        return all vectors with distance < radius. Note that many
        indexes do not implement the range_search (only the k-NN search
        is mandatory).

        :type n: int
        :param n:           number of vectors
        :type x: float
        :param x:           input vectors to search, size n * d
        :type radius: float
        :param radius:      search radius
        :type result: :py:class:`RangeSearchResult`
        :param result:      result table
        )r   ZIndex_range_searchr   rB   r2   r   r   rt  r   r   r   range_search  s    zIndex.range_searchr   c                 C   s   t | ||||S )a  
         return the indexes of the k vectors closest to the query x.

        This function is identical as search but only return labels of
        neighbors.
        :type n: int
        :param n:           number of vectors
        :type x: float
        :param x:           input vectors to search, size n * d
        :type labels: int
        :param labels:      output labels of the NNs, size n*k
        :type k: int, optional
        :param k:           number of nearest neighbours
        )r   ZIndex_assignr   rB   r2   r   r   r   r   r   assign  s    zIndex.assignc                 C   s
   t | S )z( removes all elements from the database.)r   ZIndex_resetr@   r   r   r   r|     s    zIndex.resetc                 C   s   t | |S )z|
         removes IDs from the index. Not supported by all
        indexes. Returns the number of elements removed.
        )r   ZIndex_remove_idsr   r5  r   r   r   
remove_ids  s    zIndex.remove_idsc                 C   s   t | ||S )a7  
         Reconstruct a stored vector (or an approximation if lossy coding)

        this function may not be defined for some indexes
        :type key: int
        :param key:         id of the vector to reconstruct
        :type recons: float
        :param recons:      reconstucted vector (size d)
        )r   ZIndex_reconstructr   keyreconsr   r   r   reconstruct  s    
zIndex.reconstructc                 C   s   t | |||S )a  
         Reconstruct several stored vectors (or an approximation if lossy
        coding)

        this function may not be defined for some indexes
        :type n: int
        :param n:           number of vectors to reconstruct
        :type keys: int
        :param keys:        ids of the vectors to reconstruct (size n)
        :type recons: float
        :param recons:      reconstucted vector (size n * d)
        )r   ZIndex_reconstruct_batch)r   rB   rc  r~  r   r   r   reconstruct_batch  s    zIndex.reconstruct_batchc                 C   s   t | |||S )a}  
         Reconstruct vectors i0 to i0 + ni - 1

        this function may not be defined for some indexes
        :type i0: int
        :param i0:          index of the first vector in the sequence
        :type ni: int
        :param ni:          number of vectors in the sequence
        :type recons: float
        :param recons:      reconstucted vector (size ni * d)
        )r   ZIndex_reconstruct_nr   i0nir~  r   r   r   reconstruct_n  s    zIndex.reconstruct_nc              
   C   s   t | |||||||S )a  
         Similar to search, but also reconstructs the stored vectors (or an
        approximation in the case of lossy coding) for the search results.

        If there are not enough results for a query, the resulting arrays
        is padded with -1s.

        :type n: int
        :param n:           number of vectors
        :type x: float
        :param x:           input vectors to search, size n * d
        :type k: int
        :param k:           number of extracted vectors
        :type distances: float
        :param distances:   output pairwise distances, size n*k
        :type labels: int
        :param labels:      output labels of the NNs, size n*k
        :type recons: float
        :param recons:      reconstructed vectors size (n, k, d)
        )r   ZIndex_search_and_reconstructr   rB   r2   r   r   r   r~  rt  r   r   r   search_and_reconstruct  s    zIndex.search_and_reconstructc                 C   s   t | |||S )aK  
         Computes a residual vector after indexing encoding.

        The residual vector is the difference between a vector and the
        reconstruction that can be decoded from its representation in
        the index. The residual can be used for multiple-stage indexing
        methods, like IndexIVF's methods.

        :type x: float
        :param x:           input vector, size d
        :type residual: float
        :param residual:    output residual vector, size d
        :type key: int
        :param key:         encoded index, as returned by search and assign
        )r   ZIndex_compute_residual)r   r2   Zresidualr}  r   r   r   compute_residual5  s    zIndex.compute_residualc                 C   s   t | ||||S )a  
         Computes a residual vector after indexing encoding (batch form).
        Equivalent to calling compute_residual for each vector.

        The residual vector is the difference between a vector and the
        reconstruction that can be decoded from its representation in
        the index. The residual can be used for multiple-stage indexing
        methods, like IndexIVF's methods.

        :type n: int
        :param n:           number of vectors
        :type xs: float
        :param xs:          input vectors, size (n x d)
        :type residuals: float
        :param residuals:   output residual vectors, size (n x d)
        :type keys: int
        :param keys:        encoded index, as returned by search and assign
        )r   ZIndex_compute_residual_n)r   rB   Zxs	residualsrc  r   r   r   compute_residual_nG  s    zIndex.compute_residual_nc                 C   s
   t | S )z
         Get a DistanceComputer (defined in AuxIndexStructures) object
        for this kind of index.

        DistanceComputer is implemented for indexes that support random
        access of their vectors.
        )r   ZIndex_get_distance_computerr@   r   r   r   get_distance_computer\  s    zIndex.get_distance_computerc                 C   s
   t | S z$ size of the produced codes in bytes)r   ZIndex_sa_code_sizer@   r   r   r   sa_code_sizef  s    zIndex.sa_code_sizec                 C   s   t | |||S )a  
         encode a set of vectors

        :type n: int
        :param n:       number of vectors
        :type x: float
        :param x:       input vectors, size n * d
        :type bytes: uint8_t
        :param bytes:   output encoded vectors, size n * sa_code_size()
        )r   ZIndex_sa_encoder   rB   r2   r   r   r   r   	sa_encodej  s    zIndex.sa_encodec                 C   s   t | |||S )a  
         decode a set of vectors

        :type n: int
        :param n:       number of vectors
        :type bytes: uint8_t
        :param bytes:   input encoded vectors, size n * sa_code_size()
        :type x: float
        :param x:       output vectors, size n * d
        )r   ZIndex_sa_decoder   rB   r   r2   r   r   r   	sa_decodew  s    zIndex.sa_decoder   c                 C   s   t | ||S z
         moves the entries from another dataset to self.
        On output, other is empty.
        add_id is added to all moved ids
        (for sequential ids, this would be this->ntotal)
        )r   ZIndex_merge_fromr   
otherIndexadd_idr   r   r   
merge_from  s    zIndex.merge_fromc                 C   s   t | |S z
         check that the two indexes are compatible (ie, they are
        trained in the same way and have the same
        parameters). Otherwise throw.
        )r   Z Index_check_compatible_for_merger   r  r   r   r   check_compatible_for_merge  s    z Index.check_compatible_for_mergec                 C   s   t | |||S z
         Add vectors that are computed with the standalone codec

        :type codes: uint8_t
        :param codes:  codes to add size n * sa_code_size()
        :type xids: int
        :param xids:   corresponding ids, size n
        )r   ZIndex_add_sa_codesr   rB   r   rq  r   r   r   add_sa_codes  s    	zIndex.add_sa_codes)N)N)r   )N)r   )3r   r   r,   r-   r   r   r?   r   r   r   ZIndex_d_getZIndex_d_setr   ZIndex_ntotal_getZIndex_ntotal_setntotalZIndex_verbose_getZIndex_verbose_setr   ZIndex_is_trained_getZIndex_is_trained_set
is_trainedZIndex_metric_type_getZIndex_metric_type_setrf  ZIndex_metric_arg_getZIndex_metric_arg_set
metric_argZdelete_IndexrT   rn  ro  rr  ru  rw  ry  r|   r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   rh  p  s@   





	rh  c                   @   sT   e Zd Zedd dd ddZdd ZeZdd	 Zd
d Z	dd Z
dd ZejZdS )DistanceComputerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zDistanceComputer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zDistanceComputer.__init__c                 C   s   t | |S )zv
        called before computing distances. Pointer x should remain valid
        while operator () is called
        )r   ZDistanceComputer_set_queryrE   r   r   r   	set_query  s    zDistanceComputer.set_queryc                 C   s   t | |S z. compute distance of vector i to current query)r   ZDistanceComputer___call__r   r   r   r   r   __call__  s    zDistanceComputer.__call__c	           	      C   s   t | ||||||||	S )z
        compute distances of current query to 4 stored vectors.
        certain DistanceComputer implementations may benefit
        heavily from this.
        )r   Z"DistanceComputer_distances_batch_4	r   Zidx0Zidx1Zidx2Zidx3r  r  r  r  r   r   r   distances_batch_4  s    z"DistanceComputer.distances_batch_4c                 C   s   t | ||S z, compute distance between two stored vectors)r   ZDistanceComputer_symmetric_disr   r   jr   r   r   symmetric_dis  s    zDistanceComputer.symmetric_disN)r   r   r,   r   r   r?   r   r   r  r  r  r  r   Zdelete_DistanceComputerrT   r   r   r   r   r    s   r  c                   @   sf   e Zd Zedd dd ddZeZeejej	ddZ
dd Zd	d
 Zdd Zdd Zdd ZejZdS )NegativeDistanceComputerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z!NegativeDistanceComputer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z owned by thisc                 C   s   t | t | d S r#   )r   Z!NegativeDistanceComputer_swiginitZnew_NegativeDistanceComputer)r   basedisr   r   r   r?     s    z!NegativeDistanceComputer.__init__c                 C   s   t | |S r#   )r   Z"NegativeDistanceComputer_set_queryrE   r   r   r   r    s    z"NegativeDistanceComputer.set_queryc                 C   s   t | |S r  )r   Z!NegativeDistanceComputer___call__r  r   r   r   r    s    z!NegativeDistanceComputer.__call__c	           	      C   s   t | ||||||||	S r#   )r   Z*NegativeDistanceComputer_distances_batch_4r  r   r   r   r    s    z*NegativeDistanceComputer.distances_batch_4c                 C   s   t | ||S r  )r   Z&NegativeDistanceComputer_symmetric_disr  r   r   r   r    s    z&NegativeDistanceComputer.symmetric_disN)r   r   r,   r   r   r   r   r   Z$NegativeDistanceComputer_basedis_getZ$NegativeDistanceComputer_basedis_setr  r?   r  r  r  r  Zdelete_NegativeDistanceComputerrT   r   r   r   r   r    s   r  c                   @   s`   e Zd Zedd dd ddZdd ZeZeej	ej
ZeejejZdd	 Zd
d ZejZdS )FlatCodesDistanceComputerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z"FlatCodesDistanceComputer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    z"FlatCodesDistanceComputer.__init__c                 C   s   t | |S r#   )r   Z"FlatCodesDistanceComputer___call__r  r   r   r   r    s    z"FlatCodesDistanceComputer.__call__c                 C   s   t | |S )z7 compute distance of current query to an encoded vector)r   Z*FlatCodesDistanceComputer_distance_to_coder   r   r   r   r   distance_to_code  s    z*FlatCodesDistanceComputer.distance_to_codeN)r   r   r,   r   r   r?   r   r   r   Z#FlatCodesDistanceComputer_codes_getZ#FlatCodesDistanceComputer_codes_setr   Z'FlatCodesDistanceComputer_code_size_getZ'FlatCodesDistanceComputer_code_size_setr   r  r  Z delete_FlatCodesDistanceComputerrT   r   r   r   r   r    s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd*ddZd+ddZdd  Zd!d" Zd,d$d%Zd&d' Zd(d) ZejZ dS )-IndexFlatCodeszr
     Index that encodes all vectors as fixed-size codes (size code_size). Storage
    is in the codes vector
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexFlatCodes.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z) encoded dataset, size ntotal * code_sizec                 G   s   t | t j|  d S r#   )r   ZIndexFlatCodes_swiginitZnew_IndexFlatCodesrQ   r   r   r   r?     s    zIndexFlatCodes.__init__c                 C   s   t | ||S )z default add uses sa_encode)r   ZIndexFlatCodes_addrm  r   r   r   ro    s    zIndexFlatCodes.addc                 C   s
   t | S r#   )r   ZIndexFlatCodes_resetr@   r   r   r   r|   	  s    zIndexFlatCodes.resetc                 C   s   t | |||S r#   )r   ZIndexFlatCodes_reconstruct_nr  r   r   r   r  	  s    zIndexFlatCodes.reconstruct_nc                 C   s   t | ||S r#   )r   ZIndexFlatCodes_reconstructr|  r   r   r   r  	  s    zIndexFlatCodes.reconstructc                 C   s
   t | S r#   )r   ZIndexFlatCodes_sa_code_sizer@   r   r   r   r  
	  s    zIndexFlatCodes.sa_code_sizec                 C   s   t | |S )z
         remove some ids. NB that because of the structure of the
        index, the semantics of this operation are
        different from the usual ones: the new ids are shifted
        )r   ZIndexFlatCodes_remove_idsrz  r   r   r   r{  	  s    zIndexFlatCodes.remove_idsc                 C   s
   t | S )z
         a FlatCodesDistanceComputer offers a distance_to_code method

        The default implementation explicitly decodes the vector with sa_decode.
        )r   Z,IndexFlatCodes_get_FlatCodesDistanceComputerr@   r   r   r   get_FlatCodesDistanceComputer	  s    z,IndexFlatCodes.get_FlatCodesDistanceComputerc                 C   s
   t | S r#   )r   Z$IndexFlatCodes_get_distance_computerr@   r   r   r   r  	  s    z$IndexFlatCodes.get_distance_computerNc              	   C   s   t | ||||||S )z Search implemented by decoding)r   ZIndexFlatCodes_searchrs  r   r   r   ru   	  s    zIndexFlatCodes.searchc                 C   s   t | |||||S r#   )r   ZIndexFlatCodes_range_searchrv  r   r   r   rw  $	  s    zIndexFlatCodes.range_searchc                 C   s
   t | S r#   )r   ZIndexFlatCodes_get_CodePackerr@   r   r   r   get_CodePacker'	  s    zIndexFlatCodes.get_CodePackerc                 C   s   t | |S r#   )r   Z)IndexFlatCodes_check_compatible_for_merger  r   r   r   r  *	  s    z)IndexFlatCodes.check_compatible_for_merger   c                 C   s   t | ||S r#   )r   ZIndexFlatCodes_merge_fromr  r   r   r   r  -	  s    zIndexFlatCodes.merge_fromc                 C   s   t | |||S r#   )r   ZIndexFlatCodes_add_sa_codesrp  r   r   r   r  0	  s    zIndexFlatCodes.add_sa_codesc                 C   s   t | |S r#   )r   ZIndexFlatCodes_permute_entriesr   rT  r   r   r   permute_entries3	  s    zIndexFlatCodes.permute_entries)N)N)r   )!r   r   r,   r-   r   r   r   r   r   ZIndexFlatCodes_code_size_getZIndexFlatCodes_code_size_setr   ZIndexFlatCodes_codes_getZIndexFlatCodes_codes_setr   r?   ro  r|   r  r  r  r{  r  r  ru  rw  r  r  r  r  r  Zdelete_IndexFlatCodesrT   r   r   r   r   r    s,   


r  c                   @   s|   e Zd ZdZedd dd ddZeZddd	Zdd
dZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZejZdS )	IndexFlatzB Index that stores the full vectors and performs exhaustive searchc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   <	  r4   zIndexFlat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   <	  r4   r7   r8   Nc              	   C   s   t | ||||||S r#   )r   ZIndexFlat_searchrs  r   r   r   ru  ?	  s    zIndexFlat.searchc                 C   s   t | |||||S r#   )r   ZIndexFlat_range_searchrv  r   r   r   rw  B	  s    zIndexFlat.range_searchc                 C   s   t | ||S r#   )r   ZIndexFlat_reconstructr|  r   r   r   r  E	  s    zIndexFlat.reconstructc                 C   s   t | |||||S )a  
         compute distance with a subset of vectors

        :type x: float
        :param x:       query vectors, size n * d
        :type labels: int
        :param labels:  indices of the vectors that should be compared
                           for each query vector, size n * k
        :type distances: float
        :param distances:
                           corresponding output distances, size n * k
        )r   Z!IndexFlat_compute_distance_subset)r   rB   r2   r   r   r   r   r   r   compute_distance_subsetH	  s    z!IndexFlat.compute_distance_subsetc                 G   s   t j| g|R  S r#   )r   ZIndexFlat_get_xbrQ   r   r   r   get_xbW	  s    zIndexFlat.get_xbc                 G   s   t | t j|  d S r#   )r   ZIndexFlat_swiginitZnew_IndexFlatrQ   r   r   r   r?   Z	  s    zIndexFlat.__init__c                 C   s
   t | S r#   )r   Z'IndexFlat_get_FlatCodesDistanceComputerr@   r   r   r   r  ]	  s    z'IndexFlat.get_FlatCodesDistanceComputerc                 C   s   t | |||S r#   )r   ZIndexFlat_sa_encoder  r   r   r   r  `	  s    zIndexFlat.sa_encodec                 C   s   t | |||S r#   )r   ZIndexFlat_sa_decoder  r   r   r   r  c	  s    zIndexFlat.sa_decode)N)N)r   r   r,   r-   r   r   r   r   ru  rw  r  r  r  r?   r  r  r  r   Zdelete_IndexFlatrT   r   r   r   r   r  9	  s   

r  c                   @   s4   e Zd Zedd dd ddZeZdd Zej	Z
dS )	IndexFlatIPc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   j	  r4   zIndexFlatIP.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   j	  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexFlatIP_swiginitZnew_IndexFlatIPrQ   r   r   r   r?   m	  s    zIndexFlatIP.__init__N)r   r   r,   r   r   r   r   r?   r   Zdelete_IndexFlatIPrT   r   r   r   r   r  i	  s   r  c                   @   sZ   e Zd Zedd dd ddZeZeejej	Z
dd Zdd	 Zd
d Zdd ZejZdS )IndexFlatL2c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   t	  r4   zIndexFlatL2.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   t	  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexFlatL2_swiginitZnew_IndexFlatL2rQ   r   r   r   r?   x	  s    zIndexFlatL2.__init__c                 C   s
   t | S r#   )r   Z)IndexFlatL2_get_FlatCodesDistanceComputerr@   r   r   r   r  {	  s    z)IndexFlatL2.get_FlatCodesDistanceComputerc                 C   s
   t | S r#   )r   ZIndexFlatL2_sync_l2normsr@   r   r   r   sync_l2norms~	  s    zIndexFlatL2.sync_l2normsc                 C   s
   t | S r#   )r   ZIndexFlatL2_clear_l2normsr@   r   r   r   clear_l2norms	  s    zIndexFlatL2.clear_l2normsN)r   r   r,   r   r   r   r   r   ZIndexFlatL2_cached_l2norms_getZIndexFlatL2_cached_l2norms_setZcached_l2normsr?   r  r  r  Zdelete_IndexFlatL2rT   r   r   r   r   r  s	  s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZdd
dZdd Zdd Zdd ZdddZejZdS )IndexFlat1Dz$ optimized version for 1D "vectors".c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   	  r4   zIndexFlat1D.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   	  r4   r7   r8   z) is the permutation updated continuously?z sorted database indicesTc                 C   s   t | t | d S r#   )r   ZIndexFlat1D_swiginitZnew_IndexFlat1D)r   continuous_updater   r   r   r?   	  s    zIndexFlat1D.__init__c                 C   s
   t | S )zg
        if not continuous_update, call this between the last add and
        the first search
        )r   ZIndexFlat1D_update_permutationr@   r   r   r   update_permutation	  s    zIndexFlat1D.update_permutationc                 C   s   t | ||S r#   )r   ZIndexFlat1D_addrm  r   r   r   ro  	  s    zIndexFlat1D.addc                 C   s
   t | S r#   )r   ZIndexFlat1D_resetr@   r   r   r   r|   	  s    zIndexFlat1D.resetNc              	   C   s   t | ||||||S )z+ Warn: the distances returned are L1 not L2)r   ZIndexFlat1D_searchrs  r   r   r   ru  	  s    zIndexFlat1D.search)T)N)r   r   r,   r-   r   r   r   r   r   Z!IndexFlat1D_continuous_update_getZ!IndexFlat1D_continuous_update_setr  ZIndexFlat1D_perm_getZIndexFlat1D_perm_setrT  r?   r  ro  r|   ru  Zdelete_IndexFlat1DrT   r   r   r   r   r  	  s   

r  c                   @   s  e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejZeejejd	dZeejejd
dZeejejddZeejejddZeejejddZ eej!ej"ddZ#eej$ej%ddZ&eej'ej(ddZ)eej*ej+ddZ,eej-ej.ddZ/dd Z0ej1Z2dS )ClusteringParameterszn
     Class for the clustering parameters. Can be passed to the
    constructor of the Clustering object.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   	  r4   zClusteringParameters.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   	  r4   r7   r8   z  number of clustering iterationsz[
    redo clustering this many times and keep the clusters with the best
    objective
    zg
    whether to normalize centroids after each iteration (useful for inner
    product clustering)
    z= round centroids coordinates to integer after each iteration?z% re-train index after each iteration?zd
    Use the subset of centroids provided as input and do not change them
    during iterations
    z
    If fewer than this number of training vectors per centroid are provided,
    writes a warning. Note that fewer than 1 point per centroid raises an
    exception.
    zC to limit size of dataset, otherwise the training set is subsampledz
    seed for the random number generator.
    negative values lead to seeding an internal rng with
    std::high_resolution_clock.
    zB when the training set is encoded, batch size of the codec decoderz+ whether to check for NaNs in an input dataz
    Whether to use splitmix64-based random number generator for subsampling,
    which is faster, but may pick duplicate points.
    c                 C   s   t | t   d S r#   )r   ZClusteringParameters_swiginitZnew_ClusteringParametersr@   r   r   r   r?   	  s    zClusteringParameters.__init__N)3r   r   r,   r-   r   r   r   r   r   ZClusteringParameters_niter_getZClusteringParameters_niter_setniterZClusteringParameters_nredo_getZClusteringParameters_nredo_setZnredoZ ClusteringParameters_verbose_getZ ClusteringParameters_verbose_setr   Z"ClusteringParameters_spherical_getZ"ClusteringParameters_spherical_setZ	sphericalZ&ClusteringParameters_int_centroids_getZ&ClusteringParameters_int_centroids_setZint_centroidsZ%ClusteringParameters_update_index_getZ%ClusteringParameters_update_index_setZupdate_indexZ)ClusteringParameters_frozen_centroids_getZ)ClusteringParameters_frozen_centroids_setZfrozen_centroidsZ0ClusteringParameters_min_points_per_centroid_getZ0ClusteringParameters_min_points_per_centroid_setZmin_points_per_centroidZ0ClusteringParameters_max_points_per_centroid_getZ0ClusteringParameters_max_points_per_centroid_setZmax_points_per_centroidZClusteringParameters_seed_getZClusteringParameters_seed_setr   Z*ClusteringParameters_decode_block_size_getZ*ClusteringParameters_decode_block_size_setZdecode_block_sizeZ2ClusteringParameters_check_input_data_for_NaNs_getZ2ClusteringParameters_check_input_data_for_NaNs_setZcheck_input_data_for_NaNsZ/ClusteringParameters_use_faster_subsampling_getZ/ClusteringParameters_use_faster_subsampling_setZuse_faster_subsamplingr?   Zdelete_ClusteringParametersrT   r   r   r   r   r  	  s$   r  c                   @   s   e Zd Zedd dd ddZeZeejej	ddZ
eejejddZeejejddZeejejd	dZeejejd
dZdd ZejZdS )ClusteringIterationStatsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   	  r4   z!ClusteringIterationStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   	  r4   r7   r8   z6 objective values (sum of distances reported by index)z seconds for iterationz seconds for just searchz imbalance factor of iterationz number of cluster splitsc                 C   s   t | t   d S r#   )r   Z!ClusteringIterationStats_swiginitZnew_ClusteringIterationStatsr@   r   r   r   r?   	  s    z!ClusteringIterationStats.__init__N)r   r   r,   r   r   r   r   r   Z ClusteringIterationStats_obj_getZ ClusteringIterationStats_obj_setobjZ!ClusteringIterationStats_time_getZ!ClusteringIterationStats_time_settimeZ(ClusteringIterationStats_time_search_getZ(ClusteringIterationStats_time_search_setZtime_searchZ-ClusteringIterationStats_imbalance_factor_getZ-ClusteringIterationStats_imbalance_factor_setr   Z#ClusteringIterationStats_nsplit_getZ#ClusteringIterationStats_nsplit_setZnsplitr?   Zdelete_ClusteringIterationStatsrT   r   r   r   r   r  	  s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZdd ZdddZdddZdd ZejZdS )
Clusteringa  
     K-means clustering based on assignment - centroid update iterations

    The clustering is based on an Index object that assigns training
    points to the centroids. Therefore, at each iteration the centroids
    are added to the index.

    On output, the centoids table is set to the latest version
    of the centroids and they are also added to the index. If the
    centroids table it is not empty on input, it is also used for
    initialization.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   	  r4   zClustering.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   	  r4   r7   r8    dimension of the vectors nb of centroidszp
     centroids (k * d)
    if centroids are set on input to train, they will be used as
    initialization
    ' stats at every iteration of clusteringc                 G   s   t | t j|  d S r#   )r   ZClustering_swiginitZnew_ClusteringrQ   r   r   r   r?   	  s    zClustering.__init__Nc                 C   s   t | ||||S )aH  
         run k-means training

        :type x: float
        :param x:          training vectors, size n * d
        :type index: :py:class:`Index`
        :param index:      index used for assignment
        :type x_weights: float, optional
        :param x_weights:  weight associated to each vector: NULL or size n
        )r   ZClustering_train)r   rB   r2   indexZ	x_weightsr   r   r   rn  
  s    zClustering.trainc                 C   s   t | |||||S )aB  
         run with encoded vectors

        win addition to train()'s parameters takes a codec as parameter
        to decode the input vectors.

        :type codec: :py:class:`Index`
        :param codec:      codec used to decode the vectors (nullptr =
                              vectors are in fact floats)
        )r   ZClustering_train_encoded)r   r  r   codecr  weightsr   r   r   train_encoded
  s    zClustering.train_encodedc                 C   s
   t | S )z
        Post-process the centroids after each centroid update.
        includes optional L2 normalization and nearest integer rounding
        )r   Z!Clustering_post_process_centroidsr@   r   r   r   post_process_centroids
  s    z!Clustering.post_process_centroids)N)N)r   r   r,   r-   r   r   r   r   r   ZClustering_d_getZClustering_d_setr   ZClustering_k_getZClustering_k_setr   ZClustering_centroids_getZClustering_centroids_setr9  ZClustering_iteration_stats_getZClustering_iteration_stats_setiteration_statsr?   rn  r  r  Zdelete_ClusteringrT   r   r   r   r   r  	  s   

r  c                   @   s@   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	e
jZdS )Clustering1Dzw
     Exact 1D clustering algorithm

    Since it does not use an index, it does not overload the train() function
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ,
  r4   zClustering1D.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ,
  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZClustering1D_swiginitZnew_Clustering1DrQ   r   r   r   r?   /
  s    zClustering1D.__init__c                 C   s   t | ||S r#   )r   ZClustering1D_train_exactrm  r   r   r   train_exact2
  s    zClustering1D.train_exactN)r   r   r,   r-   r   r   r   r   r?   r  r   Zdelete_Clustering1DrT   r   r   r   r   r  %
  s   r  c                   @   sX   e Zd Zedd dd ddZeZeejej	ddZ
eejejddZdd	 ZejZd
S )"ProgressiveDimClusteringParametersc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   9
  r4   z+ProgressiveDimClusteringParameters.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   9
  r4   r7   r8   z number of incremental stepsz apply PCA on inputc                 C   s   t | t   d S r#   )r   Z+ProgressiveDimClusteringParameters_swiginitZ&new_ProgressiveDimClusteringParametersr@   r   r   r   r?   >
  s    z+ProgressiveDimClusteringParameters.__init__N)r   r   r,   r   r   r   r   r   Z<ProgressiveDimClusteringParameters_progressive_dim_steps_getZ<ProgressiveDimClusteringParameters_progressive_dim_steps_setZprogressive_dim_stepsZ0ProgressiveDimClusteringParameters_apply_pca_getZ0ProgressiveDimClusteringParameters_apply_pca_setZ	apply_pcar?   Z)delete_ProgressiveDimClusteringParametersrT   r   r   r   r   r  8
  s   r  c                   @   s@   e Zd ZdZedd dd ddZeZdd Ze	j
Zd	d
 ZdS )ProgressiveDimIndexFactoryz7 generates an index suitable for clustering when calledc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   G
  r4   z#ProgressiveDimIndexFactory.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   G
  r4   r7   r8   c                 C   s   t | |S )z  ownership transferred to caller)r   Z#ProgressiveDimIndexFactory___call__)r   dimr   r   r   r  J
  s    z#ProgressiveDimIndexFactory.__call__c                 C   s   t | t   d S r#   )r   Z#ProgressiveDimIndexFactory_swiginitZnew_ProgressiveDimIndexFactoryr@   r   r   r   r?   O
  s    z#ProgressiveDimIndexFactory.__init__N)r   r   r,   r-   r   r   r   r   r  r   Z!delete_ProgressiveDimIndexFactoryrT   r?   r   r   r   r   r  D
  s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZdd Zdd ZejZdS )ProgressiveDimClusteringa  
     K-means clustering with progressive dimensions used

    The clustering first happens in dim 1, then with exponentially increasing
    dimension until d (I steps). This is typically applied after a PCA
    transformation (optional). Reference:

    "Improved Residual Vector Quantization for High-dimensional Approximate
    Nearest Neighbor Search"

    Shicong Liu, Hongtao Lu, Junru Shao, AAAI'15

    https://arxiv.org/abs/1509.05195
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   d
  r4   z!ProgressiveDimClustering.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   d
  r4   r7   r8   r  r  z centroids (k * d)r  c                 G   s   t | t j|  d S r#   )r   Z!ProgressiveDimClustering_swiginitZnew_ProgressiveDimClusteringrQ   r   r   r   r?   k
  s    z!ProgressiveDimClustering.__init__c                 C   s   t | |||S r#   )r   ZProgressiveDimClustering_train)r   rB   r2   factoryr   r   r   rn  n
  s    zProgressiveDimClustering.trainN)r   r   r,   r-   r   r   r   r   r   ZProgressiveDimClustering_d_getZProgressiveDimClustering_d_setr   ZProgressiveDimClustering_k_getZProgressiveDimClustering_k_setr   Z&ProgressiveDimClustering_centroids_getZ&ProgressiveDimClustering_centroids_setr9  Z,ProgressiveDimClustering_iteration_stats_getZ,ProgressiveDimClustering_iteration_stats_setr  r?   rn  Zdelete_ProgressiveDimClusteringrT   r   r   r   r   r  T
  s   r  c                 C   s   t | ||||S )a  
     simplified interface

    :type d: int
    :param d: dimension of the data
    :type n: int
    :param n: nb of training vectors
    :type k: int
    :param k: nb of output centroids
    :type x: float
    :param x: training set (size n * d)
    :type centroids: float
    :param centroids: output centroids (size k * d)
    :rtype: float
    :return: final quantization error
    )r   kmeans_clustering)r   rB   r   r2   r9  r   r   r   r  u
  s    r  c                 C   s   t | |||||||||	|
S r#   )r   pairwise_extra_distances)r   r   r  r   r  rH  r  r   r  r  r  r   r   r   r  
  s    r  c
           
      C   s   t | |||||||||	
S r#   )r   knn_extra_metrics)
r2   r   r   r  r  rH  r  r   r   Zindexesr   r   r   r  
  s    r  c                 C   s   t | ||||S )zm
    get a DistanceComputer that refers to this type of distance and
    indexes a flat array of size nb
    )r   get_extra_distance_computer)r   rH  r  r   r  r   r   r   r  
  s    r  c                   @   st   e Zd ZdZedd dd ddZdd ZeZee	j
e	jd	dZee	je	jd
dZdd Zdd Zdd Ze	jZdS )	Quantizerz( General interface for quantizer objectsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   
  r4   zQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   
  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   
  s    zQuantizer.__init__z size of the input vectorsz bytes per indexed vectorc                 C   s   t | ||S )zs
         Train the quantizer

        :type x: float
        :param x:       training vectors, size n * d
        )r   ZQuantizer_trainrm  r   r   r   rn  
  s    zQuantizer.trainc                 C   s   t | |||S )z
         Quantize a set of vectors

        :type x: float
        :param x:        input vectors, size n * d
        :type codes: uint8_t
        :param codes:    output codes, size n * code_size
        )r   ZQuantizer_compute_codesr   r2   r   rB   r   r   r   compute_codes
  s    	zQuantizer.compute_codesc                 C   s   t | |||S )z
         Decode a set of vectors

        :param codes:    input codes, size n * code_size
        :type x: float
        :param x:        output vectors, size n * d
        )r   ZQuantizer_decoder   r   r2   rB   r   r   r   decode
  s    zQuantizer.decodeN)r   r   r,   r-   r   r   r?   r   r   r   ZQuantizer_d_getZQuantizer_d_setr   ZQuantizer_code_size_getZQuantizer_code_size_setr   rn  r  r  Zdelete_QuantizerrT   r   r   r   r   r  
  s   		r  c                   @   s  e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZejZejZejZejZej Z!eej"ej#Z$eej%ej&ddZ'eej(ej)ddZ*eej+ej,ddZ-eej.ej/ddZ0eej1ej2ddZ3dd Z4dd Z5dd Z6dd Z7dd Z8dd Z9dd Z:dd  Z;d!d" Z<d#d$ Z=d%d& Z>d'd( Z?d)d* Z@d+d, ZAd<d.d/ZBd=d0d1ZCeejDejEd2dZFd3d4 ZGd>d5d6ZHd7d8 ZId9d: ZJejKZLd;S )?ProductQuantizerz
     Product Quantizer.
    PQ is trained using k-means, minimizing the L2 distance to centroids.
    PQ supports L2 and Inner Product search, however the quantization error is
    biased towards L2 distance.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   
  r4   zProductQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   
  r4   r7   r8   z number of subquantizersz& number of bits per quantization indexz! dimensionality of each subvectorz* number of centroids for each subquantizer verbose during training?z" parameters used during clusteringzR
    if non-NULL, use this index for assignment (should be of size
    d / M)
    zK
    Centroid table, size M * ksub * dsub.
    Layout: (M, ksub, dsub)
    zV
    Transposed centroid table, size M * ksub * dsub.
    Layout: (dsub, M, ksub)
    zK
    Squared lengths of centroids, size M * ksub
    Layout: (M, ksub)
    c                 C   s   t | ||S )z1 return the centroids associated with subvector m)r   ZProductQuantizer_get_centroids)r   r   r   r   r   r   get_centroids
  s    zProductQuantizer.get_centroidsc                 C   s   t | ||S r#   )r   ZProductQuantizer_trainrm  r   r   r   rn  
  s    zProductQuantizer.trainc                 G   s   t | t j|  d S r#   )r   ZProductQuantizer_swiginitZnew_ProductQuantizerrQ   r   r   r   r?   
  s    zProductQuantizer.__init__c                 C   s
   t | S )z9 compute derived values when d, M and nbits have been set)r   Z#ProductQuantizer_set_derived_valuesr@   r   r   r   set_derived_values
  s    z#ProductQuantizer.set_derived_valuesc                 C   s   t | ||S )z( Define the centroids for subquantizer m)r   ZProductQuantizer_set_params)r   r9  r   r   r   r   
set_params
  s    zProductQuantizer.set_paramsc                 C   s   t | ||S )z/ Quantize one vector with the product quantizer)r   ZProductQuantizer_compute_code)r   r2   r   r   r   r   compute_code
  s    zProductQuantizer.compute_codec                 C   s   t | |||S )z) same as compute_code for several vectors)r   ZProductQuantizer_compute_codesr  r   r   r   r    s    zProductQuantizer.compute_codesc                 C   s   t | |||S )zn
        speed up code assignment using assign_index
        (non-const because the index is changed)
        )r   Z0ProductQuantizer_compute_codes_with_assign_indexr  r   r   r   compute_codes_with_assign_index  s    z0ProductQuantizer.compute_codes_with_assign_indexc                 G   s   t j| g|R  S r#   )r   ZProductQuantizer_decoderQ   r   r   r   r    s    zProductQuantizer.decodec                 C   s   t | ||S )z|
        If we happen to have the distance tables precomputed, this is
        more efficient to compute the codes.
        )r   Z1ProductQuantizer_compute_code_from_distance_table)r   r`  r   r   r   r    compute_code_from_distance_table  s    z1ProductQuantizer.compute_code_from_distance_tablec                 C   s   t | ||S )a  
         Compute distance table for one vector.

        The distance table for x = [x_0 x_1 .. x_(M-1)] is a M * ksub
        matrix that contains

          dis_table (m, j) = || x_m - c_(m, j)||^2
          for m = 0..M-1 and j = 0 .. ksub - 1

        where c_(m, j) is the centroid no j of sub-quantizer m.

        :type x: float
        :param x:         input vector size d
        :type dis_table: float
        :param dis_table: output table, size M * ksub
        )r   Z'ProductQuantizer_compute_distance_tabler   r2   Z	dis_tabler   r   r   compute_distance_table  s    z'ProductQuantizer.compute_distance_tablec                 C   s   t | ||S r#   )r   Z)ProductQuantizer_compute_inner_prod_tabler  r   r   r   compute_inner_prod_table)  s    z)ProductQuantizer.compute_inner_prod_tablec                 C   s   t | |||S )a  
         compute distance table for several vectors
        :type nx: int
        :param nx:        nb of input vectors
        :type x: float
        :param x:         input vector size nx * d
        :param dis_table: output table, size nx * M * ksub
        )r   Z(ProductQuantizer_compute_distance_tablesr   r  r2   r:  r   r   r   compute_distance_tables,  s    	z(ProductQuantizer.compute_distance_tablesc                 C   s   t | |||S r#   )r   Z*ProductQuantizer_compute_inner_prod_tablesr  r   r   r   compute_inner_prod_tables7  s    z*ProductQuantizer.compute_inner_prod_tablesTc              	   C   s   t | ||||||S )aS  
         perform a search (L2 distance)
        :type x: float
        :param x:        query vectors, size nx * d
        :type nx: int
        :param nx:       nb of queries
        :type codes: uint8_t
        :param codes:    database codes, size ncodes * code_size
        :type ncodes: int
        :param ncodes:   nb of nb vectors
        :type res: :py:class:`float_maxheap_array_t`
        :param res:      heap array to store results (nh == nx)
        :type init_finalize_heap: boolean, optional
        :param init_finalize_heap:  initialize heap (input) and sort (output)?
        )r   ZProductQuantizer_searchr   r2   r  r   r   resinit_finalize_heapr   r   r   ru  :  s    zProductQuantizer.searchc              	   C   s   t | ||||||S )z2 same as search, but with inner product similarity)r   ZProductQuantizer_search_ipr  r   r   r   	search_ipL  s    zProductQuantizer.search_ipz Symmetric Distance Tablec                 C   s
   t | S r#   )r   Z"ProductQuantizer_compute_sdc_tabler@   r   r   r   compute_sdc_tableQ  s    z"ProductQuantizer.compute_sdc_tablec              	   C   s   t | ||||||S r#   )r   ZProductQuantizer_search_sdc)r   Zqcodesr   Zbcodesr   r  r  r   r   r   
search_sdcT  s    zProductQuantizer.search_sdcc                 C   s
   t | S )z
        Sync transposed centroids with regular centroids. This call
        is needed if centroids were edited directly.
        )r   Z*ProductQuantizer_sync_transposed_centroidsr@   r   r   r   sync_transposed_centroidsW  s    z*ProductQuantizer.sync_transposed_centroidsc                 C   s
   t | S )z= Clear transposed centroids table so ones are no longer used.)r   Z+ProductQuantizer_clear_transposed_centroidsr@   r   r   r   clear_transposed_centroids^  s    z+ProductQuantizer.clear_transposed_centroidsN)T)T)T)Mr   r   r,   r-   r   r   r   r   r   ZProductQuantizer_M_getZProductQuantizer_M_setMZProductQuantizer_nbits_getZProductQuantizer_nbits_setr   ZProductQuantizer_dsub_getZProductQuantizer_dsub_setZdsubZProductQuantizer_ksub_getZProductQuantizer_ksub_setr8  ZProductQuantizer_verbose_getZProductQuantizer_verbose_setr   ZProductQuantizer_Train_defaultTrain_defaultZ ProductQuantizer_Train_hot_startZTrain_hot_startZProductQuantizer_Train_sharedZTrain_sharedZ ProductQuantizer_Train_hypercubeZTrain_hypercubeZ$ProductQuantizer_Train_hypercube_pcaZTrain_hypercube_pcaZProductQuantizer_train_type_getZProductQuantizer_train_type_set
train_typeZProductQuantizer_cp_getZProductQuantizer_cp_setcpZ!ProductQuantizer_assign_index_getZ!ProductQuantizer_assign_index_setZassign_indexZProductQuantizer_centroids_getZProductQuantizer_centroids_setr9  Z)ProductQuantizer_transposed_centroids_getZ)ProductQuantizer_transposed_centroids_setZtransposed_centroidsZ)ProductQuantizer_centroids_sq_lengths_getZ)ProductQuantizer_centroids_sq_lengths_setZcentroids_sq_lengthsr  rn  r?   r  r  r  r  r  r  r  r  r  r  r  ru  r  ZProductQuantizer_sdc_table_getZProductQuantizer_sdc_table_setZ	sdc_tabler  r  r  r  Zdelete_ProductQuantizerrT   r   r   r   r   r  
  sR   


r  c                   @   sz   e Zd Zedd dd ddZeZeejej	ddZ
eejejZeejddZeejejZdd	d
Zdd ZejZdS )PQEncoderGenericc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   f  r4   zPQEncoderGeneric.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   f  r4   r7   r8   z code for this vectorz& number of bits per subquantizer indexr   c                 C   s   t | t ||| d S r#   )r   ZPQEncoderGeneric_swiginitZnew_PQEncoderGeneric)r   r   r   offsetr   r   r   r?   m  s    zPQEncoderGeneric.__init__c                 C   s   t | |S r#   )r   ZPQEncoderGeneric_encoderE   r   r   r   encodep  s    zPQEncoderGeneric.encodeN)r   )r   r   r,   r   r   r   r   r   ZPQEncoderGeneric_code_getZPQEncoderGeneric_code_setr   ZPQEncoderGeneric_offset_getZPQEncoderGeneric_offset_setr  ZPQEncoderGeneric_nbits_getr   ZPQEncoderGeneric_reg_getZPQEncoderGeneric_reg_setregr?   r  Zdelete_PQEncoderGenericrT   r   r   r   r   r  e  s   
r  c                   @   sJ   e Zd Zedd dd ddZeZeejej	Z
dd Zdd	 ZejZd
S )
PQEncoder8c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   w  r4   zPQEncoder8.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   w  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZPQEncoder8_swiginitZnew_PQEncoder8r   r   r   r   r   r   r?   {  s    zPQEncoder8.__init__c                 C   s   t | |S r#   )r   ZPQEncoder8_encoderE   r   r   r   r  ~  s    zPQEncoder8.encodeN)r   r   r,   r   r   r   r   r   ZPQEncoder8_code_getZPQEncoder8_code_setr   r?   r  Zdelete_PQEncoder8rT   r   r   r   r   r  v  s   r  c                   @   sJ   e Zd Zedd dd ddZeZeejej	Z
dd Zdd	 ZejZd
S )PQEncoder16c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zPQEncoder16.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZPQEncoder16_swiginitZnew_PQEncoder16r  r   r   r   r?     s    zPQEncoder16.__init__c                 C   s   t | |S r#   )r   ZPQEncoder16_encoderE   r   r   r   r    s    zPQEncoder16.encodeN)r   r   r,   r   r   r   r   r   ZPQEncoder16_code_getZPQEncoder16_code_setr   r?   r  Zdelete_PQEncoder16rT   r   r   r   r   r    s   r  c                   @   sz   e Zd Zedd dd ddZeZeejej	Z
eejejZeejZeejZeejejZdd Zdd	 ZejZd
S )PQDecoderGenericc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zPQDecoderGeneric.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZPQDecoderGeneric_swiginitZnew_PQDecoderGenericr  r   r   r   r?     s    zPQDecoderGeneric.__init__c                 C   s
   t | S r#   )r   ZPQDecoderGeneric_decoder@   r   r   r   r    s    zPQDecoderGeneric.decodeN)r   r   r,   r   r   r   r   r   ZPQDecoderGeneric_code_getZPQDecoderGeneric_code_setr   ZPQDecoderGeneric_offset_getZPQDecoderGeneric_offset_setr  ZPQDecoderGeneric_nbits_getr   ZPQDecoderGeneric_mask_getr   ZPQDecoderGeneric_reg_getZPQDecoderGeneric_reg_setr  r?   r  Zdelete_PQDecoderGenericrT   r   r   r   r   r    s   

r  c                   @   sP   e Zd Zedd dd ddZeZejZ	eej
ejZdd Zdd	 ZejZd
S )
PQDecoder8c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zPQDecoder8.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZPQDecoder8_swiginitZnew_PQDecoder8r  r   r   r   r?     s    zPQDecoder8.__init__c                 C   s
   t | S r#   )r   ZPQDecoder8_decoder@   r   r   r   r    s    zPQDecoder8.decodeN)r   r   r,   r   r   r   r   r   ZPQDecoder8_nbitsr   ZPQDecoder8_code_getZPQDecoder8_code_setr   r?   r  Zdelete_PQDecoder8rT   r   r   r   r   r    s   r  c                   @   sP   e Zd Zedd dd ddZeZejZ	eej
ejZdd Zdd	 ZejZd
S )PQDecoder16c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zPQDecoder16.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZPQDecoder16_swiginitZnew_PQDecoder16r  r   r   r   r?     s    zPQDecoder16.__init__c                 C   s
   t | S r#   )r   ZPQDecoder16_decoder@   r   r   r   r    s    zPQDecoder16.decodeN)r   r   r,   r   r   r   r   r   ZPQDecoder16_nbitsr   ZPQDecoder16_code_getZPQDecoder16_code_setr   r?   r  Zdelete_PQDecoder16rT   r   r   r   r   r     s   r   c                   @   s<  e Zd ZdZedd dd ddZdd ZeZee	j
e	jd	dZee	je	jd
dZee	je	jddZee	je	jddZee	je	jddZee	je	jddZee	je	jddZee	je	j ddZ!ee	j"e	j#ddZ$ee	j%e	j&ddZ'ee	j(e	j)ddZ*ee	j+e	j,ddZ-dd Z.ee	j/e	j0ddZ1ee	j2e	j3ddZ4ee	j5e	j6ddZ7dd Z8dd Z9dd Z:e	j;Z<e	j=Z>e	j?Z@e	jAZBe	jCZDe	jEZFe	jGZHe	jIZJe	jKZLe	jMZNd d! ZOd"d# ZPd$d% ZQd=d'd(ZRd>d*d+ZSd,d- ZTd?d.d/ZUee	jVe	jWd0dZXee	jYe	jZd1dZ[ee	j\e	j]Z^d2d3 Z_d@d5d6Z`d7d8 Zad9d: Zbd;d< Zce	jdZed&S )AAdditiveQuantizerz
     Abstract structure for additive quantizers

    Different from the product quantizer in which the decoded vector is the
    concatenation of M sub-vectors, additive quantizers sum M sub-vectors
    to get the decoded vector.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zAdditiveQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zAdditiveQuantizer.__init__z number of codebooksz bits for each stepz
 codebooksz
    codebook #1 is stored in rows codebook_offsets[i]:codebook_offsets[i+1]
    in the codebooks table of size total_codebook_size by d
    z' total number of bits (indexes + norms)z bits allocated for the normsz  size of the codebook in vectorsz' are all nbits = 8 (use faster decoder)r  z is trained or notzt
    auxiliary data for ST_norm_lsq2x4 and ST_norm_rq2x4
    store norms of codebook entries for 4-bit fastscan
    z store and search normsc                 C   s
   t | S r#   )r   Z)AdditiveQuantizer_compute_codebook_tablesr@   r   r   r   compute_codebook_tables  s    z)AdditiveQuantizer.compute_codebook_tablesz9 norms of all codebook entries (size total_codebook_size)z
    dot products of all codebook entries with the previous codebooks
    size sum(codebook_offsets[m] * 2^nbits[m], m=0..M-1)
    z
    norms and distance matrixes with beam search can get large, so use this
    to control for the amount of memory that can be allocated
    c                 C   s   t | |S )z" encode a norm into norm_bits bits)r   ZAdditiveQuantizer_encode_norm)r   normr   r   r   encode_norm  s    zAdditiveQuantizer.encode_normc                 C   s   t | |S )z/ encode norm by non-uniform scalar quantization)r   ZAdditiveQuantizer_encode_qcintrE   r   r   r   encode_qcint  s    zAdditiveQuantizer.encode_qcintc                 C   s   t | |S )z/ decode norm by non-uniform scalar quantization)r   ZAdditiveQuantizer_decode_qcintr   r%  r   r   r   decode_qcint  s    zAdditiveQuantizer.decode_qcintc                 C   s
   t | S )z Train the norm quantizer)r   Z$AdditiveQuantizer_set_derived_valuesr@   r   r   r   r    s    z$AdditiveQuantizer.set_derived_valuesc                 C   s   t | ||S r#   )r   ZAdditiveQuantizer_train_norm)r   rB   r  r   r   r   
train_norm  s    zAdditiveQuantizer.train_normc                 C   s   t | |||S r#   )r   ZAdditiveQuantizer_compute_codesr  r   r   r   r    s    zAdditiveQuantizer.compute_codesNc                 C   s   t | ||||S a7  
         Encode a set of vectors

        :type x: float
        :param x:      vectors to encode, size n * d
        :type codes: uint8_t
        :param codes:  output codes, size n * code_size
        :type centroids: float, optional
        :param centroids:  centroids to be added to x, size n * d
        )r   Z-AdditiveQuantizer_compute_codes_add_centroidsr   r2   r   rB   r9  r   r   r   compute_codes_add_centroids   s    z-AdditiveQuantizer.compute_codes_add_centroidsr
  c              	   C   s   t | ||||||S )an  
         pack a series of code to bit-compact format

        :type codes: int
        :param codes:        codes to be packed, size n * code_size
        :type packed_codes: uint8_t
        :param packed_codes: output bit-compact codes
        :type ld_codes: int, optional
        :param ld_codes:     leading dimension of codes
        :type norms: float, optional
        :param norms:        norms of the vectors (size n). Will be computed if
                                needed but not provided
        :type centroids: float, optional
        :param centroids:    centroids to be added to x, size n * d
        )r   ZAdditiveQuantizer_pack_codes)r   rB   r   Zpacked_codesld_codesr  r9  r   r   r   
pack_codes-  s    zAdditiveQuantizer.pack_codesc                 C   s   t | |||S z
         Decode a set of vectors

        :type codes: uint8_t
        :param codes:  codes to decode, size n * code_size
        :type x: float
        :param x:      output vectors, size n * d
        )r   ZAdditiveQuantizer_decoder   r   r2   rB   r   r   r   r  ?  s    	zAdditiveQuantizer.decodec                 C   s   t | ||||S z
         Decode a set of vectors in non-packed format

        :type codes: int
        :param codes:  codes to decode, size n * ld_codes
        :type x: float
        :param x:      output vectors, size n * d
        )r   Z!AdditiveQuantizer_decode_unpackedr   r   r2   rB   r  r   r   r   decode_unpackedJ  s    	z!AdditiveQuantizer.decode_unpackedz$ Also determines what's in the codesz" min/max for quantization of normsc                 C   s   t | ||S )z. decoding function for a code in a 64-bit word)r   ZAdditiveQuantizer_decode_64bitrm  r   r   r   decode_64bitX  s    zAdditiveQuantizer.decode_64bit      ?c                 C   s   t | |||||S )a  
         Compute inner-product look-up tables. Used in the centroid search
        functions.

        :type xq: float
        :param xq:     query vector, size (n, d)
        :type LUT: float
        :param LUT:    look-up table, size (n, total_codebook_size)
        :type alpha: float, optional
        :param alpha:  compute alpha * inner-product
        :type ld_lut: int, optional
        :param ld_lut:  leading dimension of LUT
        )r   ZAdditiveQuantizer_compute_LUTr   rB   r  LUTalphaZld_lutr   r   r   compute_LUT\  s    zAdditiveQuantizer.compute_LUTc                 C   s   t | |||||S )z exact IP search)r   Z-AdditiveQuantizer_knn_centroids_inner_product)r   rB   r  r   r   r   r   r   r   knn_centroids_inner_productl  s    z-AdditiveQuantizer.knn_centroids_inner_productc                 C   s   t | |S )z
         For L2 search we need the L2 norms of the centroids

        :type norms: float
        :param norms:    output norms table, size total_codebook_size
        )r   Z(AdditiveQuantizer_compute_centroid_normsr   r  r   r   r   compute_centroid_normsp  s    z(AdditiveQuantizer.compute_centroid_normsc              	   C   s   t | ||||||S )z( Exact L2 search, with precomputed norms)r   Z"AdditiveQuantizer_knn_centroids_L2)r   rB   r  r   r   r   centroid_normsr   r   r   knn_centroids_L2y  s    z"AdditiveQuantizer.knn_centroids_L2)N)r
  NN)r
  )r  r
  )fr   r   r,   r-   r   r   r?   r   r   r   ZAdditiveQuantizer_M_getZAdditiveQuantizer_M_setr  ZAdditiveQuantizer_nbits_getZAdditiveQuantizer_nbits_setr   ZAdditiveQuantizer_codebooks_getZAdditiveQuantizer_codebooks_setZ	codebooksZ&AdditiveQuantizer_codebook_offsets_getZ&AdditiveQuantizer_codebook_offsets_setZcodebook_offsetsZAdditiveQuantizer_tot_bits_getZAdditiveQuantizer_tot_bits_setZtot_bitsZAdditiveQuantizer_norm_bits_getZAdditiveQuantizer_norm_bits_setZ	norm_bitsZ)AdditiveQuantizer_total_codebook_size_getZ)AdditiveQuantizer_total_codebook_size_setZtotal_codebook_sizeZAdditiveQuantizer_only_8bit_getZAdditiveQuantizer_only_8bit_setZ	only_8bitZAdditiveQuantizer_verbose_getZAdditiveQuantizer_verbose_setr   Z AdditiveQuantizer_is_trained_getZ AdditiveQuantizer_is_trained_setr  ZAdditiveQuantizer_norm_tabs_getZAdditiveQuantizer_norm_tabs_setZ	norm_tabsZAdditiveQuantizer_qnorm_getZAdditiveQuantizer_qnorm_setZqnormr  Z$AdditiveQuantizer_centroid_norms_getZ$AdditiveQuantizer_centroid_norms_setr  Z-AdditiveQuantizer_codebook_cross_products_getZ-AdditiveQuantizer_codebook_cross_products_setZcodebook_cross_productsZ'AdditiveQuantizer_max_mem_distances_getZ'AdditiveQuantizer_max_mem_distances_setZmax_mem_distancesr  r  r  ZAdditiveQuantizer_ST_decompressZST_decompressZAdditiveQuantizer_ST_LUT_nonormZST_LUT_nonormZ"AdditiveQuantizer_ST_norm_from_LUTZST_norm_from_LUTZAdditiveQuantizer_ST_norm_floatZST_norm_floatZAdditiveQuantizer_ST_norm_qint8ZST_norm_qint8ZAdditiveQuantizer_ST_norm_qint4ZST_norm_qint4Z AdditiveQuantizer_ST_norm_cqint8ZST_norm_cqint8Z AdditiveQuantizer_ST_norm_cqint4ZST_norm_cqint4Z AdditiveQuantizer_ST_norm_lsq2x4ZST_norm_lsq2x4ZAdditiveQuantizer_ST_norm_rq2x4ZST_norm_rq2x4r  r  r  r  r  r  r  Z!AdditiveQuantizer_search_type_getZ!AdditiveQuantizer_search_type_setsearch_typeZAdditiveQuantizer_norm_min_getZAdditiveQuantizer_norm_min_setZnorm_minZAdditiveQuantizer_norm_max_getZAdditiveQuantizer_norm_max_setZnorm_maxr  r  r  r  r  Zdelete_AdditiveQuantizerrT   r   r   r   r   r    sb   




	r  c                  G   s
   t j|  S )a  
     Encode a residual by sampling from a centroid table.

    This is a single encoding step the residual quantizer.
    It allows low-level access to the encoding function, exposed mainly for unit
    tests.

    :type n: int
    :param n:              number of vectors to handle
    :type residuals: float
    :param residuals:      vectors to encode, size (n, beam_size, d)
    :type cent: float
    :param cent:           centroids, size (K, d)
    :type beam_size: int
    :param beam_size:      input beam size
    :type m: int
    :param m:              size of the codes for the previous encoding steps
    :type codes: int
    :param codes:          code array for the previous steps of the beam (n,
        beam_size, m)
    :type new_beam_size: int
    :param new_beam_size:  output beam size (should be <= K * beam_size)
    :type new_codes: int
    :param new_codes:      output codes, size (n, new_beam_size, m + 1)
    :type new_residuals: float
    :param new_residuals:  output residuals, size (n, new_beam_size, d)
    :type new_distances: float
    :param new_distances:  output distances, size (n, new_beam_size)
    :type assign_index: :py:class:`Index`, optional
    :param assign_index:   if non-NULL, will be used to perform assignment
    )r   beam_search_encode_stepr   r   r   r   r    s     r  c                  G   s
   t j|  S )a  
     Encode a set of vectors using their dot products with the codebooks

    :type K: int
    :param K:           number of vectors in the codebook
    :type n: int
    :param n:           nb of vectors to encode
    :type beam_size: int
    :param beam_size:   input beam size
    :type codebook_cross_norms: float
    :param codebook_cross_norms: inner product of this codebook with the m
                                    previously encoded codebooks
    :type codebook_offsets: int
    :param codebook_offsets:     offsets into codebook_cross_norms for each
                                    previous codebook
    :type query_cp: float
    :param query_cp:    dot products of query vectors with ???
    :type cent_norms_i: float
    :param cent_norms_i:  norms of centroids
    )r   beam_search_encode_step_tabr   r   r   r   r     s    r   c                   @   sz   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZeejejZdd ZejZdS )	RefineBeamMemoryPoolc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zRefineBeamMemoryPool.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZRefineBeamMemoryPool_swiginitZnew_RefineBeamMemoryPoolr@   r   r   r   r?     s    zRefineBeamMemoryPool.__init__N)r   r   r,   r   r   r   r   r   Z"RefineBeamMemoryPool_new_codes_getZ"RefineBeamMemoryPool_new_codes_set	new_codesZ&RefineBeamMemoryPool_new_residuals_getZ&RefineBeamMemoryPool_new_residuals_setnew_residualsZ"RefineBeamMemoryPool_residuals_getZ"RefineBeamMemoryPool_residuals_setr  ZRefineBeamMemoryPool_codes_getZRefineBeamMemoryPool_codes_setr   Z"RefineBeamMemoryPool_distances_getZ"RefineBeamMemoryPool_distances_setr   r?   Zdelete_RefineBeamMemoryPoolrT   r   r   r   r   r!    s   r!  c	           	      C   s   t | ||||||||	S r#   )r   refine_beam_mp)	rqrB   	beam_sizer2   out_beam_size	out_codesZout_residualsout_distancespoolr   r   r   r$    s    r$  c                   @   sl   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZdd ZejZdS )	RefineBeamLUTMemoryPoolc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z RefineBeamLUTMemoryPool.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   Z RefineBeamLUTMemoryPool_swiginitZnew_RefineBeamLUTMemoryPoolr@   r   r   r   r?     s    z RefineBeamLUTMemoryPool.__init__N)r   r   r,   r   r   r   r   r   Z%RefineBeamLUTMemoryPool_new_codes_getZ%RefineBeamLUTMemoryPool_new_codes_setr"  Z)RefineBeamLUTMemoryPool_new_distances_getZ)RefineBeamLUTMemoryPool_new_distances_setnew_distancesZ!RefineBeamLUTMemoryPool_codes_getZ!RefineBeamLUTMemoryPool_codes_setr   Z%RefineBeamLUTMemoryPool_distances_getZ%RefineBeamLUTMemoryPool_distances_setr   r?   Zdelete_RefineBeamLUTMemoryPoolrT   r   r   r   r   r+    s   r+  c              
   C   s   t | |||||||S r#   )r   refine_beam_LUT_mp)r%  rB   query_normsquery_cpr'  r(  r)  r*  r   r   r   r-    s    r-  c                   @   sz   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZeejejZdd ZejZdS )	&ComputeCodesAddCentroidsLUT0MemoryPoolc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z/ComputeCodesAddCentroidsLUT0MemoryPool.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   Z/ComputeCodesAddCentroidsLUT0MemoryPool_swiginitZ*new_ComputeCodesAddCentroidsLUT0MemoryPoolr@   r   r   r   r?     s    z/ComputeCodesAddCentroidsLUT0MemoryPool.__init__N)r   r   r,   r   r   r   r   r   Z0ComputeCodesAddCentroidsLUT0MemoryPool_codes_getZ0ComputeCodesAddCentroidsLUT0MemoryPool_codes_setr   Z0ComputeCodesAddCentroidsLUT0MemoryPool_norms_getZ0ComputeCodesAddCentroidsLUT0MemoryPool_norms_setr  Z4ComputeCodesAddCentroidsLUT0MemoryPool_distances_getZ4ComputeCodesAddCentroidsLUT0MemoryPool_distances_setr   Z4ComputeCodesAddCentroidsLUT0MemoryPool_residuals_getZ4ComputeCodesAddCentroidsLUT0MemoryPool_residuals_setr  Z;ComputeCodesAddCentroidsLUT0MemoryPool_refine_beam_pool_getZ;ComputeCodesAddCentroidsLUT0MemoryPool_refine_beam_pool_setZrefine_beam_poolr?   Z-delete_ComputeCodesAddCentroidsLUT0MemoryPoolrT   r   r   r   r   r0    s   r0  c                 C   s   t | |||||S r#   )r   #compute_codes_add_centroids_mp_lut0r%  r2   Z	codes_outrB   r9  r*  r   r   r   r1    s    r1  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZeejejZeejejZdd ZejZdS )	&ComputeCodesAddCentroidsLUT1MemoryPoolc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z/ComputeCodesAddCentroidsLUT1MemoryPool.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   Z/ComputeCodesAddCentroidsLUT1MemoryPool_swiginitZ*new_ComputeCodesAddCentroidsLUT1MemoryPoolr@   r   r   r   r?     s    z/ComputeCodesAddCentroidsLUT1MemoryPool.__init__N)r   r   r,   r   r   r   r   r   Z0ComputeCodesAddCentroidsLUT1MemoryPool_codes_getZ0ComputeCodesAddCentroidsLUT1MemoryPool_codes_setr   Z4ComputeCodesAddCentroidsLUT1MemoryPool_distances_getZ4ComputeCodesAddCentroidsLUT1MemoryPool_distances_setr   Z6ComputeCodesAddCentroidsLUT1MemoryPool_query_norms_getZ6ComputeCodesAddCentroidsLUT1MemoryPool_query_norms_setr.  Z3ComputeCodesAddCentroidsLUT1MemoryPool_query_cp_getZ3ComputeCodesAddCentroidsLUT1MemoryPool_query_cp_setr/  Z4ComputeCodesAddCentroidsLUT1MemoryPool_residuals_getZ4ComputeCodesAddCentroidsLUT1MemoryPool_residuals_setr  Z?ComputeCodesAddCentroidsLUT1MemoryPool_refine_beam_lut_pool_getZ?ComputeCodesAddCentroidsLUT1MemoryPool_refine_beam_lut_pool_setZrefine_beam_lut_poolr?   Z-delete_ComputeCodesAddCentroidsLUT1MemoryPoolrT   r   r   r   r   r3    s   r3  c                 C   s   t | |||||S r#   )r   #compute_codes_add_centroids_mp_lut1r2  r   r   r   r4    s    r4  c                   @   s  e Zd ZdZedd dd ddZeZeej	ej
ddZejZejZejZeejejddZejZejZeejejd	dZeejejd
dZeejej ddZ!eej"ej#ddZ$eej%ej&ddZ'dd Z(dd Z)d!ddZ*dd Z+d"ddZ,d#ddZ-d$ddZ.d%dd Z/ej0Z1dS )&ResidualQuantizera  
     Residual quantizer with variable number of bits per sub-quantizer

    The residual centroids are stored in a big cumulative centroid table.
    The codes are represented either as a non-compact table of size (n, M) or
    as the compact output (n, code_size).
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   
  r4   zResidualQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   
  r4   r7   r8   z% Binary or of the Train_* flags belowz. number of iterations for codebook refinement.z- beam size used for training and for encodingz use LUT for beam searchza
    Currently used mode of approximate min-k computations.
    Default value is EXACT_TOPK.
    z clustering parametersz+ if non-NULL, use this index for assignmentc                 G   s   t | t j|  d S r#   )r   ZResidualQuantizer_swiginitZnew_ResidualQuantizerrQ   r   r   r   r?   '  s    zResidualQuantizer.__init__c                 C   s   t | ||S )z Train the residual quantizer)r   ZResidualQuantizer_trainrm  r   r   r   rn  *  s    zResidualQuantizer.trainr   c                 C   s   t | ||S )z< Copy the M codebook levels from other, starting from skip_M)r   Z!ResidualQuantizer_initialize_from)r   r_   Zskip_Mr   r   r   initialize_from.  s    z!ResidualQuantizer.initialize_fromc                 C   s   t | ||S )a  
         Encode the vectors and compute codebook that minimizes the quantization
        error on these codes

        :type x: float
        :param x:      training vectors, size n * d
        :type n: int
        :param n:      nb of training vectors, n >= total_codebook_size
        :rtype: float
        :return: returns quantization error for the new codebook with old
            codes
        )r   Z%ResidualQuantizer_retrain_AQ_codebookrm  r   r   r   retrain_AQ_codebook2  s    z%ResidualQuantizer.retrain_AQ_codebookNc                 C   s   t | ||||S r	  )r   Z-ResidualQuantizer_compute_codes_add_centroidsr
  r   r   r   r  A  s    z-ResidualQuantizer.compute_codes_add_centroidsc              
   C   s   t | |||||||S )a  
         lower-level encode function

        :type n: int
        :param n:              number of vectors to handle
        :type residuals: float
        :param residuals:      vectors to encode, size (n, beam_size, d)
        :type beam_size: int
        :param beam_size:      input beam size
        :type new_beam_size: int
        :param new_beam_size:  output beam size (should be <= K * beam_size)
        :type new_codes: int
        :param new_codes:      output codes, size (n, new_beam_size, m + 1)
        :type new_residuals: float, optional
        :param new_residuals:  output residuals, size (n, new_beam_size, d)
        :type new_distances: float, optional
        :param new_distances:  output distances, size (n, new_beam_size)
        )r   ZResidualQuantizer_refine_beam)r   rB   r&  r  new_beam_sizer"  r#  r,  r   r   r   refine_beamN  s    zResidualQuantizer.refine_beamc              	   C   s   t | ||||||S r#   )r   Z!ResidualQuantizer_refine_beam_LUT)r   rB   r.  r/  r8  r"  r,  r   r   r   refine_beam_LUTc  s    z!ResidualQuantizer.refine_beam_LUTr
  c                 C   s   t | |S )z
         Beam search can consume a lot of memory. This function estimates the
        amount of mem used by refine_beam to adjust the batch size

        :type beam_size: int, optional
        :param beam_size:  if != -1, override the beam size
        )r   Z"ResidualQuantizer_memory_per_point)r   r&  r   r   r   memory_per_pointf  s    z"ResidualQuantizer.memory_per_point)r   )N)NN)N)r
  )2r   r   r,   r-   r   r   r   r   r   Z ResidualQuantizer_train_type_getZ ResidualQuantizer_train_type_setr  ZResidualQuantizer_Train_defaultr  Z'ResidualQuantizer_Train_progressive_dimZTrain_progressive_dimZ'ResidualQuantizer_Train_refine_codebookZTrain_refine_codebookZ+ResidualQuantizer_niter_codebook_refine_getZ+ResidualQuantizer_niter_codebook_refine_setZniter_codebook_refineZ ResidualQuantizer_Train_top_beamZTrain_top_beamZ&ResidualQuantizer_Skip_codebook_tablesZSkip_codebook_tablesZ#ResidualQuantizer_max_beam_size_getZ#ResidualQuantizer_max_beam_size_setZmax_beam_sizeZ"ResidualQuantizer_use_beam_LUT_getZ"ResidualQuantizer_use_beam_LUT_setZuse_beam_LUTZ&ResidualQuantizer_approx_topk_mode_getZ&ResidualQuantizer_approx_topk_mode_setapprox_topk_modeZResidualQuantizer_cp_getZResidualQuantizer_cp_setr  Z*ResidualQuantizer_assign_index_factory_getZ*ResidualQuantizer_assign_index_factory_setZassign_index_factoryr?   rn  r6  r7  r  r9  r:  r;  Zdelete_ResidualQuantizerrT   r   r   r   r   r5    s0   




	r5  c                   @   sd  e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZeejejddZeejejddZeejejddZ eej!ej"ddZ#eej$ej%ddZ&eej'ej(Z)eej*ej+Z,dd Z-ej.Z/dd Z0d*ddZ1dd Z2dd Z3dd Z4dd Z5d d! Z6d"d# Z7d$d% Z8d&d' Z9d+d(d)Z:dS ),LocalSearchQuantizeraK  
     Implementation of LSQ/LSQ++ described in the following two papers:

    Revisiting additive quantization
    Julieta Martinez, et al. ECCV 2016

    LSQ++: Lower running time and higher recall in multi-codebook quantization
    Julieta Martinez, et al. ECCV 2018

    This implementation is mostly translated from the Julia implementations
    by Julieta Martinez:
    (https://github.com/una-dinosauria/local-search-quantization,
     https://github.com/una-dinosauria/Rayuela.jl)

    The trained codes are stored in `codebooks` which is called
    `centroids` in PQ and RQ.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zLocalSearchQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z number of codes per codebookz! number of iterations in trainingz' iterations of local search in encodingz' iterations of local search in trainingz number of iterations in icmz temperature factorz regularization factorz" nb of vectors to encode at a timez seed for random generatorzo
    number of perturbation in each code
    if non-NULL, use this encoder to encode (owned by the object)
    c                 G   s   t | t j|  d S r#   )r   ZLocalSearchQuantizer_swiginitZnew_LocalSearchQuantizerrQ   r   r   r   r?     s    zLocalSearchQuantizer.__init__c                 C   s   t | ||S r#   )r   ZLocalSearchQuantizer_trainrm  r   r   r   rn    s    zLocalSearchQuantizer.trainNc                 C   s   t | ||||S )au  
         Encode a set of vectors

        :type x: float
        :param x:      vectors to encode, size n * d
        :type codes: uint8_t
        :param codes:  output codes, size n * code_size
        :type n: int
        :param n:      number of vectors
        :type centroids: float, optional
        :param centroids:  centroids to be added to x, size n * d
        )r   Z0LocalSearchQuantizer_compute_codes_add_centroidsr
  r   r   r   r    s    z0LocalSearchQuantizer.compute_codes_add_centroidsc                 C   s   t | |||S )a  
         Update codebooks given encodings

        :type x: float
        :param x:      training vectors, size n * d
        :type codes: int
        :param codes:  encoded training vectors, size n * M
        :type n: int
        :param n:      number of vectors
        )r   Z%LocalSearchQuantizer_update_codebooksr  r   r   r   update_codebooks  s    z%LocalSearchQuantizer.update_codebooksc                 C   s   t | |||||S )a  
         Encode vectors given codebooks using iterative conditional mode (icm).

        :type codes: int
        :param codes:     output codes, size n * M
        :type x: float
        :param x:         vectors to encode, size n * d
        :type n: int
        :param n:         number of vectors
        :type ils_iters: int
        :param ils_iters: number of iterations of iterative local search
        )r   ZLocalSearchQuantizer_icm_encode)r   r   r2   rB   	ils_itersgenr   r   r   
icm_encode  s    zLocalSearchQuantizer.icm_encodec              
   C   s   t | |||||||S r#   )r   Z$LocalSearchQuantizer_icm_encode_impl)r   r   r2   unariesr@  rB   r?  r   r   r   r   icm_encode_impl  s    z$LocalSearchQuantizer.icm_encode_implc                 C   s   t | |||||S r#   )r   Z$LocalSearchQuantizer_icm_encode_step)r   r   rB  binariesrB   Zn_itersr   r   r   icm_encode_step  s    z$LocalSearchQuantizer.icm_encode_stepc                 C   s   t | |||S )z
         Add some perturbation to codes

        :type codes: int
        :param codes: codes to be perturbed, size n * M
        :type n: int
        :param n:     number of vectors
        )r   Z"LocalSearchQuantizer_perturb_codes)r   r   rB   r@  r   r   r   perturb_codes  s    	z"LocalSearchQuantizer.perturb_codesc                 C   s   t | |||S )a  
         Add some perturbation to codebooks

        :type T: float
        :param T:         temperature of simulated annealing
        :type stddev: std::vector< float >
        :param stddev:    standard derivations of each dimension in training data
        )r   Z&LocalSearchQuantizer_perturb_codebooks)r   TZstddevr@  r   r   r   perturb_codebooks  s    	z&LocalSearchQuantizer.perturb_codebooksc                 C   s   t | |S )z
         Compute binary terms

        :type binaries: float
        :param binaries: binary terms, size M * M * K * K
        )r   Z)LocalSearchQuantizer_compute_binary_terms)r   rD  r   r   r   compute_binary_terms  s    z)LocalSearchQuantizer.compute_binary_termsc                 C   s   t | |||S )a  
         Compute unary terms

        :type n: int
        :param n:       number of vectors
        :type x: float
        :param x:       vectors to encode, size n * d
        :type unaries: float
        :param unaries: unary terms, size n * M * K
        )r   Z(LocalSearchQuantizer_compute_unary_terms)r   r2   rB  rB   r   r   r   compute_unary_terms  s    z(LocalSearchQuantizer.compute_unary_termsc                 C   s   t | ||||S )a  
         Helper function to compute reconstruction error

        :type codes: int
        :param codes: encoded codes, size n * M
        :type x: float
        :param x:     vectors to encode, size n * d
        :type n: int
        :param n:     number of vectors
        :type objs: float, optional
        :param objs:  if it is not null, store reconstruction
                                error of each vector into it, size n
        )r   ZLocalSearchQuantizer_evaluate)r   r   r2   rB   Zobjsr   r   r   evaluate  s    zLocalSearchQuantizer.evaluate)N)N);r   r   r,   r-   r   r   r   r   r   ZLocalSearchQuantizer_K_getZLocalSearchQuantizer_K_setKZ$LocalSearchQuantizer_train_iters_getZ$LocalSearchQuantizer_train_iters_setZtrain_itersZ)LocalSearchQuantizer_encode_ils_iters_getZ)LocalSearchQuantizer_encode_ils_iters_setZencode_ils_itersZ(LocalSearchQuantizer_train_ils_iters_getZ(LocalSearchQuantizer_train_ils_iters_setZtrain_ils_itersZ"LocalSearchQuantizer_icm_iters_getZ"LocalSearchQuantizer_icm_iters_setZ	icm_itersZLocalSearchQuantizer_p_getZLocalSearchQuantizer_p_setpZLocalSearchQuantizer_lambd_getZLocalSearchQuantizer_lambd_setlambdZ#LocalSearchQuantizer_chunk_size_getZ#LocalSearchQuantizer_chunk_size_set
chunk_sizeZ$LocalSearchQuantizer_random_seed_getZ$LocalSearchQuantizer_random_seed_setrandom_seedZLocalSearchQuantizer_nperts_getZLocalSearchQuantizer_nperts_setZnpertsZ,LocalSearchQuantizer_icm_encoder_factory_getZ,LocalSearchQuantizer_icm_encoder_factory_setZicm_encoder_factoryZ5LocalSearchQuantizer_update_codebooks_with_double_getZ5LocalSearchQuantizer_update_codebooks_with_double_setZupdate_codebooks_with_doubler?   Zdelete_LocalSearchQuantizerrT   rn  r  r>  rA  rC  rE  rF  rH  rI  rJ  rK  r   r   r   r   r=  s  s8   
	r=  c                   @   sn   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZdd ZejZdd	 Zd
d ZdS )
IcmEncoderc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIcmEncoder.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t | d S r#   )r   ZIcmEncoder_swiginitZnew_IcmEncoderr   lsqr   r   r   r?     s    zIcmEncoder.__init__c                 C   s
   t | S r#   )r   ZIcmEncoder_set_binary_termr@   r   r   r   set_binary_term  s    zIcmEncoder.set_binary_termc                 C   s   t | |||||S )a  
         Encode vectors given codebooks

        :type codes: int
        :param codes:     output codes, size n * M
        :type x: float
        :param x:         vectors to encode, size n * d
        :type gen: std::mt19937
        :param gen:       random generator
        :type n: int
        :param n:         number of vectors
        :type ils_iters: int
        :param ils_iters: number of iterations of iterative local search
        )r   ZIcmEncoder_encode)r   r   r2   r@  rB   r?  r   r   r   r    s    zIcmEncoder.encodeN)r   r   r,   r   r   r   r   r   ZIcmEncoder_binaries_getZIcmEncoder_binaries_setrD  ZIcmEncoder_verbose_getZIcmEncoder_verbose_setr   ZIcmEncoder_lsq_getZIcmEncoder_lsq_setrS  r?   Zdelete_IcmEncoderrT   rT  r  r   r   r   r   rQ    s   rQ  c                   @   s<   e Zd Zedd dd ddZeZdd Zej	Z
dd	 Zd
S )IcmEncoderFactoryc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   0  r4   zIcmEncoderFactory.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   0  r4   r7   r8   c                 C   s   t | |S r#   )r   ZIcmEncoderFactory_getrR  r   r   r   get3  s    zIcmEncoderFactory.getc                 C   s   t | t   d S r#   )r   ZIcmEncoderFactory_swiginitZnew_IcmEncoderFactoryr@   r   r   r   r?   7  s    zIcmEncoderFactory.__init__N)r   r   r,   r   r   r   r   rV  r   Zdelete_IcmEncoderFactoryrT   r?   r   r   r   r   rU  /  s
   rU  c                   @   s^   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 Zdd Zdd ZejZdS )LSQTimerz]
    A helper struct to count consuming time during training.
    It is NOT thread-safe.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   B  r4   zLSQTimer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   B  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZLSQTimer_swiginitZnew_LSQTimerr@   r   r   r   r?   F  s    zLSQTimer.__init__c                 C   s   t | |S r#   )r   ZLSQTimer_getr   r   r   r   r   rV  I  s    zLSQTimer.getc                 C   s   t | ||S r#   )r   ZLSQTimer_add)r   r   deltar   r   r   ro  L  s    zLSQTimer.addc                 C   s
   t | S r#   )r   ZLSQTimer_resetr@   r   r   r   r|   O  s    zLSQTimer.resetN)r   r   r,   r-   r   r   r   r   r   ZLSQTimer_t_getZLSQTimer_t_settr?   rV  ro  r|   Zdelete_LSQTimerrT   r   r   r   r   rW  <  s   rW  c                   @   st   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZdd Zdd	 ZejZd
S )LSQTimerScopec                 C   s
   | j  S r#   r0   r1   r   r   r   r3   V  r4   zLSQTimerScope.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   V  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZLSQTimerScope_swiginitZnew_LSQTimerScope)r   timerr   r   r   r   r?   ]  s    zLSQTimerScope.__init__c                 C   s
   t | S r#   )r   ZLSQTimerScope_finishr@   r   r   r   finish`  s    zLSQTimerScope.finishN)r   r   r,   r   r   r   r   r   ZLSQTimerScope_t0_getZLSQTimerScope_t0_sett0ZLSQTimerScope_timer_getZLSQTimerScope_timer_setr\  ZLSQTimerScope_name_getZLSQTimerScope_name_setr   ZLSQTimerScope_finished_getZLSQTimerScope_finished_setfinishedr?   r]  Zdelete_LSQTimerScoperT   r   r   r   r   r[  U  s   r[  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejZdd	 ZejZd
d Zdd Zdd ZdddZdddZdddZdd Zd ddZdS )!ProductAdditiveQuantizera  
     Product Additive Quantizers

    The product additive quantizer is a variant of AQ and PQ.
    It first splits the vector space into multiple orthogonal sub-spaces
    just like PQ does. And then it quantizes each sub-space by an independent
    additive quantizer.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   p  r4   z!ProductAdditiveQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   p  r4   r7   r8   z- number of sub-vectors we split a vector intoc                 G   s   t | t j|  d S r#   )r   Z!ProductAdditiveQuantizer_swiginitZnew_ProductAdditiveQuantizerrQ   r   r   r   r?   u  s    z!ProductAdditiveQuantizer.__init__c                 C   s   t | |||S r#   )r   ZProductAdditiveQuantizer_init)r   r   Zaqsr  r   r   r   inity  s    zProductAdditiveQuantizer.initc                 C   s   t | |S )z% Train the product additive quantizer)r   Z%ProductAdditiveQuantizer_subquantizer)r   r   r   r   r   subquantizer|  s    z%ProductAdditiveQuantizer.subquantizerc                 C   s   t | ||S r#   )r   ZProductAdditiveQuantizer_trainrm  r   r   r   rn    s    zProductAdditiveQuantizer.trainNc                 C   s   t | ||||S r	  )r   Z4ProductAdditiveQuantizer_compute_codes_add_centroidsr
  r   r   r   r    s    z4ProductAdditiveQuantizer.compute_codes_add_centroidsc                 C   s   t | ||||S r#   )r   Z/ProductAdditiveQuantizer_compute_unpacked_codesr
  r   r   r   compute_unpacked_codes  s    z/ProductAdditiveQuantizer.compute_unpacked_codesr
  c                 C   s   t | ||||S r  )r   Z(ProductAdditiveQuantizer_decode_unpackedr  r   r   r   r    s    	z(ProductAdditiveQuantizer.decode_unpackedc                 C   s   t | |||S r  )r   ZProductAdditiveQuantizer_decoder  r   r   r   r    s    	zProductAdditiveQuantizer.decoder  c                 C   s   t | |||||S )a  
         Compute inner-product look-up tables. Used in the search functions.

        :type xq: float
        :param xq:     query vector, size (n, d)
        :type LUT: float
        :param LUT:    look-up table, size (n, total_codebook_size)
        :type alpha: float, optional
        :param alpha:  compute alpha * inner-product
        :type ld_lut: int, optional
        :param ld_lut:  leading dimension of LUT
        )r   Z$ProductAdditiveQuantizer_compute_LUTr  r   r   r   r    s    z$ProductAdditiveQuantizer.compute_LUT)N)N)r
  )r  r
  )r   r   r,   r-   r   r   r   r   r   Z$ProductAdditiveQuantizer_nsplits_getZ$ProductAdditiveQuantizer_nsplits_setZnsplitsZ'ProductAdditiveQuantizer_quantizers_getZ'ProductAdditiveQuantizer_quantizers_setZ
quantizersr?   Zdelete_ProductAdditiveQuantizerrT   ra  rb  rn  r  rc  r  r  r  r   r   r   r   r`  f  s   	


r`  c                   @   s8   e Zd ZdZedd dd ddZeZdd Ze	j
Zd	S )
ProductLocalSearchQuantizerz Product Local Search Quantizerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z$ProductLocalSearchQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z$ProductLocalSearchQuantizer_swiginitZnew_ProductLocalSearchQuantizerrQ   r   r   r   r?     s    z$ProductLocalSearchQuantizer.__init__N)r   r   r,   r-   r   r   r   r   r?   r   Z"delete_ProductLocalSearchQuantizerrT   r   r   r   r   rd    s
   rd  c                   @   s8   e Zd ZdZedd dd ddZeZdd Ze	j
Zd	S )
ProductResidualQuantizerz Product Residual Quantizerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z!ProductResidualQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z!ProductResidualQuantizer_swiginitZnew_ProductResidualQuantizerrQ   r   r   r   r?     s    z!ProductResidualQuantizer.__init__N)r   r   r,   r-   r   r   r   r   r?   r   Zdelete_ProductResidualQuantizerrT   r   r   r   r   re    s
   re  c                   @   s   e Zd ZdZedd dd ddZdd ZeZee	j
e	jZee	je	jZee	je	jZd	d
 Zdd Zdd Zdd Ze	jZdS )
CodePackera,  
    Packing consists in combining a fixed number of codes of constant size
    (code_size) into a block of data where they may (or may not) be interleaved
    for efficient consumption by distance computation kernels. This exists for
    the "fast_scan" indexes on CPU and for some GPU kernels.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zCodePacker.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zCodePacker.__init__c                 C   s   t | |||S r#   )r   ZCodePacker_pack_1r   	flat_coder  blockr   r   r   pack_1  s    zCodePacker.pack_1c                 C   s   t | |||S r#   )r   ZCodePacker_unpack_1r   ri  r  rh  r   r   r   unpack_1  s    zCodePacker.unpack_1c                 C   s   t | ||S r#   )r   ZCodePacker_pack_allr   
flat_codesri  r   r   r   pack_all  s    zCodePacker.pack_allc                 C   s   t | ||S r#   )r   ZCodePacker_unpack_allr   ri  rn  r   r   r   
unpack_all  s    zCodePacker.unpack_allN)r   r   r,   r-   r   r   r?   r   r   r   ZCodePacker_code_size_getZCodePacker_code_size_setr   ZCodePacker_nvec_getZCodePacker_nvec_setZnvecZCodePacker_block_size_getZCodePacker_block_size_set
block_sizerj  rl  ro  rq  Zdelete_CodePackerrT   r   r   r   r   rf    s   rf  c                   @   sX   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dd Z
dd Zdd ZejZdS )CodePackerFlatz6 Trivial code packer where codes are stored one by onec                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zCodePackerFlat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t | d S r#   )r   ZCodePackerFlat_swiginitZnew_CodePackerFlat)r   r   r   r   r   r?     s    zCodePackerFlat.__init__c                 C   s   t | |||S r#   )r   ZCodePackerFlat_pack_1rg  r   r   r   rj    s    zCodePackerFlat.pack_1c                 C   s   t | |||S r#   )r   ZCodePackerFlat_unpack_1rk  r   r   r   rl    s    zCodePackerFlat.unpack_1c                 C   s   t | ||S r#   )r   ZCodePackerFlat_pack_allrm  r   r   r   ro    s    zCodePackerFlat.pack_allc                 C   s   t | ||S r#   )r   ZCodePackerFlat_unpack_allrp  r   r   r   rq    s    zCodePackerFlat.unpack_allN)r   r   r,   r-   r   r   r   r   r?   rj  rl  ro  rq  r   Zdelete_CodePackerFlatrT   r   r   r   r   rs    s   rs  c                   @   s   e Zd ZdZedd dd ddZdd ZeZee	j
e	jZee	je	jd	dZee	je	jd
dZdd Zdd Zdd Zdd Zdd Ze	jZdS )VectorTransformz/ Any transformation applied on a set of vectorsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zVectorTransform.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zVectorTransform.__init__zinput dimensionzb
    set if the VectorTransform does not require training, or if
    training is done already
    c                 C   s   t | ||S )z
         Perform training on a representative set of vectors. Does
        nothing by default.

        :type n: int
        :param n:      nb of training vectors
        :type x: float
        :param x:      training vecors, size n * d
        )r   ZVectorTransform_trainrm  r   r   r   rn    s    
zVectorTransform.trainc                 C   s   t | ||S )a+  
         apply the transformation and return the result in an allocated pointer
        :type n: int
        :param n: number of vectors to transform
        :type x: float
        :param x: input vectors, size n * d_in
        :rtype: float
        :return: output vectors, size n * d_out
        )r   ZVectorTransform_applyrm  r   r   r   apply%  s    
zVectorTransform.applyc                 C   s   t | |||S )a,  
         apply the transformation and return the result in a provided matrix
        :type n: int
        :param n: number of vectors to transform
        :type x: float
        :param x: input vectors, size n * d_in
        :type xt: float
        :param xt: output vectors, size n * d_out
        )r   ZVectorTransform_apply_noallocr   rB   r2   xtr   r   r   apply_noalloc1  s    
zVectorTransform.apply_noallocc                 C   s   t | |||S )zi
        reverse transformation. May not be implemented or may return
        approximate result
        )r   Z!VectorTransform_reverse_transformr   rB   rw  r2   r   r   r   reverse_transform=  s    z!VectorTransform.reverse_transformc                 C   s   t | |S r#   )r   ZVectorTransform_check_identicalr^   r   r   r   check_identicalD  s    zVectorTransform.check_identicalN)r   r   r,   r-   r   r   r?   r   r   r   ZVectorTransform_d_in_getZVectorTransform_d_in_setd_inZVectorTransform_d_out_getZVectorTransform_d_out_setd_outZVectorTransform_is_trained_getZVectorTransform_is_trained_setr  rn  ru  rx  rz  r{  Zdelete_VectorTransformrT   r   r   r   r   rt  
  s   rt  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZeejejddZeejejd	dZdddZdd Zdd Zdd Zdd ZeejejZdd Zdd ZejZ dS )LinearTransformz\
     Generic linear transformation, with bias term applied on output
    y = A * x + b
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   P  r4   zLinearTransform.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   P  r4   r7   r8   zh
    whether to use the bias term
     check if matrix A is orthonormal (enables reverse_transform)
    z) Transformation matrix, size d_out * d_inz bias vector, size d_outr   Fc                 C   s   t | t ||| dS )z1 both d_in > d_out and d_out < d_in are supportedN)r   ZLinearTransform_swiginitZnew_LinearTransform)r   r|  r}  	have_biasr   r   r   r?   Z  s    zLinearTransform.__init__c                 C   s   t | |||S )z+ same as apply, but result is pre-allocated)r   ZLinearTransform_apply_noallocrv  r   r   r   rx  ^  s    zLinearTransform.apply_noallocc                 C   s   t | |||S )zc
        compute x = A^T * (x - b)
        is reverse transform if A has orthonormal lines
        )r   Z#LinearTransform_transform_transpose)r   rB   r   r2   r   r   r   transform_transposeb  s    z#LinearTransform.transform_transposec                 C   s   t | |||S )z works only if is_orthonormal)r   Z!LinearTransform_reverse_transformry  r   r   r   rz  i  s    z!LinearTransform.reverse_transformc                 C   s
   t | S )z/ compute A^T * A to set the is_orthonormal flag)r   Z"LinearTransform_set_is_orthonormalr@   r   r   r   set_is_orthonormalm  s    z"LinearTransform.set_is_orthonormalc                 C   s   t | ||||S r#   )r   Z LinearTransform_print_if_verbose)r   r   matrB   r   r   r   r   print_if_verboser  s    z LinearTransform.print_if_verbosec                 C   s   t | |S r#   )r   ZLinearTransform_check_identicalr^   r   r   r   r{  u  s    zLinearTransform.check_identicalN)r   r   F)!r   r   r,   r-   r   r   r   r   r   ZLinearTransform_have_bias_getZLinearTransform_have_bias_setr  Z"LinearTransform_is_orthonormal_getZ"LinearTransform_is_orthonormal_setZis_orthonormalZLinearTransform_A_getZLinearTransform_A_setAZLinearTransform_b_getZLinearTransform_b_setr~   r?   rx  r  rz  r  ZLinearTransform_verbose_getZLinearTransform_verbose_setr   r  r{  Zdelete_LinearTransformrT   r   r   r   r   r~  J  s    
r~  c                   @   sH   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dd Z
ejZdS )RandomRotationMatrixz! Randomly rotate a set of vectorsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ~  r4   zRandomRotationMatrix.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ~  r4   r7   r8   c                 C   s   t | |S )z, must be called before the transform is used)r   ZRandomRotationMatrix_initrG  r   r   r   ra    s    zRandomRotationMatrix.initc                 C   s   t | ||S r#   )r   ZRandomRotationMatrix_trainrm  r   r   r   rn    s    zRandomRotationMatrix.trainc                 G   s   t | t j|  d S r#   )r   ZRandomRotationMatrix_swiginitZnew_RandomRotationMatrixrQ   r   r   r   r?     s    zRandomRotationMatrix.__init__N)r   r   r,   r-   r   r   r   r   ra  rn  r?   r   Zdelete_RandomRotationMatrixrT   r   r   r   r   r  {  s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZeejejddZeejejddZeejejddZ dddZ!dd Z"dd Z#dd Z$ej%Z&dS )	PCAMatrixzx
    Applies a principal component analysis on a set of vectors,
    with optionally whitening and random rotation.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zPCAMatrix.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z
     after transformation the components are multiplied by
    eigenvalues^eigen_power

    =0: no whitening
    =-0.5: full whitening
    zA value added to eigenvalues to avoid division by 0 when whiteningz random rotation after PCAz/ ratio between # training vectors and dimensionz8 try to distribute output eigenvectors in this many binsz Mean, size d_inz= eigenvalues of covariance matrix (= squared singular values)z PCA matrix, size d_in * d_inr   Fc              	   C   s   t | t |||| d S r#   )r   ZPCAMatrix_swiginitZnew_PCAMatrix)r   r|  r}  eigen_powerrandom_rotationr   r   r   r?     s    zPCAMatrix.__init__c                 C   s   t | ||S )zo
        train on n vectors. If n < d_in then the eigenvector matrix
        will be completed with 0s
        )r   ZPCAMatrix_trainrm  r   r   r   rn    s    zPCAMatrix.trainc                 C   s   t | |S )z copy pre-trained PCA matrix)r   ZPCAMatrix_copy_fromr^   r   r   r   	copy_from  s    zPCAMatrix.copy_fromc                 C   s
   t | S )z7 called after mean, PCAMat and eigenvalues are computed)r   ZPCAMatrix_prepare_Abr@   r   r   r   
prepare_Ab  s    zPCAMatrix.prepare_AbN)r   r   r   F)'r   r   r,   r-   r   r   r   r   r   ZPCAMatrix_eigen_power_getZPCAMatrix_eigen_power_setr  ZPCAMatrix_epsilon_getZPCAMatrix_epsilon_setepsilonZPCAMatrix_random_rotation_getZPCAMatrix_random_rotation_setr  ZPCAMatrix_max_points_per_d_getZPCAMatrix_max_points_per_d_setZmax_points_per_dZPCAMatrix_balanced_bins_getZPCAMatrix_balanced_bins_setZbalanced_binsZPCAMatrix_mean_getZPCAMatrix_mean_setmeanZPCAMatrix_eigenvalues_getZPCAMatrix_eigenvalues_setZeigenvaluesZPCAMatrix_PCAMat_getZPCAMatrix_PCAMat_setZPCAMatr?   rn  r  r  Zdelete_PCAMatrixrT   r   r   r   r   r    s    
r  c                   @   sl   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZddd	Zd
d ZejZdS )	ITQMatrixz
     ITQ implementation from

        Iterative quantization: A procrustean approach to learning binary codes
        for large-scale image retrieval,

    Yunchao Gong, Svetlana Lazebnik, Albert Gordo, Florent Perronnin,
    PAMI'12.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zITQMatrix.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   r   c                 C   s   t | t | d S r#   )r   ZITQMatrix_swiginitZnew_ITQMatrixr   r   r   r   r?     s    zITQMatrix.__init__c                 C   s   t | ||S r#   )r   ZITQMatrix_trainrm  r   r   r   rn    s    zITQMatrix.trainN)r   )r   r   r,   r-   r   r   r   r   r   ZITQMatrix_max_iter_getZITQMatrix_max_iter_setZmax_iterZITQMatrix_seed_getZITQMatrix_seed_setr   ZITQMatrix_init_rotation_getZITQMatrix_init_rotation_setZinit_rotationr?   rn  Zdelete_ITQMatrixrT   r   r   r   r   r    s   

r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejddZeejejZdd
dZdd Zdd Zdd ZejZdS )ITQTransformzH The full ITQ transform, including normalizations and PCA transformationc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zITQTransform.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z" max training points per dimensionr   Fc                 C   s   t | t ||| d S r#   )r   ZITQTransform_swiginitZnew_ITQTransform)r   r|  r}  do_pcar   r   r   r?     s    zITQTransform.__init__c                 C   s   t | ||S r#   )r   ZITQTransform_trainrm  r   r   r   rn    s    zITQTransform.trainc                 C   s   t | |||S r#   )r   ZITQTransform_apply_noallocrv  r   r   r   rx    s    zITQTransform.apply_noallocc                 C   s   t | |S r#   )r   ZITQTransform_check_identicalr^   r   r   r   r{    s    zITQTransform.check_identicalN)r   r   F)r   r   r,   r-   r   r   r   r   r   ZITQTransform_mean_getZITQTransform_mean_setr  ZITQTransform_do_pca_getZITQTransform_do_pca_setr  ZITQTransform_itq_getZITQTransform_itq_setZitqZ"ITQTransform_max_train_per_dim_getZ"ITQTransform_max_train_per_dim_setZmax_train_per_dimZITQTransform_pca_then_itq_getZITQTransform_pca_then_itq_setZpca_then_itqr?   rn  rx  r{  Zdelete_ITQTransformrT   r   r   r   r   r    s   
r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZeejejZeejejddZdddZdd Zej Z!dS )	OPQMatrixa\  
     Applies a rotation to align the dimensions with a PQ to minimize
     the reconstruction error. Can be used before an IndexPQ or an
     IndexIVFPQ. The method is the non-parametric version described in:

    "Optimized Product Quantization for Approximate Nearest Neighbor Search"
    Tiezheng Ge, Kaiming He, Qifa Ke, Jian Sun, CVPR'13
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zOPQMatrix.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z nb of subquantizersz$ Number of outer training iterationsz) Number of training iterations for the PQz$ same, for the first outer iterationz0 if there are too many training points, resamplezk
    if non-NULL, use this product quantizer for training
    should be constructed with (d_out, M, _)
    r   r   r
  c                 C   s   t | t ||| dS )z. if d2 != -1, output vectors of this dimensionN)r   ZOPQMatrix_swiginitZnew_OPQMatrix)r   r   r  Zd2r   r   r   r?     s    zOPQMatrix.__init__c                 C   s   t | ||S r#   )r   ZOPQMatrix_trainrm  r   r   r   rn  	  s    zOPQMatrix.trainN)r   r   r
  )"r   r   r,   r-   r   r   r   r   r   ZOPQMatrix_M_getZOPQMatrix_M_setr  ZOPQMatrix_niter_getZOPQMatrix_niter_setr  ZOPQMatrix_niter_pq_getZOPQMatrix_niter_pq_setZniter_pqZOPQMatrix_niter_pq_0_getZOPQMatrix_niter_pq_0_setZ
niter_pq_0ZOPQMatrix_max_train_points_getZOPQMatrix_max_train_points_setmax_train_pointsZOPQMatrix_verbose_getZOPQMatrix_verbose_setr   ZOPQMatrix_pq_getZOPQMatrix_pq_setpqr?   rn  Zdelete_OPQMatrixrT   r   r   r   r   r    s   	
r  c                   @   sb   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 Zd
d Zdd Zdd ZejZdS )RemapDimensionsTransformz
     remap dimensions for intput vectors, possibly inserting 0s
    strictly speaking this is also a linear transform but we don't want
    to compute it with matrix multiplies
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z!RemapDimensionsTransform.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   zR
    map from output dimension to input, size d_out
    -1 -> set output to 0
    c                 C   s   t | |||S r#   )r   Z&RemapDimensionsTransform_apply_noallocrv  r   r   r   rx    s    z&RemapDimensionsTransform.apply_noallocc                 C   s   t | |||S )zA reverse transform correct only when the mapping is a permutation)r   Z*RemapDimensionsTransform_reverse_transformry  r   r   r   rz     s    z*RemapDimensionsTransform.reverse_transformc                 G   s   t | t j|  d S r#   )r   Z!RemapDimensionsTransform_swiginitZnew_RemapDimensionsTransformrQ   r   r   r   r?   $  s    z!RemapDimensionsTransform.__init__c                 C   s   t | |S r#   )r   Z(RemapDimensionsTransform_check_identicalr^   r   r   r   r{  '  s    z(RemapDimensionsTransform.check_identicalN)r   r   r,   r-   r   r   r   r   r   Z RemapDimensionsTransform_map_getZ RemapDimensionsTransform_map_setmaprx  rz  r?   r{  Zdelete_RemapDimensionsTransformrT   r   r   r   r   r    s   r  c                   @   s^   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 Zdd Zdd ZejZdS )NormalizationTransformz per-vector normalizationc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   0  r4   zNormalizationTransform.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   0  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZNormalizationTransform_swiginitZnew_NormalizationTransformrQ   r   r   r   r?   4  s    zNormalizationTransform.__init__c                 C   s   t | |||S r#   )r   Z$NormalizationTransform_apply_noallocrv  r   r   r   rx  7  s    z$NormalizationTransform.apply_noallocc                 C   s   t | |||S )z0 Identity transform since norm is not revertible)r   Z(NormalizationTransform_reverse_transformry  r   r   r   rz  :  s    z(NormalizationTransform.reverse_transformc                 C   s   t | |S r#   )r   Z&NormalizationTransform_check_identicalr^   r   r   r   r{  >  s    z&NormalizationTransform.check_identicalN)r   r   r,   r-   r   r   r   r   r   ZNormalizationTransform_norm_getZNormalizationTransform_norm_setr  r?   rx  rz  r{  Zdelete_NormalizationTransformrT   r   r   r   r   r  -  s   r  c                   @   sl   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	d
Zdd Zdd Zdd Zdd ZejZdS )CenteringTransformz6 Subtract the mean of each component from the vectors.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   G  r4   zCenteringTransform.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   G  r4   r7   r8   z Mean, size d_in = d_outr   c                 C   s   t | t | d S r#   )r   ZCenteringTransform_swiginitZnew_CenteringTransformr   r   r   r   r?   K  s    zCenteringTransform.__init__c                 C   s   t | ||S )z train on n vectors.)r   ZCenteringTransform_trainrm  r   r   r   rn  N  s    zCenteringTransform.trainc                 C   s   t | |||S )z subtract the mean)r   Z CenteringTransform_apply_noallocrv  r   r   r   rx  R  s    z CenteringTransform.apply_noallocc                 C   s   t | |||S )z add the mean)r   Z$CenteringTransform_reverse_transformry  r   r   r   rz  V  s    z$CenteringTransform.reverse_transformc                 C   s   t | |S r#   )r   Z"CenteringTransform_check_identicalr^   r   r   r   r{  Z  s    z"CenteringTransform.check_identicalN)r   )r   r   r,   r-   r   r   r   r   r   ZCenteringTransform_mean_getZCenteringTransform_mean_setr  r?   rn  rx  rz  r{  Zdelete_CenteringTransformrT   r   r   r   r   r  D  s   
r  c                   @   sB   e Zd Zedd dd ddZeZeejej	Z
dd ZejZdS )	SearchParametersPreTransformc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   a  r4   z%SearchParametersPreTransform.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   a  r4   r7   r8   c                 C   s   t | t   d S r#   )r   Z%SearchParametersPreTransform_swiginitZ new_SearchParametersPreTransformr@   r   r   r   r?   e  s    z%SearchParametersPreTransform.__init__N)r   r   r,   r   r   r   r   r   Z-SearchParametersPreTransform_index_params_getZ-SearchParametersPreTransform_index_params_setZindex_paramsr?   Z#delete_SearchParametersPreTransformrT   r   r   r   r   r  `  s
   r  c                   @   s
  e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZeejejddZd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zd3ddZd4ddZdd Zdd Zd5d d!Zd"d# Zd$d% Zd&d' Z d(d) Z!d*d+ Z"d,d- Z#d6d/d0Z$d1d2 Z%ej&Z'dS )7IndexPreTransformzo
    Index that applies a LinearTransform transform on vectors before
    handing them over to a sub-index
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   q  r4   zIndexPreTransform.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   q  r4   r7   r8   zchain of transformsthe sub-indexc                 G   s   t | t j|  dS )z
        *Overload 1:*
        whether pointers are deleted in destructor

        |

        *Overload 2:*
         ltrans is the last transform before the index
        N)r   ZIndexPreTransform_swiginitZnew_IndexPreTransformrQ   r   r   r   r?   w  s    
zIndexPreTransform.__init__c                 C   s   t | |S r#   )r   Z#IndexPreTransform_prepend_transform)r   Zltransr   r   r   prepend_transform  s    z#IndexPreTransform.prepend_transformc                 C   s   t | ||S r#   )r   ZIndexPreTransform_trainrm  r   r   r   rn    s    zIndexPreTransform.trainc                 C   s   t | ||S r#   )r   ZIndexPreTransform_addrm  r   r   r   ro    s    zIndexPreTransform.addc                 C   s   t | |||S r#   )r   ZIndexPreTransform_add_with_idsrp  r   r   r   rr    s    zIndexPreTransform.add_with_idsc                 C   s
   t | S r#   )r   ZIndexPreTransform_resetr@   r   r   r   r|     s    zIndexPreTransform.resetc                 C   s   t | |S )z: removes IDs from the index. Not supported by all indexes.)r   ZIndexPreTransform_remove_idsrz  r   r   r   r{    s    zIndexPreTransform.remove_idsNc              	   C   s   t | ||||||S r#   )r   ZIndexPreTransform_searchrs  r   r   r   ru    s    zIndexPreTransform.searchc                 C   s   t | |||||S r#   )r   ZIndexPreTransform_range_searchrv  r   r   r   rw    s    zIndexPreTransform.range_searchc                 C   s   t | ||S r#   )r   ZIndexPreTransform_reconstructr|  r   r   r   r    s    zIndexPreTransform.reconstructc                 C   s   t | |||S r#   )r   ZIndexPreTransform_reconstruct_nr  r   r   r   r    s    zIndexPreTransform.reconstruct_nc              
   C   s   t | |||||||S r#   )r   Z(IndexPreTransform_search_and_reconstructr  r   r   r   r    s    z(IndexPreTransform.search_and_reconstructc                 C   s   t | ||S )z
        apply the transforms in the chain. The returned float * may be
        equal to x, otherwise it should be deallocated.
        )r   ZIndexPreTransform_apply_chainrm  r   r   r   apply_chain  s    zIndexPreTransform.apply_chainc                 C   s   t | |||S )z
        Reverse the transforms in the chain. May not be implemented for
        all transforms in the chain or may return approximate results.
        )r   ZIndexPreTransform_reverse_chainry  r   r   r   reverse_chain  s    zIndexPreTransform.reverse_chainc                 C   s
   t | S r#   )r   Z'IndexPreTransform_get_distance_computerr@   r   r   r   r    s    z'IndexPreTransform.get_distance_computerc                 C   s
   t | S r#   )r   ZIndexPreTransform_sa_code_sizer@   r   r   r   r    s    zIndexPreTransform.sa_code_sizec                 C   s   t | |||S r#   )r   ZIndexPreTransform_sa_encoder  r   r   r   r    s    zIndexPreTransform.sa_encodec                 C   s   t | |||S r#   )r   ZIndexPreTransform_sa_decoder  r   r   r   r    s    zIndexPreTransform.sa_decoder   c                 C   s   t | ||S r#   )r   ZIndexPreTransform_merge_fromr  r   r   r   r    s    zIndexPreTransform.merge_fromc                 C   s   t | |S r#   )r   Z,IndexPreTransform_check_compatible_for_merger  r   r   r   r    s    z,IndexPreTransform.check_compatible_for_merge)N)N)N)r   )(r   r   r,   r-   r   r   r   r   r   ZIndexPreTransform_chain_getZIndexPreTransform_chain_setchainZIndexPreTransform_index_getZIndexPreTransform_index_setr  Z IndexPreTransform_own_fields_getZ IndexPreTransform_own_fields_set
own_fieldsr?   r  rn  ro  rr  r|   r{  ru  rw  r  r  r  r  r  r  r  r  r  r  r  Zdelete_IndexPreTransformrT   r   r   r   r   r  k  s6   



r  c                   @   sP   e Zd Zedd dd ddZeZeejej	Z
eejejZejZdd ZdS )	IndexRefineSearchParametersc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z$IndexRefineSearchParameters.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   Z$IndexRefineSearchParameters_swiginitZnew_IndexRefineSearchParametersr@   r   r   r   r?     s    z$IndexRefineSearchParameters.__init__N)r   r   r,   r   r   r   r   r   Z(IndexRefineSearchParameters_k_factor_getZ(IndexRefineSearchParameters_k_factor_setk_factorZ1IndexRefineSearchParameters_base_index_params_getZ1IndexRefineSearchParameters_base_index_params_setZbase_index_paramsZ"delete_IndexRefineSearchParametersrT   r?   r   r   r   r   r    s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZdd Zdd Zdd Zdd Zd!ddZd"ddZdd Zdd Zdd Z dd  Z!ej"Z#dS )#IndexRefinez
    Index that queries in a base_index (a fast one) and refines the
    results with an exact search, hopefully improving the results.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexRefine.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z? faster index to pre-select the vectors that should be filteredz refinement indexz& should the base index be deallocated?z same with the refinement indexzk
    factor between k requested in search and the k requested from
    the base_index (should be >= 1)
    c                 G   s   t | t j|  d S r#   )r   ZIndexRefine_swiginitZnew_IndexRefinerQ   r   r   r   r?     s    zIndexRefine.__init__c                 C   s   t | ||S r#   )r   ZIndexRefine_trainrm  r   r   r   rn    s    zIndexRefine.trainc                 C   s   t | ||S r#   )r   ZIndexRefine_addrm  r   r   r   ro    s    zIndexRefine.addc                 C   s
   t | S r#   )r   ZIndexRefine_resetr@   r   r   r   r|     s    zIndexRefine.resetNc              	   C   s   t | ||||||S r#   )r   ZIndexRefine_searchrs  r   r   r   ru    s    zIndexRefine.searchc                 C   s   t | |||||S r#   )r   ZIndexRefine_range_searchrv  r   r   r   rw    s    zIndexRefine.range_searchc                 C   s   t | ||S r#   )r   ZIndexRefine_reconstructr|  r   r   r   r    s    zIndexRefine.reconstructc                 C   s
   t | S r#   )r   ZIndexRefine_sa_code_sizer@   r   r   r   r    s    zIndexRefine.sa_code_sizec                 C   s   t | |||S r#   )r   ZIndexRefine_sa_encoder  r   r   r   r    s    zIndexRefine.sa_encodec                 C   s   t | |||S )zk
        The sa_decode decodes from the index_refine, which is assumed to be more
        accurate
        )r   ZIndexRefine_sa_decoder  r   r   r   r     s    zIndexRefine.sa_decode)N)N)$r   r   r,   r-   r   r   r   r   r   ZIndexRefine_base_index_getZIndexRefine_base_index_setZ
base_indexZIndexRefine_refine_index_getZIndexRefine_refine_index_setZrefine_indexZIndexRefine_own_fields_getZIndexRefine_own_fields_setr  Z IndexRefine_own_refine_index_getZ IndexRefine_own_refine_index_setZown_refine_indexZIndexRefine_k_factor_getZIndexRefine_k_factor_setr  r?   rn  ro  r|   ru  rw  r  r  r  r  Zdelete_IndexRefinerT   r   r   r   r   r    s&   

r  c                   @   sB   e Zd ZdZedd dd ddZeZdd Zdd
dZ	e
jZd	S )IndexRefineFlatz
     Version where the refinement index is an IndexFlat. It has one additional
    constructor that takes a table of elements to add to the flat refinement
    index
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexRefineFlat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexRefineFlat_swiginitZnew_IndexRefineFlatrQ   r   r   r   r?     s    zIndexRefineFlat.__init__Nc              	   C   s   t | ||||||S r#   )r   ZIndexRefineFlat_searchrs  r   r   r   ru    s    zIndexRefineFlat.search)N)r   r   r,   r-   r   r   r   r   r?   ru  r   Zdelete_IndexRefineFlatrT   r   r   r   r   r  
  s   
r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZdd Zdd ZdddZdd ZejZdd Zdd Zdd Z dS )IndexLSHz? The sign of each vector component is put in a binary signaturec                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zIndexLSH.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   z nb of bits per vectorz, whether to apply a random rotation to inputz% whether we train thresholds or use 0z optional random rotationz thresholds to compare withc                 C   s   t | ||S )aP  
         Preprocesses and resizes the input to the size required to
        binarize the data

        :type x: float
        :param x: input vectors, size n * d
        :rtype: float
        :return: output vectors, size n * bits. May be the same pointer
                    as x, otherwise it should be deleted by the caller
        )r   ZIndexLSH_apply_preprocessrm  r   r   r   apply_preprocess(  s    zIndexLSH.apply_preprocessc                 C   s   t | ||S r#   )r   ZIndexLSH_trainrm  r   r   r   rn  5  s    zIndexLSH.trainNc              	   C   s   t | ||||||S r#   )r   ZIndexLSH_searchrs  r   r   r   ru  8  s    zIndexLSH.searchc                 C   s   t | |S )zh
        transfer the thresholds to a pre-processing stage (and unset
        train_thresholds)
        )r   ZIndexLSH_transfer_thresholds)r   vtr   r   r   transfer_thresholds;  s    zIndexLSH.transfer_thresholdsc                 G   s   t | t j|  d S r#   )r   ZIndexLSH_swiginitZnew_IndexLSHrQ   r   r   r   r?   C  s    zIndexLSH.__init__c                 C   s   t | |||S r#   )r   ZIndexLSH_sa_encoder  r   r   r   r  F  s    zIndexLSH.sa_encodec                 C   s   t | |||S r#   )r   ZIndexLSH_sa_decoder  r   r   r   r  I  s    zIndexLSH.sa_decode)N)!r   r   r,   r-   r   r   r   r   r   ZIndexLSH_nbits_getZIndexLSH_nbits_setr   ZIndexLSH_rotate_data_getZIndexLSH_rotate_data_setZrotate_dataZIndexLSH_train_thresholds_getZIndexLSH_train_thresholds_setZtrain_thresholdsZIndexLSH_rrot_getZIndexLSH_rrot_setZrrotZIndexLSH_thresholds_getZIndexLSH_thresholds_setZ
thresholdsr  rn  ru  r  Zdelete_IndexLSHrT   r?   r  r  r   r   r   r   r    s    
r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZeejejZeejejZeejejZeejejZ dd Z!ej"Z#d	S )
SimulatedAnnealingParametersz3 parameters used for the simulated annealing methodc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   Q  r4   z%SimulatedAnnealingParameters.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   Q  r4   r7   r8   c                 C   s   t | t   d S r#   )r   Z%SimulatedAnnealingParameters_swiginitZ new_SimulatedAnnealingParametersr@   r   r   r   r?   \  s    z%SimulatedAnnealingParameters.__init__N)$r   r   r,   r-   r   r   r   r   r   Z1SimulatedAnnealingParameters_init_temperature_getZ1SimulatedAnnealingParameters_init_temperature_setZinit_temperatureZ2SimulatedAnnealingParameters_temperature_decay_getZ2SimulatedAnnealingParameters_temperature_decay_setZtemperature_decayZ'SimulatedAnnealingParameters_n_iter_getZ'SimulatedAnnealingParameters_n_iter_setZn_iterZ'SimulatedAnnealingParameters_n_redo_getZ'SimulatedAnnealingParameters_n_redo_setZn_redoZ%SimulatedAnnealingParameters_seed_getZ%SimulatedAnnealingParameters_seed_setr   Z(SimulatedAnnealingParameters_verbose_getZ(SimulatedAnnealingParameters_verbose_setr   Z/SimulatedAnnealingParameters_only_bit_flips_getZ/SimulatedAnnealingParameters_only_bit_flips_setZonly_bit_flipsZ,SimulatedAnnealingParameters_init_random_getZ,SimulatedAnnealingParameters_init_random_setZinit_randomr?   Z#delete_SimulatedAnnealingParametersrT   r   r   r   r   r  N  s   r  c                   @   sV   e Zd ZdZedd dd ddZdd ZeZee	j
e	jZd	d
 Zdd Ze	jZdS )PermutationObjectivez% abstract class for the loss functionc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   e  r4   zPermutationObjective.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   e  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   g  s    zPermutationObjective.__init__c                 C   s   t | |S r#   )r   Z!PermutationObjective_compute_costr  r   r   r   compute_costl  s    z!PermutationObjective.compute_costc                 C   s   t | |||S r#   )r   Z PermutationObjective_cost_updater   rT  iwZjwr   r   r   cost_updateo  s    z PermutationObjective.cost_updateN)r   r   r,   r-   r   r   r?   r   r   r   ZPermutationObjective_n_getZPermutationObjective_n_setrB   r  r  Zdelete_PermutationObjectiverT   r   r   r   r   r  b  s   r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
edd Zdd	 Zeejejd
dZeejejddZeejejddZdd Zdd Zdd Zdd Zedd Zdd ZejZdS )ReproduceDistancesObjectivec                 C   s
   | j  S r#   r0   r1   r   r   r   r3   v  r4   z$ReproduceDistancesObjective.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   v  r4   r7   r8   c                 C   s
   t | S r#   )r   ZReproduceDistancesObjective_sqrr1   r   r   r   sqrz  s    zReproduceDistancesObjective.sqrc                 C   s   t | |S r#   )r   Z&ReproduceDistancesObjective_dis_weightrE   r   r   r   
dis_weight~  s    z&ReproduceDistancesObjective.dis_weightz& "real" corrected distances (size n^2)z wanted distances (size n^2)z% weights for each distance (size n^2)c                 C   s   t | ||S r#   )r   Z*ReproduceDistancesObjective_get_source_disr  r   r   r   get_source_dis  s    z*ReproduceDistancesObjective.get_source_disc                 C   s   t | |S r#   )r   Z(ReproduceDistancesObjective_compute_costr  r   r   r   r    s    z(ReproduceDistancesObjective.compute_costc                 C   s   t | |||S r#   )r   Z'ReproduceDistancesObjective_cost_updater  r   r   r   r    s    z'ReproduceDistancesObjective.cost_updatec              	   C   s   t | t |||| d S r#   )r   Z$ReproduceDistancesObjective_swiginitZnew_ReproduceDistancesObjective)r   rB   source_dis_inZtarget_dis_indis_weight_factorr   r   r   r?     s    z$ReproduceDistancesObjective.__init__c                 C   s   t | |||S r#   )r   Z.ReproduceDistancesObjective_compute_mean_stdev)r`  r   Zmean_outZ
stddev_outr   r   r   compute_mean_stdev  s    z.ReproduceDistancesObjective.compute_mean_stdevc                 C   s   t | |S r#   )r   Z1ReproduceDistancesObjective_set_affine_target_dis)r   r  r   r   r   set_affine_target_dis  s    z1ReproduceDistancesObjective.set_affine_target_disN)r   r   r,   r   r   r   r   r   Z1ReproduceDistancesObjective_dis_weight_factor_getZ1ReproduceDistancesObjective_dis_weight_factor_setr  staticmethodr  r  Z*ReproduceDistancesObjective_source_dis_getZ*ReproduceDistancesObjective_source_dis_setZ
source_disZ*ReproduceDistancesObjective_target_dis_getZ*ReproduceDistancesObjective_target_dis_setZ
target_disZ'ReproduceDistancesObjective_weights_getZ'ReproduceDistancesObjective_weights_setr  r  r  r  r?   r  r  Z"delete_ReproduceDistancesObjectiverT   r   r   r   r   r  u  s"   

r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZeejejZdd	 ZeejejZeejejd
dZdd Zdd ZejZdS )SimulatedAnnealingOptimizerz= Simulated annealing optimization algorithm for permutations.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z$SimulatedAnnealingOptimizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z size of the permutationc                 C   s   t | t || dS )z! logs values of the cost functionN)r   Z$SimulatedAnnealingOptimizer_swiginitZnew_SimulatedAnnealingOptimizer)r   r  rM  r   r   r   r?     s    z$SimulatedAnnealingOptimizer.__init__z& remember initial cost of optimizationc                 C   s   t | |S r#   )r   Z$SimulatedAnnealingOptimizer_optimizer  r   r   r   optimize  s    z$SimulatedAnnealingOptimizer.optimizec                 C   s   t | |S r#   )r   Z,SimulatedAnnealingOptimizer_run_optimization)r   Z	best_permr   r   r   run_optimization  s    z,SimulatedAnnealingOptimizer.run_optimizationN)r   r   r,   r-   r   r   r   r   r   Z#SimulatedAnnealingOptimizer_obj_getZ#SimulatedAnnealingOptimizer_obj_setr  Z!SimulatedAnnealingOptimizer_n_getZ!SimulatedAnnealingOptimizer_n_setrB   Z'SimulatedAnnealingOptimizer_logfile_getZ'SimulatedAnnealingOptimizer_logfile_setZlogfiler?   Z#SimulatedAnnealingOptimizer_rnd_getZ#SimulatedAnnealingOptimizer_rnd_setZrndZ)SimulatedAnnealingOptimizer_init_cost_getZ)SimulatedAnnealingOptimizer_init_cost_setZ	init_costr  r  Z"delete_SimulatedAnnealingOptimizerrT   r   r   r   r   r    s   r  c                   @   s   e Zd ZdZedd dd ddZeZej	Z
ejZejZeejejZeejejddZeejejddZeejejd	dZeejejZd
d Zdd Zdd Z dd Z!dd Z"ej#Z$dS )PolysemousTrainingz5 optimizes the order of indices in a ProductQuantizerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zPolysemousTraining.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z
     use 1/4 of the training points for the optimization, with
    max. ntrain_permutation. If ntrain_permutation == 0: train on
    centroids
    z( decay of exp that weights distance losszA refuse to train if it would require more than that amount of RAMc                 C   s   t | t   d S r#   )r   ZPolysemousTraining_swiginitZnew_PolysemousTrainingr@   r   r   r   r?     s    zPolysemousTraining.__init__c                 C   s   t | |||S )z
        reorder the centroids so that the Hamming distance becomes a
        good approximation of the SDC distance (called by train)
        )r   Z*PolysemousTraining_optimize_pq_for_hammingr   r  rB   r2   r   r   r   optimize_pq_for_hamming  s    z*PolysemousTraining.optimize_pq_for_hammingc                 C   s   t | |||S z" called by optimize_pq_for_hamming)r   Z#PolysemousTraining_optimize_rankingr  r   r   r   optimize_ranking  s    z#PolysemousTraining.optimize_rankingc                 C   s   t | |S r  )r   Z/PolysemousTraining_optimize_reproduce_distancesr   r  r   r   r   optimize_reproduce_distances  s    z/PolysemousTraining.optimize_reproduce_distancesc                 C   s   t | |S )z& make sure we don't blow up the memory)r   Z*PolysemousTraining_memory_usage_per_threadr  r   r   r   memory_usage_per_thread  s    z*PolysemousTraining.memory_usage_per_threadN)%r   r   r,   r-   r   r   r   r   r   ZPolysemousTraining_OT_NoneZOT_NoneZ/PolysemousTraining_OT_ReproduceDistances_affineZOT_ReproduceDistances_affineZ+PolysemousTraining_OT_Ranking_weighted_diffZOT_Ranking_weighted_diffZ(PolysemousTraining_optimization_type_getZ(PolysemousTraining_optimization_type_setZoptimization_typeZ)PolysemousTraining_ntrain_permutation_getZ)PolysemousTraining_ntrain_permutation_setZntrain_permutationZ(PolysemousTraining_dis_weight_factor_getZ(PolysemousTraining_dis_weight_factor_setr  Z!PolysemousTraining_max_memory_getZ!PolysemousTraining_max_memory_setZ
max_memoryZ"PolysemousTraining_log_pattern_getZ"PolysemousTraining_log_pattern_setZlog_patternr?   r  r  r  r  Zdelete_PolysemousTrainingrT   r   r   r   r   r    s"   r  c                   @   s  e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 Zd
d ZdddZdd Zdd Zdd ZeejejddZeejejddZejZejZejZejZej Z!ej"Z#eej$ej%Z&eej'ej(Z)eej*ej+ddZ,dd Z-dd Z.dd Z/ej0Z1dS )IndexPQz_
     Index based on a product quantizer. Stored vectors are
    approximated by PQ codes.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexPQ.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z1 The product quantizer used to encode the vectorsc                 G   s   t | t j|  d S r#   )r   ZIndexPQ_swiginitZnew_IndexPQrQ   r   r   r   r?     s    zIndexPQ.__init__c                 C   s   t | ||S r#   )r   ZIndexPQ_trainrm  r   r   r   rn    s    zIndexPQ.trainNc              	   C   s   t | ||||||S r#   )r   ZIndexPQ_searchrs  r   r   r   ru    s    zIndexPQ.searchc                 C   s   t | |||S r#   )r   ZIndexPQ_sa_encoder  r   r   r   r    s    zIndexPQ.sa_encodec                 C   s   t | |||S r#   )r   ZIndexPQ_sa_decoder  r   r   r   r    s    zIndexPQ.sa_decodec                 C   s
   t | S r#   )r   Z%IndexPQ_get_FlatCodesDistanceComputerr@   r   r   r   r    s    z%IndexPQ.get_FlatCodesDistanceComputerz false = standard PQz, parameters used for the polysemous trainingz$ Hamming threshold used for polysemyc              
   C   s   t | |||||||S r#   )r   ZIndexPQ_search_core_polysemous)r   rB   r2   r   r   r   polysemous_htZgeneralized_hammingr   r   r   search_core_polysemous  s    zIndexPQ.search_core_polysemousc                 C   s   t | |||||S )a  
        prepare query for a polysemous search, but instead of
        computing the result, just get the histogram of Hamming
        distances. May be computed on a provided dataset if xb != NULL
        :type dist_histogram: int
        :param dist_histogram: (M * nbits + 1)
        )r   Z"IndexPQ_hamming_distance_histogram)r   rB   r2   r   r  Zdist_histogramr   r   r   hamming_distance_histogram  s    z"IndexPQ.hamming_distance_histogramc                 C   s   t | |||S )a  
         compute pairwise distances between queries and database

        :type n: int
        :param n:    nb of query vectors
        :type x: float
        :param x:    query vector, size n * d
        :type dis: int
        :param dis:  output distances, size n * ntotal
        )r   ZIndexPQ_hamming_distance_table)r   rB   r2   r   r   r   r   hamming_distance_table  s    zIndexPQ.hamming_distance_table)N)2r   r   r,   r-   r   r   r   r   r   ZIndexPQ_pq_getZIndexPQ_pq_setr  r?   rn  ru  r  r  r  Z"IndexPQ_do_polysemous_training_getZ"IndexPQ_do_polysemous_training_setdo_polysemous_trainingZIndexPQ_polysemous_training_getZIndexPQ_polysemous_training_setpolysemous_trainingZIndexPQ_ST_PQZST_PQZIndexPQ_ST_HEZST_HEZIndexPQ_ST_generalized_HEZST_generalized_HEZIndexPQ_ST_SDCZST_SDCZIndexPQ_ST_polysemousZST_polysemousZ IndexPQ_ST_polysemous_generalizeZST_polysemous_generalizeZIndexPQ_search_type_getZIndexPQ_search_type_setr  ZIndexPQ_encode_signs_getZIndexPQ_encode_signs_setZencode_signsZIndexPQ_polysemous_ht_getZIndexPQ_polysemous_ht_setr  r  r  r  Zdelete_IndexPQrT   r   r   r   r   r    s2   

r  c                   @   sT   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd ZejZd	S )
SearchParametersPQz* override search parameters from the classc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   /  r4   zSearchParametersPQ.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   /  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZSearchParametersPQ_swiginitZnew_SearchParametersPQr@   r   r   r   r?   4  s    zSearchParametersPQ.__init__N)r   r   r,   r-   r   r   r   r   r   Z"SearchParametersPQ_search_type_getZ"SearchParametersPQ_search_type_setr  Z$SearchParametersPQ_polysemous_ht_getZ$SearchParametersPQ_polysemous_ht_setr  r?   Zdelete_SearchParametersPQrT   r   r   r   r   r  ,  s   r  c                   @   sj   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZdd Zd	d
 ZejZdS )IndexPQStatszs
    statistics are robust to internal threading, but not if
    IndexPQ::search is called by multiple threads
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   @  r4   zIndexPQStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   @  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZIndexPQStats_swiginitZnew_IndexPQStatsr@   r   r   r   r?   F  s    zIndexPQStats.__init__c                 C   s
   t | S r#   )r   ZIndexPQStats_resetr@   r   r   r   r|   I  s    zIndexPQStats.resetN)r   r   r,   r-   r   r   r   r   r   ZIndexPQStats_nq_getZIndexPQStats_nq_setr   ZIndexPQStats_ncode_getZIndexPQStats_ncode_setncodeZIndexPQStats_n_hamming_pass_getZIndexPQStats_n_hamming_pass_setn_hamming_passr?   r|   Zdelete_IndexPQStatsrT   r   r   r   r   r  :  s   r  c                   @   sp   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zdd
dZdd Zdd Zdd Zdd ZejZd	S )MultiIndexQuantizerze
    Quantizer where centroids are virtual: they are the Cartesian
    product of sub-centroids.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   U  r4   zMultiIndexQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   U  r4   r7   r8   c                 C   s   t | ||S r#   )r   ZMultiIndexQuantizer_trainrm  r   r   r   rn  Y  s    zMultiIndexQuantizer.trainNc              	   C   s   t | ||||||S r#   )r   ZMultiIndexQuantizer_searchrs  r   r   r   ru  \  s    zMultiIndexQuantizer.searchc                 C   s   t | ||S )z$ add and reset will crash at runtime)r   ZMultiIndexQuantizer_addrm  r   r   r   ro  _  s    zMultiIndexQuantizer.addc                 C   s
   t | S r#   )r   ZMultiIndexQuantizer_resetr@   r   r   r   r|   c  s    zMultiIndexQuantizer.resetc                 G   s   t | t j|  d S r#   )r   ZMultiIndexQuantizer_swiginitZnew_MultiIndexQuantizerrQ   r   r   r   r?   f  s    zMultiIndexQuantizer.__init__c                 C   s   t | ||S r#   )r   ZMultiIndexQuantizer_reconstructr|  r   r   r   r  i  s    zMultiIndexQuantizer.reconstruct)N)r   r   r,   r-   r   r   r   r   r   ZMultiIndexQuantizer_pq_getZMultiIndexQuantizer_pq_setr  rn  ru  ro  r|   r?   r  Zdelete_MultiIndexQuantizerrT   r   r   r   r   r  O  s   
r  c                   @   sj   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejZdd	 Zd
d ZdddZejZdS )MultiIndexQuantizer2zH MultiIndexQuantizer where the PQ assignmnet is performed by sub-indexesc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   r  r4   zMultiIndexQuantizer2.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   r  r4   r7   r8   z M Indexes on d / M dimensionsc                 G   s   t | t j|  d S r#   )r   ZMultiIndexQuantizer2_swiginitZnew_MultiIndexQuantizer2rQ   r   r   r   r?   w  s    zMultiIndexQuantizer2.__init__c                 C   s   t | ||S r#   )r   ZMultiIndexQuantizer2_trainrm  r   r   r   rn  z  s    zMultiIndexQuantizer2.trainNc              	   C   s   t | ||||||S r#   )r   ZMultiIndexQuantizer2_searchrs  r   r   r   ru  }  s    zMultiIndexQuantizer2.search)N)r   r   r,   r-   r   r   r   r   r   Z'MultiIndexQuantizer2_assign_indexes_getZ'MultiIndexQuantizer2_assign_indexes_setZassign_indexesZ#MultiIndexQuantizer2_own_fields_getZ#MultiIndexQuantizer2_own_fields_setr  r?   rn  ru  Zdelete_MultiIndexQuantizer2rT   r   r   r   r   r  o  s   
r  c                   @   sh   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zdd
dZdd Zdd Zdd ZejZd	S )IndexAdditiveQuantizerzL Abstract class for additive quantizers. The search functions are in common.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexAdditiveQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexAdditiveQuantizer_swiginitZnew_IndexAdditiveQuantizerrQ   r   r   r   r?     s    zIndexAdditiveQuantizer.__init__Nc              	   C   s   t | ||||||S r#   )r   ZIndexAdditiveQuantizer_searchrs  r   r   r   ru    s    zIndexAdditiveQuantizer.searchc                 C   s   t | |||S r#   )r   Z IndexAdditiveQuantizer_sa_encoder  r   r   r   r    s    z IndexAdditiveQuantizer.sa_encodec                 C   s   t | |||S r#   )r   Z IndexAdditiveQuantizer_sa_decoder  r   r   r   r    s    z IndexAdditiveQuantizer.sa_decodec                 C   s
   t | S r#   )r   Z4IndexAdditiveQuantizer_get_FlatCodesDistanceComputerr@   r   r   r   r    s    z4IndexAdditiveQuantizer.get_FlatCodesDistanceComputer)N)r   r   r,   r-   r   r   r   r   r   ZIndexAdditiveQuantizer_aq_getZIndexAdditiveQuantizer_aq_setaqr?   ru  r  r  r  Zdelete_IndexAdditiveQuantizerrT   r   r   r   r   r    s   
r  c                   @   sR   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 Zd
d ZejZdS )IndexResidualQuantizer
     Index based on a residual quantizer. Stored vectors are
    approximated by residual quantization codes.
    Can also be used as a codec
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexResidualQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   2 The residual quantizer used to encode the vectorsc                 G   s   t | t j|  d S r#   )r   ZIndexResidualQuantizer_swiginitZnew_IndexResidualQuantizerrQ   r   r   r   r?     s    zIndexResidualQuantizer.__init__c                 C   s   t | ||S r#   )r   ZIndexResidualQuantizer_trainrm  r   r   r   rn    s    zIndexResidualQuantizer.trainN)r   r   r,   r-   r   r   r   r   r   ZIndexResidualQuantizer_rq_getZIndexResidualQuantizer_rq_setr%  r?   rn  Zdelete_IndexResidualQuantizerrT   r   r   r   r   r    s   r  c                   @   sJ   e Zd Zedd dd ddZeZeejej	Z
dd Zdd	 ZejZd
S )IndexLocalSearchQuantizerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z"IndexLocalSearchQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z"IndexLocalSearchQuantizer_swiginitZnew_IndexLocalSearchQuantizerrQ   r   r   r   r?     s    z"IndexLocalSearchQuantizer.__init__c                 C   s   t | ||S r#   )r   ZIndexLocalSearchQuantizer_trainrm  r   r   r   rn    s    zIndexLocalSearchQuantizer.trainN)r   r   r,   r   r   r   r   r   Z!IndexLocalSearchQuantizer_lsq_getZ!IndexLocalSearchQuantizer_lsq_setrS  r?   rn  Z delete_IndexLocalSearchQuantizerrT   r   r   r   r   r    s   r  c                   @   sR   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 Zd
d ZejZdS )IndexProductResidualQuantizerz- Index based on a product residual quantizer.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z&IndexProductResidualQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   : The product residual quantizer used to encode the vectorsc                 G   s   t | t j|  d S r#   )r   Z&IndexProductResidualQuantizer_swiginitZ!new_IndexProductResidualQuantizerrQ   r   r   r   r?     s    z&IndexProductResidualQuantizer.__init__c                 C   s   t | ||S r#   )r   Z#IndexProductResidualQuantizer_trainrm  r   r   r   rn    s    z#IndexProductResidualQuantizer.trainN)r   r   r,   r-   r   r   r   r   r   Z%IndexProductResidualQuantizer_prq_getZ%IndexProductResidualQuantizer_prq_setprqr?   rn  Z$delete_IndexProductResidualQuantizerrT   r   r   r   r   r    s   r  c                   @   sR   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 Zd
d ZejZdS ) IndexProductLocalSearchQuantizerz1 Index based on a product local search quantizer.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z)IndexProductLocalSearchQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   > The product local search quantizer used to encode the vectorsc                 G   s   t | t j|  d S r#   )r   Z)IndexProductLocalSearchQuantizer_swiginitZ$new_IndexProductLocalSearchQuantizerrQ   r   r   r   r?     s    z)IndexProductLocalSearchQuantizer.__init__c                 C   s   t | ||S r#   )r   Z&IndexProductLocalSearchQuantizer_trainrm  r   r   r   rn    s    z&IndexProductLocalSearchQuantizer.trainN)r   r   r,   r-   r   r   r   r   r   Z)IndexProductLocalSearchQuantizer_plsq_getZ)IndexProductLocalSearchQuantizer_plsq_setplsqr?   rn  Z'delete_IndexProductLocalSearchQuantizerrT   r   r   r   r   r    s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zeejejd	dZd
d ZdddZdd Zdd Zdd ZejZdS )AdditiveCoarseQuantizerz
     A "virtual" index where the elements are the residual quantizer centroids.

    Intended for use as a coarse quantizer in an IndexIVF.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z AdditiveCoarseQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z AdditiveCoarseQuantizer_swiginitZnew_AdditiveCoarseQuantizerrQ   r   r   r   r?     s    z AdditiveCoarseQuantizer.__init__z* norms of centroids, useful for knn-searchc                 C   s   t | ||S z N/A)r   ZAdditiveCoarseQuantizer_addrm  r   r   r   ro    s    zAdditiveCoarseQuantizer.addNc              	   C   s   t | ||||||S r#   )r   ZAdditiveCoarseQuantizer_searchrs  r   r   r   ru    s    zAdditiveCoarseQuantizer.searchc                 C   s   t | ||S r#   )r   Z#AdditiveCoarseQuantizer_reconstructr|  r   r   r   r    s    z#AdditiveCoarseQuantizer.reconstructc                 C   s   t | ||S r#   )r   ZAdditiveCoarseQuantizer_trainrm  r   r   r   rn    s    zAdditiveCoarseQuantizer.trainc                 C   s
   t | S r  )r   ZAdditiveCoarseQuantizer_resetr@   r   r   r   r|     s    zAdditiveCoarseQuantizer.reset)N)r   r   r,   r-   r   r   r   r   r   ZAdditiveCoarseQuantizer_aq_getZAdditiveCoarseQuantizer_aq_setr  r?   Z*AdditiveCoarseQuantizer_centroid_norms_getZ*AdditiveCoarseQuantizer_centroid_norms_setr  ro  ru  r  rn  r|   Zdelete_AdditiveCoarseQuantizerrT   r   r   r   r   r    s   
r  c                   @   sB   e Zd Zedd dd ddZeZeejej	Z
ejZdd ZdS )	'SearchParametersResidualCoarseQuantizerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z0SearchParametersResidualCoarseQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   Z0SearchParametersResidualCoarseQuantizer_swiginitZ+new_SearchParametersResidualCoarseQuantizerr@   r   r   r   r?     s    z0SearchParametersResidualCoarseQuantizer.__init__N)r   r   r,   r   r   r   r   r   Z7SearchParametersResidualCoarseQuantizer_beam_factor_getZ7SearchParametersResidualCoarseQuantizer_beam_factor_setbeam_factorZ.delete_SearchParametersResidualCoarseQuantizerrT   r?   r   r   r   r   r    s
   r  c                   @   sv   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZd	d
 ZdddZdd Zdd ZejZdS )ResidualCoarseQuantizerz
     The ResidualCoarseQuantizer is a bit specialized compared to the
    default AdditiveCoarseQuantizer because it can use a beam search
    at search time (slow but may be useful for very large vocabularies)
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z ResidualCoarseQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   r  ze
    factor between the beam size and the search k
    if negative, use exact search-to-centroid
    c                 C   s   t | |S )z$ computes centroid norms if required)r   Z'ResidualCoarseQuantizer_set_beam_factor)r   Znew_beam_factorr   r   r   set_beam_factor  s    z'ResidualCoarseQuantizer.set_beam_factorNc              	   C   s   t | ||||||S r#   )r   ZResidualCoarseQuantizer_searchrs  r   r   r   ru    s    zResidualCoarseQuantizer.searchc                 C   s   t | |S )z
         Copy the M first codebook levels from other. Useful to crop a
        ResidualQuantizer to its first M quantizers.
        )r   Z'ResidualCoarseQuantizer_initialize_fromr^   r   r   r   r6  "  s    z'ResidualCoarseQuantizer.initialize_fromc                 G   s   t | t j|  d S r#   )r   Z ResidualCoarseQuantizer_swiginitZnew_ResidualCoarseQuantizerrQ   r   r   r   r?   )  s    z ResidualCoarseQuantizer.__init__)N)r   r   r,   r-   r   r   r   r   r   ZResidualCoarseQuantizer_rq_getZResidualCoarseQuantizer_rq_setr%  Z'ResidualCoarseQuantizer_beam_factor_getZ'ResidualCoarseQuantizer_beam_factor_setr  r  ru  r6  r?   Zdelete_ResidualCoarseQuantizerrT   r   r   r   r   r    s   
r  c                   @   sF   e Zd Zedd dd ddZeZeejej	ddZ
dd ZejZd	S )
LocalSearchCoarseQuantizerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   0  r4   z#LocalSearchCoarseQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   0  r4   r7   r8   r  c                 G   s   t | t j|  d S r#   )r   Z#LocalSearchCoarseQuantizer_swiginitZnew_LocalSearchCoarseQuantizerrQ   r   r   r   r?   4  s    z#LocalSearchCoarseQuantizer.__init__N)r   r   r,   r   r   r   r   r   Z"LocalSearchCoarseQuantizer_lsq_getZ"LocalSearchCoarseQuantizer_lsq_setrS  r?   Z!delete_LocalSearchCoarseQuantizerrT   r   r   r   r   r  /  s
   r  c                   @   sR   e Zd Zedd dd ddZdd ZeZeej	ej
Zdd	 Zd
d ZejZdS )IOReaderc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ;  r4   zIOReader.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ;  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   =  s    zIOReader.__init__c                 C   s   t | |||S r#   )r   ZIOReader___call__r   ptrr[   nitemsr   r   r   r  B  s    zIOReader.__call__c                 C   s
   t | S r#   )r   ZIOReader_filedescriptorr@   r   r   r   filedescriptorE  s    zIOReader.filedescriptorN)r   r   r,   r   r   r?   r   r   r   ZIOReader_name_getZIOReader_name_setr   r  r  Zdelete_IOReaderrT   r   r   r   r   r  :  s   r  c                   @   sR   e Zd Zedd dd ddZdd ZeZeej	ej
Zdd	 Zd
d ZejZdS )IOWriterc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   L  r4   zIOWriter.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   L  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   N  s    zIOWriter.__init__c                 C   s   t | |||S r#   )r   ZIOWriter___call__r  r   r   r   r  S  s    zIOWriter.__call__c                 C   s
   t | S r#   )r   ZIOWriter_filedescriptorr@   r   r   r   r  V  s    zIOWriter.filedescriptorN)r   r   r,   r   r   r?   r   r   r   ZIOWriter_name_getZIOWriter_name_setr   r  r  Zdelete_IOWriterrT   r   r   r   r   r  K  s   r  c                   @   sX   e Zd Zedd dd ddZeZeejej	Z
eejejZdd Zdd	 ZejZd
S )VectorIOReaderc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ]  r4   zVectorIOReader.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ]  r4   r7   r8   c                 C   s   t | |||S r#   )r   ZVectorIOReader___call__r  r   r   r   r  b  s    zVectorIOReader.__call__c                 C   s   t | t   d S r#   )r   ZVectorIOReader_swiginitZnew_VectorIOReaderr@   r   r   r   r?   e  s    zVectorIOReader.__init__N)r   r   r,   r   r   r   r   r   ZVectorIOReader_data_getZVectorIOReader_data_setrZ   ZVectorIOReader_rp_getZVectorIOReader_rp_setrpr  r?   Zdelete_VectorIOReaderrT   r   r   r   r   r  \  s   r  c                   @   sJ   e Zd Zedd dd ddZeZeejej	Z
dd Zdd	 ZejZd
S )VectorIOWriterc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   l  r4   zVectorIOWriter.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   l  r4   r7   r8   c                 C   s   t | |||S r#   )r   ZVectorIOWriter___call__r  r   r   r   r  p  s    zVectorIOWriter.__call__c                 C   s   t | t   d S r#   )r   ZVectorIOWriter_swiginitZnew_VectorIOWriterr@   r   r   r   r?   s  s    zVectorIOWriter.__init__N)r   r   r,   r   r   r   r   r   ZVectorIOWriter_data_getZVectorIOWriter_data_setrZ   r  r?   Zdelete_VectorIOWriterrT   r   r   r   r   r  k  s   r  c                   @   s`   e Zd Zedd dd ddZeZeejej	Z
eejejZdd ZejZdd	 Zd
d ZdS )FileIOReaderc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   z  r4   zFileIOReader.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   z  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZFileIOReader_swiginitZnew_FileIOReaderrQ   r   r   r   r?     s    zFileIOReader.__init__c                 C   s   t | |||S r#   )r   ZFileIOReader___call__r  r   r   r   r    s    zFileIOReader.__call__c                 C   s
   t | S r#   )r   ZFileIOReader_filedescriptorr@   r   r   r   r    s    zFileIOReader.filedescriptorN)r   r   r,   r   r   r   r   r   ZFileIOReader_f_getZFileIOReader_f_setfZFileIOReader_need_close_getZFileIOReader_need_close_set
need_closer?   Zdelete_FileIOReaderrT   r  r  r   r   r   r   r  y  s   r  c                   @   s`   e Zd Zedd dd ddZeZeejej	Z
eejejZdd ZejZdd	 Zd
d ZdS )FileIOWriterc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zFileIOWriter.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZFileIOWriter_swiginitZnew_FileIOWriterrQ   r   r   r   r?     s    zFileIOWriter.__init__c                 C   s   t | |||S r#   )r   ZFileIOWriter___call__r  r   r   r   r    s    zFileIOWriter.__call__c                 C   s
   t | S r#   )r   ZFileIOWriter_filedescriptorr@   r   r   r   r    s    zFileIOWriter.filedescriptorN)r   r   r,   r   r   r   r   r   ZFileIOWriter_f_getZFileIOWriter_f_setr  ZFileIOWriter_need_close_getZFileIOWriter_need_close_setr  r?   Zdelete_FileIOWriterrT   r  r  r   r   r   r   r    s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejddZeejejddZeejejd	dZeejejZeejejZd
d Zdd Zej Z!dS )BufferedIOReaderzB wraps an ioreader to make buffered reads to avoid too small readsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zBufferedIOReader.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z offset in input streamz# number of bytes returned to callerz' range of available bytes in the bufferc                 G   s   t | t j|  dS )z
        :type bsz: int, optional
        :param bsz:    buffer size (bytes). Reads will be done by batched of
                          this size
        N)r   ZBufferedIOReader_swiginitZnew_BufferedIOReaderrQ   r   r   r   r?     s    zBufferedIOReader.__init__c                 C   s   t | |||S r#   )r   ZBufferedIOReader___call__r  r   r   r   r    s    zBufferedIOReader.__call__N)"r   r   r,   r-   r   r   r   r   r   ZBufferedIOReader_reader_getZBufferedIOReader_reader_setreaderZBufferedIOReader_bsz_getZBufferedIOReader_bsz_setbszZBufferedIOReader_ofs_getZBufferedIOReader_ofs_setofsZBufferedIOReader_ofs2_getZBufferedIOReader_ofs2_setofs2ZBufferedIOReader_b0_getZBufferedIOReader_b0_setb0ZBufferedIOReader_b1_getZBufferedIOReader_b1_setb1ZBufferedIOReader_buffer_getZBufferedIOReader_buffer_setbufferr?   r  Zdelete_BufferedIOReaderrT   r   r   r   r   r    s   r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejddZeejejddZeejejZdd	 Zd
d ZejZdS )BufferedIOWriterc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zBufferedIOWriter.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z% number of bytes received from callerz amount of data in bufferc                 G   s   t | t j|  d S r#   )r   ZBufferedIOWriter_swiginitZnew_BufferedIOWriterrQ   r   r   r   r?     s    zBufferedIOWriter.__init__c                 C   s   t | |||S r#   )r   ZBufferedIOWriter___call__r  r   r   r   r    s    zBufferedIOWriter.__call__N)r   r   r,   r   r   r   r   r   ZBufferedIOWriter_writer_getZBufferedIOWriter_writer_setwriterZBufferedIOWriter_bsz_getZBufferedIOWriter_bsz_setr  ZBufferedIOWriter_ofs_getZBufferedIOWriter_ofs_setr  ZBufferedIOWriter_ofs2_getZBufferedIOWriter_ofs2_setr  ZBufferedIOWriter_b0_getZBufferedIOWriter_b0_setr  ZBufferedIOWriter_buffer_getZBufferedIOWriter_buffer_setr  r?   r  Zdelete_BufferedIOWriterrT   r   r   r   r   r    s   r  c                  G   s
   t j|  S r#   )r   fourccr   r   r   r   r    s    r  c                  G   s
   t j|  S r#   )r   
fourcc_invr   r   r   r   r    s    r  c                 C   s
   t | S r#   )r   fourcc_inv_printabler1   r   r   r   r    s    r  c                   @   sP   e Zd ZdZedd dd ddZdd ZeZe	j
Zd	d
 Zdd Zdd ZdS )InvertedListsIteratorz_
    Definition of inverted lists + a few common classes that implement
    the interface.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zInvertedListsIterator.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zInvertedListsIterator.__init__c                 C   s
   t | S r#   )r   Z"InvertedListsIterator_is_availabler@   r   r   r   is_available  s    z"InvertedListsIterator.is_availablec                 C   s
   t | S r#   )r   ZInvertedListsIterator_nextr@   r   r   r   rH     s    zInvertedListsIterator.nextc                 C   s
   t | S r#   )r   Z&InvertedListsIterator_get_id_and_codesr@   r   r   r   get_id_and_codes  s    z&InvertedListsIterator.get_id_and_codesN)r   r   r,   r-   r   r   r?   r   r   r   Zdelete_InvertedListsIteratorrT   r  rH   r  r   r   r   r   r    s   r  c                   @   s@  e Zd ZdZedd dd ddZdd ZeZee	j
e	jd	dZee	je	jd
dZee	je	jddZe	jZe	jZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd7ddZd8dd Z d9d!d"Z!d#d$ Z"d%d& Z#d'd( Z$d)d* Z%d+d, Z&d-d. Z'e	j(Z)e	j*Z+e	j,Z-e	j.Z/e	j0Z1d/d0 Z2d1d2 Z3d3d4 Z4d5d6 Z5dS ):InvertedListsz
     Table of inverted lists
    multithreading rules:
    - concurrent read accesses are allowed
    - concurrent update accesses are allowed
    - for resize and add_entries, only concurrent access to different lists
      are allowed
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zInvertedLists.__init__ number of possible key values code size per vector in bytesz8 request to use iterator rather than get_codes / get_idsc                 C   s   t | |S )z get the size of a list)r   ZInvertedLists_list_sizer   list_nor   r   r   	list_size  s    zInvertedLists.list_sizec                 C   s   t | |S )z
         get the codes for an inverted list
        must be released by release_codes

        :rtype: uint8_t
        :return: codes    size list_size * code_size
        )r   ZInvertedLists_get_codesr  r   r   r   	get_codes	  s    zInvertedLists.get_codesc                 C   s   t | |S )z
         get the ids for an inverted list
        must be released by release_ids

        :rtype: int
        :return: ids      size list_size
        )r   ZInvertedLists_get_idsr  r   r   r   get_ids  s    zInvertedLists.get_idsc                 C   s   t | ||S )zC release codes returned by get_codes (default implementation is nop)r   ZInvertedLists_release_codesr   r  r   r   r   r   release_codes  s    zInvertedLists.release_codesc                 C   s   t | ||S )z  release ids returned by get_ids)r   ZInvertedLists_release_idsr   r  r'  r   r   r   release_ids!  s    zInvertedLists.release_idsc                 C   s   t | ||S )zN
        :rtype: int
        :return: a single id in an inverted list
        )r   ZInvertedLists_get_single_idr   r  r  r   r   r   get_single_id%  s    zInvertedLists.get_single_idc                 C   s   t | ||S )z
        :rtype: uint8_t
        :return: a single code in an inverted list
            (should be deallocated with release_codes)
        )r   ZInvertedLists_get_single_coder
  r   r   r   get_single_code,  s    zInvertedLists.get_single_codec                 C   s   t | ||S )zs
        prepare the following lists (default does nothing)
        a list can be -1 hence the signed long
        )r   ZInvertedLists_prefetch_listsr   list_nosnlistr   r   r   prefetch_lists4  s    zInvertedLists.prefetch_listsNc                 C   s   t | ||S )z check if the list is empty)r   ZInvertedLists_is_emptyr   r  inverted_list_contextr   r   r   is_empty;  s    zInvertedLists.is_emptyc                 C   s   t | ||S )z) get iterable for lists that use_iterator)r   ZInvertedLists_get_iteratorr  r   r   r   get_iterator?  s    zInvertedLists.get_iteratorc                 C   s   t | ||||S )z" add one entry to an inverted list)r   ZInvertedLists_add_entry)r   r  Ztheidr   r  r   r   r   	add_entryC  s    zInvertedLists.add_entryc                 C   s   t | ||||S r#   )r   ZInvertedLists_add_entriesr   r  n_entryr'  r   r   r   r   add_entriesG  s    zInvertedLists.add_entriesc                 C   s   t | ||||S r#   )r   ZInvertedLists_update_entry)r   r  r  idr   r   r   r   update_entryJ  s    zInvertedLists.update_entryc                 C   s   t | |||||S r#   )r   ZInvertedLists_update_entriesr   r  r  r  r'  r   r   r   r   update_entriesM  s    zInvertedLists.update_entriesc                 C   s   t | ||S r#   )r   ZInvertedLists_resizer   r  new_sizer   r   r   r]   P  s    zInvertedLists.resizec                 C   s
   t | S r#   )r   ZInvertedLists_resetr@   r   r   r   r|   S  s    zInvertedLists.resetc                 C   s   t | ||S )z- move all entries from oivf (empty on output))r   ZInvertedLists_merge_from)r   Zoivfr  r   r   r   r  V  s    zInvertedLists.merge_fromc                 C   s   t | ||||S )z
         copy a subset of the entries index to the other index
        :rtype: int
        :return: number of entries copied
        )r   ZInvertedLists_copy_subset_tor   r_   Zsubset_typeZa1Za2r   r   r   copy_subset_to_  s    zInvertedLists.copy_subset_toc                 C   s
   t | S )z& 1= perfectly balanced, >1: imbalanced)r   ZInvertedLists_imbalance_factorr@   r   r   r   r   g  s    zInvertedLists.imbalance_factorc                 C   s
   t | S )z, display some stats about the inverted lists)r   ZInvertedLists_print_statsr@   r   r   r   print_statsk  s    zInvertedLists.print_statsc                 C   s
   t | S )z sum up list sizes)r   ZInvertedLists_compute_ntotalr@   r   r   r   compute_ntotalo  s    zInvertedLists.compute_ntotal)N)N)N)6r   r   r,   r-   r   r   r?   r   r   r   ZInvertedLists_nlist_getZInvertedLists_nlist_setr  ZInvertedLists_code_size_getZInvertedLists_code_size_setr   ZInvertedLists_use_iterator_getZInvertedLists_use_iterator_setZuse_iteratorZdelete_InvertedListsrT   ZInvertedLists_INVALID_CODE_SIZEZINVALID_CODE_SIZEr  r  r  r  r	  r  r  r  r  r  r  r  r  r  r]   r|   r  Z"InvertedLists_SUBSET_TYPE_ID_RANGEZSUBSET_TYPE_ID_RANGEZ InvertedLists_SUBSET_TYPE_ID_MODZSUBSET_TYPE_ID_MODZ'InvertedLists_SUBSET_TYPE_ELEMENT_RANGEZSUBSET_TYPE_ELEMENT_RANGEZ*InvertedLists_SUBSET_TYPE_INVLIST_FRACTIONZSUBSET_TYPE_INVLIST_FRACTIONZ!InvertedLists_SUBSET_TYPE_INVLISTZSUBSET_TYPE_INVLISTr   r   r!  r"  r   r   r   r   r    sF   	




r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd ZdddZejZdS )ArrayInvertedListsz> simple (default) implementation as an array of inverted listsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   x  r4   zArrayInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   x  r4   r7   r8   z Inverted lists for indexesc                 C   s   t | t || d S r#   )r   ZArrayInvertedLists_swiginitZnew_ArrayInvertedLists)r   r  r   r   r   r   r?   }  s    zArrayInvertedLists.__init__c                 C   s   t | |S r#   )r   ZArrayInvertedLists_list_sizer  r   r   r   r    s    zArrayInvertedLists.list_sizec                 C   s   t | |S r#   )r   ZArrayInvertedLists_get_codesr  r   r   r   r    s    zArrayInvertedLists.get_codesc                 C   s   t | |S r#   )r   ZArrayInvertedLists_get_idsr  r   r   r   r    s    zArrayInvertedLists.get_idsc                 C   s   t | ||||S r#   )r   ZArrayInvertedLists_add_entriesr  r   r   r   r    s    zArrayInvertedLists.add_entriesc                 C   s   t | |||||S r#   )r   Z!ArrayInvertedLists_update_entriesr  r   r   r   r    s    z!ArrayInvertedLists.update_entriesc                 C   s   t | ||S r#   )r   ZArrayInvertedLists_resizer  r   r   r   r]     s    zArrayInvertedLists.resizec                 C   s   t | |S )z6 permute the inverted lists, map maps new_id to old_id)r   Z#ArrayInvertedLists_permute_invlistsr   r  r   r   r   permute_invlists  s    z#ArrayInvertedLists.permute_invlistsNc                 C   s   t | ||S r#   )r   ZArrayInvertedLists_is_emptyr  r   r   r   r    s    zArrayInvertedLists.is_empty)N)r   r   r,   r-   r   r   r   r   r   ZArrayInvertedLists_codes_getZArrayInvertedLists_codes_setr   ZArrayInvertedLists_ids_getZArrayInvertedLists_ids_setr'  r?   r  r  r  r  r  r]   r%  r  Zdelete_ArrayInvertedListsrT   r   r   r   r   r#  u  s   
r#  c                   @   sP   e Zd ZdZedd dd ddZdd ZeZd	d
 Z	dd Z
dd ZejZdS )ReadOnlyInvertedListsz+ invlists that fail for all write functionsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zReadOnlyInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zReadOnlyInvertedLists.__init__c                 C   s   t | ||||S r#   )r   Z!ReadOnlyInvertedLists_add_entriesr  r   r   r   r    s    z!ReadOnlyInvertedLists.add_entriesc                 C   s   t | |||||S r#   )r   Z$ReadOnlyInvertedLists_update_entriesr  r   r   r   r    s    z$ReadOnlyInvertedLists.update_entriesc                 C   s   t | ||S r#   )r   ZReadOnlyInvertedLists_resizer  r   r   r   r]     s    zReadOnlyInvertedLists.resizeN)r   r   r,   r-   r   r   r?   r   r   r  r  r]   r   Zdelete_ReadOnlyInvertedListsrT   r   r   r   r   r&    s   r&  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZejZdS )HStackInvertedListsz# Horizontal stack of inverted listsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zHStackInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t || dS z1 build InvertedLists by concatenating nil of themN)r   ZHStackInvertedLists_swiginitZnew_HStackInvertedListsr   nililsr   r   r   r?     s    zHStackInvertedLists.__init__c                 C   s   t | |S r#   )r   ZHStackInvertedLists_list_sizer  r   r   r   r    s    zHStackInvertedLists.list_sizec                 C   s   t | |S r#   )r   ZHStackInvertedLists_get_codesr  r   r   r   r    s    zHStackInvertedLists.get_codesc                 C   s   t | |S r#   )r   ZHStackInvertedLists_get_idsr  r   r   r   r    s    zHStackInvertedLists.get_idsc                 C   s   t | ||S r#   )r   Z"HStackInvertedLists_prefetch_listsr  r   r   r   r    s    z"HStackInvertedLists.prefetch_listsc                 C   s   t | ||S r#   )r   Z!HStackInvertedLists_release_codesr  r   r   r   r    s    z!HStackInvertedLists.release_codesc                 C   s   t | ||S r#   )r   ZHStackInvertedLists_release_idsr  r   r   r   r	    s    zHStackInvertedLists.release_idsc                 C   s   t | ||S r#   )r   Z!HStackInvertedLists_get_single_idr
  r   r   r   r    s    z!HStackInvertedLists.get_single_idc                 C   s   t | ||S r#   )r   Z#HStackInvertedLists_get_single_coder
  r   r   r   r    s    z#HStackInvertedLists.get_single_codeN)r   r   r,   r-   r   r   r   r   r   ZHStackInvertedLists_ils_getZHStackInvertedLists_ils_setr+  r?   r  r  r  r  r  r	  r  r  Zdelete_HStackInvertedListsrT   r   r   r   r   r'    s   r'  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZejZdS )SliceInvertedListsz3 vertical slice of indexes in another InvertedListsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zSliceInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t ||| d S r#   )r   ZSliceInvertedLists_swiginitZnew_SliceInvertedLists)r   ilr  i1r   r   r   r?     s    zSliceInvertedLists.__init__c                 C   s   t | |S r#   )r   ZSliceInvertedLists_list_sizer  r   r   r   r    s    zSliceInvertedLists.list_sizec                 C   s   t | |S r#   )r   ZSliceInvertedLists_get_codesr  r   r   r   r    s    zSliceInvertedLists.get_codesc                 C   s   t | |S r#   )r   ZSliceInvertedLists_get_idsr  r   r   r   r    s    zSliceInvertedLists.get_idsc                 C   s   t | ||S r#   )r   Z SliceInvertedLists_release_codesr  r   r   r   r    s    z SliceInvertedLists.release_codesc                 C   s   t | ||S r#   )r   ZSliceInvertedLists_release_idsr  r   r   r   r	    s    zSliceInvertedLists.release_idsc                 C   s   t | ||S r#   )r   Z SliceInvertedLists_get_single_idr
  r   r   r   r    s    z SliceInvertedLists.get_single_idc                 C   s   t | ||S r#   )r   Z"SliceInvertedLists_get_single_coder
  r   r   r   r    s    z"SliceInvertedLists.get_single_codec                 C   s   t | ||S r#   )r   Z!SliceInvertedLists_prefetch_listsr  r   r   r   r    s    z!SliceInvertedLists.prefetch_listsN)r   r   r,   r-   r   r   r   r   r   ZSliceInvertedLists_il_getZSliceInvertedLists_il_setr-  ZSliceInvertedLists_i0_getZSliceInvertedLists_i0_setr  ZSliceInvertedLists_i1_getZSliceInvertedLists_i1_setr.  r?   r  r  r  r  r	  r  r  r  Zdelete_SliceInvertedListsrT   r   r   r   r   r,    s    r,  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZdd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd ZejZdS )VStackInvertedListsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zVStackInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t || dS r(  )r   ZVStackInvertedLists_swiginitZnew_VStackInvertedListsr)  r   r   r   r?     s    zVStackInvertedLists.__init__c                 C   s   t | |S r#   )r   ZVStackInvertedLists_list_sizer  r   r   r   r    s    zVStackInvertedLists.list_sizec                 C   s   t | |S r#   )r   ZVStackInvertedLists_get_codesr  r   r   r   r    s    zVStackInvertedLists.get_codesc                 C   s   t | |S r#   )r   ZVStackInvertedLists_get_idsr  r   r   r   r    s    zVStackInvertedLists.get_idsc                 C   s   t | ||S r#   )r   Z!VStackInvertedLists_release_codesr  r   r   r   r    s    z!VStackInvertedLists.release_codesc                 C   s   t | ||S r#   )r   ZVStackInvertedLists_release_idsr  r   r   r   r	    s    zVStackInvertedLists.release_idsc                 C   s   t | ||S r#   )r   Z!VStackInvertedLists_get_single_idr
  r   r   r   r    s    z!VStackInvertedLists.get_single_idc                 C   s   t | ||S r#   )r   Z#VStackInvertedLists_get_single_coder
  r   r   r   r    s    z#VStackInvertedLists.get_single_codec                 C   s   t | ||S r#   )r   Z"VStackInvertedLists_prefetch_listsr  r   r   r   r    s    z"VStackInvertedLists.prefetch_listsN)r   r   r,   r   r   r   r   r   ZVStackInvertedLists_ils_getZVStackInvertedLists_ils_setr+  ZVStackInvertedLists_cumsz_getZVStackInvertedLists_cumsz_setZcumszr?   r  r  r  r  r	  r  r  r  Zdelete_VStackInvertedListsrT   r   r   r   r   r/    s   r/  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZejZdS )MaskedInvertedListsz
     use the first inverted lists if they are non-empty otherwise use the second

    This is useful if il1 has a few inverted lists that are too long,
    and that il0 has replacement lists for those, with empty lists for
    the others.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ,  r4   zMaskedInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ,  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZMaskedInvertedLists_swiginitZnew_MaskedInvertedLists)r   il0il1r   r   r   r?   1  s    zMaskedInvertedLists.__init__c                 C   s   t | |S r#   )r   ZMaskedInvertedLists_list_sizer  r   r   r   r  4  s    zMaskedInvertedLists.list_sizec                 C   s   t | |S r#   )r   ZMaskedInvertedLists_get_codesr  r   r   r   r  7  s    zMaskedInvertedLists.get_codesc                 C   s   t | |S r#   )r   ZMaskedInvertedLists_get_idsr  r   r   r   r  :  s    zMaskedInvertedLists.get_idsc                 C   s   t | ||S r#   )r   Z!MaskedInvertedLists_release_codesr  r   r   r   r  =  s    z!MaskedInvertedLists.release_codesc                 C   s   t | ||S r#   )r   ZMaskedInvertedLists_release_idsr  r   r   r   r	  @  s    zMaskedInvertedLists.release_idsc                 C   s   t | ||S r#   )r   Z!MaskedInvertedLists_get_single_idr
  r   r   r   r  C  s    z!MaskedInvertedLists.get_single_idc                 C   s   t | ||S r#   )r   Z#MaskedInvertedLists_get_single_coder
  r   r   r   r  F  s    z#MaskedInvertedLists.get_single_codec                 C   s   t | ||S r#   )r   Z"MaskedInvertedLists_prefetch_listsr  r   r   r   r  I  s    z"MaskedInvertedLists.prefetch_listsN)r   r   r,   r-   r   r   r   r   r   ZMaskedInvertedLists_il0_getZMaskedInvertedLists_il0_setr1  ZMaskedInvertedLists_il1_getZMaskedInvertedLists_il1_setr2  r?   r  r  r  r  r	  r  r  r  Zdelete_MaskedInvertedListsrT   r   r   r   r   r0  #  s   r0  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZejZdS )StopWordsInvertedListszr
    if the inverted list in il is smaller than maxsize then return it,
    otherwise return an empty invlist
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   U  r4   zStopWordsInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   U  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZStopWordsInvertedLists_swiginitZnew_StopWordsInvertedLists)r   r-  maxsizer   r   r   r?   Z  s    zStopWordsInvertedLists.__init__c                 C   s   t | |S r#   )r   Z StopWordsInvertedLists_list_sizer  r   r   r   r  ]  s    z StopWordsInvertedLists.list_sizec                 C   s   t | |S r#   )r   Z StopWordsInvertedLists_get_codesr  r   r   r   r  `  s    z StopWordsInvertedLists.get_codesc                 C   s   t | |S r#   )r   ZStopWordsInvertedLists_get_idsr  r   r   r   r  c  s    zStopWordsInvertedLists.get_idsc                 C   s   t | ||S r#   )r   Z$StopWordsInvertedLists_release_codesr  r   r   r   r  f  s    z$StopWordsInvertedLists.release_codesc                 C   s   t | ||S r#   )r   Z"StopWordsInvertedLists_release_idsr  r   r   r   r	  i  s    z"StopWordsInvertedLists.release_idsc                 C   s   t | ||S r#   )r   Z$StopWordsInvertedLists_get_single_idr
  r   r   r   r  l  s    z$StopWordsInvertedLists.get_single_idc                 C   s   t | ||S r#   )r   Z&StopWordsInvertedLists_get_single_coder
  r   r   r   r  o  s    z&StopWordsInvertedLists.get_single_codec                 C   s   t | ||S r#   )r   Z%StopWordsInvertedLists_prefetch_listsr  r   r   r   r  r  s    z%StopWordsInvertedLists.prefetch_listsN)r   r   r,   r-   r   r   r   r   r   ZStopWordsInvertedLists_il0_getZStopWordsInvertedLists_il0_setr1  Z"StopWordsInvertedLists_maxsize_getZ"StopWordsInvertedLists_maxsize_setr4  r?   r  r  r  r  r	  r  r  r  Zdelete_StopWordsInvertedListsrT   r   r   r   r   r3  O  s   r3  c                   @   s   e Zd ZdZedd dd ddZdd ZeZee	j
d	dZee	jd
dZdd Zdd Zdd Ze	jZedd Zedd Zedd Zedd ZdS )InvertedListsIOHooka  
     Callbacks to handle other types of InvertedList objects.

    The callbacks should be registered with add_callback before calling
    read_index or read_InvertedLists. The callbacks for
    OnDiskInvertedLists are registrered by default. The invlist type is
    identified by:

    - the key (a fourcc) at read time
    - the class name (as given by typeid.name) at write time
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zInvertedListsIOHook.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zInvertedListsIOHook.__init__z string version of the fourccz typeid.namec                 C   s   t | ||S )z7 write the index to the IOWriter (including the fourcc))r   ZInvertedListsIOHook_write)r   r+  r  r   r   r   r     s    zInvertedListsIOHook.writec                 C   s   t | ||S )z3 called when the fourcc matches this class's fourcc)r   ZInvertedListsIOHook_read)r   r  io_flagsr   r   r   r     s    zInvertedListsIOHook.readc                 C   s   t | |||||S )a  
         read from a ArrayInvertedLists into this invertedlist type.
        For this to work, the callback has to be enabled and the io_flag has to
        be set to IO_FLAG_SKIP_IVF_DATA | (16 upper bits of the fourcc)

        (default implementation fails)
        )r   Z+InvertedListsIOHook_read_ArrayInvertedLists)r   r  r6  r  r   sizesr   r   r   read_ArrayInvertedLists  s    z+InvertedListsIOHook.read_ArrayInvertedListsc                 C   s
   t | S r#   )r   Z InvertedListsIOHook_add_callbackarg1r   r   r   add_callback  s    z InvertedListsIOHook.add_callbackc                   C   s   t  S r#   )r   Z#InvertedListsIOHook_print_callbacksr   r   r   r   print_callbacks  s    z#InvertedListsIOHook.print_callbacksc                 C   s
   t | S r#   )r   ZInvertedListsIOHook_lookup)hr   r   r   lookup  s    zInvertedListsIOHook.lookupc                 C   s
   t | S r#   )r   Z$InvertedListsIOHook_lookup_classname)	classnamer   r   r   lookup_classname  s    z$InvertedListsIOHook.lookup_classnameN)r   r   r,   r-   r   r   r?   r   r   r   ZInvertedListsIOHook_key_getr}  Z!InvertedListsIOHook_classname_getr?  r   r   r8  Zdelete_InvertedListsIOHookrT   r  r;  r<  r>  r@  r   r   r   r   r5  x  s$   	


r5  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZeejejZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zej Z!dS )BlockInvertedListsaZ  
     Inverted Lists that are organized by blocks.

    Different from the regular inverted lists, the codes are organized by blocks
    of size block_size bytes that reprsent a set of n_per_block. Therefore, code
    allocations are always rounded up to block_size bytes. The codes are also
    aligned on 32-byte boundaries for use with SIMD.

    To avoid misinterpretations, the code_size is set to (size_t)(-1), even if
    arguably the amount of memory consumed by code is block_size / n_per_block.

    The writing functions add_entries and update_entries operate on block-aligned
    data.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zBlockInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZBlockInvertedLists_swiginitZnew_BlockInvertedListsrQ   r   r   r   r?     s    zBlockInvertedLists.__init__c                 C   s   t | |S r#   )r   ZBlockInvertedLists_list_sizer  r   r   r   r    s    zBlockInvertedLists.list_sizec                 C   s   t | |S r#   )r   ZBlockInvertedLists_get_codesr  r   r   r   r    s    zBlockInvertedLists.get_codesc                 C   s   t | |S r#   )r   ZBlockInvertedLists_get_idsr  r   r   r   r    s    zBlockInvertedLists.get_idsc                 C   s   t | |S )z" remove ids from the InvertedLists)r   ZBlockInvertedLists_remove_idsrz  r   r   r   r{    s    zBlockInvertedLists.remove_idsc                 C   s   t | ||||S r#   )r   ZBlockInvertedLists_add_entriesr  r   r   r   r    s    zBlockInvertedLists.add_entriesc                 C   s   t | |||||S z not implemented)r   Z!BlockInvertedLists_update_entriesr  r   r   r   r    s    z!BlockInvertedLists.update_entriesc                 C   s   t | ||S r#   )r   ZBlockInvertedLists_resizer  r   r   r   r]     s    zBlockInvertedLists.resizeN)"r   r   r,   r-   r   r   r   r   r   Z"BlockInvertedLists_n_per_block_getZ"BlockInvertedLists_n_per_block_setZn_per_blockZ!BlockInvertedLists_block_size_getZ!BlockInvertedLists_block_size_setrr  ZBlockInvertedLists_packer_getZBlockInvertedLists_packer_setpackerZBlockInvertedLists_codes_getZBlockInvertedLists_codes_setr   ZBlockInvertedLists_ids_getZBlockInvertedLists_ids_setr'  r?   r  r  r  r{  r  r  r]   Zdelete_BlockInvertedListsrT   r   r   r   r   rA    s"   rA  c                 C   s   t | |S r#   )r   lo_build)Zlist_idr  r   r   r   rD    s    rD  c                 C   s
   t | S r#   )r   	lo_listnolor   r   r   rE    s    rE  c                 C   s
   t | S r#   )r   	lo_offsetrF  r   r   r   rH    s    rH  c                   @   s   e Zd ZdZedd dd ddZeZej	Z
ejZejZeejejZeejejddZeejejZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z ej!Z"dS )	DirectMapz8Direct map: a way to map back from ids to inverted listsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zDirectMap.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   zF map for direct access to the elements. Map ids to LO-encoded entries.c                 C   s   t | t   d S r#   )r   ZDirectMap_swiginitZnew_DirectMapr@   r   r   r   r?     s    zDirectMap.__init__c                 C   s   t | |||S )z set type and initialize)r   ZDirectMap_set_type)r   new_typeinvlistsr  r   r   r   set_type  s    zDirectMap.set_typec                 C   s   t | |S )z get an entry)r   ZDirectMap_getr   r  r   r   r   rV    s    zDirectMap.getc                 C   s
   t | S )z for quick checks)r   ZDirectMap_nor@   r   r   r   no  s    zDirectMap.noc                 C   s   t | |S )zT
        update the direct_map

         throw if Array and ids is not NULL
        )r   ZDirectMap_check_can_add)r   r'  r   r   r   check_can_add  s    zDirectMap.check_can_addc                 C   s   t | |||S )z non thread-safe version)r   ZDirectMap_add_single_id)r   r  r  r  r   r   r   add_single_id  s    zDirectMap.add_single_idc                 C   s
   t | S )z remove all entries)r   ZDirectMap_clearr@   r   r   r   rY     s    zDirectMap.clearc                 C   s   t | ||S )z
        operations on inverted lists that require translation with a DirectMap

         remove ids from the InvertedLists, possibly using the direct map
        )r   ZDirectMap_remove_ids)r   r5  rK  r   r   r   r{    s    zDirectMap.remove_idsc                 C   s   t | |||||S )z% update entries, using the direct map)r   ZDirectMap_update_codes)r   rK  rB   r'  r  r   r   r   r   update_codes#  s    zDirectMap.update_codesN)#r   r   r,   r-   r   r   r   r   r   ZDirectMap_NoMapZNoMapZDirectMap_ArrayZArrayZDirectMap_HashtableZ	HashtableZDirectMap_type_getZDirectMap_type_setr   ZDirectMap_array_getZDirectMap_array_setarrayZDirectMap_hashtable_getZDirectMap_hashtable_setZ	hashtabler?   rL  rV  rN  rO  rP  rY   r{  rQ  Zdelete_DirectMaprT   r   r   r   r   rI    s&   rI  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZeejejZeejejZdd Zd	d
 ZejZdS )DirectMapAddz+ Thread-safe way of updating the direct_mapc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   -  r4   zDirectMapAdd.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   -  r4   r7   r8   c                 C   s   t | t ||| d S r#   )r   ZDirectMapAdd_swiginitZnew_DirectMapAdd)r   
direct_maprB   rq  r   r   r   r?   6  s    zDirectMapAdd.__init__c                 C   s   t | |||S )z5 add vector i (with id xids[i]) at list_no and offset)r   ZDirectMapAdd_add)r   r   r  r  r   r   r   ro  9  s    zDirectMapAdd.addN)r   r   r,   r-   r   r   r   r   r   ZDirectMapAdd_direct_map_getZDirectMapAdd_direct_map_setrT  ZDirectMapAdd_type_getZDirectMapAdd_type_setr   ZDirectMapAdd_ntotal_getZDirectMapAdd_ntotal_setr  ZDirectMapAdd_n_getZDirectMapAdd_n_setrB   ZDirectMapAdd_xids_getZDirectMapAdd_xids_setrq  ZDirectMapAdd_all_ofs_getZDirectMapAdd_all_ofs_setZall_ofsr?   ro  Zdelete_DirectMapAddrT   r   r   r   r   rS  *  s   rS  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZeejejddZdd Zdd Zdd Zdd Zdd Zej Z!dS )Level1Quantizerz
     Encapsulates a quantizer object for the IndexIVF

    The class isolates the fields that are independent of the storage
    of the lists (especially training)
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   H  r4   zLevel1Quantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   H  r4   r7   r8   . quantizer that maps vectors to inverted listsz number of inverted listsz
    = 0: use the quantizer as index in a kmeans training
    = 1: just pass on the training set to the train() of the quantizer
    = 2: kmeans training on a flat index + add the centroids to the quantizer
    " whether object owns the quantizer& to override default clustering params) to override index used during clusteringc                 C   s   t | ||||S )zF Trains the quantizer and calls train_residual to train sub-quantizers)r   ZLevel1Quantizer_train_q1)r   rB   r2   r   rf  r   r   r   train_q1U  s    zLevel1Quantizer.train_q1c                 C   s
   t | S )z7 compute the number of bytes required to store list ids)r   Z Level1Quantizer_coarse_code_sizer@   r   r   r   coarse_code_sizeY  s    z Level1Quantizer.coarse_code_sizec                 C   s   t | ||S r#   )r   ZLevel1Quantizer_encode_listno)r   r  r   r   r   r   encode_listno]  s    zLevel1Quantizer.encode_listnoc                 C   s   t | |S r#   )r   ZLevel1Quantizer_decode_listnor  r   r   r   decode_listno`  s    zLevel1Quantizer.decode_listnoc                 G   s   t | t j|  d S r#   )r   ZLevel1Quantizer_swiginitZnew_Level1QuantizerrQ   r   r   r   r?   c  s    zLevel1Quantizer.__init__N)"r   r   r,   r-   r   r   r   r   r   ZLevel1Quantizer_quantizer_getZLevel1Quantizer_quantizer_set	quantizerZLevel1Quantizer_nlist_getZLevel1Quantizer_nlist_setr  Z*Level1Quantizer_quantizer_trains_alone_getZ*Level1Quantizer_quantizer_trains_alone_setZquantizer_trains_aloneZLevel1Quantizer_own_fields_getZLevel1Quantizer_own_fields_setr  ZLevel1Quantizer_cp_getZLevel1Quantizer_cp_setr  Z$Level1Quantizer_clustering_index_getZ$Level1Quantizer_clustering_index_setclustering_indexrZ  r[  r\  r]  r?   Zdelete_Level1QuantizerrT   r   r   r   r   rU  @  s   rU  c                   @   sx   e Zd Zedd dd ddZeZeejej	ddZ
eejejddZeejejZeejejddZejZd	d
 ZdS )SearchParametersIVFc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   j  r4   zSearchParametersIVF.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   j  r4   r7   r8    number of probes at query time' max nb of codes to visit to do a queryz( context object to pass to InvertedListsc                 C   s   t | t   d S r#   )r   ZSearchParametersIVF_swiginitZnew_SearchParametersIVFr@   r   r   r   r?   r  s    zSearchParametersIVF.__init__N)r   r   r,   r   r   r   r   r   ZSearchParametersIVF_nprobe_getZSearchParametersIVF_nprobe_setnprobeZ!SearchParametersIVF_max_codes_getZ!SearchParametersIVF_max_codes_set	max_codesZ(SearchParametersIVF_quantizer_params_getZ(SearchParametersIVF_quantizer_params_setZquantizer_paramsZ-SearchParametersIVF_inverted_list_context_getZ-SearchParametersIVF_inverted_list_context_setr  Zdelete_SearchParametersIVFrT   r?   r   r   r   r   r`  i  s   r`  c                   @   sl   e Zd Zedd dd ddZdd ZeZeej	ej
ddZeejejd	dZdddZdddZejZd
S )IndexIVFInterfacec                 C   s
   | j  S r#   r0   r1   r   r   r   r3   x  r4   zIndexIVFInterface.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   x  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   z  s    zIndexIVFInterface.__init__ra  rb  Nc                 C   s   t | |||||||||	|
S )a  
         search a set of vectors, that are pre-quantized by the IVF
         quantizer. Fill in the corresponding heaps with the query
         results. The default implementation uses InvertedListScanners
         to do the search.

        :type n: int
        :param n:      nb of vectors to query
        :type x: float
        :param x:      query vectors, size nx * d
        :type assign: int
        :param assign: coarse quantization indices, size nx * nprobe
        :type centroid_dis: float
        :param centroid_dis:
                          distances to coarse centroids, size nx * nprobe
        :param distance:
                          output distances, size n * k
        :type labels: int
        :param labels: output labels, size n * k
        :type store_pairs: boolean
        :param store_pairs: store inv list index + inv list offset
                                instead in upper/lower 32 bit of result,
                                instead of ids (used for reranking).
        :type params: :py:class:`IVFSearchParameters`, optional
        :param params: used to override the object's search parameters
        :type stats: :py:class:`IndexIVFStats`, optional
        :param stats:  search stats to be updated (can be null)
        )r   Z$IndexIVFInterface_search_preassignedr   rB   r2   r   ry  centroid_disr   r   store_pairsrt  statsr   r   r   search_preassigned  s    z$IndexIVFInterface.search_preassignedFc
           
      C   s   t | |||||||||	
S )aZ  
         Range search a set of vectors, that are pre-quantized by the IVF
         quantizer. Fill in the RangeSearchResults results. The default
        implementation uses InvertedListScanners to do the search.

        :param n:      nb of vectors to query
        :type x: float
        :param x:      query vectors, size nx * d
        :param assign: coarse quantization indices, size nx * nprobe
        :param centroid_dis:
                          distances to coarse centroids, size nx * nprobe
        :type result: :py:class:`RangeSearchResult`
        :param result: Output results
        :type store_pairs: boolean, optional
        :param store_pairs: store inv list index + inv list offset
                                instead in upper/lower 32 bit of result,
                                instead of ids (used for reranking).
        :type params: :py:class:`IVFSearchParameters`, optional
        :param params: used to override the object's search parameters
        :type stats: :py:class:`IndexIVFStats`, optional
        :param stats:  search stats to be updated (can be null)
        )r   Z*IndexIVFInterface_range_search_preassigned
r   r  r2   r   rc  
coarse_disr   rh  rt  ri  r   r   r   range_search_preassigned  s    z*IndexIVFInterface.range_search_preassigned)NN)FNN)r   r   r,   r   r   r?   r   r   r   ZIndexIVFInterface_nprobe_getZIndexIVFInterface_nprobe_setrc  ZIndexIVFInterface_max_codes_getZIndexIVFInterface_max_codes_setrd  rj  rm  Zdelete_IndexIVFInterfacerT   r   r   r   r   re  w  s   

re  c                   @   s  e Zd ZdZedd dd ddZdd ZeZee	j
e	jd	dZee	je	jZee	je	jd
dZee	je	jddZee	jZee	je	jddZee	je	jddZdd Zdd Zdd Z dd Z!dQddZ"dRddZ#dd Z$dd Z%d d! Z&dSd"d#Z'dTd$d%Z(dUd&d'Z)dVd(d)Z*dWd*d+Z+d,d- Z,d.d/ Z-d0d1 Z.dXd2d3Z/dYd4d5Z0d6d7 Z1d8d9 Z2d:d; Z3d<d= Z4d>d? Z5d@dA Z6e	j7Z8dBdC Z9dDdE Z:dZdGdHZ;dIdJ Z<d[dKdLZ=dMdN Z>dOdP Z?dS )\IndexIVFa  
     Index based on a inverted file (IVF)

    In the inverted file, the quantizer (an Index instance) provides a
    quantization index for each vector to be added. The quantization
    index maps to a list (aka inverted list or posting list), where the
    id of the vector is stored.

    The inverted list object is required only after trainng. If none is
    set externally, an ArrayInvertedLists is used automatically.

    At search time, the vector to be searched is also quantized, and
    only the list corresponding to the quantization index is
    searched. This speeds up the search by making it
    non-exhaustive. This can be relaxed using multi-probe search: a few
    (nprobe) quantization indices are selected and several inverted
    lists are visited.

    Sub-classes implement a post-filtering of the index that refines
    the distance estimation from the query to databse vectors.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexIVF.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zIndexIVF.__init__ Access to the actual datar   a]  
     Parallel mode determines how queries are parallelized with OpenMP

    0 (default): split over queries
    1: parallelize over inverted lists
    2: parallelize over both
    3: split over queries with a finer granularity

    PARALLEL_MODE_NO_HEAP_INIT: binary or with the previous to
    prevent the heap to be initialized and finalized
    z\
    optional map that maps back ids to invlist entries. This
    enables reconstruct()
    zX
    do the codes in the invlists encode the vectors relative to the
    centroids?
    c                 C   s
   t | S r#   )r   ZIndexIVF_resetr@   r   r   r   r|     s    zIndexIVF.resetc                 C   s   t | ||S )zE Trains the quantizer and calls train_encoder to train sub-quantizers)r   ZIndexIVF_trainrm  r   r   r   rn    s    zIndexIVF.trainc                 C   s   t | ||S )z! Calls add_with_ids with NULL ids)r   ZIndexIVF_addrm  r   r   r   ro    s    zIndexIVF.addc                 C   s   t | |||S )z1 default implementation that calls encode_vectors)r   ZIndexIVF_add_with_idsrp  r   r   r   rr    s    zIndexIVF.add_with_idsNc                 C   s   t | |||||S )aE  
         Implementation of vector addition where the vector assignments are
        predefined. The default implementation hands over the code extraction to
        encode_vectors.

        :type precomputed_idx: int
        :param precomputed_idx:    quantization indices for the input vectors
            (size n)
        )r   ZIndexIVF_add_corer   rB   r2   rq  precomputed_idxr  r   r   r   add_core  s    
zIndexIVF.add_coreFc                 C   s   t | |||||S )a1  
         Encodes a set of vectors as they would appear in the inverted lists

        :type list_nos: int
        :param list_nos:   inverted list ids as returned by the
                              quantizer (size n). -1s are ignored.
        :type codes: uint8_t
        :param codes:      output codes, size n * code_size
        :type include_listno: boolean, optional
        :param include_listno:
                              include the list ids in the code (in this case add
                              ceil(log8(nlist)) to the code size)
        )r   ZIndexIVF_encode_vectorsr   rB   r2   r  r   include_listnor   r   r   encode_vectors
  s    zIndexIVF.encode_vectorsc                 C   s   t | |||S r  )r   ZIndexIVF_add_sa_codesr  r   r   r   r    s    	zIndexIVF.add_sa_codesc                 C   s   t | |||S )z
         Train the encoder for the vectors.

        If by_residual then it is called with residuals and corresponding assign
        array, otherwise x is the raw training vectors and assign=nullptr
        )r   ZIndexIVF_train_encoderr   rB   r2   ry  r   r   r   train_encoder%  s    zIndexIVF.train_encoderc                 C   s
   t | S )zh
        can be redefined by subclasses to indicate how many training vectors
        they need
        )r   Z"IndexIVF_train_encoder_num_vectorsr@   r   r   r   train_encoder_num_vectors.  s    z"IndexIVF.train_encoder_num_vectorsc                 C   s   t | |||||||||	|
S r#   )r   ZIndexIVF_search_preassignedrf  r   r   r   rj  5  s    zIndexIVF.search_preassignedc
           
      C   s   t | |||||||||	
S r#   )r   Z!IndexIVF_range_search_preassignedrk  r   r   r   rm  8  s    z!IndexIVF.range_search_preassignedc              	   C   s   t | ||||||S z/ assign the vectors, then call search_preassign)r   ZIndexIVF_searchrs  r   r   r   ru  ;  s    zIndexIVF.searchc                 C   s   t | |||||S r#   )r   ZIndexIVF_range_searchrv  r   r   r   rw  ?  s    zIndexIVF.range_searchc                 C   s   t | ||S )z
         Get a scanner for this index (store_pairs means ignore labels)

        The default search implementation uses this to compute the distances
        )r   Z IndexIVF_get_InvertedListScannerr   rh  r5  r   r   r   get_InvertedListScannerB  s    z IndexIVF.get_InvertedListScannerc                 C   s   t | ||S )zI reconstruct a vector. Works only if maintain_direct_map is set to 1 or 2)r   ZIndexIVF_reconstructr|  r   r   r   r  J  s    zIndexIVF.reconstructc                 C   s   t | |||S )a=  
         Update a subset of vectors.

        The index must have a direct_map

        :type nv: int
        :param nv:     nb of vectors to update
        :type idx: int
        :param idx:    vector indices to update, size nv
        :type v: float
        :param v:      vectors of new values, size nv*d
        )r   ZIndexIVF_update_vectorsr   nvr   r6   r   r   r   update_vectorsN  s    zIndexIVF.update_vectorsc                 C   s   t | |||S )a  
         Reconstruct a subset of the indexed vectors.

        Overrides default implementation to bypass reconstruct() which requires
        direct_map to be maintained.

        :type i0: int
        :param i0:     first vector to reconstruct
        :type ni: int
        :param ni:     nb of vectors to reconstruct
        :type recons: float
        :param recons: output array of reconstructed vectors, size ni * d
        )r   ZIndexIVF_reconstruct_nr  r   r   r   r  ]  s    zIndexIVF.reconstruct_nc              
   C   s   t | |||||||S )a  
         Similar to search, but also reconstructs the stored vectors (or an
        approximation in the case of lossy coding) for the search results.

        Overrides default implementation to avoid having to maintain direct_map
        and instead fetch the code offsets through the `store_pairs` flag in
        search_preassigned().

        :type recons: float
        :param recons:      reconstructed vectors size (n, k, d)
        )r   ZIndexIVF_search_and_reconstructr  r   r   r   r  m  s    zIndexIVF.search_and_reconstructc	           	      C   s   t | ||||||||	S )a  
         Similar to search, but also returns the codes corresponding to the
        stored vectors for the search results.

        :param codes:      codes (n, k, code_size)
        :type include_listno: boolean, optional
        :param include_listno:
                              include the list ids in the code (in this case add
                              ceil(log8(nlist)) to the code size)
        )r   Z IndexIVF_search_and_return_codes)	r   rB   r2   r   r   r   r~  rt  rt  r   r   r   search_and_return_codes{  s    z IndexIVF.search_and_return_codesc                 C   s   t | |||S a1  
         Reconstruct a vector given the location in terms of (inv list index +
        inv list offset) instead of the id.

        Useful for reconstructing when the direct_map is not maintained and
        the inv list offset is computed by search_preassigned() with
        `store_pairs` set.
        )r   Z IndexIVF_reconstruct_from_offsetr   r  r  r~  r   r   r   reconstruct_from_offset  s    	z IndexIVF.reconstruct_from_offsetc                 C   s   t | |S z Dataset manipulation functions)r   ZIndexIVF_remove_idsrz  r   r   r   r{    s    zIndexIVF.remove_idsc                 C   s   t | |S r#   )r   Z#IndexIVF_check_compatible_for_merger  r   r   r   r    s    z#IndexIVF.check_compatible_for_mergec                 C   s   t | ||S r#   )r   ZIndexIVF_merge_fromr  r   r   r   r    s    zIndexIVF.merge_fromc                 C   s
   t | S r#   )r   ZIndexIVF_get_CodePackerr@   r   r   r   r    s    zIndexIVF.get_CodePackerc                 C   s   t | ||||S )z
         copy a subset of the entries index to the other index
        see Invlists::copy_subset_to for the meaning of subset_type
        )r   ZIndexIVF_copy_subset_tor  r   r   r   r     s    zIndexIVF.copy_subset_toc                 C   s   t | |S r#   )r   ZIndexIVF_get_list_sizer  r   r   r   get_list_size  s    zIndexIVF.get_list_sizec                 C   s
   t | S )z are the ids sorted?)r   ZIndexIVF_check_ids_sortedr@   r   r   r   check_ids_sorted  s    zIndexIVF.check_ids_sortedTc                 C   s   t | |S z
         initialize a direct map

        :type new_maintain_direct_map: boolean, optional
        :param new_maintain_direct_map:    if true, create a direct map,
                                              else clear it
        )r   ZIndexIVF_make_direct_mapr   Znew_maintain_direct_mapr   r   r   make_direct_map  s    zIndexIVF.make_direct_mapc                 C   s   t | |S r#   )r   ZIndexIVF_set_direct_map_typer   r   r   r   r   set_direct_map_type  s    zIndexIVF.set_direct_map_typec                 C   s   t | ||S )zC replace the inverted lists, old one is deallocated if own_invlists)r   ZIndexIVF_replace_invlistsr   r-  r   r   r   r   replace_invlists  s    zIndexIVF.replace_invlistsc                 C   s
   t | S r#   )r   ZIndexIVF_sa_code_sizer@   r   r   r   r    s    zIndexIVF.sa_code_sizec                 C   s   t | |||S )a  
         encode a set of vectors
        sa_encode will call encode_vectors with include_listno=true
        :type n: int
        :param n:      nb of vectors to encode
        :type x: float
        :param x:      the vectors to encode
        :type bytes: uint8_t
        :param bytes:  output array for the codes
        :rtype: void
        :return: nb of bytes written to codes
        )r   ZIndexIVF_sa_encoder  r   r   r   r    s    zIndexIVF.sa_encode)N)F)NN)FNN)N)N)FN)N)FN)T)F)@r   r   r,   r-   r   r   r?   r   r   r   ZIndexIVF_invlists_getZIndexIVF_invlists_setrK  ZIndexIVF_own_invlists_getZIndexIVF_own_invlists_setown_invlistsZIndexIVF_code_size_getZIndexIVF_code_size_setr   ZIndexIVF_parallel_mode_getZIndexIVF_parallel_mode_setZparallel_modeZ'IndexIVF_PARALLEL_MODE_NO_HEAP_INIT_getZPARALLEL_MODE_NO_HEAP_INITZIndexIVF_direct_map_getZIndexIVF_direct_map_setrT  ZIndexIVF_by_residual_getZIndexIVF_by_residual_setby_residualr|   rn  ro  rr  rr  ru  r  rw  rx  rj  rm  ru  rw  r{  r  r~  r  r  r  r  r{  r  r  r  r   Zdelete_IndexIVFrT   r  r  r  r  r  r  r  r   r   r   r   rn    sX   


	









rn  c                   @   s   e Zd ZdZedd dd ddZdd ZeZee	j
e	jd	dZee	je	jd
dZee	je	jddZee	je	jddZee	je	jddZdd Zdd Zdd Zdd Zdd Zdd Zdd Ze	j Z!dS )InvertedListScannerz
     Object that handles a query. The inverted lists to scan are
    provided externally. The object has a lot of state, but
    distance_to_code and scan_codes can be called in multiple
    threads
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zInvertedListScanner.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?     s    zInvertedListScanner.__init__z remember current listz  keep maximum instead of minimumz/ store positions in invlists rather than labelsz search in this subset of idsz- used in default implementation of scan_codesc                 C   s   t | |S z" from now on we handle this query.)r   ZInvertedListScanner_set_queryr   Zquery_vectorr   r   r   r    s    zInvertedListScanner.set_queryc                 C   s   t | ||S z- following codes come from this inverted list)r   ZInvertedListScanner_set_listr   r  rl  r   r   r   set_list  s    zInvertedListScanner.set_listc                 C   s   t | |S z( compute a single query-to-code distance)r   Z$InvertedListScanner_distance_to_coder  r   r   r   r    s    z$InvertedListScanner.distance_to_codec              	   C   s   t | ||||||S )a  
         scan a set of codes, compute distances to current query and
        update heap of results if necessary. Default implementation
        calls distance_to_code.

        :type n: int
        :param n:      number of codes to scan
        :type codes: uint8_t
        :param codes:  codes to scan (n * code_size)
        :type ids: int
        :param ids:        corresponding ids (ignored if store_pairs)
        :type distances: float
        :param distances:  heap distances (size k)
        :type labels: int
        :param labels:     heap labels (size k)
        :type k: int
        :param k:          heap size
        :rtype: int
        :return: number of heap updates performed
        )r   ZInvertedListScanner_scan_codesr   rB   r   r'  r   r   r   r   r   r   
scan_codes  s    zInvertedListScanner.scan_codesc                 C   s   t | |||||S r#   )r   Z!InvertedListScanner_iterate_codes)r   iteratorr   r   r   r  r   r   r   iterate_codes
  s    z!InvertedListScanner.iterate_codesc                 C   s   t | |||||S )z
         scan a set of codes, compute distances to current query and
        update results if distances are below radius

        (default implementation fails)
        )r   Z$InvertedListScanner_scan_codes_ranger   rB   r   r'  r   r   r   r   r   scan_codes_range  s    z$InvertedListScanner.scan_codes_rangec                 C   s   t | ||||S r#   )r   Z'InvertedListScanner_iterate_codes_range)r   r  r   r   r  r   r   r   iterate_codes_range  s    z'InvertedListScanner.iterate_codes_rangeN)"r   r   r,   r-   r   r   r?   r   r   r   ZInvertedListScanner_list_no_getZInvertedListScanner_list_no_setr  Z InvertedListScanner_keep_max_getZ InvertedListScanner_keep_max_setr   Z#InvertedListScanner_store_pairs_getZ#InvertedListScanner_store_pairs_setrh  ZInvertedListScanner_sel_getZInvertedListScanner_sel_setr5  Z!InvertedListScanner_code_size_getZ!InvertedListScanner_code_size_setr   r  r  r  r  r  r  r  Zdelete_InvertedListScannerrT   r   r   r   r   r    s"   	r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZeejejZeejejZdd Zdd	 Zd
d ZejZdS )IndexIVFStatsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexIVFStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZIndexIVFStats_swiginitZnew_IndexIVFStatsr@   r   r   r   r?   &  s    zIndexIVFStats.__init__c                 C   s
   t | S r#   )r   ZIndexIVFStats_resetr@   r   r   r   r|   )  s    zIndexIVFStats.resetc                 C   s   t | |S r#   )r   ZIndexIVFStats_addr^   r   r   r   ro  ,  s    zIndexIVFStats.addN)r   r   r,   r   r   r   r   r   ZIndexIVFStats_nq_getZIndexIVFStats_nq_setr   ZIndexIVFStats_nlist_getZIndexIVFStats_nlist_setr  ZIndexIVFStats_ndis_getZIndexIVFStats_ndis_setndisZIndexIVFStats_nheap_updates_getZIndexIVFStats_nheap_updates_setZnheap_updatesZ#IndexIVFStats_quantization_time_getZ#IndexIVFStats_quantization_time_setZquantization_timeZIndexIVFStats_search_time_getZIndexIVFStats_search_time_setZsearch_timer?   r|   ro  Zdelete_IndexIVFStatsrT   r   r   r   r   r    s   r  c                 C   s   t | |S )zn
     check if two indexes have the same parameters and are trained in
    the same way, otherwise throw.
    )r   r  )index1index2r   r   r   r  3  s    r  c                  G   s
   t j|  S r#   )r   extract_index_ivfr   r   r   r   r  :  s    r  c                  G   s
   t j|  S r#   )r   try_extract_index_ivfr   r   r   r   r  =  s    r  c                 C   s   t | ||S )z
     Merge index1 into index0. Works on IndexIVF's and IndexIVF's
     embedded in a IndexPreTransform. On output, the index1 is empty.

    :type shift_ids: boolean
    :param shift_ids:: translate the ids from index1 to index0->prev_ntotal
    )r   
merge_into)Zindex0r  	shift_idsr   r   r   r  @  s    r  c                 C   s   t | |||S r#   )r   search_centroid)r  r2   rB   Zcentroid_idsr   r   r   r  J  s    r  c              
   C   s   t | |||||||S r#   )r   search_and_return_centroids)r  rB   Zxinr   r   r   Zquery_centroid_idsZresult_centroid_idsr   r   r   r  M  s    r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZdd Zdd ZejZdS )SlidingIndexWindowz
     A set of IndexIVFs concatenated together in a FIFO fashion.
    at each "step", the oldest index slice is removed and a new index is added.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   U  r4   zSlidingIndexWindow.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   U  r4   r7   r8   z. common index that contains the sliding windowz InvertedLists of indexz$ number of slices currently in indexz same as index->nlistz$ cumulative list sizes at each slicec                 C   s   t | t | dS )z, index should be initially empty and trainedN)r   ZSlidingIndexWindow_swiginitZnew_SlidingIndexWindowr   r  r   r   r   r?   ]  s    zSlidingIndexWindow.__init__c                 C   s   t | ||S )a$  
         Add one index to the current index and remove the oldest one.

        :type sub_index: :py:class:`Index`
        :param sub_index:        slice to swap in (can be NULL)
        :type remove_oldest: boolean
        :param remove_oldest:    if true, remove the oldest slices
        )r   ZSlidingIndexWindow_step)r   Z	sub_indexZremove_oldestr   r   r   stepa  s    	zSlidingIndexWindow.stepN)r   r   r,   r-   r   r   r   r   r   ZSlidingIndexWindow_index_getZSlidingIndexWindow_index_setr  ZSlidingIndexWindow_ils_getZSlidingIndexWindow_ils_setr+  ZSlidingIndexWindow_n_slice_getZSlidingIndexWindow_n_slice_setZn_sliceZSlidingIndexWindow_nlist_getZSlidingIndexWindow_nlist_setr  ZSlidingIndexWindow_sizes_getZSlidingIndexWindow_sizes_setr7  r?   r  Zdelete_SlidingIndexWindowrT   r   r   r   r   r  O  s   
r  c                 C   s   t | ||S )z( Get a subset of inverted lists [i0, i1))r   get_invlist_range)r  r  r.  r   r   r   r  p  s    r  c                 C   s   t | |||S )z Set a subset of inverted lists)r   set_invlist_range)r  r  r.  srcr   r   r   r  t  s    r  c	           	      C   s   t | ||||||||	S )a  
     search an IndexIVF, possibly embedded in an IndexPreTransform with
    given parameters. This is a way to set the nprobe and get
    statdistics in a thread-safe way.

    Optionally returns (if non-nullptr):
    - nb_dis: number of distances computed
    - ms_per_stage: [0]: preprocessing time
                    [1]: coarse quantization,
                    [2]: list scanning
    )r   search_with_parameters)	r  rB   r2   r   r   r   rt  nb_disms_per_stager   r   r   r  x  s    r  c              
   C   s   t | |||||||S )z4 same as search_with_parameters but for range search)r   range_search_with_parameters)r  rB   r2   r   r   rt  r  r  r   r   r   r    s    r  c                 C   s   t | |S )z
     Build an IndexIVFResidualQuantizer from an ResidualQuantizer, using the
    nlevel first components as coarse quantizer and the rest as codes in invlists
    )r   ivf_residual_from_quantizer)r:  Znlevelr   r   r   r    s    r  c                 C   s   t | |||S )a  
     add from codes. NB that the norm component is not used, so the code_size can
    be provided.

    :type ivfrq: :py:class:`IndexIVFResidualQuantizer`
    :param ivfrq:      index to populate with the codes
    :type codes: uint8_t
    :param codes:      codes to add, size (ncode, code_size)
    :type code_size: int, optional
    :param code_size:  override the ivfrq's code_size, useful if the norm encoding
                          is different
    )r    ivf_residual_add_from_flat_codes)Zivfrqr  r   r   r   r   r   r    s    r  c                   @   s  e Zd ZdZedd dd ddZeZej	Z
ejZejZejZejZejZejZejZejZeejejZejZej Z!ej"Z#ej$Z%eej&ej'Z(eej)ej*Z+eej,ej-ddZ.eej/ej0ddZ1d	d
 Z2dd Z3dd Z4dd Z5dd Z6dd Z7dd Z8dddZ9ej:Z;dS )ScalarQuantizerz
    The uniform quantizer has a range [vmin, vmax]. The range can be
    the same for all dimensions (uniform) or specific per dimension
    (default).
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zScalarQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z bits per scalar codez% trained values (including the range)c                 G   s   t | t j|  d S r#   )r   ZScalarQuantizer_swiginitZnew_ScalarQuantizerrQ   r   r   r   r?     s    zScalarQuantizer.__init__c                 C   s
   t | S )z- updates internal values based on qtype and d)r   Z!ScalarQuantizer_set_derived_sizesr@   r   r   r   set_derived_sizes  s    z!ScalarQuantizer.set_derived_sizesc                 C   s   t | ||S r#   )r   ZScalarQuantizer_trainrm  r   r   r   rn    s    zScalarQuantizer.trainc                 C   s   t | |||S )z
         Encode a set of vectors

        :type x: float
        :param x:      vectors to encode, size n * d
        :type codes: uint8_t
        :param codes:  output codes, size n * code_size
        )r   ZScalarQuantizer_compute_codesr  r   r   r   r    s    	zScalarQuantizer.compute_codesc                 C   s   t | |||S )z
         Decode a set of vectors

        :param codes:  codes to decode, size n * code_size
        :type x: float
        :param x:      output vectors, size n * d
        )r   ZScalarQuantizer_decoder  r   r   r   r    s    zScalarQuantizer.decodec                 C   s
   t | S r#   )r   Z ScalarQuantizer_select_quantizerr@   r   r   r   select_quantizer  s    z ScalarQuantizer.select_quantizerc                 G   s   t j| g|R  S r#   )r   Z%ScalarQuantizer_get_distance_computerrQ   r   r   r   r    s    z%ScalarQuantizer.get_distance_computerFc                 C   s   t | |||||S r#   )r   Z*ScalarQuantizer_select_InvertedListScanner)r   rH  r^  rh  r5  r  r   r   r   select_InvertedListScanner  s    z*ScalarQuantizer.select_InvertedListScannerN)F)<r   r   r,   r-   r   r   r   r   r   ZScalarQuantizer_QT_8bitZQT_8bitZScalarQuantizer_QT_4bitZQT_4bitZScalarQuantizer_QT_8bit_uniformZQT_8bit_uniformZScalarQuantizer_QT_4bit_uniformZQT_4bit_uniformZScalarQuantizer_QT_fp16ZQT_fp16ZScalarQuantizer_QT_8bit_directZQT_8bit_directZScalarQuantizer_QT_6bitZQT_6bitZScalarQuantizer_QT_bf16ZQT_bf16Z%ScalarQuantizer_QT_8bit_direct_signedZQT_8bit_direct_signedZScalarQuantizer_qtype_getZScalarQuantizer_qtype_setZqtypeZScalarQuantizer_RS_minmaxZ	RS_minmaxZScalarQuantizer_RS_meanstdZ
RS_meanstdZScalarQuantizer_RS_quantilesZRS_quantilesZScalarQuantizer_RS_optimZRS_optimZScalarQuantizer_rangestat_getZScalarQuantizer_rangestat_setZ	rangestatZ!ScalarQuantizer_rangestat_arg_getZ!ScalarQuantizer_rangestat_arg_setZrangestat_argZScalarQuantizer_bits_getZScalarQuantizer_bits_setbitsZScalarQuantizer_trained_getZScalarQuantizer_trained_settrainedr?   r  rn  r  r  r  r  r  Zdelete_ScalarQuantizerrT   r   r   r   r   r    s<   

r  c                   @   st   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 Zd
d ZdddZdd Zdd Zdd ZejZdS )IndexScalarQuantizerz'Flat index built on a scalar quantizer.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexScalarQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z Used to encode the vectorsc                 G   s   t | t j|  d S r#   )r   ZIndexScalarQuantizer_swiginitZnew_IndexScalarQuantizerrQ   r   r   r   r?     s    zIndexScalarQuantizer.__init__c                 C   s   t | ||S r#   )r   ZIndexScalarQuantizer_trainrm  r   r   r   rn    s    zIndexScalarQuantizer.trainNc              	   C   s   t | ||||||S r#   )r   ZIndexScalarQuantizer_searchrs  r   r   r   ru     s    zIndexScalarQuantizer.searchc                 C   s
   t | S r#   )r   Z2IndexScalarQuantizer_get_FlatCodesDistanceComputerr@   r   r   r   r    s    z2IndexScalarQuantizer.get_FlatCodesDistanceComputerc                 C   s   t | |||S r#   )r   ZIndexScalarQuantizer_sa_encoder  r   r   r   r    s    zIndexScalarQuantizer.sa_encodec                 C   s   t | |||S r#   )r   ZIndexScalarQuantizer_sa_decoder  r   r   r   r  	  s    zIndexScalarQuantizer.sa_decode)N)r   r   r,   r-   r   r   r   r   r   ZIndexScalarQuantizer_sq_getZIndexScalarQuantizer_sq_setsqr?   rn  ru  r  r  r  Zdelete_IndexScalarQuantizerrT   r   r   r   r   r    s   
r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 Zdd ZdddZdddZdd Zdd Zdd ZejZdS )IndexIVFScalarQuantizerz
     An IVF implementation where the components of the residuals are
    encoded with a scalar quantizer. All distance computations
    are asymmetric, so the encoded vectors are decoded and approximate
    distances are computed.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z IndexIVFScalarQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z IndexIVFScalarQuantizer_swiginitZnew_IndexIVFScalarQuantizerrQ   r   r   r   r?     s    z IndexIVFScalarQuantizer.__init__c                 C   s   t | |||S r#   )r   Z%IndexIVFScalarQuantizer_train_encoderrv  r   r   r   rw    s    z%IndexIVFScalarQuantizer.train_encoderc                 C   s
   t | S r#   )r   Z1IndexIVFScalarQuantizer_train_encoder_num_vectorsr@   r   r   r   rx  !  s    z1IndexIVFScalarQuantizer.train_encoder_num_vectorsFc                 C   s   t | |||||S r#   )r   Z&IndexIVFScalarQuantizer_encode_vectorsr   rB   r2   r  r   Zinclude_listnosr   r   r   ru  $  s    z&IndexIVFScalarQuantizer.encode_vectorsNc                 C   s   t | |||||S r#   )r   Z IndexIVFScalarQuantizer_add_corerp  r   r   r   rr  '  s    z IndexIVFScalarQuantizer.add_corec                 C   s   t | ||S r#   )r   Z/IndexIVFScalarQuantizer_get_InvertedListScannerrz  r   r   r   r{  *  s    z/IndexIVFScalarQuantizer.get_InvertedListScannerc                 C   s   t | |||S r#   )r   Z/IndexIVFScalarQuantizer_reconstruct_from_offsetr  r   r   r   r  -  s    z/IndexIVFScalarQuantizer.reconstruct_from_offsetc                 C   s   t | |||S r#   )r   Z!IndexIVFScalarQuantizer_sa_decoder  r   r   r   r  0  s    z!IndexIVFScalarQuantizer.sa_decode)F)N)r   r   r,   r-   r   r   r   r   r   ZIndexIVFScalarQuantizer_sq_getZIndexIVFScalarQuantizer_sq_setr  r?   rw  rx  ru  rr  r{  r  r  Zdelete_IndexIVFScalarQuantizerrT   r   r   r   r   r    s   

r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZejZejZejZejZeejejZeej ej!ddZ"dd Z#dd Z$dddZ%dd Z&dd Z'ej(Z)dS )IndexIVFSpectralHasha  
     Inverted list that stores binary codes of size nbit. Before the
    binary conversion, the dimension of the vectors is transformed from
    dim d into dim nbit by vt (a random rotation by default).

    Each coordinate is subtracted from a value determined by
    threshold_type, and split into intervals of size period. Half of
    the interval is a 0 bit, the other half a 1.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   A  r4   zIndexIVFSpectralHash.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   A  r4   r7   r8   z" transformation from d to nbit dimz own the vtz# nb of bits of the binary signaturez interval size for 0s and 1szH
    Trained threshold.
    size nlist * nbit or 0 if Thresh_global
    c                 G   s   t | t j|  d S r#   )r   ZIndexIVFSpectralHash_swiginitZnew_IndexIVFSpectralHashrQ   r   r   r   r?   U  s    zIndexIVFSpectralHash.__init__c                 C   s   t | |||S r#   )r   Z"IndexIVFSpectralHash_train_encoderrv  r   r   r   rw  X  s    z"IndexIVFSpectralHash.train_encoderFc                 C   s   t | |||||S r#   )r   Z#IndexIVFSpectralHash_encode_vectorsr  r   r   r   ru  [  s    z#IndexIVFSpectralHash.encode_vectorsc                 C   s   t | ||S r#   )r   Z,IndexIVFSpectralHash_get_InvertedListScannerrz  r   r   r   r{  ^  s    z,IndexIVFSpectralHash.get_InvertedListScannerc                 G   s   t j| g|R  S )a  
        *Overload 1:*
         replace the vector transform for an empty (and possibly untrained) index

        |

        *Overload 2:*
         convenience function to get the VT from an index constucted by an
        index_factory (should end in "LSH")

        |

        *Overload 3:*
         convenience function to get the VT from an index constucted by an
        index_factory (should end in "LSH")
        )r   ZIndexIVFSpectralHash_replace_vtrQ   r   r   r   
replace_vta  s    zIndexIVFSpectralHash.replace_vtN)F)*r   r   r,   r-   r   r   r   r   r   ZIndexIVFSpectralHash_vt_getZIndexIVFSpectralHash_vt_setr  Z#IndexIVFSpectralHash_own_fields_getZ#IndexIVFSpectralHash_own_fields_setr  ZIndexIVFSpectralHash_nbit_getZIndexIVFSpectralHash_nbit_setr   ZIndexIVFSpectralHash_period_getZIndexIVFSpectralHash_period_setZperiodZ"IndexIVFSpectralHash_Thresh_globalZThresh_globalZ$IndexIVFSpectralHash_Thresh_centroidZThresh_centroidZ)IndexIVFSpectralHash_Thresh_centroid_halfZThresh_centroid_halfZ"IndexIVFSpectralHash_Thresh_medianZThresh_medianZ'IndexIVFSpectralHash_threshold_type_getZ'IndexIVFSpectralHash_threshold_type_setZthreshold_typeZ IndexIVFSpectralHash_trained_getZ IndexIVFSpectralHash_trained_setr  r?   rw  ru  r{  r  Zdelete_IndexIVFSpectralHashrT   r   r   r   r   r  6  s&   

r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 Zdd ZdddZdd Zdd Zdd ZejZdS )IndexIVFAdditiveQuantizerz]
    Abstract class for IVF additive quantizers.
    The search functions are in common.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   }  r4   z"IndexIVFAdditiveQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   }  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z"IndexIVFAdditiveQuantizer_swiginitZnew_IndexIVFAdditiveQuantizerrQ   r   r   r   r?     s    z"IndexIVFAdditiveQuantizer.__init__c                 C   s   t | |||S r#   )r   Z'IndexIVFAdditiveQuantizer_train_encoderrv  r   r   r   rw    s    z'IndexIVFAdditiveQuantizer.train_encoderc                 C   s
   t | S r#   )r   Z3IndexIVFAdditiveQuantizer_train_encoder_num_vectorsr@   r   r   r   rx    s    z3IndexIVFAdditiveQuantizer.train_encoder_num_vectorsFc                 C   s   t | |||||S r#   )r   Z(IndexIVFAdditiveQuantizer_encode_vectorsr  r   r   r   ru    s    z(IndexIVFAdditiveQuantizer.encode_vectorsc                 C   s   t | ||S r#   )r   Z1IndexIVFAdditiveQuantizer_get_InvertedListScannerrz  r   r   r   r{    s    z1IndexIVFAdditiveQuantizer.get_InvertedListScannerc                 C   s   t | |||S r#   )r   Z#IndexIVFAdditiveQuantizer_sa_decoder   rB   r   r2   r   r   r   r    s    z#IndexIVFAdditiveQuantizer.sa_decodec                 C   s   t | |||S r#   )r   Z1IndexIVFAdditiveQuantizer_reconstruct_from_offsetr  r   r   r   r    s    z1IndexIVFAdditiveQuantizer.reconstruct_from_offsetN)F)r   r   r,   r-   r   r   r   r   r   Z IndexIVFAdditiveQuantizer_aq_getZ IndexIVFAdditiveQuantizer_aq_setr  Z3IndexIVFAdditiveQuantizer_use_precomputed_table_getZ3IndexIVFAdditiveQuantizer_use_precomputed_table_setuse_precomputed_tabler?   rw  rx  ru  r{  r  r  Z delete_IndexIVFAdditiveQuantizerrT   r   r   r   r   r  w  s   
r  c                   @   sJ   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 ZejZd
S )IndexIVFResidualQuantizerv
     IndexIVF based on a residual quantizer. Stored vectors are
    approximated by residual quantization codes.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z"IndexIVFResidualQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   r  c                 G   s   t | t j|  d S r#   )r   Z"IndexIVFResidualQuantizer_swiginitZnew_IndexIVFResidualQuantizerrQ   r   r   r   r?     s    z"IndexIVFResidualQuantizer.__init__N)r   r   r,   r-   r   r   r   r   r   Z IndexIVFResidualQuantizer_rq_getZ IndexIVFResidualQuantizer_rq_setr%  r?   Z delete_IndexIVFResidualQuantizerrT   r   r   r   r   r    s   r  c                   @   sJ   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 ZejZd
S )IndexIVFLocalSearchQuantizerr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z%IndexIVFLocalSearchQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z- The LSQ quantizer used to encode the vectorsc                 G   s   t | t j|  d S r#   )r   Z%IndexIVFLocalSearchQuantizer_swiginitZ new_IndexIVFLocalSearchQuantizerrQ   r   r   r   r?     s    z%IndexIVFLocalSearchQuantizer.__init__N)r   r   r,   r-   r   r   r   r   r   Z$IndexIVFLocalSearchQuantizer_lsq_getZ$IndexIVFLocalSearchQuantizer_lsq_setrS  r?   Z#delete_IndexIVFLocalSearchQuantizerrT   r   r   r   r   r    s   r  c                   @   sJ   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 ZejZd
S ) IndexIVFProductResidualQuantizerz
     IndexIVF based on a product residual quantizer. Stored vectors are
    approximated by product residual quantization codes.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z)IndexIVFProductResidualQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   r  c                 G   s   t | t j|  d S r#   )r   Z)IndexIVFProductResidualQuantizer_swiginitZ$new_IndexIVFProductResidualQuantizerrQ   r   r   r   r?     s    z)IndexIVFProductResidualQuantizer.__init__N)r   r   r,   r-   r   r   r   r   r   Z(IndexIVFProductResidualQuantizer_prq_getZ(IndexIVFProductResidualQuantizer_prq_setr  r?   Z'delete_IndexIVFProductResidualQuantizerrT   r   r   r   r   r    s   r  c                   @   sJ   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 ZejZd
S )#IndexIVFProductLocalSearchQuantizerz
     IndexIVF based on a product local search quantizer. Stored vectors are
    approximated by product local search quantization codes.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z,IndexIVFProductLocalSearchQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   r  c                 G   s   t | t j|  d S r#   )r   Z,IndexIVFProductLocalSearchQuantizer_swiginitZ'new_IndexIVFProductLocalSearchQuantizerrQ   r   r   r   r?     s    z,IndexIVFProductLocalSearchQuantizer.__init__N)r   r   r,   r-   r   r   r   r   r   Z,IndexIVFProductLocalSearchQuantizer_plsq_getZ,IndexIVFProductLocalSearchQuantizer_plsq_setr  r?   Z*delete_IndexIVFProductLocalSearchQuantizerrT   r   r   r   r   r    s   r  c                   @   s^   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZejZdd ZdS )	SearchParametersHNSWc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zSearchParametersHNSW.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZSearchParametersHNSW_swiginitZnew_SearchParametersHNSWr@   r   r   r   r?     s    zSearchParametersHNSW.__init__N)r   r   r,   r   r   r   r   r   Z!SearchParametersHNSW_efSearch_getZ!SearchParametersHNSW_efSearch_setefSearchZ0SearchParametersHNSW_check_relative_distance_getZ0SearchParametersHNSW_check_relative_distance_setcheck_relative_distanceZ&SearchParametersHNSW_bounded_queue_getZ&SearchParametersHNSW_bounded_queue_setZbounded_queueZdelete_SearchParametersHNSWrT   r?   r   r   r   r   r    s   r  c                   @   s  e Zd Zedd dd ddZeZeejej	ddZ
eejejddZeejejddZeejejd	dZeejejd
dZeejejddZeejejZeejejddZeej ej!ddZ"eej#ej$ddZ%eej&ej'ddZ(eej)ej*ddZ+dd Z,dd Z-dd Z.dd Z/dd Z0d8ddZ1dd Z2d d! Z3d9d#d$Z4d:d%d&Z5d;d(d)Z6d<d*d+Z7d,d- Z8d.d/ Z9d0d1 Z:d=d2d3Z;e<d>d4d5Z=d6d7 Z>ej?Z@d'S )?HNSWc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zHNSW.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z- assignment probability to each layer (sum=1)zf
    number of neighbors stored per layer (cumulative), should not
    be changed after first add
    z5 level of each vector (base level = 1), size = ntotalzf
    offsets[i] is the offset in the neighbors array where vector i is stored
    size ntotal + 1
    z
    neighbors[offsets[i]:offsets[i+1]] is the list of neighbors of vector i
    for all levels. this is where all storage goes.
    zW
    entry point in the search structure (one of the points with maximum
    level
    z maximum levelz& expansion factor at construction timez  expansion factor at search timezW
    during search: do we check whether the next best distance is good
    enough?
    z% use bounded queue during explorationc                 C   s   t | ||S )z
        initialize the assign_probas and cum_nneighbor_per_level to
        have 2*M links on level 0 and M links on levels > 0
        )r   ZHNSW_set_default_probas)r   r  Z	levelMultr   r   r   set_default_probas  s    zHNSW.set_default_probasc                 C   s   t | ||S )z< set nb of neighbors for this level (before adding anything))r   ZHNSW_set_nb_neighbors)r   Zlevel_norB   r   r   r   set_nb_neighbors  s    zHNSW.set_nb_neighborsc                 C   s   t | |S )z nb of neighbors for this level)r   ZHNSW_nb_neighborsr   layer_nor   r   r   nb_neighbors  s    zHNSW.nb_neighborsc                 C   s   t | |S )z0 cumumlative nb up to (and excluding) this level)r   ZHNSW_cum_nb_neighborsr  r   r   r   cum_nb_neighbors  s    zHNSW.cum_nb_neighborsc                 C   s   t | ||||S )zA range of entries in the neighbors table of vertex no at layer_no)r   ZHNSW_neighbor_range)r   rN  r  beginendr   r   r   neighbor_range  s    zHNSW.neighbor_range    c                 C   s   t | t | dS )z* only mandatory parameter: nb of neighborsN)r   ZHNSW_swiginitZnew_HNSW)r   r  r   r   r   r?     s    zHNSW.__init__c                 C   s
   t | S )z$ pick a random level for a new point)r   ZHNSW_random_levelr@   r   r   r   random_level!  s    zHNSW.random_levelc                 C   s   t | |S )z0 add n random levels to table (for debugging...))r   ZHNSW_fill_with_random_linksrA   r   r   r   fill_with_random_links%  s    zHNSW.fill_with_random_linksFc	           	      C   s   t | ||||||||	S r#   )r   ZHNSW_add_links_starting_from)	r   ptdispt_idnearest	d_nearestlevellocksr  keep_max_size_level0r   r   r   add_links_starting_from)  s    zHNSW.add_links_starting_fromc              	   C   s   t | ||||||S )zk
         add point pt_id on all levels <= pt_level and build the link
        structure for them.
        )r   ZHNSW_add_with_locks)r   r  Zpt_levelr  r  r  r  r   r   r   add_with_locks,  s    zHNSW.add_with_locksNc                 C   s   t | ||||S )z, search interface for 1 point, single thread)r   ZHNSW_search)r   qdisr  r  rt  r   r   r   ru  3  s    zHNSW.searchc
           
      C   s   t | |||||||||	
S )z+ search only in level 0 from a given vertex)r   ZHNSW_search_level_0)
r   r  r  rc  Z	nearest_i	nearest_dr  Zsearch_statsr  rt  r   r   r   search_level_07  s    zHNSW.search_level_0c                 C   s
   t | S r#   )r   Z
HNSW_resetr@   r   r   r   r|   ;  s    z
HNSW.resetc                 C   s   t | |S r#   )r   ZHNSW_clear_neighbor_tablesr   r  r   r   r   clear_neighbor_tables>  s    zHNSW.clear_neighbor_tablesc                 C   s   t | |S r#   )r   ZHNSW_print_neighbor_statsr  r   r   r   print_neighbor_statsA  s    zHNSW.print_neighbor_statsc                 C   s   t | ||S r#   )r   ZHNSW_prepare_level_tab)r   rB   Zpreset_levelsr   r   r   prepare_level_tabD  s    zHNSW.prepare_level_tabc                 C   s   t | ||||S r#   )r   ZHNSW_shrink_neighbor_list)r  inputoutputmax_sizer  r   r   r   shrink_neighbor_listG  s    zHNSW.shrink_neighbor_listc                 C   s   t | |S r#   )r   ZHNSW_permute_entriesr$  r   r   r   r  K  s    zHNSW.permute_entries)r  )F)F)N)N)F)F)Ar   r   r,   r   r   r   r   r   ZHNSW_assign_probas_getZHNSW_assign_probas_setZassign_probasZ HNSW_cum_nneighbor_per_level_getZ HNSW_cum_nneighbor_per_level_setZcum_nneighbor_per_levelZHNSW_levels_getZHNSW_levels_setZlevelsZHNSW_offsets_getZHNSW_offsets_setoffsetsZHNSW_neighbors_getZHNSW_neighbors_set	neighborsZHNSW_entry_point_getZHNSW_entry_point_setentry_pointZHNSW_rng_getZHNSW_rng_setrngZHNSW_max_level_getZHNSW_max_level_setZ	max_levelZHNSW_efConstruction_getZHNSW_efConstruction_setZefConstructionZHNSW_efSearch_getZHNSW_efSearch_setr  Z HNSW_check_relative_distance_getZ HNSW_check_relative_distance_setr  ZHNSW_search_bounded_queue_getZHNSW_search_bounded_queue_setZsearch_bounded_queuer  r  r  r  r  r?   r  r  r  r  ru  r  r|   r  r  r  r  r  r  Zdelete_HNSWrT   r   r   r   r   r    sD   





r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejddZeejejddZeejejddZd	d
 Zdd Zdd ZejZdS )	HNSWStatsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   R  r4   zHNSWStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   R  r4   r7   r8   z number of vectors searchedz< number of queries for which the candidate list is exhaustedz number of distances computedc                 C   s
   t | S )z- number of hops aka number of edges traversed)r   ZHNSWStats_resetr@   r   r   r   r|   Y  s    zHNSWStats.resetc                 C   s   t | |S r#   )r   ZHNSWStats_combiner^   r   r   r   combine]  s    zHNSWStats.combinec                 C   s   t | t   d S r#   )r   ZHNSWStats_swiginitZnew_HNSWStatsr@   r   r   r   r?   `  s    zHNSWStats.__init__N)r   r   r,   r   r   r   r   r   ZHNSWStats_n1_getZHNSWStats_n1_setr   ZHNSWStats_n2_getZHNSWStats_n2_setr   ZHNSWStats_ndis_getZHNSWStats_ndis_setr  ZHNSWStats_nhops_getZHNSWStats_nhops_setZnhopsr|   r  r?   Zdelete_HNSWStatsrT   r   r   r   r   r  Q  s   r  c	           	      C   s   t | ||||||||	S r#   )r   search_from_candidates)	hnswr  r  
candidatesr  ri  r  Znres_inrt  r   r   r   r  g  s    r  c                 C   s   t | ||||S r#   )r   greedy_update_nearest)r  r  r  r  r  r   r   r   r  j  s    r  c                 C   s   t | |||||S r#   )r   search_from_candidate_unbounded)r  noder  Zefr  ri  r   r   r   r  m  s    r  c              
   C   s   t | |||||||S r#   )r   search_neighbors_to_add)r  r  resultsr  Zd_entry_pointr  r  Zreference_versionr   r   r   r  p  s    r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZeejejZdd ZejZd	d
 Zdd Zd'ddZd(ddZdd Zdd Z dd Z!d)ddZ"dd Z#dd Z$dd  Z%d!d" Z&d#d$ Z'd%d& Z(dS )*	IndexHNSWe
     The HNSW index is a normal random-access index with a HNSW
    link structure built on top
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   x  r4   zIndexHNSW.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   x  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexHNSW_swiginitZnew_IndexHNSWrQ   r   r   r   r?     s    zIndexHNSW.__init__c                 C   s   t | ||S r#   )r   ZIndexHNSW_addrm  r   r   r   ro    s    zIndexHNSW.addc                 C   s   t | ||S z Trains the storage if needed)r   ZIndexHNSW_trainrm  r   r   r   rn    s    zIndexHNSW.trainNc              	   C   s   t | ||||||S z entry point for search)r   ZIndexHNSW_searchrs  r   r   r   ru    s    zIndexHNSW.searchc                 C   s   t | |||||S r#   )r   ZIndexHNSW_range_searchrv  r   r   r   rw    s    zIndexHNSW.range_searchc                 C   s   t | ||S r#   )r   ZIndexHNSW_reconstructr|  r   r   r   r    s    zIndexHNSW.reconstructc                 C   s
   t | S r#   )r   ZIndexHNSW_resetr@   r   r   r   r|     s    zIndexHNSW.resetc                 C   s   t | |S r#   )r   Z"IndexHNSW_shrink_level_0_neighborsr   r[   r   r   r   shrink_level_0_neighbors  s    z"IndexHNSW.shrink_level_0_neighborsr   c                 C   s   t | |||||||||	|
S )a  
         Perform search only on level 0, given the starting points for
        each vertex.

        :type search_type: int, optional
        :param search_type: 1:perform one search per nprobe, 2: enqueue
                               all entry points
        )r   ZIndexHNSW_search_level_0)r   rB   r2   r   r  r  r   r   rc  r  rt  r   r   r   r    s    	zIndexHNSW.search_level_0c                 C   s   t | |||S z alternative graph building)r   Z$IndexHNSW_init_level_0_from_knngraph)r   r   r   r   r   r   r   init_level_0_from_knngraph  s    z$IndexHNSW.init_level_0_from_knngraphc                 C   s   t | |||S r  )r   Z(IndexHNSW_init_level_0_from_entry_points)r   ZnptZpointsZnearestsr   r   r   init_level_0_from_entry_points  s    z(IndexHNSW.init_level_0_from_entry_pointsc                 C   s
   t | S r#   )r   ZIndexHNSW_reorder_linksr@   r   r   r   reorder_links  s    zIndexHNSW.reorder_linksc                 C   s
   t | S r#   )r   ZIndexHNSW_link_singletonsr@   r   r   r   link_singletons  s    zIndexHNSW.link_singletonsc                 C   s   t | |S r#   )r   ZIndexHNSW_permute_entriesr  r   r   r   r    s    zIndexHNSW.permute_entriesc                 C   s
   t | S r#   )r   ZIndexHNSW_get_distance_computerr@   r   r   r   r    s    zIndexHNSW.get_distance_computer)N)N)r   r   N))r   r   r,   r-   r   r   r   r   r   ZIndexHNSW_hnsw_getZIndexHNSW_hnsw_setr  ZIndexHNSW_own_fields_getZIndexHNSW_own_fields_setr  ZIndexHNSW_storage_getZIndexHNSW_storage_setstorageZIndexHNSW_init_level0_getZIndexHNSW_init_level0_setZinit_level0Z"IndexHNSW_keep_max_size_level0_getZ"IndexHNSW_keep_max_size_level0_setr  r?   Zdelete_IndexHNSWrT   ro  rn  ru  rw  r  r|   r  r  r   r  r  r  r  r  r   r   r   r   r  r  s0   


r  c                   @   s8   e Zd ZdZedd dd ddZeZdd Ze	j
Zd	S )
IndexHNSWFlatz_
    Flat index topped with with a HNSW structure to access elements
    more efficiently.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexHNSWFlat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexHNSWFlat_swiginitZnew_IndexHNSWFlatrQ   r   r   r   r?     s    zIndexHNSWFlat.__init__N)r   r   r,   r-   r   r   r   r   r?   r   Zdelete_IndexHNSWFlatrT   r   r   r   r   r    s
   r  c                   @   s@   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	e
jZdS )IndexHNSWPQz]
    PQ index topped with with a HNSW structure to access elements
    more efficiently.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexHNSWPQ.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexHNSWPQ_swiginitZnew_IndexHNSWPQrQ   r   r   r   r?     s    zIndexHNSWPQ.__init__c                 C   s   t | ||S r#   )r   ZIndexHNSWPQ_trainrm  r   r   r   rn    s    zIndexHNSWPQ.trainN)r   r   r,   r-   r   r   r   r   r?   rn  r   Zdelete_IndexHNSWPQrT   r   r   r   r   r    s   r  c                   @   s8   e Zd ZdZedd dd ddZeZdd Ze	j
Zd	S )
IndexHNSWSQz]
    SQ index topped with with a HNSW structure to access elements
    more efficiently.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexHNSWSQ.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexHNSWSQ_swiginitZnew_IndexHNSWSQrQ   r   r   r   r?     s    zIndexHNSWSQ.__init__N)r   r   r,   r-   r   r   r   r   r?   r   Zdelete_IndexHNSWSQrT   r   r   r   r   r    s
   r  c                   @   sJ   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dddZ
ejZdS )IndexHNSW2Levelz/ 2-level code structure with fast random accessc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexHNSW2Level.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexHNSW2Level_swiginitZnew_IndexHNSW2LevelrQ   r   r   r   r?     s    zIndexHNSW2Level.__init__c                 C   s
   t | S r#   )r   ZIndexHNSW2Level_flip_to_ivfr@   r   r   r   flip_to_ivf  s    zIndexHNSW2Level.flip_to_ivfNc              	   C   s   t | ||||||S r  )r   ZIndexHNSW2Level_searchrs  r   r   r   ru    s    zIndexHNSW2Level.search)N)r   r   r,   r-   r   r   r   r   r?   r	  ru  r   Zdelete_IndexHNSW2LevelrT   r   r   r   r   r    s   
r  c                   @   sj   e Zd Zedd dd ddZeZdd Zeej	ej
ddZeejejd	dZd
d ZdddZejZdS )IndexHNSWCagrac                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zIndexHNSWCagra.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexHNSWCagra_swiginitZnew_IndexHNSWCagrarQ   r   r   r   r?     s    zIndexHNSWCagra.__init__a  
    When set to true, the index is immutable.
    This option is used to copy the knn graph from GpuIndexCagra
    to the base level of IndexHNSWCagra without adding upper levels.
    Doing so enables to search the HNSW index, but removes the
    ability to add vectors.
    z
    When `base_level_only` is set to `True`, the search function
    searches only the base level knn graph of the HNSW index.
    This parameter selects the entry point by randomly selecting
    some points and using the best one.
    c                 C   s   t | ||S r#   )r   ZIndexHNSWCagra_addrm  r   r   r   ro    s    zIndexHNSWCagra.addNc              	   C   s   t | ||||||S r  )r   ZIndexHNSWCagra_searchrs  r   r   r   ru    s    zIndexHNSWCagra.search)N)r   r   r,   r   r   r   r   r?   r   Z"IndexHNSWCagra_base_level_only_getZ"IndexHNSWCagra_base_level_only_setZbase_level_onlyZ4IndexHNSWCagra_num_base_level_search_entrypoints_getZ4IndexHNSWCagra_num_base_level_search_entrypoints_setZ!num_base_level_search_entrypointsro  ru  Zdelete_IndexHNSWCagrarT   r   r   r   r   r
    s   
r
  c                 C   s   t | |||S )ab  
     SMAWK algorithm. Find the row minima of a monotone matrix.

    Expose this for testing.

    :type nrows: int
    :param nrows:    number of rows
    :type ncols: int
    :param ncols:    number of columns
    :type x: float
    :param x:        input matrix, size (nrows, ncols)
    :type argmins: int
    :param argmins:  argmin of each row
    )r   smawk)ZnrowsZncolsr2   Zargminsr   r   r   r    s    r  c                 C   s   t | |||S )u}  
     Exact 1D K-Means by dynamic programming

    From  "Fast Exact k-Means, k-Medians and Bregman Divergence Clustering in 1D"
    Allan Grønlund, Kasper Green Larsen, Alexander Mathiasen, Jesper Sindahl
    Nielsen, Stefan Schneider, Mingzhou Song, ArXiV'17

    Section 2.2

    https://arxiv.org/abs/1701.07204

    :type x: float
    :param x:          input 1D array
    :type n: int
    :param n:          input array length
    :type nclusters: int
    :param nclusters:  number of clusters
    :type centroids: float
    :param centroids:  output centroids, size nclusters
    :rtype: float
    :return: imbalancce factor
    )r   kmeans1d)r2   rB   Z	nclustersr9  r   r   r   r  /  s    r  c                   @   sf   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZdd Zdd	 ZejZd
S )Neighborc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   H  r4   zNeighbor.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   H  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZNeighbor_swiginitZnew_NeighborrQ   r   r   r   r?   N  s    zNeighbor.__init__c                 C   s   t | |S r#   )r   ZNeighbor___lt__r^   r   r   r   __lt__Q  s    zNeighbor.__lt__N)r   r   r,   r   r   r   r   r   ZNeighbor_id_getZNeighbor_id_setr  ZNeighbor_distance_getZNeighbor_distance_setrF   ZNeighbor_flag_getZNeighbor_flag_setflagr?   r  Zdelete_NeighborrT   r   r   r   r   r  G  s   r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZeejejZeejejZdd Zdd	 ZejZd
S )Nhoodc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   X  r4   zNhood.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   X  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZNhood_swiginitZ	new_NhoodrQ   r   r   r   r?   a  s    zNhood.__init__c                 C   s   t | ||S r#   )r   ZNhood_insert)r   r  distr   r   r   r   d  s    zNhood.insertN)r   r   r,   r   r   r   r   r   ZNhood_pool_getZNhood_pool_setr*  ZNhood_M_getZNhood_M_setr  ZNhood_nn_old_getZNhood_nn_old_setZnn_oldZNhood_nn_new_getZNhood_nn_new_setZnn_newZNhood_rnn_old_getZNhood_rnn_old_setZrnn_oldZNhood_rnn_new_getZNhood_rnn_new_setZrnn_newr?   r   Zdelete_NhoodrT   r   r   r   r   r  W  s   r  c                   @   s$  e Zd Zedd dd ddZeZdd Zej	Z
dd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZeejejZeejejZeejejZeejejZeej ej!Z"eej#ej$Z%eej&ej'Z(eej)ej*Z+eej,ej-Z.eej/ej0Z1eej2ej3Z4eej5ej6Z7dS )	NNDescentc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   k  r4   zNNDescent.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   k  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZNNDescent_swiginitZnew_NNDescent)r   r   rL  r   r   r   r?   n  s    zNNDescent.__init__c                 C   s   t | |||S r#   )r   ZNNDescent_build)r   r  rB   r   r   r   r   buildr  s    zNNDescent.buildc                 C   s   t | |||||S r#   )r   ZNNDescent_search)r   r  Ztopkindicesdistsr  r   r   r   ru  u  s    zNNDescent.searchc                 C   s
   t | S r#   )r   ZNNDescent_resetr@   r   r   r   r|   x  s    zNNDescent.resetc                 C   s   t | |S )z" Initialize the KNN graph randomly)r   ZNNDescent_init_graphr   r  r   r   r   
init_graph{  s    zNNDescent.init_graphc                 C   s   t | ||S )z Perform NNDescent algorithm)r   ZNNDescent_nndescent)r   r  r   r   r   r   	nndescent  s    zNNDescent.nndescentc                 C   s   t | |S )z  Perform local join on each node)r   ZNNDescent_joinr  r   r   r   join  s    zNNDescent.joinc                 C   s
   t | S )z> Sample new neighbors for each node to peform local join later)r   ZNNDescent_updater@   r   r   r   update  s    zNNDescent.updatec                 C   s   t | ||||S )zF Sample a small number of points to evaluate the quality of KNNG built)r   ZNNDescent_generate_eval_set)r   r  r%  r6   Nr   r   r   generate_eval_set  s    zNNDescent.generate_eval_setc                 C   s   t | ||S )z# Evaluate the quality of KNNG built)r   ZNNDescent_eval_recall)r   Zctrl_pointsZacc_eval_setr   r   r   eval_recall  s    zNNDescent.eval_recallN)8r   r   r,   r   r   r   r   r?   r   Zdelete_NNDescentrT   r  ru  r|   r  r  r  r  r  r  ZNNDescent_has_built_getZNNDescent_has_built_setZ	has_builtZNNDescent_S_getZNNDescent_S_setSZNNDescent_R_getZNNDescent_R_setRZNNDescent_iter_getZNNDescent_iter_setiterZNNDescent_search_L_getZNNDescent_search_L_setsearch_LZNNDescent_random_seed_getZNNDescent_random_seed_setrP  ZNNDescent_K_getZNNDescent_K_setrL  ZNNDescent_d_getZNNDescent_d_setr   ZNNDescent_L_getZNNDescent_L_setLZNNDescent_ntotal_getZNNDescent_ntotal_setr  ZNNDescent_graph_getZNNDescent_graph_setgraphZNNDescent_final_graph_getZNNDescent_final_graph_setfinal_graphr   r   r   r   r  j  s2   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejZeejejZdd	 ZejZd
d Zdd ZdddZdd Zdd ZdS )IndexNNDescentzp
     The NNDescent index is a normal random-access index with an NNDescent
    link structure built on top
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexNNDescent.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z Faiss results are 64-bitc                 G   s   t | t j|  d S r#   )r   ZIndexNNDescent_swiginitZnew_IndexNNDescentrQ   r   r   r   r?     s    zIndexNNDescent.__init__c                 C   s   t | ||S r#   )r   ZIndexNNDescent_addrm  r   r   r   ro    s    zIndexNNDescent.addc                 C   s   t | ||S r  )r   ZIndexNNDescent_trainrm  r   r   r   rn    s    zIndexNNDescent.trainNc              	   C   s   t | ||||||S r  )r   ZIndexNNDescent_searchrs  r   r   r   ru    s    zIndexNNDescent.searchc                 C   s   t | ||S r#   )r   ZIndexNNDescent_reconstructr|  r   r   r   r    s    zIndexNNDescent.reconstructc                 C   s
   t | S r#   )r   ZIndexNNDescent_resetr@   r   r   r   r|     s    zIndexNNDescent.reset)N)r   r   r,   r-   r   r   r   r   r   ZIndexNNDescent_nndescent_getZIndexNNDescent_nndescent_setr  ZIndexNNDescent_own_fields_getZIndexNNDescent_own_fields_setr  ZIndexNNDescent_storage_getZIndexNNDescent_storage_setr  r?   Zdelete_IndexNNDescentrT   ro  rn  ru  r  r|   r   r   r   r   r%    s   
r%  c                   @   s8   e Zd ZdZedd dd ddZeZdd Ze	j
Zd	S )
IndexNNDescentFlatzd
    Flat index topped with with a NNDescent structure to access elements
    more efficiently.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexNNDescentFlat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexNNDescentFlat_swiginitZnew_IndexNNDescentFlatrQ   r   r   r   r?     s    zIndexNNDescentFlat.__init__N)r   r   r,   r-   r   r   r   r   r?   r   Zdelete_IndexNNDescentFlatrT   r   r   r   r   r&    s
   r&  c                   @   sd   e Zd ZdZedd dd ddZeZddd	ZdddZ	dd Z
dd Zdd Zdd ZejZdS )IndexIVFFlatz
     Inverted file with stored vectors. Here the inverted file
    pre-selects the vectors to be searched, but they are not otherwise
    encoded, the code array just contains the raw float entries.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexIVFFlat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   Nc                 C   s   t | |||||S r#   )r   ZIndexIVFFlat_add_corerp  r   r   r   rr    s    zIndexIVFFlat.add_coreFc                 C   s   t | |||||S r#   )r   ZIndexIVFFlat_encode_vectorsr  r   r   r   ru    s    zIndexIVFFlat.encode_vectorsc                 C   s   t | ||S r#   )r   Z$IndexIVFFlat_get_InvertedListScannerrz  r   r   r   r{    s    z$IndexIVFFlat.get_InvertedListScannerc                 C   s   t | |||S r#   )r   Z$IndexIVFFlat_reconstruct_from_offsetr  r   r   r   r    s    z$IndexIVFFlat.reconstruct_from_offsetc                 C   s   t | |||S r#   )r   ZIndexIVFFlat_sa_decoder  r   r   r   r    s    zIndexIVFFlat.sa_decodec                 G   s   t | t j|  d S r#   )r   ZIndexIVFFlat_swiginitZnew_IndexIVFFlatrQ   r   r   r   r?     s    zIndexIVFFlat.__init__)N)F)r   r   r,   r-   r   r   r   r   rr  ru  r{  r  r  r?   r   Zdelete_IndexIVFFlatrT   r   r   r   r   r'    s   

r'  c                   @   s   e Zd Zedd dd ddZeZeejej	ddZ
dd Zd	d
 ZdddZdd ZdddZdd Zdd Zdd ZejZdS )IndexIVFFlatDedupc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexIVFFlatDedup.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z
    Maps ids stored in the index to the ids of vectors that are
    the same. When a vector is unique, it does not appear in the
    instances map
    c                 C   s   t | ||S )z also dedups the training set)r   ZIndexIVFFlatDedup_trainrm  r   r   r   rn    s    zIndexIVFFlatDedup.trainc                 C   s   t | |||S )z& implemented for all IndexIVF* classes)r   ZIndexIVFFlatDedup_add_with_idsrp  r   r   r   rr    s    zIndexIVFFlatDedup.add_with_idsNc                 C   s   t | |||||||||	|
S r#   )r   Z$IndexIVFFlatDedup_search_preassignedrf  r   r   r   rj    s    z$IndexIVFFlatDedup.search_preassignedc                 C   s   t | |S r#   )r   ZIndexIVFFlatDedup_remove_idsrz  r   r   r   r{    s    zIndexIVFFlatDedup.remove_idsc                 C   s   t | |||||S rB  )r   ZIndexIVFFlatDedup_range_searchrv  r   r   r   rw  	  s    zIndexIVFFlatDedup.range_searchc                 C   s   t | |||S rB  )r   Z IndexIVFFlatDedup_update_vectorsr|  r   r   r   r~    s    z IndexIVFFlatDedup.update_vectorsc                 C   s   t | |||S rB  )r   Z)IndexIVFFlatDedup_reconstruct_from_offsetr  r   r   r   r    s    z)IndexIVFFlatDedup.reconstruct_from_offsetc                 G   s   t | t j|  d S r#   )r   ZIndexIVFFlatDedup_swiginitZnew_IndexIVFFlatDeduprQ   r   r   r   r?     s    zIndexIVFFlatDedup.__init__)NN)N)r   r   r,   r   r   r   r   r   ZIndexIVFFlatDedup_instances_getZIndexIVFFlatDedup_instances_setZ	instancesrn  rr  rj  r{  rw  r~  r  r?   Zdelete_IndexIVFFlatDeduprT   r   r   r   r   r(    s   

r(  c                 C   s
   t | S r#   )r   storage_distance_computer)r  r   r   r   r)    s    r)  c                   @   s8  e Zd Zedd dd ddZeZeejej	ddZ
eejejddZeejejddZeejejd	dZeejejd
dZeejejddZeejejddZeejejddZeej ej!ddZ"d+ddZ#dd Z$dd Z%dd Z&dd Z'dd Z(dd Z)dd Z*d d! Z+d"d# Z,d$d% Z-d&d' Z.d(d) Z/ej0Z1d*S ),NSGc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zNSG.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z nb of nodes nb of neighbors per nodez/ length of the search path at construction timez) candidate pool size at construction timez length of the search pathz enterpointz NSG graph structurez NSG is built or notz random generatorr  c                 C   s   t | t | d S r#   )r   ZNSG_swiginitZnew_NSG)r   r  r   r   r   r?   +  s    zNSG.__init__c                 C   s   t | ||||S r#   )r   Z	NSG_build)r   r  rB   	knn_graphr   r   r   r   r  .  s    z	NSG.buildc                 C   s
   t | S r#   )r   Z	NSG_resetr@   r   r   r   r|   1  s    z	NSG.resetc                 C   s   t | |||||S r#   )r   Z
NSG_search)r   r   r   r   r   r  r   r   r   ru  4  s    z
NSG.searchc                 C   s   t | ||S r#   )r   ZNSG_init_graph)r   r  r,  r   r   r   r  7  s    zNSG.init_graphc                 C   s   t | ||||S r#   )r   ZNSG_add_reverse_links)r   qr  r   r#  r   r   r   add_reverse_links:  s    zNSG.add_reverse_linksc              	   C   s   t | ||||||S r#   )r   ZNSG_sync_prune)r   r-  r*  r   r  r,  r#  r   r   r   
sync_prune=  s    zNSG.sync_prunec                 C   s   t | ||||S r#   )r   ZNSG_link)r   r  r,  r#  r   r   r   r   link@  s    zNSG.linkc                 C   s   t | ||S r#   )r   ZNSG_tree_grow)r   r  degreesr   r   r   	tree_growC  s    zNSG.tree_growc                 C   s   t | |||S r#   )r   ZNSG_dfs)r   r  rootZcntr   r   r   dfsF  s    zNSG.dfsc                 C   s   t | ||||S r#   )r   ZNSG_attach_unlinked)r   r  r  Zvt2r1  r   r   r   attach_unlinkedI  s    zNSG.attach_unlinkedc                 C   s
   t | S r#   )r   ZNSG_check_graphr@   r   r   r   check_graphL  s    zNSG.check_graphc                 C   s
   t | S r#   )r   ZNSG_get_final_graphr@   r   r   r   get_final_graphO  s    zNSG.get_final_graphN)r  )2r   r   r,   r   r   r   r   r   ZNSG_ntotal_getZNSG_ntotal_setr  Z	NSG_R_getZ	NSG_R_setr  Z	NSG_L_getZ	NSG_L_setr"  Z	NSG_C_getZ	NSG_C_setCZNSG_search_L_getZNSG_search_L_setr!  ZNSG_enterpoint_getZNSG_enterpoint_setZ
enterpointZNSG_final_graph_getZNSG_final_graph_setr$  ZNSG_is_built_getZNSG_is_built_setis_builtZNSG_rng_getZNSG_rng_setr  r?   r  r|   ru  r  r.  r/  r0  r2  r4  r5  r6  r7  Z
delete_NSGrT   r   r   r   r   r*    s2   
r*  c                   @   s   e Zd Zedd dd ddZeZeejej	ddZ
eejejddZeejejddZeejejd	dZd
d ZejZdd Zdd ZdS )NSG_Graph_intc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   V  r4   zNSG_Graph_int.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   V  r4   r7   r8   z, the flattened adjacency matrix, size N-by-Kr+  z total nb of nodesz+ the underlying data owned by itself or notc                 G   s   t | t j|  d S r#   )r   ZNSG_Graph_int_swiginitZnew_NSG_Graph_intrQ   r   r   r   r?   ]  s    zNSG_Graph_int.__init__c                 G   s   t j| g|R  S r#   )r   ZNSG_Graph_int_atrQ   r   r   r   r\   a  s    zNSG_Graph_int.atc                 C   s   t | ||S r#   )r   ZNSG_Graph_int_get_neighbors)r   r   r  r   r   r   get_neighborsd  s    zNSG_Graph_int.get_neighborsN)r   r   r,   r   r   r   r   r   ZNSG_Graph_int_data_getZNSG_Graph_int_data_setrZ   ZNSG_Graph_int_K_getZNSG_Graph_int_K_setrL  ZNSG_Graph_int_N_getZNSG_Graph_int_N_setr  ZNSG_Graph_int_own_fields_getZNSG_Graph_int_own_fields_setr  r?   Zdelete_NSG_Graph_intrT   r\   r;  r   r   r   r   r:  U  s   r:  c                   @   s  e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejZeejejd	dZeejejd
dZeejejddZeejejddZeejejZ eej!ej"Z#eej$ej%Z&dd Z'ej(Z)dd Z*dd Z+dd Z,dddZ-dd Z.dd Z/dd Z0dS )IndexNSGzc
     The NSG index is a normal random-access index with a NSG
    link structure built on top
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   o  r4   zIndexNSG.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   o  r4   r7   r8   z the link structurez the sequential storagez the index is built or notz K of KNN graph for buildingzx
    indicate how to build a knn graph
    - 0: build NSG with brute force search
    - 1: build NSG with NNDescent
    z parameters for nndescentc                 G   s   t | t j|  d S r#   )r   ZIndexNSG_swiginitZnew_IndexNSGrQ   r   r   r   r?     s    zIndexNSG.__init__c                 C   s   t | ||||S r#   )r   ZIndexNSG_build)r   rB   r2   r,  GKr   r   r   r    s    zIndexNSG.buildc                 C   s   t | ||S r#   )r   ZIndexNSG_addrm  r   r   r   ro    s    zIndexNSG.addc                 C   s   t | ||S r  )r   ZIndexNSG_trainrm  r   r   r   rn    s    zIndexNSG.trainNc              	   C   s   t | ||||||S r  )r   ZIndexNSG_searchrs  r   r   r   ru    s    zIndexNSG.searchc                 C   s   t | ||S r#   )r   ZIndexNSG_reconstructr|  r   r   r   r    s    zIndexNSG.reconstructc                 C   s
   t | S r#   )r   ZIndexNSG_resetr@   r   r   r   r|     s    zIndexNSG.resetc                 C   s   t | |||S r#   )r   ZIndexNSG_check_knn_graph)r   r,  rB   rL  r   r   r   check_knn_graph  s    zIndexNSG.check_knn_graph)N)1r   r   r,   r-   r   r   r   r   r   ZIndexNSG_nsg_getZIndexNSG_nsg_setZnsgZIndexNSG_own_fields_getZIndexNSG_own_fields_setr  ZIndexNSG_storage_getZIndexNSG_storage_setr  ZIndexNSG_is_built_getZIndexNSG_is_built_setr9  ZIndexNSG_GK_getZIndexNSG_GK_setr=  ZIndexNSG_build_type_getZIndexNSG_build_type_setZ
build_typeZIndexNSG_nndescent_S_getZIndexNSG_nndescent_S_setZnndescent_SZIndexNSG_nndescent_R_getZIndexNSG_nndescent_R_setZnndescent_RZIndexNSG_nndescent_L_getZIndexNSG_nndescent_L_setZnndescent_LZIndexNSG_nndescent_iter_getZIndexNSG_nndescent_iter_setZnndescent_iterr?   Zdelete_IndexNSGrT   r  ro  rn  ru  r  r|   r>  r   r   r   r   r<  i  s,   
r<  c                   @   s8   e Zd ZdZedd dd ddZeZdd Ze	j
Zd	S )
IndexNSGFlatz^
    Flat index topped with with a NSG structure to access elements
    more efficiently.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexNSGFlat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexNSGFlat_swiginitZnew_IndexNSGFlatrQ   r   r   r   r?     s    zIndexNSGFlat.__init__N)r   r   r,   r-   r   r   r   r   r?   r   Zdelete_IndexNSGFlatrT   r   r   r   r   r?    s
   r?  c                   @   s@   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	e
jZdS )
IndexNSGPQz\
    PQ index topped with with a NSG structure to access elements
    more efficiently.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexNSGPQ.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexNSGPQ_swiginitZnew_IndexNSGPQrQ   r   r   r   r?     s    zIndexNSGPQ.__init__c                 C   s   t | ||S r#   )r   ZIndexNSGPQ_trainrm  r   r   r   rn    s    zIndexNSGPQ.trainN)r   r   r,   r-   r   r   r   r   r?   rn  r   Zdelete_IndexNSGPQrT   r   r   r   r   r@    s   r@  c                   @   s8   e Zd ZdZedd dd ddZeZdd Ze	j
Zd	S )

IndexNSGSQz\
    SQ index topped with with a NSG structure to access elements
    more efficiently.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexNSGSQ.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexNSGSQ_swiginitZnew_IndexNSGSQrQ   r   r   r   r?     s    zIndexNSGSQ.__init__N)r   r   r,   r-   r   r   r   r   r?   r   Zdelete_IndexNSGSQrT   r   r   r   r   rA    s
   rA  c                   @   s^   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZdd ZejZdS )	OnDiskOneListc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zOnDiskOneList.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZOnDiskOneList_swiginitZnew_OnDiskOneListr@   r   r   r   r?     s    zOnDiskOneList.__init__N)r   r   r,   r   r   r   r   r   ZOnDiskOneList_size_getZOnDiskOneList_size_setr[   ZOnDiskOneList_capacity_getZOnDiskOneList_capacity_setcapacityZOnDiskOneList_offset_getZOnDiskOneList_offset_setr  r?   Zdelete_OnDiskOneListrT   r   r   r   r   rB    s   rB  c                   @   s:  e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZeejejZeejejZdd Zd	d
 Zdd Zdd Zdd Zdd Z d+ddZ!d,ddZ"dd Z#dd Z$ej%Z&eej'ej(Z)eej*ej+Z,eej-ej.Z/dd Z0dd Z1d d! Z2d"d# Z3d$d% Z4d&d' Z5d(d) Z6d*S )-OnDiskInvertedListsa.  
     On-disk storage of inverted lists.

    The data is stored in a mmapped chunk of memory (base pointer ptr,
    size totsize). Each list is a range of memory that contains (object
    List) that contains:

    - uint8_t codes[capacity * code_size]
    - followed by idx_t ids[capacity]

    in each of the arrays, the size <= capacity first elements are
    used, the rest is not initialized.

    Addition and resize are supported by:
    - roundind up the capacity of the lists to a power of two
    - maintaining a list of empty slots, sorted by size.
    - resizing the mmapped block is adjusted as needed.

    An OnDiskInvertedLists is compact if the size == capacity for all
    lists and there are no available slots.

    Addition to the invlists is slow. For incremental add it is better
    to use a default ArrayInvertedLists object and convert it to an
    OnDisk with merge_from.

    When it is known that a set of lists will be accessed, it is useful
    to call prefetch_lists, that launches a set of threads to read the
    lists in parallel.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zOnDiskInvertedLists.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | |S r#   )r   ZOnDiskInvertedLists_list_sizer  r   r   r   r    s    zOnDiskInvertedLists.list_sizec                 C   s   t | |S r#   )r   ZOnDiskInvertedLists_get_codesr  r   r   r   r    s    zOnDiskInvertedLists.get_codesc                 C   s   t | |S r#   )r   ZOnDiskInvertedLists_get_idsr  r   r   r   r    s    zOnDiskInvertedLists.get_idsc                 C   s   t | ||||S r#   )r   ZOnDiskInvertedLists_add_entriesr  r   r   r   r    s    zOnDiskInvertedLists.add_entriesc                 C   s   t | |||||S r#   )r   Z"OnDiskInvertedLists_update_entriesr  r   r   r   r    s    z"OnDiskInvertedLists.update_entriesc                 C   s   t | ||S r#   )r   ZOnDiskInvertedLists_resizer  r   r   r   r]     s    zOnDiskInvertedLists.resizeFc                 C   s   t | ||||S r#   )r   Z'OnDiskInvertedLists_merge_from_multiple)r   r+  Zn_ilr  r   r   r   r   merge_from_multiple  s    z'OnDiskInvertedLists.merge_from_multiplec                 C   s   t | ||S )z( same as merge_from for a single invlist)r   Z OnDiskInvertedLists_merge_from_1)r   r-  r   r   r   r   merge_from_1  s    z OnDiskInvertedLists.merge_from_1c                 C   s   t | ||S )zI restrict the inverted lists to l0:l1 without touching the mmapped region)r   Z!OnDiskInvertedLists_crop_invlists)r   Zl0l1r   r   r   crop_invlists  s    z!OnDiskInvertedLists.crop_invlistsc                 C   s   t | ||S r#   )r   Z"OnDiskInvertedLists_prefetch_listsr  r   r   r   r    s    z"OnDiskInvertedLists.prefetch_listsc                 C   s
   t | S r#   )r   ZOnDiskInvertedLists_do_mmapr@   r   r   r   do_mmap&  s    zOnDiskInvertedLists.do_mmapc                 C   s   t | |S r#   )r   Z"OnDiskInvertedLists_update_totsize)r   Znew_totsizer   r   r   update_totsize)  s    z"OnDiskInvertedLists.update_totsizec                 C   s   t | ||S r#   )r   Z!OnDiskInvertedLists_resize_lockedr  r   r   r   resize_locked,  s    z!OnDiskInvertedLists.resize_lockedc                 C   s   t | |S r#   )r   Z!OnDiskInvertedLists_allocate_slot)r   rC  r   r   r   allocate_slot/  s    z!OnDiskInvertedLists.allocate_slotc                 C   s   t | ||S r#   )r   ZOnDiskInvertedLists_free_slot)r   r  rC  r   r   r   	free_slot2  s    zOnDiskInvertedLists.free_slotc                 C   s   t | |S )z2 override all list sizes and make a packed storage)r   Z'OnDiskInvertedLists_set_all_lists_sizes)r   r7  r   r   r   set_all_lists_sizes5  s    z'OnDiskInvertedLists.set_all_lists_sizesc                 G   s   t | t j|  d S r#   )r   ZOnDiskInvertedLists_swiginitZnew_OnDiskInvertedListsrQ   r   r   r   r?   9  s    zOnDiskInvertedLists.__init__N)FF)F)7r   r   r,   r-   r   r   r   r   r   ZOnDiskInvertedLists_lists_getZOnDiskInvertedLists_lists_setlistsZOnDiskInvertedLists_slots_getZOnDiskInvertedLists_slots_setslotsZ OnDiskInvertedLists_filename_getZ OnDiskInvertedLists_filename_setfilenameZOnDiskInvertedLists_totsize_getZOnDiskInvertedLists_totsize_setZtotsizeZOnDiskInvertedLists_ptr_getZOnDiskInvertedLists_ptr_setr  Z!OnDiskInvertedLists_read_only_getZ!OnDiskInvertedLists_read_only_setZ	read_onlyr  r  r  r  r  r]   rE  rF  rH  r  Zdelete_OnDiskInvertedListsrT   ZOnDiskInvertedLists_locks_getZOnDiskInvertedLists_locks_setr  ZOnDiskInvertedLists_pf_getZOnDiskInvertedLists_pf_setpfZ(OnDiskInvertedLists_prefetch_nthread_getZ(OnDiskInvertedLists_prefetch_nthread_setZprefetch_nthreadrI  rJ  rK  rL  rM  rN  r?   r   r   r   r   rD    s<   

rD  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejddZdd	 Zd
d Zdd ZejZdS )ZnSphereSearcha   
     returns the nearest vertex in the sphere to a query. Returns only
    the coordinates, not an id.

    Algorithm: all points are derived from a one atom vector up to a
    permutation and sign changes. The search function finds the most
    appropriate atom and transformation.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   H  r4   zZnSphereSearch.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   H  r4   r7   r8   z size dim * ntatomc                 C   s   t | t || d S r#   )r   ZZnSphereSearch_swiginitZnew_ZnSphereSearchr   r  r   r   r   r   r?   O  s    zZnSphereSearch.__init__c                 G   s   t j| g|R  S )a  
        *Overload 1:*
        find nearest centroid. x does not need to be normalized

        |

        *Overload 2:*
        full call. Requires externally-allocated temp space

        |

        *Overload 3:*
        full call. Requires externally-allocated temp space
        )r   ZZnSphereSearch_searchrQ   r   r   r   ru  R  s    zZnSphereSearch.searchc                 C   s   t | ||||S r#   )r   ZZnSphereSearch_search_multi)r   rB   r2   Zc_outZdp_outr   r   r   search_multic  s    zZnSphereSearch.search_multiN)r   r   r,   r-   r   r   r   r   r   ZZnSphereSearch_dimS_getZZnSphereSearch_dimS_setZdimSZZnSphereSearch_r2_getZZnSphereSearch_r2_setr   ZZnSphereSearch_natom_getZZnSphereSearch_natom_setZnatomZZnSphereSearch_voc_getZZnSphereSearch_voc_setZvocr?   ru  rU  Zdelete_ZnSphereSearchrT   r   r   r   r   rS  >  s   	rS  c                   @   s|   e Zd Zedd dd ddZdd ZeZeej	ej
ddZeejejZd	d
 Zdd Zdd Zdd Zdd ZejZdS )EnumeratedVectorsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   j  r4   zEnumeratedVectors.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   j  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   l  s    zEnumeratedVectors.__init__z size of the collectionc                 C   s   t | |S )z" encode a vector from a collection)r   ZEnumeratedVectors_encoderE   r   r   r   r  r  s    zEnumeratedVectors.encodec                 C   s   t | ||S )z
 decode it)r   ZEnumeratedVectors_decoder   r   r%  r   r   r   r  v  s    zEnumeratedVectors.decodec                 C   s   t | |||S r#   )r   ZEnumeratedVectors_encode_multi)r   ncr%  r   r   r   r   encode_multiz  s    zEnumeratedVectors.encode_multic                 C   s   t | |||S r#   )r   ZEnumeratedVectors_decode_multi)r   rX  r   r%  r   r   r   decode_multi}  s    zEnumeratedVectors.decode_multic              	   C   s   t | ||||||S r#   )r   ZEnumeratedVectors_find_nn)r   rB   r   r   r  r   r   r   r   r   find_nn  s    zEnumeratedVectors.find_nnN)r   r   r,   r   r   r?   r   r   r   ZEnumeratedVectors_nv_getZEnumeratedVectors_nv_setr}  ZEnumeratedVectors_dim_getZEnumeratedVectors_dim_setr  r  r  rY  rZ  r[  Zdelete_EnumeratedVectorsrT   r   r   r   r   rV  i  s   rV  c                   @   sP   e Zd Zedd dd ddZeZeejej	Z
eejejZdd ZejZdS )	Repeatc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zRepeat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZRepeat_swiginitZ
new_Repeatr@   r   r   r   r?     s    zRepeat.__init__N)r   r   r,   r   r   r   r   r   ZRepeat_val_getZRepeat_val_setvalZRepeat_n_getZRepeat_n_setrB   r?   Zdelete_RepeatrT   r   r   r   r   r\    s   r\  c                   @   sn   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd	d
Zdd Zdd Zdd ZejZdS )Repeatsz
    Repeats: used to encode a vector that has n occurrences of
    val. Encodes the signs and permutation of the vector. Useful for
    atoms.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zRepeats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   r   Nc                 C   s   t | t || d S r#   )r   ZRepeats_swiginitZnew_Repeats)r   r  r%  r   r   r   r?     s    zRepeats.__init__c                 C   s
   t | S r#   )r   ZRepeats_countr@   r   r   r   count  s    zRepeats.countc                 C   s   t | |S r#   )r   ZRepeats_encoder  r   r   r   r    s    zRepeats.encodec                 C   s   t | ||S r#   )r   ZRepeats_decoderW  r   r   r   r    s    zRepeats.decode)r   N)r   r   r,   r-   r   r   r   r   r   ZRepeats_dim_getZRepeats_dim_setr  ZRepeats_repeats_getZRepeats_repeats_setZrepeatsr?   r_  r  r  Zdelete_RepeatsrT   r   r   r   r   r^    s   
r^  c                   @   sz   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZdd Zd	d
 Zdd Zdd ZejZdS )ZnSphereCodecz
     codec that can return ids for the encoded vectors

    uses the ZnSphereSearch to encode the vector by encoding the
    permutation and signs. Depends on ZnSphereSearch because it uses
    the atom numbers
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zZnSphereCodec.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZZnSphereCodec_swiginitZnew_ZnSphereCodecrT  r   r   r   r?     s    zZnSphereCodec.__init__c                 C   s   t | |S r#   )r   ZZnSphereCodec_search_and_encoderE   r   r   r   search_and_encode  s    zZnSphereCodec.search_and_encodec                 C   s   t | ||S r#   )r   ZZnSphereCodec_decoderW  r   r   r   r    s    zZnSphereCodec.decodec                 C   s   t | |S )z/ takes vectors that do not need to be centroids)r   ZZnSphereCodec_encoderE   r   r   r   r    s    zZnSphereCodec.encodeN)r   r   r,   r-   r   r   r   r   r   ZZnSphereCodec_code_segments_getZZnSphereCodec_code_segments_setZcode_segmentsZZnSphereCodec_nv_getZZnSphereCodec_nv_setr}  ZZnSphereCodec_code_size_getZZnSphereCodec_code_size_setr   r?   ra  r  r  Zdelete_ZnSphereCodecrT   r   r   r   r   r`    s   r`  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZdd Zd	d
 Zdd Zdd ZeejejZeejejZeejejZeejej Z!dd Z"dd Z#dd Z$ej%Z&dS )ZnSphereCodecRecz
     recursive sphere codec

    Uses a recursive decomposition on the dimensions to encode
    centroids found by the ZnSphereSearch. The codes are *not*
    compatible with the ones of ZnSpehreCodec
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zZnSphereCodecRec.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZZnSphereCodecRec_swiginitZnew_ZnSphereCodecRecrT  r   r   r   r?     s    zZnSphereCodecRec.__init__c                 C   s   t | |S r#   )r   Z ZnSphereCodecRec_encode_centroidr  r   r   r   encode_centroid  s    z ZnSphereCodecRec.encode_centroidc                 C   s   t | ||S r#   )r   ZZnSphereCodecRec_decoderW  r   r   r   r    s    zZnSphereCodecRec.decodec                 C   s   t | |S )z[
        vectors need to be centroids (does not work on arbitrary
        vectors)
        )r   ZZnSphereCodecRec_encoderE   r   r   r   r    s    zZnSphereCodecRec.encodec                 C   s   t | ||S r#   )r   ZZnSphereCodecRec_get_nv)r   ldr2ar   r   r   get_nv  s    zZnSphereCodecRec.get_nvc                 C   s   t | |||S r#   )r   ZZnSphereCodecRec_get_nv_cum)r   rd  r2tre  r   r   r   
get_nv_cum  s    zZnSphereCodecRec.get_nv_cumc                 C   s   t | ||||S r#   )r   ZZnSphereCodecRec_set_nv_cum)r   rd  rg  re  r6   r   r   r   
set_nv_cum  s    zZnSphereCodecRec.set_nv_cumN)'r   r   r,   r-   r   r   r   r   r   ZZnSphereCodecRec_r2_getZZnSphereCodecRec_r2_setr   ZZnSphereCodecRec_log2_dim_getZZnSphereCodecRec_log2_dim_setZlog2_dimZZnSphereCodecRec_code_size_getZZnSphereCodecRec_code_size_setr   r?   rc  r  r  ZZnSphereCodecRec_all_nv_getZZnSphereCodecRec_all_nv_setZall_nvZZnSphereCodecRec_all_nv_cum_getZZnSphereCodecRec_all_nv_cum_setZ
all_nv_cumZ$ZnSphereCodecRec_decode_cache_ld_getZ$ZnSphereCodecRec_decode_cache_ld_setZdecode_cache_ldZ!ZnSphereCodecRec_decode_cache_getZ!ZnSphereCodecRec_decode_cache_setZdecode_cacherf  rh  ri  Zdelete_ZnSphereCodecRecrT   r   r   r   r   rb    s$   rb  c                   @   sd   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 Zdd ZejZdS )ZnSphereCodecAltzg
     Codec that uses the recursive codec if dim is a power of 2 and
    the regular one otherwise
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zZnSphereCodecAlt.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZZnSphereCodecAlt_swiginitZnew_ZnSphereCodecAltrT  r   r   r   r?     s    zZnSphereCodecAlt.__init__c                 C   s   t | |S r#   )r   ZZnSphereCodecAlt_encoderE   r   r   r   r  	  s    zZnSphereCodecAlt.encodec                 C   s   t | ||S r#   )r   ZZnSphereCodecAlt_decoderW  r   r   r   r    s    zZnSphereCodecAlt.decodeN)r   r   r,   r-   r   r   r   r   r   ZZnSphereCodecAlt_use_rec_getZZnSphereCodecAlt_use_rec_setZuse_recZZnSphereCodecAlt_znc_rec_getZZnSphereCodecAlt_znc_rec_setZznc_recr?   r  r  Zdelete_ZnSphereCodecAltrT   r   r   r   r   rj    s   rj  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejZeejejddZdd Zdd Zdd Zdd Zdd Zej Z!dS )IndexLatticezC Index that encodes a vector with a series of Zn lattice quantizersc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexLattice.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z number of sub-vectorsz dimension of sub-vectorsz the lattice quantizerz0 nb bits used to encode the scale, per subvectorz5 mins and maxes of the vector norms, per subquantizerc              	   C   s   t | t |||| d S r#   )r   ZIndexLattice_swiginitZnew_IndexLattice)r   r   nsq
scale_nbitr   r   r   r   r?     s    zIndexLattice.__init__c                 C   s   t | ||S r#   )r   ZIndexLattice_trainrm  r   r   r   rn  !  s    zIndexLattice.trainc                 C   s
   t | S r#   )r   ZIndexLattice_sa_code_sizer@   r   r   r   r  $  s    zIndexLattice.sa_code_sizec                 C   s   t | |||S r#   )r   ZIndexLattice_sa_encoder  r   r   r   r  '  s    zIndexLattice.sa_encodec                 C   s   t | |||S r#   )r   ZIndexLattice_sa_decoder  r   r   r   r  *  s    zIndexLattice.sa_decodeN)"r   r   r,   r-   r   r   r   r   r   ZIndexLattice_nsq_getZIndexLattice_nsq_setrl  ZIndexLattice_dsq_getZIndexLattice_dsq_setZdsqZ IndexLattice_zn_sphere_codec_getZ IndexLattice_zn_sphere_codec_setZzn_sphere_codecZIndexLattice_scale_nbit_getZIndexLattice_scale_nbit_setrm  ZIndexLattice_lattice_nbit_getZIndexLattice_lattice_nbit_setZlattice_nbitZIndexLattice_trained_getZIndexLattice_trained_setr  r?   rn  r  r  r  Zdelete_IndexLatticerT   r   r   r   r   rk    s   rk  c                   @   sX   e Zd Zedd dd ddZeZeejej	ddZ
eejejddZdd	 ZejZd
S )IVFPQSearchParametersc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   1  r4   zIVFPQSearchParameters.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   1  r4   r7   r8   % use table computation or on-the-fly?( Hamming thresh for polysemous filteringc                 C   s   t | t   d S r#   )r   ZIVFPQSearchParameters_swiginitZnew_IVFPQSearchParametersr@   r   r   r   r?   6  s    zIVFPQSearchParameters.__init__N)r   r   r,   r   r   r   r   r   Z.IVFPQSearchParameters_scan_table_threshold_getZ.IVFPQSearchParameters_scan_table_threshold_setscan_table_thresholdZ'IVFPQSearchParameters_polysemous_ht_getZ'IVFPQSearchParameters_polysemous_ht_setr  r?   Zdelete_IVFPQSearchParametersrT   r   r   r   r   rn  0  s   rn  c                   @   s&  e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZeejejddZeejejddZd,ddZdd Zd-ddZ d.ddZ!dd Z"dd Z#dd Z$dd Z%d d! Z&d/d"d#Z'd$d% Z(d&d' Z)d(d) Z*d*d+ Z+ej,Z-dS )0
IndexIVFPQzz
     Inverted file with Product Quantizer encoding. Each residual
    vector is encoded as a product quantizer code.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   B  r4   zIndexIVFPQ.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   B  r4   r7   r8    produces the codesz% reorder PQ centroids after training?z if NULL, use defaultro  rp  z
     Precompute table that speed up query preprocessing at some
    memory cost (used only for by_residual with L2 metric)
    zA
    if use_precompute_table
    size nlist * pq.M * pq.ksub
    Fc                 C   s   t | |||||S r#   )r   ZIndexIVFPQ_encode_vectorsr  r   r   r   ru  R  s    zIndexIVFPQ.encode_vectorsc                 C   s   t | |||S r#   )r   ZIndexIVFPQ_sa_decoder  r   r   r   r  U  s    zIndexIVFPQ.sa_decodeNc                 C   s   t | |||||S r#   )r   ZIndexIVFPQ_add_corerp  r   r   r   rr  X  s    zIndexIVFPQ.add_corec              	   C   s   t | ||||||S )z
        same as add_core, also:
        - output 2nd level residuals if residuals_2 != NULL
        - accepts precomputed_idx = nullptr
        )r   ZIndexIVFPQ_add_core_o)r   rB   r2   rq  Zresiduals_2rq  r  r   r   r   
add_core_o[  s    zIndexIVFPQ.add_core_oc                 C   s   t | |||S )z trains the product quantizer)r   ZIndexIVFPQ_train_encoderrv  r   r   r   rw  c  s    zIndexIVFPQ.train_encoderc                 C   s
   t | S r#   )r   Z$IndexIVFPQ_train_encoder_num_vectorsr@   r   r   r   rx  g  s    z$IndexIVFPQ.train_encoder_num_vectorsc                 C   s   t | |||S r#   )r   Z"IndexIVFPQ_reconstruct_from_offsetr  r   r   r   r  j  s    z"IndexIVFPQ.reconstruct_from_offsetc                 C   s   t | ||S )a  
         Find exact duplicates in the dataset.

        the duplicates are returned in pre-allocated arrays (see the
        max sizes).

        :type lims: int
        :param lims:   limits between groups of duplicates
                           (max size ntotal / 2 + 1)
        :type ids: int
        :param ids:    ids[lims[i]] : ids[lims[i+1]-1] is a group of
                           duplicates (max size ntotal)
        :rtype: int
        :return: n      number of groups found
        )r   ZIndexIVFPQ_find_duplicates)r   r'  r[  r   r   r   find_duplicatesm  s    zIndexIVFPQ.find_duplicatesc                 C   s   t | |||S r#   )r   ZIndexIVFPQ_encode)r   r}  r2   r   r   r   r   r    s    zIndexIVFPQ.encodec                 C   s   t | |||||S )a  
         Encode multiple vectors

        :type n: int
        :param n:       nb vectors to encode
        :type keys: int
        :param keys:    posting list ids for those vectors (size n)
        :type x: float
        :param x:       vectors (size n * d)
        :type codes: uint8_t
        :param codes:   output codes (size n * code_size)
        :type compute_keys: boolean, optional
        :param compute_keys:  if false, assume keys are precomputed,
                                 otherwise compute them
        )r   ZIndexIVFPQ_encode_multiple)r   rB   rc  r2   r   Zcompute_keysr   r   r   encode_multiple  s    zIndexIVFPQ.encode_multiplec                 C   s   t | ||||S )z inverse of encode_multiple)r   ZIndexIVFPQ_decode_multiple)r   rB   rc  Zxcodesr2   r   r   r   decode_multiple  s    zIndexIVFPQ.decode_multiplec                 C   s   t | ||S r#   )r   Z"IndexIVFPQ_get_InvertedListScannerrz  r   r   r   r{    s    z"IndexIVFPQ.get_InvertedListScannerc                 C   s
   t | S )z build precomputed table)r   ZIndexIVFPQ_precompute_tabler@   r   r   r   precompute_table  s    zIndexIVFPQ.precompute_tablec                 G   s   t | t j|  d S r#   )r   ZIndexIVFPQ_swiginitZnew_IndexIVFPQrQ   r   r   r   r?     s    zIndexIVFPQ.__init__)F)N)NN)F).r   r   r,   r-   r   r   r   r   r   ZIndexIVFPQ_pq_getZIndexIVFPQ_pq_setr  Z%IndexIVFPQ_do_polysemous_training_getZ%IndexIVFPQ_do_polysemous_training_setr  Z"IndexIVFPQ_polysemous_training_getZ"IndexIVFPQ_polysemous_training_setr  Z#IndexIVFPQ_scan_table_threshold_getZ#IndexIVFPQ_scan_table_threshold_setrq  ZIndexIVFPQ_polysemous_ht_getZIndexIVFPQ_polysemous_ht_setr  Z$IndexIVFPQ_use_precomputed_table_getZ$IndexIVFPQ_use_precomputed_table_setr  Z IndexIVFPQ_precomputed_table_getZ IndexIVFPQ_precomputed_table_setprecomputed_tableru  r  rr  rt  rw  rx  r  ru  r  rv  rw  r{  rx  r?   Zdelete_IndexIVFPQrT   r   r   r   r   rr  <  s2   



rr  c                 C   s   t | |||||S )ap  
     Pre-compute distance tables for IVFPQ with by-residual and METRIC_L2

    :type use_precomputed_table: int
    :param use_precomputed_table: (I/O)
               =-1: force disable
               =0: decide heuristically (default: use tables only if they are
                   < precomputed_tables_max_bytes), set use_precomputed_table on
        output =1: tables that work for all quantizers (size 256 * nlist * M) =2:
        specific version for MultiIndexQuantizer (much more compact)
    :type precomputed_table: faiss::AlignedTable< float,32 >
    :param precomputed_table: precomputed table to initialize
    )r   "initialize_IVFPQ_precomputed_table)r  r^  r  ry  r  r   r   r   r   rz    s    rz  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejZeejejd	dZd
d Zdd ZejZdS )IndexIVFPQStatsz
    statistics are robust to internal threading, but not if
    IndexIVFPQ::search_preassigned is called by multiple threads
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexIVFPQStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z nb of refines (IVFPQR)z5 nb of passed Hamming distance tests (for polysemous)z only for IVFPQRc                 C   s   t | t   d S r#   )r   ZIndexIVFPQStats_swiginitZnew_IndexIVFPQStatsr@   r   r   r   r?     s    zIndexIVFPQStats.__init__c                 C   s
   t | S r#   )r   ZIndexIVFPQStats_resetr@   r   r   r   r|     s    zIndexIVFPQStats.resetN)r   r   r,   r-   r   r   r   r   r   ZIndexIVFPQStats_nrefine_getZIndexIVFPQStats_nrefine_setZnrefineZ"IndexIVFPQStats_n_hamming_pass_getZ"IndexIVFPQStats_n_hamming_pass_setr  Z!IndexIVFPQStats_search_cycles_getZ!IndexIVFPQStats_search_cycles_setZsearch_cyclesZ!IndexIVFPQStats_refine_cycles_getZ!IndexIVFPQStats_refine_cycles_setZrefine_cyclesr?   r|   Zdelete_IndexIVFPQStatsrT   r   r   r   r   r{    s   r{  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZd
d Zdd Zdd Zdd Zdd ZdddZdd Zdd Zd ddZdd ZejZdS )!IndexIVFPQRz0 Index with an additional level of PQ refinementc                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   zIndexIVFPQR.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   z 3rd level quantizerz corresponding codeszH factor between k requested in search and the k requested from the IVFPQc                 C   s
   t | S r#   )r   ZIndexIVFPQR_resetr@   r   r   r   r|     s    zIndexIVFPQR.resetc                 C   s   t | |S r#   )r   ZIndexIVFPQR_remove_idsrz  r   r   r   r{    s    zIndexIVFPQR.remove_idsc                 C   s   t | |||S )z" trains the two product quantizers)r   ZIndexIVFPQR_train_encoderrv  r   r   r   rw    s    zIndexIVFPQR.train_encoderc                 C   s
   t | S r#   )r   Z%IndexIVFPQR_train_encoder_num_vectorsr@   r   r   r   rx    s    z%IndexIVFPQR.train_encoder_num_vectorsc                 C   s   t | |||S r#   )r   ZIndexIVFPQR_add_with_idsrp  r   r   r   rr    s    zIndexIVFPQR.add_with_idsNc                 C   s   t | |||||S )zB same as add_with_ids, but optionally use the precomputed list ids)r   ZIndexIVFPQR_add_corerp  r   r   r   rr    s    zIndexIVFPQR.add_corec                 C   s   t | |||S r#   )r   Z#IndexIVFPQR_reconstruct_from_offsetr  r   r   r   r    s    z#IndexIVFPQR.reconstruct_from_offsetc                 C   s   t | ||S r#   )r   ZIndexIVFPQR_merge_fromr  r   r   r   r    s    zIndexIVFPQR.merge_fromc                 C   s   t | |||||||||	|
S r#   )r   ZIndexIVFPQR_search_preassignedrf  r   r   r   rj    s    zIndexIVFPQR.search_preassignedc                 G   s   t | t j|  d S r#   )r   ZIndexIVFPQR_swiginitZnew_IndexIVFPQRrQ   r   r   r   r?     s    zIndexIVFPQR.__init__)N)NN)r   r   r,   r-   r   r   r   r   r   ZIndexIVFPQR_refine_pq_getZIndexIVFPQR_refine_pq_setZ	refine_pqZIndexIVFPQR_refine_codes_getZIndexIVFPQR_refine_codes_setZrefine_codesZIndexIVFPQR_k_factor_getZIndexIVFPQR_k_factor_setr  r|   r{  rw  rx  rr  rr  r  r  rj  r?   Zdelete_IndexIVFPQRrT   r   r   r   r   r|    s"   

r|  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZdd ZejZdd ZdddZdd Zdd Zdd Zdd ZdS )Index2Layerz
     Same as an IndexIVFPQ without the inverted lists: codes are stored
    sequentially

    The class is mainly inteded to store encoded vectors that can be
    accessed randomly, the search function is not implemented.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zIndex2Layer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   z first level quantizerz& second level quantizer is always a PQz< size of the code for the first level (ceil(log8(q1.nlist)))z& size of the code for the second levelc                 G   s   t | t j|  d S r#   )r   ZIndex2Layer_swiginitZnew_Index2LayerrQ   r   r   r   r?     s    zIndex2Layer.__init__c                 C   s   t | ||S r#   )r   ZIndex2Layer_trainrm  r   r   r   rn    s    zIndex2Layer.trainNc              	   C   s   t | ||||||S rB  )r   ZIndex2Layer_searchrs  r   r   r   ru    s    zIndex2Layer.searchc                 C   s
   t | S r#   )r   Z!Index2Layer_get_distance_computerr@   r   r   r   r    s    z!Index2Layer.get_distance_computerc                 C   s   t | |S )z* transfer the flat codes to an IVFPQ index)r   ZIndex2Layer_transfer_to_IVFPQr^   r   r   r   transfer_to_IVFPQ  s    zIndex2Layer.transfer_to_IVFPQc                 C   s   t | |||S r#   )r   ZIndex2Layer_sa_encoder  r   r   r   r    s    zIndex2Layer.sa_encodec                 C   s   t | |||S r#   )r   ZIndex2Layer_sa_decoder  r   r   r   r    s    zIndex2Layer.sa_decode)N)r   r   r,   r-   r   r   r   r   r   ZIndex2Layer_q1_getZIndex2Layer_q1_setq1ZIndex2Layer_pq_getZIndex2Layer_pq_setr  ZIndex2Layer_code_size_1_getZIndex2Layer_code_size_1_setZcode_size_1ZIndex2Layer_code_size_2_getZIndex2Layer_code_size_2_setZcode_size_2r?   Zdelete_Index2LayerrT   rn  ru  r  r~  r  r  r   r   r   r   r}    s   
r}  c                   @   sT  e Zd ZdZedd dd ddZdd ZeZee	j
e	jZee	je	jZee	je	jZee	je	jZee	je	jZee	je	jZee	je	jZee	je	j Z!ee	j"e	j#Z$ee	j%e	j&Z'ee	j(e	j)Z*ee	j+e	j,Z-d	d
 Z.dd Z/d'ddZ0dd Z1dd Z2dd Z3dd Z4dd Z5dd Z6dd Z7d(dd Z8d!d" Z9d#d$ Z:d%d& Z;e	j<Z=dS ))IndexFastScana*  
     Fast scan version of IndexPQ and IndexAQ. Works for 4-bit PQ and AQ for now.

    The codes are not stored sequentially but grouped in blocks of size bbs.
    This makes it possible to compute distances quickly with SIMD instructions.
    The trailing codes (padding codes that are added to complete the last code)
    are garbage.

    Implementations:
    12: blocked loop with internal loop on Q with qbs
    13: same with reservoir accumulator to store results
    14: no qbs with heap accumulator
    15: no qbs with reservoir accumulator
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   1  r4   zIndexFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   1  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   3  s    zIndexFastScan.__init__c                 C   s   t | |||||S r#   )r   ZIndexFastScan_init_fastscan)r   r   r  r   metricbbsr   r   r   init_fastscanC  s    zIndexFastScan.init_fastscanc                 C   s
   t | S r#   )r   ZIndexFastScan_resetr@   r   r   r   r|   F  s    zIndexFastScan.resetNc              	   C   s   t | ||||||S r#   )r   ZIndexFastScan_searchrs  r   r   r   ru  I  s    zIndexFastScan.searchc                 C   s   t | ||S r#   )r   ZIndexFastScan_addrm  r   r   r   ro  L  s    zIndexFastScan.addc                 C   s   t | |||S r#   )r   ZIndexFastScan_compute_codesr   r   rB   r2   r   r   r   r  O  s    zIndexFastScan.compute_codesc                 C   s   t | |||S r#   )r   ZIndexFastScan_compute_float_LUTr   lutrB   r2   r   r   r   compute_float_LUTR  s    zIndexFastScan.compute_float_LUTc                 C   s   t | ||||S r#   )r   Z#IndexFastScan_compute_quantized_LUT)r   rB   r2   r  normalizersr   r   r   compute_quantized_LUTU  s    z#IndexFastScan.compute_quantized_LUTc                 C   s   t | ||S r#   )r   ZIndexFastScan_reconstructr|  r   r   r   r  X  s    zIndexFastScan.reconstructc                 C   s   t | |S r#   )r   ZIndexFastScan_remove_idsrz  r   r   r   r{  [  s    zIndexFastScan.remove_idsc                 C   s
   t | S r#   )r   ZIndexFastScan_get_CodePackerr@   r   r   r   r  ^  s    zIndexFastScan.get_CodePackerr   c                 C   s   t | ||S r#   )r   ZIndexFastScan_merge_fromr  r   r   r   r  a  s    zIndexFastScan.merge_fromc                 C   s   t | |S r#   )r   Z(IndexFastScan_check_compatible_for_merger  r   r   r   r  d  s    z(IndexFastScan.check_compatible_for_mergec                 C   s
   t | S )z9 standalone codes interface (but the codes are flattened))r   ZIndexFastScan_sa_code_sizer@   r   r   r   r  g  s    zIndexFastScan.sa_code_sizec                 C   s   t | |||S r#   )r   ZIndexFastScan_sa_encoder  r   r   r   r  k  s    zIndexFastScan.sa_encode)N)r   )>r   r   r,   r-   r   r   r?   r   r   r   ZIndexFastScan_implem_getZIndexFastScan_implem_setimplemZIndexFastScan_skip_getZIndexFastScan_skip_setskipZIndexFastScan_bbs_getZIndexFastScan_bbs_setr  ZIndexFastScan_qbs_getZIndexFastScan_qbs_setqbsZIndexFastScan_M_getZIndexFastScan_M_setr  ZIndexFastScan_nbits_getZIndexFastScan_nbits_setr   ZIndexFastScan_ksub_getZIndexFastScan_ksub_setr8  ZIndexFastScan_code_size_getZIndexFastScan_code_size_setr   ZIndexFastScan_ntotal2_getZIndexFastScan_ntotal2_setZntotal2ZIndexFastScan_M2_getZIndexFastScan_M2_setM2ZIndexFastScan_codes_getZIndexFastScan_codes_setr   ZIndexFastScan_orig_codes_getZIndexFastScan_orig_codes_setZ
orig_codesr  r|   ru  ro  r  r  r  r  r{  r  r  r  r  r  Zdelete_IndexFastScanrT   r   r   r   r   r  !  s>   

r  c                   @   st   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZdd Zdd	 ZejZd
S )FastScanStatsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   r  r4   zFastScanStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   r  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZFastScanStats_swiginitZnew_FastScanStatsr@   r   r   r   r?   y  s    zFastScanStats.__init__c                 C   s
   t | S r#   )r   ZFastScanStats_resetr@   r   r   r   r|   |  s    zFastScanStats.resetN)r   r   r,   r   r   r   r   r   ZFastScanStats_t0_getZFastScanStats_t0_setr^  ZFastScanStats_t1_getZFastScanStats_t1_sett1ZFastScanStats_t2_getZFastScanStats_t2_sett2ZFastScanStats_t3_getZFastScanStats_t3_sett3r?   r|   Zdelete_FastScanStatsrT   r   r   r   r   r  q  s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZdd ZejZd	d
 Zdd Zdd Zdd Zdd ZdddZdd ZdS )IndexAdditiveQuantizerFastScana  
     Fast scan version of IndexAQ. Works for 4-bit AQ for now.

    The codes are not stored sequentially but grouped in blocks of size bbs.
    This makes it possible to compute distances quickly with SIMD instructions.

    Implementations:
    12: blocked loop with internal loop on Q with qbs
    13: same with reservoir accumulator to store results
    14: no qbs with heap accumulator
    15: no qbs with reservoir accumulator
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z'IndexAdditiveQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t j| g|R  S r#   )r   Z#IndexAdditiveQuantizerFastScan_initrQ   r   r   r   ra    s    z#IndexAdditiveQuantizerFastScan.initc                 G   s   t | t j|  dS )z
        *Overload 1:*
        build from an existing IndexAQ

        |

        *Overload 2:*
        build from an existing IndexAQ
        N)r   Z'IndexAdditiveQuantizerFastScan_swiginitZ"new_IndexAdditiveQuantizerFastScanrQ   r   r   r   r?     s    
z'IndexAdditiveQuantizerFastScan.__init__c                 C   s   t | ||S r#   )r   Z$IndexAdditiveQuantizerFastScan_trainrm  r   r   r   rn    s    z$IndexAdditiveQuantizerFastScan.trainc                 C   s   t | ||S r#   )r   Z2IndexAdditiveQuantizerFastScan_estimate_norm_scalerm  r   r   r   estimate_norm_scale  s    z2IndexAdditiveQuantizerFastScan.estimate_norm_scalec                 C   s   t | |||S r#   )r   Z,IndexAdditiveQuantizerFastScan_compute_codesr  r   r   r   r    s    z,IndexAdditiveQuantizerFastScan.compute_codesc                 C   s   t | |||S r#   )r   Z0IndexAdditiveQuantizerFastScan_compute_float_LUTr  r   r   r   r    s    z0IndexAdditiveQuantizerFastScan.compute_float_LUTNc              	   C   s   t | ||||||S r#   )r   Z%IndexAdditiveQuantizerFastScan_searchrs  r   r   r   ru    s    z%IndexAdditiveQuantizerFastScan.searchc                 C   s   t | |||S )a  
         Decode a set of vectors.

         NOTE: The codes in the IndexAdditiveQuantizerFastScan object are non-
               contiguous. But this method requires a contiguous representation.

        :type n: int
        :param n:       number of vectors
        :type bytes: uint8_t
        :param bytes:   input encoded vectors, size n * code_size
        :type x: float
        :param x:       output vectors, size n * d
        )r   Z(IndexAdditiveQuantizerFastScan_sa_decoder  r   r   r   r    s    z(IndexAdditiveQuantizerFastScan.sa_decode)N)r   r   r,   r-   r   r   r   r   r   Z%IndexAdditiveQuantizerFastScan_aq_getZ%IndexAdditiveQuantizerFastScan_aq_setr  Z/IndexAdditiveQuantizerFastScan_rescale_norm_getZ/IndexAdditiveQuantizerFastScan_rescale_norm_setrescale_normZ-IndexAdditiveQuantizerFastScan_norm_scale_getZ-IndexAdditiveQuantizerFastScan_norm_scale_set
norm_scaleZ3IndexAdditiveQuantizerFastScan_max_train_points_getZ3IndexAdditiveQuantizerFastScan_max_train_points_setr  ra  Z%delete_IndexAdditiveQuantizerFastScanrT   r?   rn  r  r  r  ru  r  r   r   r   r   r    s    
r  c                   @   sJ   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 ZejZd
S )IndexResidualQuantizerFastScanr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z'IndexResidualQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   r  c                 G   s   t | t j|  d S r#   )r   Z'IndexResidualQuantizerFastScan_swiginitZ"new_IndexResidualQuantizerFastScanrQ   r   r   r   r?     s    z'IndexResidualQuantizerFastScan.__init__N)r   r   r,   r-   r   r   r   r   r   Z%IndexResidualQuantizerFastScan_rq_getZ%IndexResidualQuantizerFastScan_rq_setr%  r?   Z%delete_IndexResidualQuantizerFastScanrT   r   r   r   r   r    s   r  c                   @   sF   e Zd ZdZedd dd ddZeZeej	ej
Zdd ZejZd	S )
!IndexLocalSearchQuantizerFastScanz
     Index based on a local search quantizer. Stored vectors are
    approximated by local search quantization codes.
    Can also be used as a codec
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z*IndexLocalSearchQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z*IndexLocalSearchQuantizerFastScan_swiginitZ%new_IndexLocalSearchQuantizerFastScanrQ   r   r   r   r?     s    z*IndexLocalSearchQuantizerFastScan.__init__N)r   r   r,   r-   r   r   r   r   r   Z)IndexLocalSearchQuantizerFastScan_lsq_getZ)IndexLocalSearchQuantizerFastScan_lsq_setrS  r?   Z(delete_IndexLocalSearchQuantizerFastScanrT   r   r   r   r   r    s   r  c                   @   sJ   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 ZejZd
S )%IndexProductResidualQuantizerFastScanz
     Index based on a product residual quantizer. Stored vectors are
    approximated by product residual quantization codes.
    Can also be used as a codec
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3     r4   z.IndexProductResidualQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3     r4   r7   r8   r  c                 G   s   t | t j|  d S r#   )r   Z.IndexProductResidualQuantizerFastScan_swiginitZ)new_IndexProductResidualQuantizerFastScanrQ   r   r   r   r?     s    z.IndexProductResidualQuantizerFastScan.__init__N)r   r   r,   r-   r   r   r   r   r   Z-IndexProductResidualQuantizerFastScan_prq_getZ-IndexProductResidualQuantizerFastScan_prq_setr  r?   Z,delete_IndexProductResidualQuantizerFastScanrT   r   r   r   r   r    s   r  c                   @   sJ   e Zd ZdZedd dd ddZeZeej	ej
ddZdd	 ZejZd
S )(IndexProductLocalSearchQuantizerFastScanz
     Index based on a product local search quantizer. Stored vectors are
    approximated by product local search quantization codes.
    Can also be used as a codec
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   z1IndexProductLocalSearchQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   r  c                 G   s   t | t j|  d S r#   )r   Z1IndexProductLocalSearchQuantizerFastScan_swiginitZ,new_IndexProductLocalSearchQuantizerFastScanrQ   r   r   r   r?      s    z1IndexProductLocalSearchQuantizerFastScan.__init__N)r   r   r,   r-   r   r   r   r   r   Z1IndexProductLocalSearchQuantizerFastScan_plsq_getZ1IndexProductLocalSearchQuantizerFastScan_plsq_setr  r?   Z/delete_IndexProductLocalSearchQuantizerFastScanrT   r   r   r   r   r    s   r  c                   @   sf   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 Zdd Zdd Zdd ZejZdS )IndexPQFastScana  
     Fast scan version of IndexPQ. Works for 4-bit PQ for now.

    The codes are not stored sequentially but grouped in blocks of size bbs.
    This makes it possible to compute distances quickly with SIMD instructions.

    Implementations:
    12: blocked loop with internal loop on Q with qbs
    13: same with reservoir accumulator to store results
    14: no qbs with heap accumulator
    15: no qbs with reservoir accumulator
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zIndexPQFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   c                 G   s   t | t j|  dS )z
        *Overload 1:*
        build from an existing IndexPQ

        |

        *Overload 2:*
        build from an existing IndexPQ
        N)r   ZIndexPQFastScan_swiginitZnew_IndexPQFastScanrQ   r   r   r   r?      s    
zIndexPQFastScan.__init__c                 C   s   t | ||S r#   )r   ZIndexPQFastScan_trainrm  r   r   r   rn  *   s    zIndexPQFastScan.trainc                 C   s   t | |||S r#   )r   ZIndexPQFastScan_compute_codesr  r   r   r   r  -   s    zIndexPQFastScan.compute_codesc                 C   s   t | |||S r#   )r   Z!IndexPQFastScan_compute_float_LUTr  r   r   r   r  0   s    z!IndexPQFastScan.compute_float_LUTc                 C   s   t | |||S r#   )r   ZIndexPQFastScan_sa_decoder  r   r   r   r  3   s    zIndexPQFastScan.sa_decodeN)r   r   r,   r-   r   r   r   r   r   ZIndexPQFastScan_pq_getZIndexPQFastScan_pq_setr  r?   rn  r  r  r  Zdelete_IndexPQFastScanrT   r   r   r   r   r     s   r  c                   @   s4   e Zd Zedd dd ddZeZdd Zej	Z
dS )	simd16uint16c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   :   r4   zsimd16uint16.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   :   r4   r7   r8   c                 C   s   t | t   d S r#   )r   Zsimd16uint16_swiginitZnew_simd16uint16r@   r   r   r   r?   =   s    zsimd16uint16.__init__N)r   r   r,   r   r   r   r   r?   r   Zdelete_simd16uint16rT   r   r   r   r   r  9   s   r  c                   @   sr   e Zd ZdZedd dd ddZdd ZeZee	j
e	jZee	je	jZee	je	jZd	d
 Zdd Ze	jZdS )SIMDResultHandlerzA This file contains callbacks for kernels that compute distances.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   F   r4   zSIMDResultHandler.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   F   r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   H   s    zSIMDResultHandler.__init__c                 C   s   t | ||||S )z
        called when 32 distances are computed and provided in two
        simd16uint16. (q, b) indicate which entry it is in the block.
        )r   ZSIMDResultHandler_handler   r-  r~   Zd0Zd1r   r   r   handleO   s    zSIMDResultHandler.handlec                 C   s   t | ||S )z* set the sub-matrix that is being computed)r   Z"SIMDResultHandler_set_block_origin)r   r  j0r   r   r   set_block_originV   s    z"SIMDResultHandler.set_block_originN)r   r   r,   r-   r   r   r?   r   r   r   ZSIMDResultHandler_is_CMax_getZSIMDResultHandler_is_CMax_setZis_CMaxZ SIMDResultHandler_sizeof_ids_getZ SIMDResultHandler_sizeof_ids_setZ
sizeof_idsZ!SIMDResultHandler_with_fields_getZ!SIMDResultHandler_with_fields_setZwith_fieldsr  r  Zdelete_SIMDResultHandlerrT   r   r   r   r   r  C   s   r  c                   @   s   e Zd Zedd dd ddZdd ZeZeej	ej
ZeejejZeejejddZeejejZeejejZeejejZd	d
 Zdd ZejZdS )SIMDResultHandlerToFloatc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ^   r4   z!SIMDResultHandlerToFloat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ^   r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   `   s    z!SIMDResultHandlerToFloat.__init__zE these fields are used mainly for the IVF variants (with_id_map=true)c                 C   s   t | |S r#   )r   ZSIMDResultHandlerToFloat_beginr  r   r   r   r  j   s    zSIMDResultHandlerToFloat.beginc                 C   s
   t | S r#   )r   ZSIMDResultHandlerToFloat_endr@   r   r   r   r  m   s    zSIMDResultHandlerToFloat.endN)r   r   r,   r   r   r?   r   r   r   ZSIMDResultHandlerToFloat_nq_getZSIMDResultHandlerToFloat_nq_setr   Z#SIMDResultHandlerToFloat_ntotal_getZ#SIMDResultHandlerToFloat_ntotal_setr  Z#SIMDResultHandlerToFloat_id_map_getZ#SIMDResultHandlerToFloat_id_map_setid_mapZ"SIMDResultHandlerToFloat_q_map_getZ"SIMDResultHandlerToFloat_q_map_setZq_mapZ"SIMDResultHandlerToFloat_dbias_getZ"SIMDResultHandlerToFloat_dbias_setZdbiasZ(SIMDResultHandlerToFloat_normalizers_getZ(SIMDResultHandlerToFloat_normalizers_setr  r  r  Zdelete_SIMDResultHandlerToFloatrT   r   r   r   r   r  ]   s   r  c                   @   sV   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 ZejZdd ZdS )DummyResultHandlerzx
     Dummy structure that just computes a chqecksum on results
    (to avoid the computation to be optimized away)
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   y   r4   zDummyResultHandler.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   y   r4   r7   r8   c                 C   s   t | ||||S r#   )r   ZDummyResultHandler_handler  r   r   r   r  }   s    zDummyResultHandler.handlec                 C   s   t | ||S r#   )r   Z#DummyResultHandler_set_block_origin)r   rW   Zarg3r   r   r   r     s    z#DummyResultHandler.set_block_originc                 C   s   t | t   d S r#   )r   ZDummyResultHandler_swiginitZnew_DummyResultHandlerr@   r   r   r   r?      s    zDummyResultHandler.__init__N)r   r   r,   r-   r   r   r   r   r   ZDummyResultHandler_cs_getZDummyResultHandler_cs_setr   r  r  Zdelete_DummyResultHandlerrT   r?   r   r   r   r   r  s   s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZdd Zd	d
 Zdd ZejZdS )StoreResultHandlerzg
     memorize results in a nq-by-nb matrix.

    j0 is the current upper-left block of the matrix
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zStoreResultHandler.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZStoreResultHandler_swiginitZnew_StoreResultHandler)r   rZ   rd  r   r   r   r?      s    zStoreResultHandler.__init__c                 C   s   t | ||||S r#   )r   ZStoreResultHandler_handler  r   r   r   r     s    zStoreResultHandler.handlec                 C   s   t | ||S r#   )r   Z#StoreResultHandler_set_block_origin)r   Zi0_inZj0_inr   r   r   r     s    z#StoreResultHandler.set_block_originN)r   r   r,   r-   r   r   r   r   r   ZStoreResultHandler_data_getZStoreResultHandler_data_setrZ   ZStoreResultHandler_ld_getZStoreResultHandler_ld_setrd  ZStoreResultHandler_i0_getZStoreResultHandler_i0_setr  ZStoreResultHandler_j0_getZStoreResultHandler_j0_setr  r?   r  r  Zdelete_StoreResultHandlerrT   r   r   r   r   r     s   r  c                   @   sv  e Zd ZdZedd dd ddZdd ZeZee	j
e	jZee	je	jZee	je	jZee	je	jZee	je	jZee	je	jZee	je	jZee	je	j Z!ee	j"e	j#Z$ee	j%e	j&Z'd	d
 Z(dd Z)e	j*Z+ee	j,e	j-ddZ.dd Z/dd Z0dd Z1dd Z2d/ddZ3d0ddZ4d1ddZ5d2ddZ6d3dd Z7d4d!d"Z8d5d#d$Z9d6d%d&Z:d'd( Z;d)d* Z<d+d, Z=d-d. Z>dS )7IndexIVFFastScana  
     Fast scan version of IVFPQ and IVFAQ. Works for 4-bit PQ/AQ for now.

    The codes in the inverted lists are not stored sequentially but
    grouped in blocks of size bbs. This makes it possible to very quickly
    compute distances with SIMD instructions.

    Implementations (implem):
    0: auto-select implementation (default)
    1: orig's search, re-implemented
    2: orig's search, re-ordered by invlist
    10: optimizer int16 search, collect results in heap, no qbs
    11: idem, collect results in reservoir
    12: optimizer int16 search, collect results in heap, uses qbs
    13: idem, collect results in reservoir
    14: internally multithreaded implem over nq * nprobe
    15: same with reservoir

    For range search, only 10 and 12 are supported.
    add 100 to the implem to force single-thread scanning (the coarse quantizer
    may still use multiple threads).
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3      r4   zIndexIVFFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3      r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?      s    zIndexIVFFastScan.__init__c              	   C   s   t | ||||||S )z called by implementations)r   ZIndexIVFFastScan_init_fastscan)r   fine_quantizerr  r   r  r  r  r   r   r   r     s    zIndexIVFFastScan.init_fastscanc                 C   s
   t | S r#   )r   Z!IndexIVFFastScan_init_code_packerr@   r   r   r   init_code_packer   s    z!IndexIVFFastScan.init_code_packerz& orig's inverted lists (for debugging)c                 C   s   t | |||S r#   )r   ZIndexIVFFastScan_add_with_idsrp  r   r   r   rr     s    zIndexIVFFastScan.add_with_idsc                 C   s
   t | S r#   )r   Z#IndexIVFFastScan_lookup_table_is_3dr@   r   r   r   lookup_table_is_3d   s    z#IndexIVFFastScan.lookup_table_is_3dc                 C   s   t | |||||S r#   )r   ZIndexIVFFastScan_compute_LUTr   rB   r2   cqr:  biasesr   r   r   r     s    zIndexIVFFastScan.compute_LUTc              	   C   s   t | ||||||S r#   )r   Z"IndexIVFFastScan_compute_LUT_uint8)r   rB   r2   r  r:  r  r  r   r   r   compute_LUT_uint8   s    z"IndexIVFFastScan.compute_LUT_uint8Nc              	   C   s   t | ||||||S r#   )r   ZIndexIVFFastScan_searchrs  r   r   r   ru     s    zIndexIVFFastScan.searchc                 C   s   t | |||||||||	|
S r#   )r   Z#IndexIVFFastScan_search_preassignedrf  r   r   r   rj     s    z#IndexIVFFastScan.search_preassignedc                 C   s   t | |||||S r#   )r   ZIndexIVFFastScan_range_searchrv  r   r   r   rw     s    zIndexIVFFastScan.range_searchc	           	      C   s   t | ||||||||	S r#   )r   Z'IndexIVFFastScan_search_dispatch_implem)	r   rB   r2   r   r   r   r  scalerrt  r   r   r   search_dispatch_implem   s    z'IndexIVFFastScan.search_dispatch_implemc              
   C   s   t | |||||||S r#   )r   Z-IndexIVFFastScan_range_search_dispatch_implem)r   rB   r2   r   ZrresZcq_inr  rt  r   r   r   range_search_dispatch_implem   s    z-IndexIVFFastScan.range_search_dispatch_implemc	           	      C   s   t | ||||||||	S r#   )r   Z!IndexIVFFastScan_search_implem_10	r   rB   r2   handlerr  Zndis_outZ	nlist_outr  rt  r   r   r   search_implem_10   s    z!IndexIVFFastScan.search_implem_10c	           	      C   s   t | ||||||||	S r#   )r   Z!IndexIVFFastScan_search_implem_12r  r   r   r   search_implem_12   s    z!IndexIVFFastScan.search_implem_12c
           
      C   s   t | |||||||||	
S r#   )r   Z!IndexIVFFastScan_search_implem_14)
r   rB   r2   r   r   r   r  implr  rt  r   r   r   search_implem_14   s    z!IndexIVFFastScan.search_implem_14c                 C   s   t | |||S r#   )r   Z(IndexIVFFastScan_reconstruct_from_offsetr  r   r   r   r     s    z(IndexIVFFastScan.reconstruct_from_offsetc                 C   s
   t | S r#   )r   ZIndexIVFFastScan_get_CodePackerr@   r   r   r   r     s    zIndexIVFFastScan.get_CodePackerc                 C   s
   t | S r#   )r   Z*IndexIVFFastScan_reconstruct_orig_invlistsr@   r   r   r   reconstruct_orig_invlists   s    z*IndexIVFFastScan.reconstruct_orig_invlistsc                 C   s   t | |||S )a  
         Decode a set of vectors.

         NOTE: The codes in the IndexFastScan object are non-contiguous.
               But this method requires a contiguous representation.

        :type n: int
        :param n:       number of vectors
        :type bytes: uint8_t
        :param bytes:   input encoded vectors, size n * code_size
        :type x: float
        :param x:       output vectors, size n * d
        )r   ZIndexIVFFastScan_sa_decoder  r   r   r   r  !  s    zIndexIVFFastScan.sa_decode)N)NN)N)N)N)N)N)N)?r   r   r,   r-   r   r   r?   r   r   r   ZIndexIVFFastScan_bbs_getZIndexIVFFastScan_bbs_setr  ZIndexIVFFastScan_M_getZIndexIVFFastScan_M_setr  ZIndexIVFFastScan_nbits_getZIndexIVFFastScan_nbits_setr   ZIndexIVFFastScan_ksub_getZIndexIVFFastScan_ksub_setr8  ZIndexIVFFastScan_M2_getZIndexIVFFastScan_M2_setr  ZIndexIVFFastScan_implem_getZIndexIVFFastScan_implem_setr  ZIndexIVFFastScan_skip_getZIndexIVFFastScan_skip_setr  ZIndexIVFFastScan_qbs_getZIndexIVFFastScan_qbs_setr  ZIndexIVFFastScan_qbs2_getZIndexIVFFastScan_qbs2_setZqbs2Z#IndexIVFFastScan_fine_quantizer_getZ#IndexIVFFastScan_fine_quantizer_setr  r  r  Zdelete_IndexIVFFastScanrT   Z"IndexIVFFastScan_orig_invlists_getZ"IndexIVFFastScan_orig_invlists_setZorig_invlistsrr  r  r  r  ru  rj  rw  r  r  r  r  r  r  r  r  r  r   r   r   r   r     sD   







r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZeejejZeejejZeejejZeejejZeej ej!Z"dd Z#dd	 Z$d
d Z%dd Z&ej'Z(dS )IVFFastScanStatsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   !  r4   zIVFFastScanStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   !  r4   r7   r8   c                 C   s   t | |S r#   )r   ZIVFFastScanStats_Mcy_atr  r   r   r   Mcy_at !  s    zIVFFastScanStats.Mcy_atc                 C   s   t | |S r#   )r   Z!IVFFastScanStats_Mcy_reservoir_atr  r   r   r   Mcy_reservoir_at#!  s    z!IVFFastScanStats.Mcy_reservoir_atc                 C   s   t | t   d S r#   )r   ZIVFFastScanStats_swiginitZnew_IVFFastScanStatsr@   r   r   r   r?   &!  s    zIVFFastScanStats.__init__c                 C   s
   t | S r#   )r   ZIVFFastScanStats_resetr@   r   r   r   r|   )!  s    zIVFFastScanStats.resetN))r   r   r,   r   r   r   r   r   ZIVFFastScanStats_times_getZIVFFastScanStats_times_settimesZ.IVFFastScanStats_t_compute_distance_tables_getZ.IVFFastScanStats_t_compute_distance_tables_setZt_compute_distance_tablesZIVFFastScanStats_t_round_getZIVFFastScanStats_t_round_setZt_roundZ IVFFastScanStats_t_copy_pack_getZ IVFFastScanStats_t_copy_pack_setZt_copy_packZIVFFastScanStats_t_scan_getZIVFFastScanStats_t_scan_setZt_scanZIVFFastScanStats_t_to_flat_getZIVFFastScanStats_t_to_flat_setZ	t_to_flatZ$IVFFastScanStats_reservoir_times_getZ$IVFFastScanStats_reservoir_times_setZreservoir_timesZ IVFFastScanStats_t_aq_encode_getZ IVFFastScanStats_t_aq_encode_setZt_aq_encodeZ%IVFFastScanStats_t_aq_norm_encode_getZ%IVFFastScanStats_t_aq_norm_encode_setZt_aq_norm_encoder  r  r?   r|   Zdelete_IVFFastScanStatsrT   r   r   r   r   r  !  s    r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZdd ZejZd	d
 Zdd Zdd Zdd ZdddZdddZdd Zdd ZdS )!IndexIVFAdditiveQuantizerFastScanas  
     Fast scan version of IVFAQ. Works for 4-bit AQ for now.

    The codes in the inverted lists are not stored sequentially but
    grouped in blocks of size bbs. This makes it possible to very quickly
    compute distances with SIMD instructions.

    Implementations (implem):
    0: auto-select implementation (default)
    1: orig's search, re-implemented
    2: orig's search, re-ordered by invlist
    10: optimizer int16 search, collect results in heap, no qbs
    11: idem, collect results in reservoir
    12: optimizer int16 search, collect results in heap, uses qbs
    13: idem, collect results in reservoir
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   A!  r4   z*IndexIVFAdditiveQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   A!  r4   r7   r8   c                 C   s   t | ||||S r#   )r   Z&IndexIVFAdditiveQuantizerFastScan_init)r   r  r  r  r  r   r   r   ra  H!  s    z&IndexIVFAdditiveQuantizerFastScan.initc                 G   s   t | t j|  d S r#   )r   Z*IndexIVFAdditiveQuantizerFastScan_swiginitZ%new_IndexIVFAdditiveQuantizerFastScanrQ   r   r   r   r?   L!  s    z*IndexIVFAdditiveQuantizerFastScan.__init__c                 C   s   t | |||S r#   )r   Z/IndexIVFAdditiveQuantizerFastScan_train_encoderrv  r   r   r   rw  O!  s    z/IndexIVFAdditiveQuantizerFastScan.train_encoderc                 C   s
   t | S r#   )r   Z;IndexIVFAdditiveQuantizerFastScan_train_encoder_num_vectorsr@   r   r   r   rx  R!  s    z;IndexIVFAdditiveQuantizerFastScan.train_encoder_num_vectorsc                 C   s   t | ||S r#   )r   Z5IndexIVFAdditiveQuantizerFastScan_estimate_norm_scalerm  r   r   r   r  U!  s    z5IndexIVFAdditiveQuantizerFastScan.estimate_norm_scaleFc                 C   s   t | |||||S )zq
        same as the regular IVFAQ encoder. The codes are not reorganized by
        blocks a that point
        )r   Z0IndexIVFAdditiveQuantizerFastScan_encode_vectorsrs  r   r   r   ru  X!  s    z0IndexIVFAdditiveQuantizerFastScan.encode_vectorsNc              	   C   s   t | ||||||S r#   )r   Z(IndexIVFAdditiveQuantizerFastScan_searchrs  r   r   r   ru  _!  s    z(IndexIVFAdditiveQuantizerFastScan.searchc                 C   s
   t | S r#   )r   Z4IndexIVFAdditiveQuantizerFastScan_lookup_table_is_3dr@   r   r   r   r  b!  s    z4IndexIVFAdditiveQuantizerFastScan.lookup_table_is_3dc                 C   s   t | |||||S r#   )r   Z-IndexIVFAdditiveQuantizerFastScan_compute_LUTr  r   r   r   r  e!  s    z-IndexIVFAdditiveQuantizerFastScan.compute_LUT)F)N) r   r   r,   r-   r   r   r   r   r   Z(IndexIVFAdditiveQuantizerFastScan_aq_getZ(IndexIVFAdditiveQuantizerFastScan_aq_setr  Z2IndexIVFAdditiveQuantizerFastScan_rescale_norm_getZ2IndexIVFAdditiveQuantizerFastScan_rescale_norm_setr  Z0IndexIVFAdditiveQuantizerFastScan_norm_scale_getZ0IndexIVFAdditiveQuantizerFastScan_norm_scale_setr  Z6IndexIVFAdditiveQuantizerFastScan_max_train_points_getZ6IndexIVFAdditiveQuantizerFastScan_max_train_points_setr  ra  Z(delete_IndexIVFAdditiveQuantizerFastScanrT   r?   rw  rx  r  ru  ru  r  r  r   r   r   r   r  /!  s"   

r  c                   @   sB   e Zd Zedd dd ddZeZeejej	Z
dd ZejZdS )	$IndexIVFLocalSearchQuantizerFastScanc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   k!  r4   z-IndexIVFLocalSearchQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   k!  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z-IndexIVFLocalSearchQuantizerFastScan_swiginitZ(new_IndexIVFLocalSearchQuantizerFastScanrQ   r   r   r   r?   o!  s    z-IndexIVFLocalSearchQuantizerFastScan.__init__N)r   r   r,   r   r   r   r   r   Z,IndexIVFLocalSearchQuantizerFastScan_lsq_getZ,IndexIVFLocalSearchQuantizerFastScan_lsq_setrS  r?   Z+delete_IndexIVFLocalSearchQuantizerFastScanrT   r   r   r   r   r  j!  s
   r  c                   @   sB   e Zd Zedd dd ddZeZeejej	Z
dd ZejZdS )	!IndexIVFResidualQuantizerFastScanc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   v!  r4   z*IndexIVFResidualQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   v!  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z*IndexIVFResidualQuantizerFastScan_swiginitZ%new_IndexIVFResidualQuantizerFastScanrQ   r   r   r   r?   z!  s    z*IndexIVFResidualQuantizerFastScan.__init__N)r   r   r,   r   r   r   r   r   Z(IndexIVFResidualQuantizerFastScan_rq_getZ(IndexIVFResidualQuantizerFastScan_rq_setr%  r?   Z(delete_IndexIVFResidualQuantizerFastScanrT   r   r   r   r   r  u!  s
   r  c                   @   sB   e Zd Zedd dd ddZeZeejej	Z
dd ZejZdS )	+IndexIVFProductLocalSearchQuantizerFastScanc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   !  r4   z4IndexIVFProductLocalSearchQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   !  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z4IndexIVFProductLocalSearchQuantizerFastScan_swiginitZ/new_IndexIVFProductLocalSearchQuantizerFastScanrQ   r   r   r   r?   !  s    z4IndexIVFProductLocalSearchQuantizerFastScan.__init__N)r   r   r,   r   r   r   r   r   Z4IndexIVFProductLocalSearchQuantizerFastScan_plsq_getZ4IndexIVFProductLocalSearchQuantizerFastScan_plsq_setr  r?   Z2delete_IndexIVFProductLocalSearchQuantizerFastScanrT   r   r   r   r   r  !  s
   r  c                   @   sB   e Zd Zedd dd ddZeZeejej	Z
dd ZejZdS )	(IndexIVFProductResidualQuantizerFastScanc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   !  r4   z1IndexIVFProductResidualQuantizerFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   !  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   Z1IndexIVFProductResidualQuantizerFastScan_swiginitZ,new_IndexIVFProductResidualQuantizerFastScanrQ   r   r   r   r?   !  s    z1IndexIVFProductResidualQuantizerFastScan.__init__N)r   r   r,   r   r   r   r   r   Z0IndexIVFProductResidualQuantizerFastScan_prq_getZ0IndexIVFProductResidualQuantizerFastScan_prq_setr  r?   Z/delete_IndexIVFProductResidualQuantizerFastScanrT   r   r   r   r   r  !  s
   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZdd Zdd Zdd ZdddZdd ZejZdS )IndexIVFIndependentQuantizerz
     An IVF index with a quantizer that has a different input dimension from the
    payload size. The vectors to encode are obtained from the input vectors by a
    VectorTransform.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   !  r4   z%IndexIVFIndependentQuantizer.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   !  r4   r7   r8   z1 quantizer is fed directly with the input vectorsz- transform before the IVF vectors are appliedz) the IVF index, controls nlist and nprobez  whether *this owns the 3 fieldsc                 G   s   t | t j|  d S r#   )r   Z%IndexIVFIndependentQuantizer_swiginitZ new_IndexIVFIndependentQuantizerrQ   r   r   r   r?   !  s    z%IndexIVFIndependentQuantizer.__init__c                 C   s   t | ||S r#   )r   Z"IndexIVFIndependentQuantizer_trainrm  r   r   r   rn  !  s    z"IndexIVFIndependentQuantizer.trainc                 C   s   t | ||S r#   )r   Z IndexIVFIndependentQuantizer_addrm  r   r   r   ro  !  s    z IndexIVFIndependentQuantizer.addNc              	   C   s   t | ||||||S r#   )r   Z#IndexIVFIndependentQuantizer_searchrs  r   r   r   ru  !  s    z#IndexIVFIndependentQuantizer.searchc                 C   s
   t | S r#   )r   Z"IndexIVFIndependentQuantizer_resetr@   r   r   r   r|   !  s    z"IndexIVFIndependentQuantizer.reset)N)r   r   r,   r-   r   r   r   r   r   Z*IndexIVFIndependentQuantizer_quantizer_getZ*IndexIVFIndependentQuantizer_quantizer_setr^  Z#IndexIVFIndependentQuantizer_vt_getZ#IndexIVFIndependentQuantizer_vt_setr  Z*IndexIVFIndependentQuantizer_index_ivf_getZ*IndexIVFIndependentQuantizer_index_ivf_setZ	index_ivfZ+IndexIVFIndependentQuantizer_own_fields_getZ+IndexIVFIndependentQuantizer_own_fields_setr  r?   rn  ro  ru  r|   Z#delete_IndexIVFIndependentQuantizerrT   r   r   r   r   r  !  s   
r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZd
d Zdd Zdd Zdd ZdddZdd Zdd ZejZdS )IndexIVFPQFastScanas  
     Fast scan version of IVFPQ. Works for 4-bit PQ for now.

    The codes in the inverted lists are not stored sequentially but
    grouped in blocks of size bbs. This makes it possible to very quickly
    compute distances with SIMD instructions.

    Implementations (implem):
    0: auto-select implementation (default)
    1: orig's search, re-implemented
    2: orig's search, re-ordered by invlist
    10: optimizer int16 search, collect results in heap, no qbs
    11: idem, collect results in reservoir
    12: optimizer int16 search, collect results in heap, uses qbs
    13: idem, collect results in reservoir
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   !  r4   zIndexIVFPQFastScan.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   !  r4   r7   r8   rs  z precomputed tables managementz4 if use_precompute_table size (nlist, pq.M, pq.ksub)c                 G   s   t | t j|  d S r#   )r   ZIndexIVFPQFastScan_swiginitZnew_IndexIVFPQFastScanrQ   r   r   r   r?   !  s    zIndexIVFPQFastScan.__init__c                 C   s   t | |||S r#   )r   Z IndexIVFPQFastScan_train_encoderrv  r   r   r   rw  !  s    z IndexIVFPQFastScan.train_encoderc                 C   s
   t | S r#   )r   Z,IndexIVFPQFastScan_train_encoder_num_vectorsr@   r   r   r   rx  !  s    z,IndexIVFPQFastScan.train_encoder_num_vectorsc                 C   s
   t | S )zA build precomputed table, possibly updating use_precomputed_table)r   Z#IndexIVFPQFastScan_precompute_tabler@   r   r   r   rx  !  s    z#IndexIVFPQFastScan.precompute_tableFc                 C   s   t | |||||S )zq
        same as the regular IVFPQ encoder. The codes are not reorganized by
        blocks a that point
        )r   Z!IndexIVFPQFastScan_encode_vectorsrs  r   r   r   ru  !  s    z!IndexIVFPQFastScan.encode_vectorsc                 C   s
   t | S r#   )r   Z%IndexIVFPQFastScan_lookup_table_is_3dr@   r   r   r   r  !  s    z%IndexIVFPQFastScan.lookup_table_is_3dc                 C   s   t | |||||S r#   )r   ZIndexIVFPQFastScan_compute_LUTr  r   r   r   r  !  s    zIndexIVFPQFastScan.compute_LUTN)F)r   r   r,   r-   r   r   r   r   r   ZIndexIVFPQFastScan_pq_getZIndexIVFPQFastScan_pq_setr  Z,IndexIVFPQFastScan_use_precomputed_table_getZ,IndexIVFPQFastScan_use_precomputed_table_setr  Z(IndexIVFPQFastScan_precomputed_table_getZ(IndexIVFPQFastScan_precomputed_table_setry  r?   rw  rx  rx  ru  r  r  Zdelete_IndexIVFPQFastScanrT   r   r   r   r   r  !  s   
r  c                 C   s   t | ||||S )a  
     Functions to quantize PQ floating-point Look Up Tables (LUT) to uint8, and
    biases to uint16. The accumulation is supposed to take place in uint16.
    The quantization coefficients are float (a, b) such that

         original_value = quantized_value * a / b

    The hardest part of the quantization is with multiple LUTs that need to be
    added up together. In that case, coefficient a has to be chosen so that
    the sum fits in a uint16 accumulator.
    )r   round_uint8_per_column)r`  rB   r   a_outb_outr   r   r   r  !  s    r  c                 C   s   t | |||||S r#   )r   round_uint8_per_column_multi)r`  r   rB   r   r  r  r   r   r   r  !  s    r  c                 C   s   t | |||||||||	|
S )a)  
     LUT quantization to uint8 and bias to uint16.

    (nprobe, M, ksub, lut_is_3d) determine the size of the the LUT

     LUT input:
     - 2D size (M, ksub): single matrix per probe (lut_is_3d=false)
     - 3D size (nprobe, M, ksub): separate LUT per probe (lut_is_3d=true)
     bias input:
     - nullptr: bias is 0
     - size (nprobe): one bias per probe
     Output:
     - LUTq uint8 version of the LUT (M size is rounded up to M2)
     - biasq (or nullptr): uint16 version of the LUT
     - a, b: scalars to approximate the true distance
    )r   quantize_LUT_and_bias)rc  r  r8  Z	lut_is_3dr  biasLUTqr  biasqr  r  r   r   r   r  !  s    r  c                 C   s    t | |||||||||	|
|S r#   )r   aq_quantize_LUT_and_bias)rc  r  r8  r  r  M_normr  r  r  r  r  r  r   r   r   r  "  s    r  c                 C   s   t | |||S r#   )r   aq_estimate_norm_scale)r  r8  r  r  r   r   r   r  "  s    r  c                   @   s.  e Zd ZdZedd dd ddZdd ZeZee	j
e	jd	dZee	je	jd
dZee	je	jddZee	je	jddZee	je	jddZee	je	jddZe	jZdd Zdd Zdd Z d2ddZ!d3ddZ"d4ddZ#dd Z$dd  Z%d!d" Z&d#d$ Z'd5d%d&Z(d'd( Z)d6d*d+Z*d,d- Z+d.d/ Z,d0d1 Z-dS )7IndexBinaryz
     Abstract structure for a binary index.

    Supports adding vertices and searching them.

    All queries are symmetric because there is no distinction between codes and
    vectors.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   "  r4   zIndexBinary.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   "  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   !"  s    zIndexBinary.__init__ri  z' number of bytes per vector ( = d / 8 )rj  rk  zX
    set if the Index does not require training, or if training is done
    already
    rl  c                 C   s   t | ||S )z
         Perform training on a representative set of vectors.

        :type n: int
        :param n:      nb of training vectors
        :type x: uint8_t
        :param x:      training vecors, size n * d / 8
        )r   ZIndexBinary_trainrm  r   r   r   rn  /"  s    	zIndexBinary.trainc                 C   s   t | ||S )z
         Add n vectors of dimension d to the index.

        Vectors are implicitly assigned labels ntotal .. ntotal + n - 1
        :type x: uint8_t
        :param x:      input matrix, size n * d / 8
        )r   ZIndexBinary_addrm  r   r   r   ro  :"  s    zIndexBinary.addc                 C   s   t | |||S )a  
         Same as add, but stores xids instead of sequential ids.

        The default implementation fails with an assertion, as it is
        not supported by all indexes.

        :type xids: int
        :param xids: if non-null, ids to store for the vectors (size n)
        )r   ZIndexBinary_add_with_idsrp  r   r   r   rr  D"  s    
zIndexBinary.add_with_idsNc              	   C   s   t | ||||||S )a  
         Query n vectors of dimension d to the index.

        return at most k vectors. If there are not enough results for a
        query, the result array is padded with -1s.

        :type x: uint8_t
        :param x:           input vectors to search, size n * d / 8
        :type labels: int
        :param labels:      output labels of the NNs, size n*k
        :type distances: int
        :param distances:   output pairwise distances, size n*k
        )r   ZIndexBinary_searchrs  r   r   r   ru  P"  s    zIndexBinary.searchc                 C   s   t | |||||S )a  
         Query n vectors of dimension d to the index.

        return all vectors with distance < radius. Note that many indexes
        do not implement the range_search (only the k-NN search is
        mandatory). The distances are converted to float to reuse the
        RangeSearchResult structure, but they are integer. By convention,
        only distances < radius (strict comparison) are returned,
        ie. radius = 0 does not return any result and 1 returns only
        exact same vectors.

        :type x: uint8_t
        :param x:           input vectors to search, size n * d / 8
        :type radius: int
        :param radius:      search radius
        :type result: :py:class:`RangeSearchResult`
        :param result:      result table
        )r   ZIndexBinary_range_searchrv  r   r   r   rw  `"  s    zIndexBinary.range_searchr   c                 C   s   t | ||||S )a`  
         Return the indexes of the k vectors closest to the query x.

        This function is identical to search but only returns labels of
        neighbors.
        :type x: uint8_t
        :param x:           input vectors to search, size n * d / 8
        :type labels: int
        :param labels:      output labels of the NNs, size n*k
        )r   ZIndexBinary_assignrx  r   r   r   ry  u"  s    zIndexBinary.assignc                 C   s
   t | S )z( Removes all elements from the database.)r   ZIndexBinary_resetr@   r   r   r   r|   "  s    zIndexBinary.resetc                 C   s   t | |S )z: Removes IDs from the index. Not supported by all indexes.)r   ZIndexBinary_remove_idsrz  r   r   r   r{  "  s    zIndexBinary.remove_idsc                 C   s   t | ||S )a  
         Reconstruct a stored vector.

        This function may not be defined for some indexes.
        :type key: int
        :param key:         id of the vector to reconstruct
        :type recons: uint8_t
        :param recons:      reconstucted vector (size d / 8)
        )r   ZIndexBinary_reconstructr|  r   r   r   r  "  s    
zIndexBinary.reconstructc                 C   s   t | |||S )z
         Reconstruct vectors i0 to i0 + ni - 1.

        This function may not be defined for some indexes.
        :type recons: uint8_t
        :param recons:      reconstucted vectors (size ni * d / 8)
        )r   ZIndexBinary_reconstruct_nr  r   r   r   r  "  s    zIndexBinary.reconstruct_nc              
   C   s   t | |||||||S )af  
         Similar to search, but also reconstructs the stored vectors (or an
        approximation in the case of lossy coding) for the search results.

        If there are not enough results for a query, the resulting array
        is padded with -1s.

        :type recons: uint8_t
        :param recons:      reconstructed vectors size (n, k, d)
        )r   Z"IndexBinary_search_and_reconstructr  r   r   r   r  "  s    z"IndexBinary.search_and_reconstructc                 C   s
   t | S )z2 Display the actual class name and some more info.)r   ZIndexBinary_displayr@   r   r   r   display"  s    zIndexBinary.displayr   c                 C   s   t | ||S r  )r   ZIndexBinary_merge_fromr  r   r   r   r  "  s    zIndexBinary.merge_fromc                 C   s   t | |S r  )r   Z&IndexBinary_check_compatible_for_merger  r   r   r   r  "  s    z&IndexBinary.check_compatible_for_mergec                 C   s
   t | S r  )r   ZIndexBinary_sa_code_sizer@   r   r   r   r  "  s    zIndexBinary.sa_code_sizec                 C   s   t | |||S )z& Same as add_with_ids for IndexBinary.)r   ZIndexBinary_add_sa_codesr  r   r   r   r  "  s    zIndexBinary.add_sa_codes)N)N)r   )N)r   ).r   r   r,   r-   r   r   r?   r   r   r   ZIndexBinary_d_getZIndexBinary_d_setr   ZIndexBinary_code_size_getZIndexBinary_code_size_setr   ZIndexBinary_ntotal_getZIndexBinary_ntotal_setr  ZIndexBinary_verbose_getZIndexBinary_verbose_setr   ZIndexBinary_is_trained_getZIndexBinary_is_trained_setr  ZIndexBinary_metric_type_getZIndexBinary_metric_type_setrf  Zdelete_IndexBinaryrT   rn  ro  rr  ru  rw  ry  r|   r{  r  r  r  r  r  r  r  r  r   r   r   r   r  "  s6   	






	r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejZeejejZd	d
 Zdd ZdddZdddZdd Zdd Zdd ZejZdS )IndexBinaryFlatzC Index that stores the full vectors and performs exhaustive search.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   "  r4   zIndexBinaryFlat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   "  r4   r7   r8   z& database vectors, size ntotal * d / 8t
     Select between using a heap or counting to select the k smallest values
    when scanning inverted lists.
    c                 C   s   t | ||S r#   )r   ZIndexBinaryFlat_addrm  r   r   r   ro  "  s    zIndexBinaryFlat.addc                 C   s
   t | S r#   )r   ZIndexBinaryFlat_resetr@   r   r   r   r|   "  s    zIndexBinaryFlat.resetNc              	   C   s   t | ||||||S r#   )r   ZIndexBinaryFlat_searchrs  r   r   r   ru  "  s    zIndexBinaryFlat.searchc                 C   s   t | |||||S r#   )r   ZIndexBinaryFlat_range_searchrv  r   r   r   rw  "  s    zIndexBinaryFlat.range_searchc                 C   s   t | ||S r#   )r   ZIndexBinaryFlat_reconstructr|  r   r   r   r  "  s    zIndexBinaryFlat.reconstructc                 C   s   t | |S )z
         Remove some ids. Note that because of the indexing structure,
        the semantics of this operation are different from the usual ones:
        the new ids are shifted.
        )r   ZIndexBinaryFlat_remove_idsrz  r   r   r   r{  "  s    zIndexBinaryFlat.remove_idsc                 G   s   t | t j|  d S r#   )r   ZIndexBinaryFlat_swiginitZnew_IndexBinaryFlatrQ   r   r   r   r?   "  s    zIndexBinaryFlat.__init__)N)N)r   r   r,   r-   r   r   r   r   r   ZIndexBinaryFlat_xb_getZIndexBinaryFlat_xb_setr  ZIndexBinaryFlat_use_heap_getZIndexBinaryFlat_use_heap_setuse_heapZ$IndexBinaryFlat_query_batch_size_getZ$IndexBinaryFlat_query_batch_size_setZquery_batch_sizeZ$IndexBinaryFlat_approx_topk_mode_getZ$IndexBinaryFlat_approx_topk_mode_setr<  ro  r|   ru  rw  r  r{  r?   Zdelete_IndexBinaryFlatrT   r   r   r   r   r  "  s   

r  c                   @   s  e Zd ZdZedd dd ddZeZeej	ej
ddZeejejZeejejddZeejejd	dZeejejd
dZeejejddZeejejddZeejejddZ eej!ej"ddZ#eej$ej%ddZ&eej'ej(ddZ)eej*ej+ddZ,dd Z-ej.Z/dd Z0dd Z1dd Z2dd Z3dd Z4dAdd Z5dBd"d#Z6dCd$d%Z7dDd&d'Z8d(d) Z9d*d+ Z:d,d- Z;dEd.d/Z<d0d1 Z=d2d3 Z>d4d5 Z?d6d7 Z@d8d9 ZAdFd;d<ZBd=d> ZCdGd?d@ZDdS )HIndexBinaryIVFa_  
     Index based on a inverted file (IVF)

    In the inverted file, the quantizer (an IndexBinary instance) provides a
    quantization index for each vector to be added. The quantization
    index maps to a list (aka inverted list or posting list), where the
    id of the vector is stored.

    Otherwise the object is similar to the IndexIVF
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   #  r4   zIndexBinaryIVF.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   #  r4   r7   r8   ro  ra  rb  r  z collect computations per batchz> map for direct access to the elements. Enables reconstruct().rV  r  rW  rX  rY  c                 G   s   t | t j|  d S r#   )r   ZIndexBinaryIVF_swiginitZnew_IndexBinaryIVFrQ   r   r   r   r?   #  s    zIndexBinaryIVF.__init__c                 C   s
   t | S r#   )r   ZIndexBinaryIVF_resetr@   r   r   r   r|   #  s    zIndexBinaryIVF.resetc                 C   s   t | ||S )z Trains the quantizer)r   ZIndexBinaryIVF_trainrm  r   r   r   rn  #  s    zIndexBinaryIVF.trainc                 C   s   t | ||S r#   )r   ZIndexBinaryIVF_addrm  r   r   r   ro  #  s    zIndexBinaryIVF.addc                 C   s   t | |||S r#   )r   ZIndexBinaryIVF_add_with_idsrp  r   r   r   rr  "#  s    zIndexBinaryIVF.add_with_idsc                 C   s   t | ||||S )z
         Implementation of vector addition where the vector assignments are
        predefined.

        :type precomputed_idx: int
        :param precomputed_idx:    quantization indices for the input vectors
            (size n)
        )r   ZIndexBinaryIVF_add_core)r   rB   r2   rq  rq  r   r   r   rr  %#  s    	zIndexBinaryIVF.add_coreNc
           
      C   s   t | |||||||||	
S )aB  
         Search a set of vectors, that are pre-quantized by the IVF
         quantizer. Fill in the corresponding heaps with the query
         results. search() calls this.

        :type n: int
        :param n:      nb of vectors to query
        :type x: uint8_t
        :param x:      query vectors, size nx * d
        :type assign: int
        :param assign: coarse quantization indices, size nx * nprobe
        :type centroid_dis: int
        :param centroid_dis:
                          distances to coarse centroids, size nx * nprobe
        :param distance:
                          output distances, size n * k
        :type labels: int
        :param labels: output labels, size n * k
        :type store_pairs: boolean
        :param store_pairs: store inv list index + inv list offset
                                instead in upper/lower 32 bit of result,
                                instead of ids (used for reranking).
        :type params: :py:class:`IVFSearchParameters`, optional
        :param params: used to override the object's search parameters
        )r   Z!IndexBinaryIVF_search_preassigned)
r   rB   r2   r   ry  rg  r   r   rh  rt  r   r   r   rj  0#  s    z!IndexBinaryIVF.search_preassignedFc                 C   s   t | |S r#   )r   Z&IndexBinaryIVF_get_InvertedListScanner)r   rh  r   r   r   r{  L#  s    z&IndexBinaryIVF.get_InvertedListScannerc              	   C   s   t | ||||||S ry  )r   ZIndexBinaryIVF_searchrs  r   r   r   ru  O#  s    zIndexBinaryIVF.searchc                 C   s   t | |||||S r#   )r   ZIndexBinaryIVF_range_searchrv  r   r   r   rw  S#  s    zIndexBinaryIVF.range_searchc              	   C   s   t | ||||||S r#   )r   Z'IndexBinaryIVF_range_search_preassigned)r   rB   r2   r   ry  rg  r   r   r   r   rm  V#  s    z'IndexBinaryIVF.range_search_preassignedc                 C   s   t | ||S r#   )r   ZIndexBinaryIVF_reconstructr|  r   r   r   r  Y#  s    zIndexBinaryIVF.reconstructc                 C   s   t | |||S )a  
         Reconstruct a subset of the indexed vectors.

        Overrides default implementation to bypass reconstruct() which requires
        direct_map to be maintained.

        :type i0: int
        :param i0:     first vector to reconstruct
        :type ni: int
        :param ni:     nb of vectors to reconstruct
        :type recons: uint8_t
        :param recons: output array of reconstructed vectors, size ni * d / 8
        )r   ZIndexBinaryIVF_reconstruct_nr  r   r   r   r  \#  s    zIndexBinaryIVF.reconstruct_nc              
   C   s   t | |||||||S )a  
         Similar to search, but also reconstructs the stored vectors (or an
        approximation in the case of lossy coding) for the search results.

        Overrides default implementation to avoid having to maintain direct_map
        and instead fetch the code offsets through the `store_pairs` flag in
        search_preassigned().

        :type recons: uint8_t
        :param recons:      reconstructed vectors size (n, k, d / 8)
        )r   Z%IndexBinaryIVF_search_and_reconstructr  r   r   r   r  l#  s    z%IndexBinaryIVF.search_and_reconstructc                 C   s   t | |||S r  )r   Z&IndexBinaryIVF_reconstruct_from_offsetr  r   r   r   r  z#  s    	z&IndexBinaryIVF.reconstruct_from_offsetc                 C   s   t | |S r  )r   ZIndexBinaryIVF_remove_idsrz  r   r   r   r{  #  s    zIndexBinaryIVF.remove_idsc                 C   s   t | ||S r#   )r   ZIndexBinaryIVF_merge_from)r   r_   r  r   r   r   r  #  s    zIndexBinaryIVF.merge_fromc                 C   s   t | |S r#   )r   Z)IndexBinaryIVF_check_compatible_for_merger  r   r   r   r  #  s    z)IndexBinaryIVF.check_compatible_for_mergec                 C   s   t | |S r#   )r   ZIndexBinaryIVF_get_list_sizer  r   r   r   r  #  s    zIndexBinaryIVF.get_list_sizeTc                 C   s   t | |S r  )r   ZIndexBinaryIVF_make_direct_mapr  r   r   r   r  #  s    zIndexBinaryIVF.make_direct_mapc                 C   s   t | |S r#   )r   Z"IndexBinaryIVF_set_direct_map_typer  r   r   r   r  #  s    z"IndexBinaryIVF.set_direct_map_typec                 C   s   t | ||S r#   )r   ZIndexBinaryIVF_replace_invlistsr  r   r   r   r  #  s    zIndexBinaryIVF.replace_invlists)N)F)N)N)N)T)F)Er   r   r,   r-   r   r   r   r   r   ZIndexBinaryIVF_invlists_getZIndexBinaryIVF_invlists_setrK  ZIndexBinaryIVF_own_invlists_getZIndexBinaryIVF_own_invlists_setr  ZIndexBinaryIVF_nprobe_getZIndexBinaryIVF_nprobe_setrc  ZIndexBinaryIVF_max_codes_getZIndexBinaryIVF_max_codes_setrd  ZIndexBinaryIVF_use_heap_getZIndexBinaryIVF_use_heap_setr  Z%IndexBinaryIVF_per_invlist_search_getZ%IndexBinaryIVF_per_invlist_search_setZper_invlist_searchZIndexBinaryIVF_direct_map_getZIndexBinaryIVF_direct_map_setrT  ZIndexBinaryIVF_quantizer_getZIndexBinaryIVF_quantizer_setr^  ZIndexBinaryIVF_nlist_getZIndexBinaryIVF_nlist_setr  ZIndexBinaryIVF_own_fields_getZIndexBinaryIVF_own_fields_setr  ZIndexBinaryIVF_cp_getZIndexBinaryIVF_cp_setr  Z#IndexBinaryIVF_clustering_index_getZ#IndexBinaryIVF_clustering_index_setr_  r?   Zdelete_IndexBinaryIVFrT   r|   rn  ro  rr  rr  rj  r{  ru  rw  rm  r  r  r  r  r{  r  r  r  r  r  r  r   r   r   r   r  "  sL   






r  c                   @   s\   e Zd Zedd dd ddZdd ZeZdd	 Zd
d Z	dd Z
dd Zdd ZejZdS )BinaryInvertedListScannerc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   #  r4   z"BinaryInvertedListScanner.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   #  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   #  s    z"BinaryInvertedListScanner.__init__c                 C   s   t | |S r  )r   Z#BinaryInvertedListScanner_set_queryr  r   r   r   r  #  s    z#BinaryInvertedListScanner.set_queryc                 C   s   t | ||S r  )r   Z"BinaryInvertedListScanner_set_listr  r   r   r   r  #  s    z"BinaryInvertedListScanner.set_listc                 C   s   t | |S r  )r   Z*BinaryInvertedListScanner_distance_to_coder  r   r   r   r  #  s    z*BinaryInvertedListScanner.distance_to_codec              	   C   s   t | ||||||S )a@  
         compute the distances to codes. (distances, labels) should be
        organized as a min- or max-heap

        :type n: int
        :param n:      number of codes to scan
        :type codes: uint8_t
        :param codes:  codes to scan (n * code_size)
        :type ids: int
        :param ids:        corresponding ids (ignored if store_pairs)
        :type distances: int
        :param distances:  heap distances (size k)
        :type labels: int
        :param labels:     heap labels (size k)
        :type k: int
        :param k:          heap size
        )r   Z$BinaryInvertedListScanner_scan_codesr  r   r   r   r  #  s    z$BinaryInvertedListScanner.scan_codesc                 C   s   t | |||||S r#   )r   Z*BinaryInvertedListScanner_scan_codes_ranger  r   r   r   r  #  s    z*BinaryInvertedListScanner.scan_codes_rangeN)r   r   r,   r   r   r?   r   r   r  r  r  r  r  r   Z delete_BinaryInvertedListScannerrT   r   r   r   r   r  #  s   r  c                   @   sz   e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZdd	 ZejZd
d Zdd ZdddZdd ZdS )IndexBinaryFromFloatz
     IndexBinary backed by a float Index.

    Supports adding vertices and searching them.

    All queries are symmetric because there is no distinction between codes and
    vectors.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   #  r4   zIndexBinaryFromFloat.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   #  r4   r7   r8   z' Whether object owns the index pointer.c                 G   s   t | t j|  d S r#   )r   ZIndexBinaryFromFloat_swiginitZnew_IndexBinaryFromFloatrQ   r   r   r   r?   #  s    zIndexBinaryFromFloat.__init__c                 C   s   t | ||S r#   )r   ZIndexBinaryFromFloat_addrm  r   r   r   ro  #  s    zIndexBinaryFromFloat.addc                 C   s
   t | S r#   )r   ZIndexBinaryFromFloat_resetr@   r   r   r   r|   #  s    zIndexBinaryFromFloat.resetNc              	   C   s   t | ||||||S r#   )r   ZIndexBinaryFromFloat_searchrs  r   r   r   ru  #  s    zIndexBinaryFromFloat.searchc                 C   s   t | ||S r#   )r   ZIndexBinaryFromFloat_trainrm  r   r   r   rn  #  s    zIndexBinaryFromFloat.train)N)r   r   r,   r-   r   r   r   r   r   ZIndexBinaryFromFloat_index_getZIndexBinaryFromFloat_index_setr  Z#IndexBinaryFromFloat_own_fields_getZ#IndexBinaryFromFloat_own_fields_setr  r?   Zdelete_IndexBinaryFromFloatrT   ro  r|   ru  rn  r   r   r   r   r  #  s   	
r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZdd ZejZd	d
 Zdd Zdd ZdddZdd Zdd ZdS )IndexBinaryHNSWr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   #  r4   zIndexBinaryHNSW.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   #  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexBinaryHNSW_swiginitZnew_IndexBinaryHNSWrQ   r   r   r   r?   #  s    zIndexBinaryHNSW.__init__c                 C   s
   t | S r#   )r   Z%IndexBinaryHNSW_get_distance_computerr@   r   r   r   r  $  s    z%IndexBinaryHNSW.get_distance_computerc                 C   s   t | ||S r#   )r   ZIndexBinaryHNSW_addrm  r   r   r   ro  $  s    zIndexBinaryHNSW.addc                 C   s   t | ||S r  )r   ZIndexBinaryHNSW_trainrm  r   r   r   rn  $  s    zIndexBinaryHNSW.trainNc              	   C   s   t | ||||||S r  )r   ZIndexBinaryHNSW_searchrs  r   r   r   ru  $  s    zIndexBinaryHNSW.searchc                 C   s   t | ||S r#   )r   ZIndexBinaryHNSW_reconstructr|  r   r   r   r  $  s    zIndexBinaryHNSW.reconstructc                 C   s
   t | S r#   )r   ZIndexBinaryHNSW_resetr@   r   r   r   r|   $  s    zIndexBinaryHNSW.reset)N)r   r   r,   r-   r   r   r   r   r   ZIndexBinaryHNSW_hnsw_getZIndexBinaryHNSW_hnsw_setr  ZIndexBinaryHNSW_own_fields_getZIndexBinaryHNSW_own_fields_setr  ZIndexBinaryHNSW_storage_getZIndexBinaryHNSW_storage_setr  r?   Zdelete_IndexBinaryHNSWrT   r  ro  rn  ru  r  r|   r   r   r   r   r  #  s   
r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZdd Zd	d
 Zdd Zdd ZdddZdddZdd Zdd ZejZdS )IndexBinaryHash+ just uses the b first bits as a hash valuec                 C   s
   | j  S r#   r0   r1   r   r   r   r3   $  r4   zIndexBinaryHash.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   $  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexBinaryHash_swiginitZnew_IndexBinaryHashrQ   r   r   r   r?   !$  s    zIndexBinaryHash.__init__c                 C   s
   t | S r#   )r   ZIndexBinaryHash_resetr@   r   r   r   r|   $$  s    zIndexBinaryHash.resetc                 C   s   t | ||S r#   )r   ZIndexBinaryHash_addrm  r   r   r   ro  '$  s    zIndexBinaryHash.addc                 C   s   t | |||S r#   )r   ZIndexBinaryHash_add_with_idsrp  r   r   r   rr  *$  s    zIndexBinaryHash.add_with_idsNc                 C   s   t | |||||S r#   )r   ZIndexBinaryHash_range_searchrv  r   r   r   rw  -$  s    zIndexBinaryHash.range_searchc              	   C   s   t | ||||||S r#   )r   ZIndexBinaryHash_searchrs  r   r   r   ru  0$  s    zIndexBinaryHash.searchc                 C   s
   t | S r#   )r   ZIndexBinaryHash_displayr@   r   r   r   r  3$  s    zIndexBinaryHash.displayc                 C   s
   t | S r#   )r   ZIndexBinaryHash_hashtable_sizer@   r   r   r   hashtable_size6$  s    zIndexBinaryHash.hashtable_size)N)N)r   r   r,   r-   r   r   r   r   r   ZIndexBinaryHash_invlists_getZIndexBinaryHash_invlists_setrK  ZIndexBinaryHash_b_getZIndexBinaryHash_b_setr~   ZIndexBinaryHash_nflip_getZIndexBinaryHash_nflip_setnflipr?   r|   ro  rr  rw  ru  r  r  Zdelete_IndexBinaryHashrT   r   r   r   r   r  $  s   

r  c                   @   st   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZdd Zdd	 ZejZd
S )IndexBinaryHashStatsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   =$  r4   zIndexBinaryHashStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   =$  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZIndexBinaryHashStats_swiginitZnew_IndexBinaryHashStatsr@   r   r   r   r?   D$  s    zIndexBinaryHashStats.__init__c                 C   s
   t | S r#   )r   ZIndexBinaryHashStats_resetr@   r   r   r   r|   G$  s    zIndexBinaryHashStats.resetN)r   r   r,   r   r   r   r   r   ZIndexBinaryHashStats_nq_getZIndexBinaryHashStats_nq_setr   ZIndexBinaryHashStats_n0_getZIndexBinaryHashStats_n0_setn0ZIndexBinaryHashStats_nlist_getZIndexBinaryHashStats_nlist_setr  ZIndexBinaryHashStats_ndis_getZIndexBinaryHashStats_ndis_setr  r?   r|   Zdelete_IndexBinaryHashStatsrT   r   r   r   r   r  <$  s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejddZeejejddZeejejd	dZd
d ZejZdd Zdd ZdddZ dddZ!dd Z"dS )IndexBinaryMultiHashr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   P$  r4   zIndexBinaryMultiHash.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   P$  r4   r7   r8   z nb of hash mapsz nb bits per hash mapz# nb bit flips to use at search timec                 G   s   t | t j|  d S r#   )r   ZIndexBinaryMultiHash_swiginitZnew_IndexBinaryMultiHashrQ   r   r   r   r?   Y$  s    zIndexBinaryMultiHash.__init__c                 C   s
   t | S r#   )r   ZIndexBinaryMultiHash_resetr@   r   r   r   r|   ]$  s    zIndexBinaryMultiHash.resetc                 C   s   t | ||S r#   )r   ZIndexBinaryMultiHash_addrm  r   r   r   ro  `$  s    zIndexBinaryMultiHash.addNc                 C   s   t | |||||S r#   )r   Z!IndexBinaryMultiHash_range_searchrv  r   r   r   rw  c$  s    z!IndexBinaryMultiHash.range_searchc              	   C   s   t | ||||||S r#   )r   ZIndexBinaryMultiHash_searchrs  r   r   r   ru  f$  s    zIndexBinaryMultiHash.searchc                 C   s
   t | S r#   )r   Z#IndexBinaryMultiHash_hashtable_sizer@   r   r   r   r  i$  s    z#IndexBinaryMultiHash.hashtable_size)N)N)#r   r   r,   r-   r   r   r   r   r   Z IndexBinaryMultiHash_storage_getZ IndexBinaryMultiHash_storage_setr  Z#IndexBinaryMultiHash_own_fields_getZ#IndexBinaryMultiHash_own_fields_setr  ZIndexBinaryMultiHash_maps_getZIndexBinaryMultiHash_maps_setmapsZIndexBinaryMultiHash_nhash_getZIndexBinaryMultiHash_nhash_setZnhashZIndexBinaryMultiHash_b_getZIndexBinaryMultiHash_b_setr~   ZIndexBinaryMultiHash_nflip_getZIndexBinaryMultiHash_nflip_setr  r?   Zdelete_IndexBinaryMultiHashrT   r|   ro  rw  ru  r  r   r   r   r   r  M$  s    

r  c                   @   sz   e Zd ZdZedd dd ddZdd ZeZe	j
Zd	d
 Zdd Zdd Zdd Zdd Zdd Zee	je	jddZdS )ThreadedIndexBaser
    A holder of indices in a collection of threads
    The interface to this class itself is not thread safe
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   t$  r4   zThreadedIndexBase.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   t$  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   v$  s    zThreadedIndexBase.__init__c                 C   s   t | |S a  
        override an index that is managed by ourselves.
        WARNING: once an index is added, it becomes unsafe to touch it from any
        other thread than that on which is managing it, until we are shut
        down. Use runOnIndex to perform work on it instead.
        )r   ZThreadedIndexBase_addIndexr  r   r   r   addIndex{$  s    zThreadedIndexBase.addIndexc                 C   s   t | |S z
        Remove an index that is managed by ourselves.
        This will flush all pending work on that index, and then shut
        down its managing thread, and will remove the index.
        )r   ZThreadedIndexBase_removeIndexr  r   r   r   removeIndex$  s    zThreadedIndexBase.removeIndexc                 G   s   t j| g|R  S r#   )r   ZThreadedIndexBase_runOnIndexrQ   r   r   r   
runOnIndex$  s    zThreadedIndexBase.runOnIndexc                 C   s
   t | S zL
        faiss::Index API
        All indices receive the same call
        )r   ZThreadedIndexBase_resetr@   r   r   r   r|   $  s    zThreadedIndexBase.resetc                 C   s
   t | S z" Returns the number of sub-indices)r   ZThreadedIndexBase_countr@   r   r   r   r_  $  s    zThreadedIndexBase.countc                 G   s   t j| g|R  S z
        *Overload 1:*
        Returns the i-th sub-index

        |

        *Overload 2:*
        Returns the i-th sub-index (const version)
        )r   ZThreadedIndexBase_atrQ   r   r   r   r\   $  s    
zThreadedIndexBase.atE Whether or not we are responsible for deleting our contained indicesN)r   r   r,   r-   r   r   r?   r   r   r   Zdelete_ThreadedIndexBaserT   r  r  r  r|   r_  r\   Z!ThreadedIndexBase_own_indices_getZ!ThreadedIndexBase_own_indices_setown_indicesr   r   r   r   r  n$  s   	r  c                   @   sz   e Zd ZdZedd dd ddZdd ZeZe	j
Zd	d
 Zdd Zdd Zdd Zdd Zdd Zee	je	jddZdS )ThreadedIndexBaseBinaryr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   $  r4   z ThreadedIndexBaseBinary.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   $  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   $  s    z ThreadedIndexBaseBinary.__init__c                 C   s   t | |S r  )r   Z ThreadedIndexBaseBinary_addIndexr  r   r   r   r  $  s    z ThreadedIndexBaseBinary.addIndexc                 C   s   t | |S r  )r   Z#ThreadedIndexBaseBinary_removeIndexr  r   r   r   r  $  s    z#ThreadedIndexBaseBinary.removeIndexc                 G   s   t j| g|R  S r#   )r   Z"ThreadedIndexBaseBinary_runOnIndexrQ   r   r   r   r  $  s    z"ThreadedIndexBaseBinary.runOnIndexc                 C   s
   t | S r  )r   ZThreadedIndexBaseBinary_resetr@   r   r   r   r|   $  s    zThreadedIndexBaseBinary.resetc                 C   s
   t | S r  )r   ZThreadedIndexBaseBinary_countr@   r   r   r   r_  $  s    zThreadedIndexBaseBinary.countc                 G   s   t j| g|R  S r  )r   ZThreadedIndexBaseBinary_atrQ   r   r   r   r\   $  s    
zThreadedIndexBaseBinary.atr  N)r   r   r,   r-   r   r   r?   r   r   r   Zdelete_ThreadedIndexBaseBinaryrT   r  r  r  r|   r_  r\   Z'ThreadedIndexBaseBinary_own_indices_getZ'ThreadedIndexBaseBinary_own_indices_setr  r   r   r   r   r  $  s   	r  c                   @   s   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dd Z
dd Zdd ZdddZdd ZeejejZdd ZejZdS )IndexShards<Index that concatenates the results from several sub-indexesc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   $  r4   zIndexShards.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   $  r4   r7   r8   c                 G   s   t | t j|  dS a7  
        *Overload 1:*

        The dimension that all sub-indices must share will be the dimension of
        the first sub-index added

        :type threaded: boolean, optional
        :param threaded:     do we use one thread per sub_index or do
                                queries sequentially?
        :type successive_ids: boolean, optional
        :param successive_ids: should we shift the returned ids by
                                the size of each sub-index or return them
                                as they are?

        |

        *Overload 2:*

        :type threaded: boolean, optional
        :param threaded:     do we use one thread per sub_index or do
                                queries sequentially?
        :type successive_ids: boolean, optional
        :param successive_ids: should we shift the returned ids by
                                the size of each sub-index or return them
                                as they are?

        |

        *Overload 3:*

        :type threaded: boolean, optional
        :param threaded:     do we use one thread per sub_index or do
                                queries sequentially?
        :param successive_ids: should we shift the returned ids by
                                the size of each sub-index or return them
                                as they are?

        |

        *Overload 4:*

        :param threaded:     do we use one thread per sub_index or do
                                queries sequentially?
        :param successive_ids: should we shift the returned ids by
                                the size of each sub-index or return them
                                as they are?

        |

        *Overload 5:*
         int version due to the implicit bool conversion ambiguity of int as
         dimension

        |

        *Overload 6:*
         int version due to the implicit bool conversion ambiguity of int as
         dimension

        |

        *Overload 7:*
         int version due to the implicit bool conversion ambiguity of int as
         dimension
        N)r   ZIndexShards_swiginitZnew_IndexShardsrQ   r   r   r   r?   $  s    BzIndexShards.__init__c                 C   s   t | |S z Alias for addIndex())r   ZIndexShards_add_shardr  r   r   r   	add_shard.%  s    zIndexShards.add_shardc                 C   s   t | |S z Alias for removeIndex())r   ZIndexShards_remove_shardr  r   r   r   remove_shard2%  s    zIndexShards.remove_shardc                 C   s   t | ||S z; supported only for sub-indices that implement add_with_ids)r   ZIndexShards_addrm  r   r   r   ro  6%  s    zIndexShards.addc                 C   s   t | |||S a>  
        Cases (successive_ids, xids):
        - true, non-NULL       ERROR: it makes no sense to pass in ids and
                               request them to be shifted
        - true, NULL           OK: but should be called only once (calls add()
                               on sub-indexes).
        - false, non-NULL      OK: will call add_with_ids with passed in xids
                               distributed evenly over shards
        - false, NULL          OK: will call add_with_ids on each sub-index,
                               starting at ntotal
        )r   ZIndexShards_add_with_idsrp  r   r   r   rr  :%  s    zIndexShards.add_with_idsNc              	   C   s   t | ||||||S r#   )r   ZIndexShards_searchrs  r   r   r   ru  H%  s    zIndexShards.searchc                 C   s   t | ||S r#   )r   ZIndexShards_trainrm  r   r   r   rn  K%  s    zIndexShards.trainc                 C   s
   t | S zd
        Synchronize the top-level index (IndexShards) with data in the
        sub-indices
        )r   ZIndexShards_syncWithSubIndexesr@   r   r   r   syncWithSubIndexesO%  s    zIndexShards.syncWithSubIndexes)N)r   r   r,   r-   r   r   r   r   r?   r  r  ro  rr  ru  rn  r   ZIndexShards_successive_ids_getZIndexShards_successive_ids_setsuccessive_idsr  Zdelete_IndexShardsrT   r   r   r   r   r  $  s   D
r  c                   @   s   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dd Z
dd Zdd ZdddZdd ZeejejZdd ZejZdS )IndexBinaryShardsr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   \%  r4   zIndexBinaryShards.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   \%  r4   r7   r8   c                 G   s   t | t j|  dS r  )r   ZIndexBinaryShards_swiginitZnew_IndexBinaryShardsrQ   r   r   r   r?   _%  s    BzIndexBinaryShards.__init__c                 C   s   t | |S r  )r   ZIndexBinaryShards_add_shardr  r   r   r   r  %  s    zIndexBinaryShards.add_shardc                 C   s   t | |S r  )r   ZIndexBinaryShards_remove_shardr  r   r   r   r  %  s    zIndexBinaryShards.remove_shardc                 C   s   t | ||S r  )r   ZIndexBinaryShards_addrm  r   r   r   ro  %  s    zIndexBinaryShards.addc                 C   s   t | |||S r  )r   ZIndexBinaryShards_add_with_idsrp  r   r   r   rr  %  s    zIndexBinaryShards.add_with_idsNc              	   C   s   t | ||||||S r#   )r   ZIndexBinaryShards_searchrs  r   r   r   ru  %  s    zIndexBinaryShards.searchc                 C   s   t | ||S r#   )r   ZIndexBinaryShards_trainrm  r   r   r   rn  %  s    zIndexBinaryShards.trainc                 C   s
   t | S r  )r   Z$IndexBinaryShards_syncWithSubIndexesr@   r   r   r   r  %  s    z$IndexBinaryShards.syncWithSubIndexes)N)r   r   r,   r-   r   r   r   r   r?   r  r  ro  rr  ru  rn  r   Z$IndexBinaryShards_successive_ids_getZ$IndexBinaryShards_successive_ids_setr  r  Zdelete_IndexBinaryShardsrT   r   r   r   r   r  Y%  s   D
r  c                   @   s\   e Zd ZdZedd dd ddZeZdd	d
Zdd Z	dd Z
dd ZdddZejZdS )IndexShardsIVFz
    IndexShards with a common coarse quantizer. All the indexes added should be
    IndexIVFInterface indexes so that the search_precomputed can be called.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   %  r4   zIndexShardsIVF.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   %  r4   r7   r8   FTc              	   C   s   t | t |||| d S r#   )r   ZIndexShardsIVF_swiginitZnew_IndexShardsIVF)r   r^  r  threadedr  r   r   r   r?   %  s    zIndexShardsIVF.__init__c                 C   s   t | |S r#   )r   ZIndexShardsIVF_addIndexr  r   r   r   r  %  s    zIndexShardsIVF.addIndexc                 C   s   t | |||S r#   )r   ZIndexShardsIVF_add_with_idsrp  r   r   r   rr  %  s    zIndexShardsIVF.add_with_idsc                 C   s   t | ||S r#   )r   ZIndexShardsIVF_trainrm  r   r   r   rn  %  s    zIndexShardsIVF.trainNc              	   C   s   t | ||||||S r#   )r   ZIndexShardsIVF_searchrs  r   r   r   ru  %  s    zIndexShardsIVF.search)FT)N)r   r   r,   r-   r   r   r   r   r?   r  rr  rn  ru  r   Zdelete_IndexShardsIVFrT   r   r   r   r   r  %  s   

r  c                   @   sr   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dd Z
dd Zdd ZdddZdd Zdd ZejZdS )IndexReplicas
    Takes individual faiss::Index instances, and splits queries for
    sending to each Index instance, and joins the results together
    when done.
    Each index is managed by a separate CPU thread.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   %  r4   zIndexReplicas.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   %  r4   r7   r8   c                 G   s   t | t j|  dS a  
        *Overload 1:*
        The dimension that all sub-indices must share will be the dimension of
        the first sub-index added
        :type threaded: boolean, optional
        :param threaded: do we use one thread per sub-index or do queries
            sequentially?

        |

        *Overload 2:*
        :type d: int
        :param d: the dimension that all sub-indices must share
        :type threaded: boolean, optional
        :param threaded: do we use one thread per sub index or do queries
            sequentially?

        |

        *Overload 3:*
        :type d: int
        :param d: the dimension that all sub-indices must share
        :param threaded: do we use one thread per sub index or do queries
            sequentially?

        |

        *Overload 4:*
        int version due to the implicit bool conversion ambiguity of int as
        dimension

        |

        *Overload 5:*
        int version due to the implicit bool conversion ambiguity of int as
        dimension
        N)r   ZIndexReplicas_swiginitZnew_IndexReplicasrQ   r   r   r   r?   %  s    &zIndexReplicas.__init__c                 C   s   t | |S r  )r   ZIndexReplicas_add_replicar  r   r   r   add_replica&  s    zIndexReplicas.add_replicac                 C   s   t | |S r  )r   ZIndexReplicas_remove_replicar  r   r   r   remove_replica &  s    zIndexReplicas.remove_replicac                 C   s   t | ||S r  )r   ZIndexReplicas_trainrm  r   r   r   rn  $&  s    zIndexReplicas.trainc                 C   s   t | ||S r  )r   ZIndexReplicas_addrm  r   r   r   ro  +&  s    zIndexReplicas.addNc              	   C   s   t | ||||||S z
        faiss::Index API
        Query is partitioned into a slice for each sub-index
        split by ceil(n / #indices) for our sub-indices
        )r   ZIndexReplicas_searchrs  r   r   r   ru  2&  s    zIndexReplicas.searchc                 C   s   t | ||S z" reconstructs from the first index)r   ZIndexReplicas_reconstructr   rW   r6   r   r   r   r  :&  s    zIndexReplicas.reconstructc                 C   s
   t | S r  )r   Z IndexReplicas_syncWithSubIndexesr@   r   r   r   r  >&  s    z IndexReplicas.syncWithSubIndexes)N)r   r   r,   r-   r   r   r   r   r?   r  r  rn  ro  ru  r  r  r   Zdelete_IndexReplicasrT   r   r   r   r   r  %  s   (
r  c                   @   sr   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dd Z
dd Zdd ZdddZdd Zdd ZejZdS )IndexBinaryReplicasr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   P&  r4   zIndexBinaryReplicas.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   P&  r4   r7   r8   c                 G   s   t | t j|  dS r  )r   ZIndexBinaryReplicas_swiginitZnew_IndexBinaryReplicasrQ   r   r   r   r?   S&  s    &zIndexBinaryReplicas.__init__c                 C   s   t | |S r  )r   ZIndexBinaryReplicas_add_replicar  r   r   r   r  {&  s    zIndexBinaryReplicas.add_replicac                 C   s   t | |S r  )r   Z"IndexBinaryReplicas_remove_replicar  r   r   r   r  &  s    z"IndexBinaryReplicas.remove_replicac                 C   s   t | ||S r  )r   ZIndexBinaryReplicas_trainrm  r   r   r   rn  &  s    zIndexBinaryReplicas.trainc                 C   s   t | ||S r  )r   ZIndexBinaryReplicas_addrm  r   r   r   ro  &  s    zIndexBinaryReplicas.addNc              	   C   s   t | ||||||S r  )r   ZIndexBinaryReplicas_searchrs  r   r   r   ru  &  s    zIndexBinaryReplicas.searchc                 C   s   t | ||S r   )r   ZIndexBinaryReplicas_reconstructr  r   r   r   r  &  s    zIndexBinaryReplicas.reconstructc                 C   s
   t | S r  )r   Z&IndexBinaryReplicas_syncWithSubIndexesr@   r   r   r   r  &  s    z&IndexBinaryReplicas.syncWithSubIndexes)N)r   r   r,   r-   r   r   r   r   r?   r  r  rn  ro  ru  r  r  r   Zdelete_IndexBinaryReplicasrT   r   r   r   r   r  H&  s   (
r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZddd	Zd
d Zdd Zdd ZdddZdd Zdd ZejZdS )IndexSplitVectorsz
     splits input vectors in segments and assigns each segment to a sub-index
    used to distribute a MultiIndexQuantizer
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   &  r4   zIndexSplitVectors.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   &  r4   r7   r8   Fc                 C   s   t | t || dS )z sum of dimensions seen so farN)r   ZIndexSplitVectors_swiginitZnew_IndexSplitVectors)r   r   r  r   r   r   r?   &  s    zIndexSplitVectors.__init__c                 C   s   t | |S r#   )r   ZIndexSplitVectors_add_sub_indexrV   r   r   r   add_sub_index&  s    zIndexSplitVectors.add_sub_indexc                 C   s
   t | S r#   )r   Z'IndexSplitVectors_sync_with_sub_indexesr@   r   r   r   sync_with_sub_indexes&  s    z'IndexSplitVectors.sync_with_sub_indexesc                 C   s   t | ||S r#   )r   ZIndexSplitVectors_addrm  r   r   r   ro  &  s    zIndexSplitVectors.addNc              	   C   s   t | ||||||S r#   )r   ZIndexSplitVectors_searchrs  r   r   r   ru  &  s    zIndexSplitVectors.searchc                 C   s   t | ||S r#   )r   ZIndexSplitVectors_trainrm  r   r   r   rn  &  s    zIndexSplitVectors.trainc                 C   s
   t | S r#   )r   ZIndexSplitVectors_resetr@   r   r   r   r|   &  s    zIndexSplitVectors.reset)F)N)r   r   r,   r-   r   r   r   r   r   Z IndexSplitVectors_own_fields_getZ IndexSplitVectors_own_fields_setr  ZIndexSplitVectors_threaded_getZIndexSplitVectors_threaded_setr  Z!IndexSplitVectors_sub_indexes_getZ!IndexSplitVectors_sub_indexes_setZsub_indexesZIndexSplitVectors_sum_d_getZIndexSplitVectors_sum_d_setZsum_dr?   r  r  ro  ru  rn  r|   Zdelete_IndexSplitVectorsrT   r   r   r   r   r  &  s   

r  c                   @   sh   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 ZdddZdd Zdd ZejZdS )IndexRandomzQ
     index that returns random results.
    used mainly for time benchmarks
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   &  r4   zIndexRandom.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   &  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexRandom_swiginitZnew_IndexRandomrQ   r   r   r   r?   &  s    zIndexRandom.__init__c                 C   s   t | ||S r#   )r   ZIndexRandom_addrm  r   r   r   ro  &  s    zIndexRandom.addNc              	   C   s   t | ||||||S r#   )r   ZIndexRandom_searchrs  r   r   r   ru  &  s    zIndexRandom.searchc                 C   s   t | ||S r#   )r   ZIndexRandom_reconstructr|  r   r   r   r  &  s    zIndexRandom.reconstructc                 C   s
   t | S r#   )r   ZIndexRandom_resetr@   r   r   r   r|   &  s    zIndexRandom.reset)N)r   r   r,   r-   r   r   r   r   r   ZIndexRandom_seed_getZIndexRandom_seed_setr   r?   ro  ru  r  r|   Zdelete_IndexRandomrT   r   r   r   r   r  &  s   
r  c                   @   s~   e Zd ZdZedd dd ddZdd ZeZee	j
e	jd	dZee	je	jd
dZe	jZdd ZdddZdd Zdd ZdS )IndexRowwiseMinMaxBasea  
     Index wrapper that performs rowwise normalization to [0,1], preserving
     the coefficients. This is a vector codec index only.

     Basically, this index performs a rowwise scaling to [0,1] of every row
     in an input dataset before calling subindex::train() and
     subindex::sa_encode(). sa_encode() call stores the scaling coefficients
      (scaler and minv) in the very beginning of every output code. The format:
         [scaler][minv][subindex::sa_encode() output]
     The de-scaling in sa_decode() is done using:
         output_rescaled = scaler * output + minv

     An additional ::train_inplace() function is provided in order to do
     an inplace scaling before calling subindex::train() and, thus, avoiding
     the cloning of the input dataset, but modifying the input dataset because
     of the scaling and the scaling back. It is up to user to call
     this function instead of ::train()

     Derived classes provide different data types for scaling coefficients.
     Currently, versions with fp16 and fp32 scaling coefficients are available.
    fp16 version adds 4 extra bytes per encoded vector
    fp32 version adds 8 extra bytes per encoded vector
     Provides base functions for rowwise normalizing indices.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   '  r4   zIndexRowwiseMinMaxBase.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   '  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   '  s    zIndexRowwiseMinMaxBase.__init__z
 sub-indexz: whether the subindex needs to be freed in the destructor.c                 C   s   t | ||S r#   )r   ZIndexRowwiseMinMaxBase_addrm  r   r   r   ro  '  s    zIndexRowwiseMinMaxBase.addNc              	   C   s   t | ||||||S r#   )r   ZIndexRowwiseMinMaxBase_searchrs  r   r   r   ru  '  s    zIndexRowwiseMinMaxBase.searchc                 C   s
   t | S r#   )r   ZIndexRowwiseMinMaxBase_resetr@   r   r   r   r|   '  s    zIndexRowwiseMinMaxBase.resetc                 C   s   t | ||S r#   )r   Z$IndexRowwiseMinMaxBase_train_inplacerm  r   r   r   train_inplace'  s    z$IndexRowwiseMinMaxBase.train_inplace)N)r   r   r,   r-   r   r   r?   r   r   r   Z IndexRowwiseMinMaxBase_index_getZ IndexRowwiseMinMaxBase_index_setr  Z%IndexRowwiseMinMaxBase_own_fields_getZ%IndexRowwiseMinMaxBase_own_fields_setr  Zdelete_IndexRowwiseMinMaxBaserT   ro  ru  r|   r  r   r   r   r   r  &  s   
r  c                   @   s`   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dd Z
dd Zdd Zdd ZejZdS )IndexRowwiseMinMaxFP16z, Stores scaling coefficients as fp16 values.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   '  r4   zIndexRowwiseMinMaxFP16.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   '  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexRowwiseMinMaxFP16_swiginitZnew_IndexRowwiseMinMaxFP16rQ   r   r   r   r?    '  s    zIndexRowwiseMinMaxFP16.__init__c                 C   s   t | ||S r#   )r   ZIndexRowwiseMinMaxFP16_trainrm  r   r   r   rn  #'  s    zIndexRowwiseMinMaxFP16.trainc                 C   s   t | ||S r#   )r   Z$IndexRowwiseMinMaxFP16_train_inplacerm  r   r   r   r  &'  s    z$IndexRowwiseMinMaxFP16.train_inplacec                 C   s
   t | S r#   )r   Z#IndexRowwiseMinMaxFP16_sa_code_sizer@   r   r   r   r  )'  s    z#IndexRowwiseMinMaxFP16.sa_code_sizec                 C   s   t | |||S r#   )r   Z IndexRowwiseMinMaxFP16_sa_encoder  r   r   r   r  ,'  s    z IndexRowwiseMinMaxFP16.sa_encodec                 C   s   t | |||S r#   )r   Z IndexRowwiseMinMaxFP16_sa_decoder  r   r   r   r  /'  s    z IndexRowwiseMinMaxFP16.sa_decodeN)r   r   r,   r-   r   r   r   r   r?   rn  r  r  r  r  r   Zdelete_IndexRowwiseMinMaxFP16rT   r   r   r   r   r	  '  s   r	  c                   @   s`   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dd Z
dd Zdd Zdd ZejZdS )IndexRowwiseMinMaxz, Stores scaling coefficients as fp32 values.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   8'  r4   zIndexRowwiseMinMax.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   8'  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexRowwiseMinMax_swiginitZnew_IndexRowwiseMinMaxrQ   r   r   r   r?   ;'  s    zIndexRowwiseMinMax.__init__c                 C   s   t | ||S r#   )r   ZIndexRowwiseMinMax_trainrm  r   r   r   rn  >'  s    zIndexRowwiseMinMax.trainc                 C   s   t | ||S r#   )r   Z IndexRowwiseMinMax_train_inplacerm  r   r   r   r  A'  s    z IndexRowwiseMinMax.train_inplacec                 C   s
   t | S r#   )r   ZIndexRowwiseMinMax_sa_code_sizer@   r   r   r   r  D'  s    zIndexRowwiseMinMax.sa_code_sizec                 C   s   t | |||S r#   )r   ZIndexRowwiseMinMax_sa_encoder  r   r   r   r  G'  s    zIndexRowwiseMinMax.sa_encodec                 C   s   t | |||S r#   )r   ZIndexRowwiseMinMax_sa_decoder  r   r   r   r  J'  s    zIndexRowwiseMinMax.sa_decodeN)r   r   r,   r-   r   r   r   r   r?   rn  r  r  r  r  r   Zdelete_IndexRowwiseMinMaxrT   r   r   r   r   r
  5'  s   r
  c                   @   sz   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZeejejZddd	Zd
d ZejZdS )Linearz! minimal translation of nn.Linearc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   S'  r4   zLinear.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   S'  r4   r7   r8   Tc                 C   s   t | t ||| d S r#   )r   ZLinear_swiginitZ
new_Linear)r   in_featuresout_featuresr  r   r   r   r?   Z'  s    zLinear.__init__c                 C   s   t | |S r#   )r   ZLinear___call__rE   r   r   r   r  ]'  s    zLinear.__call__N)T)r   r   r,   r-   r   r   r   r   r   ZLinear_in_features_getZLinear_in_features_setr  ZLinear_out_features_getZLinear_out_features_setr  ZLinear_weight_getZLinear_weight_setweightZLinear_bias_getZLinear_bias_setr  r?   r  Zdelete_LinearrT   r   r   r   r   r  P'  s   
r  c                   @   sr   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZdd Zd	d
 Zdd ZejZdS )	Embeddingz$ minimal translation of nn.Embeddingc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   f'  r4   zEmbedding.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   f'  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZEmbedding_swiginitZnew_Embedding)r   num_embeddingsembedding_dimr   r   r   r?   l'  s    zEmbedding.__init__c                 C   s   t | |S r#   )r   ZEmbedding___call__rV   r   r   r   r  o'  s    zEmbedding.__call__c                 G   s   t j| g|R  S r#   )r   ZEmbedding_datarQ   r   r   r   rZ   r'  s    zEmbedding.dataN)r   r   r,   r-   r   r   r   r   r   ZEmbedding_num_embeddings_getZEmbedding_num_embeddings_setr  ZEmbedding_embedding_dim_getZEmbedding_embedding_dim_setr  ZEmbedding_weight_getZEmbedding_weight_setr  r?   r  rZ   Zdelete_EmbeddingrT   r   r   r   r   r  c'  s   r  c                   @   s\   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 ZejZdS )FFNz
    Feed forward layer that expands to a hidden dimension, applies a ReLU non
    linearity and maps back to the orignal dimension
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ~'  r4   zFFN.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ~'  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZFFN_swiginitZnew_FFN)r   r   r=  r   r   r   r?   '  s    zFFN.__init__c                 C   s   t | |S r#   )r   ZFFN___call__rE   r   r   r   r  '  s    zFFN.__call__N)r   r   r,   r-   r   r   r   r   r   ZFFN_linear1_getZFFN_linear1_setZlinear1ZFFN_linear2_getZFFN_linear2_setZlinear2r?   r  Z
delete_FFNrT   r   r   r   r   r  x'  s   r  c                   @   s   e Zd Zedd dd ddZeZeejej	ddZ
eejejZeejejZeejejZdd ZeejejZeejejZeejejZd	d
 ZdddZdd Z ej!Z"dS )	QINCoStepc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   '  r4   zQINCoStep.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   '  r4   r7   r8   zG d: input dim, K: codebook size, L: # of residual blocks, h: hidden dimc              	   C   s   t | t |||| d S r#   )r   ZQINCoStep_swiginitZnew_QINCoStep)r   r   rL  r"  r=  r   r   r   r?   '  s    zQINCoStep.__init__c                 C   s   t | |S r#   )r   ZQINCoStep_get_residual_blockr  r   r   r   get_residual_block'  s    zQINCoStep.get_residual_blockNc                 C   s   t | |||S )z
         encode a set of vectors x with intial estimate xhat. Optionally return
        the delta to be added to xhat to form the new xhat
        )r   ZQINCoStep_encode)r   xhatr2   r  r   r   r   r  '  s    zQINCoStep.encodec                 C   s   t | ||S r#   )r   ZQINCoStep_decode)r   r  r   r   r   r   r  '  s    zQINCoStep.decode)N)#r   r   r,   r   r   r   r   r   ZQINCoStep_d_getZQINCoStep_d_setr   ZQINCoStep_K_getZQINCoStep_K_setrL  ZQINCoStep_L_getZQINCoStep_L_setr"  ZQINCoStep_h_getZQINCoStep_h_setr=  r?   ZQINCoStep_codebook_getZQINCoStep_codebook_setZcodebookZQINCoStep_MLPconcat_getZQINCoStep_MLPconcat_setZ	MLPconcatZQINCoStep_residual_blocks_getZQINCoStep_residual_blocks_setZresidual_blocksr  r  r  Zdelete_QINCoSteprT   r   r   r   r   r  '  s   
r  c                   @   s`   e Zd Zedd dd ddZdd ZeZeej	ej
ZeejejZdd	 Zd
d ZejZdS )NeuralNetCodecc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   '  r4   zNeuralNetCodec.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   '  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   '  s    zNeuralNetCodec.__init__c                 C   s   t | |S r#   )r   ZNeuralNetCodec_decoder   r   r   r   r   r  '  s    zNeuralNetCodec.decodec                 C   s   t | |S r#   )r   ZNeuralNetCodec_encoderE   r   r   r   r  '  s    zNeuralNetCodec.encodeN)r   r   r,   r   r   r?   r   r   r   ZNeuralNetCodec_d_getZNeuralNetCodec_d_setr   ZNeuralNetCodec_M_getZNeuralNetCodec_M_setr  r  r  Zdelete_NeuralNetCodecrT   r   r   r   r   r  '  s   r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZeejejZeejejZdd Zdd	 Zd
d Zdd ZejZdS )QINCoc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   '  r4   zQINCo.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   '  r4   r7   r8   c              
   C   s   t | t ||||| d S r#   )r   ZQINCo_swiginitZ	new_QINCo)r   r   rL  r"  r  r=  r   r   r   r?   '  s    zQINCo.__init__c                 C   s   t | |S r#   )r   ZQINCo_get_stepr  r   r   r   get_step'  s    zQINCo.get_stepc                 C   s   t | |S r#   )r   ZQINCo_decoder  r   r   r   r  '  s    zQINCo.decodec                 C   s   t | |S r#   )r   ZQINCo_encoderE   r   r   r   r  '  s    zQINCo.encodeN)r   r   r,   r   r   r   r   r   ZQINCo_K_getZQINCo_K_setrL  ZQINCo_L_getZQINCo_L_setr"  ZQINCo_h_getZQINCo_h_setr=  ZQINCo_codebook0_getZQINCo_codebook0_setZ	codebook0ZQINCo_steps_getZQINCo_steps_setZstepsr?   r  r  r  Zdelete_QINCorT   r   r   r   r   r  '  s   r  c                   @   sv   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZddd	Zd
d Zdd Zdd Zdd ZejZdS )Tensor2D< Implements a few neural net layers, mainly to support QINCoc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   '  r4   zTensor2D.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   '  r4   r7   r8   Nc                 C   s   t | t ||| d S r#   )r   ZTensor2D_swiginitZnew_Tensor2Dr   r  r   rZ   r   r   r   r?   '  s    zTensor2D.__init__c                 C   s   t | |S r#   )r   ZTensor2D___iadd__rV   r   r   r   rN   '  s    zTensor2D.__iadd__c                 C   s   t | |S z% get column #j as a 1-column Tensor2D)r   ZTensor2D_columnr   r  r   r   r   column'  s    zTensor2D.columnc                 C   s
   t | S r#   )r   ZTensor2D_numelr@   r   r   r   numel'  s    zTensor2D.numelc                 G   s   t j| g|R  S r#   )r   ZTensor2D_datarQ   r   r   r   rZ   '  s    zTensor2D.data)N)r   r   r,   r-   r   r   r   r   r   ZTensor2D_shape_getZTensor2D_shape_setshapeZTensor2D_v_getZTensor2D_v_setr6   r?   rN   r  r   rZ   Zdelete_Tensor2DrT   r   r   r   r   r  '  s   
r  c                   @   sv   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZddd	Zd
d Zdd Zdd Zdd ZejZdS )Int32Tensor2Dr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   '  r4   zInt32Tensor2D.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   '  r4   r7   r8   Nc                 C   s   t | t ||| d S r#   )r   ZInt32Tensor2D_swiginitZnew_Int32Tensor2Dr  r   r   r   r?   '  s    zInt32Tensor2D.__init__c                 C   s   t | |S r#   )r   ZInt32Tensor2D___iadd__rV   r   r   r   rN   '  s    zInt32Tensor2D.__iadd__c                 C   s   t | |S r  )r   ZInt32Tensor2D_columnr  r   r   r   r  '  s    zInt32Tensor2D.columnc                 C   s
   t | S r#   )r   ZInt32Tensor2D_numelr@   r   r   r   r   (  s    zInt32Tensor2D.numelc                 G   s   t j| g|R  S r#   )r   ZInt32Tensor2D_datarQ   r   r   r   rZ   (  s    zInt32Tensor2D.data)N)r   r   r,   r-   r   r   r   r   r   ZInt32Tensor2D_shape_getZInt32Tensor2D_shape_setr!  ZInt32Tensor2D_v_getZInt32Tensor2D_v_setr6   r?   rN   r  r   rZ   Zdelete_Int32Tensor2DrT   r   r   r   r   r"  '  s   
r"  c                   @   sv   e Zd Zedd dd ddZeZeejej	Z
eejejZeejejZdd Zdd	 Zd
d Zdd ZejZdS )IndexNeuralNetCodecc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   (  r4   zIndexNeuralNetCodec.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   (  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexNeuralNetCodec_swiginitZnew_IndexNeuralNetCodecrQ   r   r   r   r?   (  s    zIndexNeuralNetCodec.__init__c                 C   s   t | ||S r#   )r   ZIndexNeuralNetCodec_trainrm  r   r   r   rn  (  s    zIndexNeuralNetCodec.trainc                 C   s   t | |||S r#   )r   ZIndexNeuralNetCodec_sa_encode)r   rB   r2   r   r   r   r   r  (  s    zIndexNeuralNetCodec.sa_encodec                 C   s   t | |||S r#   )r   ZIndexNeuralNetCodec_sa_decoder  r   r   r   r  (  s    zIndexNeuralNetCodec.sa_decodeN)r   r   r,   r   r   r   r   r   ZIndexNeuralNetCodec_net_getZIndexNeuralNetCodec_net_setnetZIndexNeuralNetCodec_M_getZIndexNeuralNetCodec_M_setr  ZIndexNeuralNetCodec_nbits_getZIndexNeuralNetCodec_nbits_setr   r?   rn  r  r  Zdelete_IndexNeuralNetCodecrT   r   r   r   r   r#  
(  s   r#  c                   @   sB   e Zd Zedd dd ddZeZeejej	Z
dd ZejZdS )	
IndexQINCoc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   !(  r4   zIndexQINCo.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   !(  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIndexQINCo_swiginitZnew_IndexQINCorQ   r   r   r   r?   %(  s    zIndexQINCo.__init__N)r   r   r,   r   r   r   r   r   ZIndexQINCo_qinco_getZIndexQINCo_qinco_setZqincor?   Zdelete_IndexQINCorT   r   r   r   r   r%   (  s
   r%  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZdddZdd ZejZdS )RangeSearchResulta	  
    The objective is to have a simple result structure while
    minimizing the number of mem copies in the result. The method
    do_allocation can be overloaded to allocate the result tables in
    the matrix type of a scripting language like Lua or Python.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   3(  r4   zRangeSearchResult.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   3(  r4   r7   r8   r   z size (nq + 1)z0 result for query i is labels[lims[i]:lims[i+1]]z% corresponding distances (not sorted)z  size of the result buffers usedTc                 C   s   t | t || dS )z1 lims must be allocated on input to range_search.N)r   ZRangeSearchResult_swiginitZnew_RangeSearchResult)r   r   Z
alloc_limsr   r   r   r?   ;(  s    zRangeSearchResult.__init__c                 C   s
   t | S )zd
        called when lims contains the nb of elements result entries
        for each query
        )r   ZRangeSearchResult_do_allocationr@   r   r   r   do_allocation?(  s    zRangeSearchResult.do_allocationN)T)r   r   r,   r-   r   r   r   r   r   ZRangeSearchResult_nq_getZRangeSearchResult_nq_setr   ZRangeSearchResult_lims_getZRangeSearchResult_lims_setr[  ZRangeSearchResult_labels_getZRangeSearchResult_labels_setr   ZRangeSearchResult_distances_getZRangeSearchResult_distances_setr   Z!RangeSearchResult_buffer_size_getZ!RangeSearchResult_buffer_size_setbuffer_sizer?   r'  Zdelete_RangeSearchResultrT   r   r   r   r   r&  +(  s   
r&  c                   @   s~   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejddZdd	 ZejZd
d Zdd Zdd ZdS )
BufferListzu
    List of temporary buffers used to store results before they are
    copied to the RangeSearchResult object.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   O(  r4   zBufferList.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   O(  r4   r7   r8   z" write pointer in the last buffer.c                 C   s   t | t | d S r#   )r   ZBufferList_swiginitZnew_BufferList)r   r(  r   r   r   r?   U(  s    zBufferList.__init__c                 C   s
   t | S )z create a new buffer)r   ZBufferList_append_bufferr@   r   r   r   append_bufferY(  s    zBufferList.append_bufferc                 C   s   t | ||S )z: add one result, possibly appending a new buffer if needed)r   ZBufferList_add)r   r  r   r   r   r   ro  ](  s    zBufferList.addc                 C   s   t | ||||S )zr
        copy elemnts ofs:ofs+n-1 seen as linear data in the buffers to
        tables dest_ids, dest_dis
        )r   ZBufferList_copy_range)r   r  rB   Zdest_idsZdest_disr   r   r   
copy_rangea(  s    zBufferList.copy_rangeN)r   r   r,   r-   r   r   r   r   r   ZBufferList_buffer_size_getZBufferList_buffer_size_setr(  ZBufferList_buffers_getZBufferList_buffers_setbuffersZBufferList_wp_getZBufferList_wp_setZwpr?   Zdelete_BufferListrT   r*  ro  r+  r   r   r   r   r)  I(  s   r)  c                   @   sj   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejZdd Zd	d
 ZejZdS )RangeQueryResultz$ result structure for a single queryc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   m(  r4   zRangeQueryResult.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   m(  r4   r7   r8   c                 C   s   t | ||S )z1 called by search function to report a new result)r   ZRangeQueryResult_add)r   r   r  r   r   r   ro  s(  s    zRangeQueryResult.addc                 C   s   t | t   d S r#   )r   ZRangeQueryResult_swiginitZnew_RangeQueryResultr@   r   r   r   r?   w(  s    zRangeQueryResult.__init__N)r   r   r,   r-   r   r   r   r   r   ZRangeQueryResult_qno_getZRangeQueryResult_qno_setqnoZRangeQueryResult_nres_getZRangeQueryResult_nres_setZnresZRangeQueryResult_pres_getZRangeQueryResult_pres_setZpresro  r?   Zdelete_RangeQueryResultrT   r   r   r   r   r-  j(  s   r-  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zeejejd	dZd
d Zdd Zdd ZdddZedddZejZdS )RangeSearchPartialResultz/ the entries in the buffers are split per queryc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   (  r4   z!RangeSearchPartialResult.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   (  r4   r7   r8   c                 C   s   t | t | dS )z/ eventually the result will be stored in res_inN)r   Z!RangeSearchPartialResult_swiginitZnew_RangeSearchPartialResult)r   Zres_inr   r   r   r?   (  s    z!RangeSearchPartialResult.__init__z% query ids + nb of results per query.c                 C   s   t | |S )z begin a new result)r   Z#RangeSearchPartialResult_new_result)r   r.  r   r   r   
new_result(  s    z#RangeSearchPartialResult.new_resultc                 C   s
   t | S r#   )r   Z!RangeSearchPartialResult_finalizer@   r   r   r   finalize(  s    z!RangeSearchPartialResult.finalizec                 C   s
   t | S )z, called by range_search before do_allocation)r   Z!RangeSearchPartialResult_set_limsr@   r   r   r   set_lims(  s    z!RangeSearchPartialResult.set_limsFc                 C   s   t | |S )z+ called by range_search after do_allocation)r   Z$RangeSearchPartialResult_copy_result)r   incrementalr   r   r   copy_result(  s    z$RangeSearchPartialResult.copy_resultTc                 C   s   t | |S )z{
        merge a set of PartialResult's into one RangeSearchResult
        on output the partialresults are empty!
        )r   ZRangeSearchPartialResult_merge)Zpartial_resultsZ	do_deleter   r   r   merge(  s    zRangeSearchPartialResult.mergeN)F)T)r   r   r,   r-   r   r   r   r   r   Z RangeSearchPartialResult_res_getZ RangeSearchPartialResult_res_setr  r?   Z$RangeSearchPartialResult_queries_getZ$RangeSearchPartialResult_queries_setZqueriesr0  r1  r2  r4  r  r5  Zdelete_RangeSearchPartialResultrT   r   r   r   r   r/  }(  s   
r/  c                   @   sl   e Zd Zedd dd ddZdd ZeZdd	 Ze	j
Zed
d Zedd Zedd Zedd ZdS )InterruptCallbackc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   (  r4   zInterruptCallback.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   (  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   (  s    zInterruptCallback.__init__c                 C   s
   t | S r#   )r   Z InterruptCallback_want_interruptr@   r   r   r   want_interrupt(  s    z InterruptCallback.want_interruptc                   C   s   t  S r#   )r   Z InterruptCallback_clear_instancer   r   r   r   clear_instance(  s    z InterruptCallback.clear_instancec                   C   s   t  S )z
         check if:
        - an interrupt callback is set
        - the callback returns true
        if this is the case, then throw an exception. Should not be called
        from multiple threads.
        )r   ZInterruptCallback_checkr   r   r   r   check(  s    	zInterruptCallback.checkc                   C   s   t  S )z
        same as check() but return true if is interrupted instead of
        throwing. Can be called from multiple threads.
        )r   Z InterruptCallback_is_interruptedr   r   r   r   is_interrupted(  s    z InterruptCallback.is_interruptedc                 C   s
   t | S )z
         assuming each iteration takes a certain number of flops, what
        is a reasonable interval to check for interrupts?
        )r   Z!InterruptCallback_get_period_hint)Zflopsr   r   r   get_period_hint(  s    z!InterruptCallback.get_period_hintN)r   r   r,   r   r   r?   r   r   r7  r   Zdelete_InterruptCallbackrT   r  r8  r9  r:  r;  r   r   r   r   r6  (  s   



r6  c                   @   sl   e Zd Zedd dd ddZeZeejej	Z
eejejZdd Zdd	 Zed
d Zdd ZejZdS )TimeoutCallbackc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   (  r4   zTimeoutCallback.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   (  r4   r7   r8   c                 C   s
   t | S r#   )r   ZTimeoutCallback_want_interruptr@   r   r   r   r7  (  s    zTimeoutCallback.want_interruptc                 C   s   t | |S r#   )r   ZTimeoutCallback_set_timeout)r   timeout_in_secondsr   r   r   set_timeout(  s    zTimeoutCallback.set_timeoutc                 C   s
   t | S r#   )r   ZTimeoutCallback_reset)r=  r   r   r   r|   (  s    zTimeoutCallback.resetc                 C   s   t | t   d S r#   )r   ZTimeoutCallback_swiginitZnew_TimeoutCallbackr@   r   r   r   r?   (  s    zTimeoutCallback.__init__N)r   r   r,   r   r   r   r   r   ZTimeoutCallback_start_getZTimeoutCallback_start_setstartZTimeoutCallback_timeout_getZTimeoutCallback_timeout_settimeoutr7  r>  r  r|   r?   Zdelete_TimeoutCallbackrT   r   r   r   r   r<  (  s   
r<  c                   @   sl   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 Zdd Zdd ZejZdS )VisitedTablez. set implementation optimized for fast access.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   (  r4   zVisitedTable.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   (  r4   r7   r8   c                 C   s   t | t | d S r#   )r   ZVisitedTable_swiginitZnew_VisitedTabler  r   r   r   r?   (  s    zVisitedTable.__init__c                 C   s   t | |S )z set flag #no to true)r   ZVisitedTable_setr   rN  r   r   r   r   (  s    zVisitedTable.setc                 C   s   t | |S )z get flag #no)r   ZVisitedTable_getrB  r   r   r   rV  (  s    zVisitedTable.getc                 C   s
   t | S )z reset all flags to false)r   ZVisitedTable_advancer@   r   r   r   rK   (  s    zVisitedTable.advanceN)r   r   r,   r-   r   r   r   r   r   ZVisitedTable_visited_getZVisitedTable_visited_setvisitedZVisitedTable_visno_getZVisitedTable_visno_setZvisnor?   r   rV  rK   Zdelete_VisitedTablerT   r   r   r   r   rA  (  s   rA  c                   @   s@   e Zd ZdZedd dd ddZdd ZeZd	d
 Z	e
jZdS )
IDSelectorz% Encapsulates a set of ids to handle.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )  r4   zIDSelector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   )  s    zIDSelector.__init__c                 C   s   t | |S r#   )r   ZIDSelector_is_memberrM  r   r   r   	is_member)  s    zIDSelector.is_memberN)r   r   r,   r-   r   r   r?   r   r   rE  r   Zdelete_IDSelectorrT   r   r   r   r   rD  (  s   rD  c                   @   sx   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZeejejddZdd	d
Zdd Zdd ZejZdS )IDSelectorRangez ids between [imin, imax)c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )  r4   zIDSelectorRange.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )  r4   r7   r8   zb
    Assume that the ids to handle are sorted. In some cases this can speed
    up processing
    Fc                 C   s   t | t ||| d S r#   )r   ZIDSelectorRange_swiginitZnew_IDSelectorRange)r   iminimaxassume_sortedr   r   r   r?   )  s    zIDSelectorRange.__init__c                 C   s   t | |S r#   )r   ZIDSelectorRange_is_memberrM  r   r   r   rE  )  s    zIDSelectorRange.is_memberc                 C   s   t | ||||S )zg
        for sorted ids, find the range of list indices where the valid ids are
        stored
        )r   Z&IDSelectorRange_find_sorted_ids_bounds)r   r  r'  ZjminZjmaxr   r   r   find_sorted_ids_bounds )  s    z&IDSelectorRange.find_sorted_ids_boundsN)F)r   r   r,   r-   r   r   r   r   r   ZIDSelectorRange_imin_getZIDSelectorRange_imin_setrG  ZIDSelectorRange_imax_getZIDSelectorRange_imax_setrH  Z!IDSelectorRange_assume_sorted_getZ!IDSelectorRange_assume_sorted_setrI  r?   rE  rJ  Zdelete_IDSelectorRangerT   r   r   r   r   rF  )  s   
rF  c                   @   s\   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 ZejZdS )IDSelectorArrayz
     Simple array of elements

    is_member calls are very inefficient, but some operations can use the ids
    directly.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   2)  r4   zIDSelectorArray.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   2)  r4   r7   r8   c                 C   s   t | t || dS )a  
         Construct with an array of ids to process

        :type n: int
        :param n: number of ids to store
        :type ids: int
        :param ids: elements to store. The pointer should remain valid during
                       IDSelectorArray's lifetime
        N)r   ZIDSelectorArray_swiginitZnew_IDSelectorArray)r   rB   r'  r   r   r   r?   7)  s    
zIDSelectorArray.__init__c                 C   s   t | |S r#   )r   ZIDSelectorArray_is_memberrM  r   r   r   rE  C)  s    zIDSelectorArray.is_memberN)r   r   r,   r-   r   r   r   r   r   ZIDSelectorArray_n_getZIDSelectorArray_n_setrB   ZIDSelectorArray_ids_getZIDSelectorArray_ids_setr'  r?   rE  Zdelete_IDSelectorArrayrT   r   r   r   r   rK  *)  s   rK  c                   @   s\   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 ZejZdS )IDSelectorBatcha  
     Ids from a set.

    Repetitions of ids in the indices set passed to the constructor does not hurt
    performance.

    The hash function used for the bloom filter and GCC's implementation of
    unordered_set are just the least significant bits of the id. This works fine
    for random ids or ids in sequences but will produce many hash collisions if
    lsb's are always the same
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   V)  r4   zIDSelectorBatch.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   V)  r4   r7   r8   c                 C   s   t | t || dS )z
         Construct with an array of ids to process

        :type n: int
        :param n: number of ids to store
        :param ids: elements to store. The pointer can be released after
                       construction
        N)r   ZIDSelectorBatch_swiginitZnew_IDSelectorBatch)r   rB   r  r   r   r   r?   [)  s    	zIDSelectorBatch.__init__c                 C   s   t | |S r#   )r   ZIDSelectorBatch_is_memberrM  r   r   r   rE  f)  s    zIDSelectorBatch.is_memberN)r   r   r,   r-   r   r   r   r   r   ZIDSelectorBatch_nbits_getZIDSelectorBatch_nbits_setr   ZIDSelectorBatch_mask_getZIDSelectorBatch_mask_setr   r?   rE  Zdelete_IDSelectorBatchrT   r   r   r   r   rL  I)  s   rL  c                   @   s\   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 ZejZdS )IDSelectorBitmapzB One bit per element. Constructed with a bitmap, size ceil(n / 8).c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   o)  r4   zIDSelectorBitmap.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   o)  r4   r7   r8   c                 C   s   t | t || dS )a  
         Construct with a binary mask

        :type n: int
        :param n: size of the bitmap array
        :type bitmap: uint8_t
        :param bitmap: id will be selected iff id / 8 < n and bit number
                          (i%8) of bitmap[floor(i / 8)] is 1.
        N)r   ZIDSelectorBitmap_swiginitZnew_IDSelectorBitmap)r   rB   bitmapr   r   r   r?   t)  s    
zIDSelectorBitmap.__init__c                 C   s   t | |S r#   )r   ZIDSelectorBitmap_is_memberrM  r   r   r   rE  )  s    zIDSelectorBitmap.is_memberN)r   r   r,   r-   r   r   r   r   r   ZIDSelectorBitmap_n_getZIDSelectorBitmap_n_setrB   ZIDSelectorBitmap_bitmap_getZIDSelectorBitmap_bitmap_setrN  r?   rE  Zdelete_IDSelectorBitmaprT   r   r   r   r   rM  l)  s   rM  c                   @   sN   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 ZejZdS )IDSelectorNotz0 reverts the membership test of another selectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )  r4   zIDSelectorNot.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )  r4   r7   r8   c                 C   s   t | t | d S r#   )r   ZIDSelectorNot_swiginitZnew_IDSelectorNotrz  r   r   r   r?   )  s    zIDSelectorNot.__init__c                 C   s   t | |S r#   )r   ZIDSelectorNot_is_memberrM  r   r   r   rE  )  s    zIDSelectorNot.is_memberN)r   r   r,   r-   r   r   r   r   r   ZIDSelectorNot_sel_getZIDSelectorNot_sel_setr5  r?   rE  Zdelete_IDSelectorNotrT   r   r   r   r   rO  )  s   rO  c                   @   s@   e Zd ZdZedd dd ddZeZdd Ze	j
Zd	d
 ZdS )IDSelectorAllz. selects all entries (useful for benchmarking)c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )  r4   zIDSelectorAll.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )  r4   r7   r8   c                 C   s   t | |S r#   )r   ZIDSelectorAll_is_memberrM  r   r   r   rE  )  s    zIDSelectorAll.is_memberc                 C   s   t | t   d S r#   )r   ZIDSelectorAll_swiginitZnew_IDSelectorAllr@   r   r   r   r?   )  s    zIDSelectorAll.__init__N)r   r   r,   r-   r   r   r   r   rE  r   Zdelete_IDSelectorAllrT   r?   r   r   r   r   rP  )  s   rP  c                   @   s\   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 ZejZdS )IDSelectorAndz\
    does an AND operation on the the two given IDSelector's is_membership
    results.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )  r4   zIDSelectorAnd.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZIDSelectorAnd_swiginitZnew_IDSelectorAndr   lhsrhsr   r   r   r?   )  s    zIDSelectorAnd.__init__c                 C   s   t | |S r#   )r   ZIDSelectorAnd_is_memberrM  r   r   r   rE  )  s    zIDSelectorAnd.is_memberN)r   r   r,   r-   r   r   r   r   r   ZIDSelectorAnd_lhs_getZIDSelectorAnd_lhs_setrS  ZIDSelectorAnd_rhs_getZIDSelectorAnd_rhs_setrT  r?   rE  Zdelete_IDSelectorAndrT   r   r   r   r   rQ  )  s   rQ  c                   @   s\   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 ZejZdS )IDSelectorOrz[
    does an OR operation on the the two given IDSelector's is_membership
    results.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )  r4   zIDSelectorOr.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZIDSelectorOr_swiginitZnew_IDSelectorOrrR  r   r   r   r?   )  s    zIDSelectorOr.__init__c                 C   s   t | |S r#   )r   ZIDSelectorOr_is_memberrM  r   r   r   rE  )  s    zIDSelectorOr.is_memberN)r   r   r,   r-   r   r   r   r   r   ZIDSelectorOr_lhs_getZIDSelectorOr_lhs_setrS  ZIDSelectorOr_rhs_getZIDSelectorOr_rhs_setrT  r?   rE  Zdelete_IDSelectorOrrT   r   r   r   r   rU  )  s   rU  c                   @   s\   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd Zd	d
 ZejZdS )IDSelectorXOrz\
    does an XOR operation on the the two given IDSelector's is_membership
    results.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )  r4   zIDSelectorXOr.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZIDSelectorXOr_swiginitZnew_IDSelectorXOrrR  r   r   r   r?   )  s    zIDSelectorXOr.__init__c                 C   s   t | |S r#   )r   ZIDSelectorXOr_is_memberrM  r   r   r   rE  )  s    zIDSelectorXOr.is_memberN)r   r   r,   r-   r   r   r   r   r   ZIDSelectorXOr_lhs_getZIDSelectorXOr_lhs_setrS  ZIDSelectorXOr_rhs_getZIDSelectorXOr_rhs_setrT  r?   rE  Zdelete_IDSelectorXOrrT   r   r   r   r   rV  )  s   rV  c                   @   sT   e Zd Zedd dd ddZeZeejZ	eej
ejZdd Zdd	 ZejZd
S )IDSelectorTranslatedc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )  r4   zIDSelectorTranslated.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )  r4   r7   r8   c                 G   s   t | t j|  d S r#   )r   ZIDSelectorTranslated_swiginitZnew_IDSelectorTranslatedrQ   r   r   r   r?   )  s    zIDSelectorTranslated.__init__c                 C   s   t | |S r#   )r   ZIDSelectorTranslated_is_memberrM  r   r   r   rE  )  s    zIDSelectorTranslated.is_memberN)r   r   r,   r   r   r   r   r   ZIDSelectorTranslated_id_map_getr  ZIDSelectorTranslated_sel_getZIDSelectorTranslated_sel_setr5  r?   rE  Zdelete_IDSelectorTranslatedrT   r   r   r   r   rW  )  s   
rW  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZeejejddZd	d
 Zdd Zd#ddZdd Zdd Zdd Zd$ddZd%ddZdd Zdd Zdd  ZejZd!d" ZdS )&
IndexIDMap, Index that translates search results to idsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )  r4   zIndexIDMap.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )  r4   r7   r8   r  )whether pointers are deleted in destructoc                 C   s   t | |||S zi
        :type xids: int
        :param xids: if non-null, ids to store for the vectors (size n)
        )r   ZIndexIDMap_add_with_idsrp  r   r   r   rr  )  s    zIndexIDMap.add_with_idsc                 C   s   t | ||S z! this will fail. Use add_with_ids)r   ZIndexIDMap_addrm  r   r   r   ro   *  s    zIndexIDMap.addNc              	   C   s   t | ||||||S r#   )r   ZIndexIDMap_searchrs  r   r   r   ru  *  s    zIndexIDMap.searchc                 C   s   t | ||S r#   )r   ZIndexIDMap_trainrm  r   r   r   rn  *  s    zIndexIDMap.trainc                 C   s
   t | S r#   )r   ZIndexIDMap_resetr@   r   r   r   r|   
*  s    zIndexIDMap.resetc                 C   s   t | |S z  remove ids adapted to IndexFlat)r   ZIndexIDMap_remove_idsrz  r   r   r   r{  *  s    zIndexIDMap.remove_idsc                 C   s   t | |||||S r#   )r   ZIndexIDMap_range_searchrv  r   r   r   rw  *  s    zIndexIDMap.range_searchr   c                 C   s   t | ||S r#   )r   ZIndexIDMap_merge_fromr  r   r   r   r  *  s    zIndexIDMap.merge_fromc                 C   s   t | |S r#   )r   Z%IndexIDMap_check_compatible_for_merger  r   r   r   r  *  s    z%IndexIDMap.check_compatible_for_mergec                 C   s
   t | S r#   )r   ZIndexIDMap_sa_code_sizer@   r   r   r   r  *  s    zIndexIDMap.sa_code_sizec                 C   s   t | |||S r#   )r   ZIndexIDMap_add_sa_codesrp  r   r   r   r  *  s    zIndexIDMap.add_sa_codesc                 G   s   t | t j|  d S r#   )r   ZIndexIDMap_swiginitZnew_IndexIDMaprQ   r   r   r   r?   !*  s    zIndexIDMap.__init__)N)N)r   ) r   r   r,   r-   r   r   r   r   r   ZIndexIDMap_index_getZIndexIDMap_index_setr  ZIndexIDMap_own_fields_getZIndexIDMap_own_fields_setr  ZIndexIDMap_id_map_getZIndexIDMap_id_map_setr  rr  ro  ru  rn  r|   r{  rw  r  r  r  r  Zdelete_IndexIDMaprT   r?   r   r   r   r   rX  )  s&   


rX  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ZeejejddZeejejddZd	d
 Zdd Zd#ddZdd Zdd Zdd Zd$ddZd%ddZdd Zdd Zdd  ZejZd!d" ZdS )&IndexBinaryIDMaprY  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   )*  r4   zIndexBinaryIDMap.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   )*  r4   r7   r8   r  rZ  c                 C   s   t | |||S r[  )r   ZIndexBinaryIDMap_add_with_idsrp  r   r   r   rr  /*  s    zIndexBinaryIDMap.add_with_idsc                 C   s   t | ||S r\  )r   ZIndexBinaryIDMap_addrm  r   r   r   ro  6*  s    zIndexBinaryIDMap.addNc              	   C   s   t | ||||||S r#   )r   ZIndexBinaryIDMap_searchrs  r   r   r   ru  :*  s    zIndexBinaryIDMap.searchc                 C   s   t | ||S r#   )r   ZIndexBinaryIDMap_trainrm  r   r   r   rn  =*  s    zIndexBinaryIDMap.trainc                 C   s
   t | S r#   )r   ZIndexBinaryIDMap_resetr@   r   r   r   r|   @*  s    zIndexBinaryIDMap.resetc                 C   s   t | |S r]  )r   ZIndexBinaryIDMap_remove_idsrz  r   r   r   r{  C*  s    zIndexBinaryIDMap.remove_idsc                 C   s   t | |||||S r#   )r   ZIndexBinaryIDMap_range_searchrv  r   r   r   rw  G*  s    zIndexBinaryIDMap.range_searchr   c                 C   s   t | ||S r#   )r   ZIndexBinaryIDMap_merge_fromr  r   r   r   r  J*  s    zIndexBinaryIDMap.merge_fromc                 C   s   t | |S r#   )r   Z+IndexBinaryIDMap_check_compatible_for_merger  r   r   r   r  M*  s    z+IndexBinaryIDMap.check_compatible_for_mergec                 C   s
   t | S r#   )r   ZIndexBinaryIDMap_sa_code_sizer@   r   r   r   r  P*  s    zIndexBinaryIDMap.sa_code_sizec                 C   s   t | |||S r#   )r   ZIndexBinaryIDMap_add_sa_codesrp  r   r   r   r  S*  s    zIndexBinaryIDMap.add_sa_codesc                 G   s   t | t j|  d S r#   )r   ZIndexBinaryIDMap_swiginitZnew_IndexBinaryIDMaprQ   r   r   r   r?   W*  s    zIndexBinaryIDMap.__init__)N)N)r   ) r   r   r,   r-   r   r   r   r   r   ZIndexBinaryIDMap_index_getZIndexBinaryIDMap_index_setr  ZIndexBinaryIDMap_own_fields_getZIndexBinaryIDMap_own_fields_setr  ZIndexBinaryIDMap_id_map_getZIndexBinaryIDMap_id_map_setr  rr  ro  ru  rn  r|   r{  rw  r  r  r  r  Zdelete_IndexBinaryIDMaprT   r?   r   r   r   r   r^  &*  s&   


r^  c                   @   sx   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 Zdd Zdd Zdd ZdddZejZdd ZdS )IndexIDMap2o
    same as IndexIDMap but also provides an efficient reconstruction
    implementation via a 2-way index
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   b*  r4   zIndexIDMap2.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   b*  r4   r7   r8   c                 C   s
   t | S z make the rev_map from scratch)r   ZIndexIDMap2_construct_rev_mapr@   r   r   r   construct_rev_mapf*  s    zIndexIDMap2.construct_rev_mapc                 C   s   t | |||S r#   )r   ZIndexIDMap2_add_with_idsrp  r   r   r   rr  j*  s    zIndexIDMap2.add_with_idsc                 C   s   t | |S r#   )r   ZIndexIDMap2_remove_idsrz  r   r   r   r{  m*  s    zIndexIDMap2.remove_idsc                 C   s   t | ||S r#   )r   ZIndexIDMap2_reconstructr|  r   r   r   r  p*  s    zIndexIDMap2.reconstructc                 C   s
   t | S z2 check that the rev_map and the id_map are in sync)r   ZIndexIDMap2_check_consistencyr@   r   r   r   check_consistencys*  s    zIndexIDMap2.check_consistencyr   c                 C   s   t | ||S r#   )r   ZIndexIDMap2_merge_fromr  r   r   r   r  w*  s    zIndexIDMap2.merge_fromc                 G   s   t | t j|  d S r#   )r   ZIndexIDMap2_swiginitZnew_IndexIDMap2rQ   r   r   r   r?   {*  s    zIndexIDMap2.__init__N)r   )r   r   r,   r-   r   r   r   r   r   ZIndexIDMap2_rev_map_getZIndexIDMap2_rev_map_setrev_maprb  rr  r{  r  rd  r  Zdelete_IndexIDMap2rT   r?   r   r   r   r   r_  \*  s   
r_  c                   @   sx   e Zd ZdZedd dd ddZeZeej	ej
Zdd Zd	d
 Zdd Zdd Zdd ZdddZejZdd ZdS )IndexBinaryIDMap2r`  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   *  r4   zIndexBinaryIDMap2.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   *  r4   r7   r8   c                 C   s
   t | S ra  )r   Z#IndexBinaryIDMap2_construct_rev_mapr@   r   r   r   rb  *  s    z#IndexBinaryIDMap2.construct_rev_mapc                 C   s   t | |||S r#   )r   ZIndexBinaryIDMap2_add_with_idsrp  r   r   r   rr  *  s    zIndexBinaryIDMap2.add_with_idsc                 C   s   t | |S r#   )r   ZIndexBinaryIDMap2_remove_idsrz  r   r   r   r{  *  s    zIndexBinaryIDMap2.remove_idsc                 C   s   t | ||S r#   )r   ZIndexBinaryIDMap2_reconstructr|  r   r   r   r  *  s    zIndexBinaryIDMap2.reconstructc                 C   s
   t | S rc  )r   Z#IndexBinaryIDMap2_check_consistencyr@   r   r   r   rd  *  s    z#IndexBinaryIDMap2.check_consistencyr   c                 C   s   t | ||S r#   )r   ZIndexBinaryIDMap2_merge_fromr  r   r   r   r  *  s    zIndexBinaryIDMap2.merge_fromc                 G   s   t | t j|  d S r#   )r   ZIndexBinaryIDMap2_swiginitZnew_IndexBinaryIDMap2rQ   r   r   r   r?   *  s    zIndexBinaryIDMap2.__init__N)r   )r   r   r,   r-   r   r   r   r   r   ZIndexBinaryIDMap2_rev_map_getZIndexBinaryIDMap2_rev_map_setre  rb  rr  r{  r  rd  r  Zdelete_IndexBinaryIDMap2rT   r?   r   r   r   r   rf  *  s   
rf  c                 C   s
   t | S r#   )r   downcast_indexr  r   r   r   rg  *  s    rg  c                 C   s
   t | S r#   )r   downcast_VectorTransform)r  r   r   r   ri  *  s    ri  c                 C   s
   t | S r#   )r   downcast_IndexBinaryrh  r   r   r   rj  *  s    rj  c                 C   s
   t | S r#   )r   downcast_InvertedLists)r-  r   r   r   rk  *  s    rk  c                 C   s
   t | S r#   )r   downcast_AdditiveQuantizerr  r   r   r   rl  *  s    rl  c                 C   s
   t | S r#   )r   downcast_Quantizerrm  r   r   r   rn  *  s    rn  c                  G   s
   t j|  S r#   )r   write_indexr   r   r   r   ro  *  s    ro  c                  G   s
   t j|  S r#   )r   write_index_binaryr   r   r   r   rp  *  s    rp  c                  G   s
   t j|  S r#   )r   
read_indexr   r   r   r   rq  *  s    rq  c                  G   s
   t j|  S r#   )r   read_index_binaryr   r   r   r   rr  *  s    rr  c                  G   s
   t j|  S r#   )r   write_VectorTransformr   r   r   r   rs  *  s    rs  c                  G   s
   t j|  S r#   )r   read_VectorTransformr   r   r   r   rt  *  s    rt  c                  G   s
   t j|  S r#   )r   read_ProductQuantizerr   r   r   r   ru  *  s    ru  c                  G   s
   t j|  S r#   )r   write_ProductQuantizerr   r   r   r   rv  *  s    rv  c                 C   s   t | |S r#   )r   write_InvertedLists)r+  r  r   r   r   rw  *  s    rw  c                 C   s   t | |S r#   )r   read_InvertedLists)r  r6  r   r   r   rx  *  s    rx  c                 C   s
   t | S r#   )r   clone_indexr9  r   r   r   ry  *  s    ry  c                   @   sP   e Zd ZdZedd dd ddZeZdd Zd	d
 Z	dd Z
ejZdd ZdS )Clonerz
     Cloner class, useful to override classes with other cloning
    functions. The cloning function above just calls
    Cloner::clone_Index.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   *  r4   zCloner.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   *  r4   r7   r8   c                 C   s   t | |S r#   )r   ZCloner_clone_VectorTransformrV   r   r   r   clone_VectorTransform*  s    zCloner.clone_VectorTransformc                 C   s   t | |S r#   )r   ZCloner_clone_IndexrV   r   r   r   clone_Index*  s    zCloner.clone_Indexc                 C   s   t | |S r#   )r   ZCloner_clone_IndexIVFrV   r   r   r   clone_IndexIVF*  s    zCloner.clone_IndexIVFc                 C   s   t | t   d S r#   )r   ZCloner_swiginitZ
new_Clonerr@   r   r   r   r?   *  s    zCloner.__init__N)r   r   r,   r-   r   r   r   r   r{  r|  r}  r   Zdelete_ClonerrT   r?   r   r   r   r   rz  *  s   rz  c                 C   s
   t | S r#   )r   clone_Quantizer)Zquantr   r   r   r~  *  s    r~  c                 C   s
   t | S r#   )r   clone_binary_indexrh  r   r   r   r  +  s    r  c                   @   s   e Zd ZdZedd dd ddZdd ZeZee	j
e	jd	dZee	je	jd
dZee	je	jddZee	je	jddZee	je	jddZdd Zdd Ze	jZdS )AutoTuneCriterionz]
    Evaluation criterion. Returns a performance measure in [0,1],
    higher is better.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   	+  r4   zAutoTuneCriterion.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   	+  r4   r7   r8   c                 O   s   t dd S r:   r;   r<   r   r   r   r?   +  s    zAutoTuneCriterion.__init__z- nb of queries this criterion is evaluated onz( nb of NNs that the query should requestz, nb of GT NNs required to evaluate criterionz* Ground-truth distances (size nq * gt_nnn)z( Ground-truth indexes (size nq * gt_nnn)c                 C   s   t | |||S )z
         Intitializes the gt_D and gt_I vectors. Must be called before evaluating

        :type gt_D_in: float
        :param gt_D_in:  size nq * gt_nnn
        :type gt_I_in: int
        :param gt_I_in:  size nq * gt_nnn
        )r   Z!AutoTuneCriterion_set_groundtruth)r   gt_nnnZgt_D_inZgt_I_inr   r   r   set_groundtruth+  s    	z!AutoTuneCriterion.set_groundtruthc                 C   s   t | ||S )z
         Evaluate the criterion.

        :type D: float
        :param D:  size nq * nnn
        :type I: int
        :param I:  size nq * nnn
        :rtype: float
        :return: the criterion, between 0 and 1. Larger is better.
        )r   ZAutoTuneCriterion_evaluater   r   r   r   r   r   rK  +  s    zAutoTuneCriterion.evaluateN)r   r   r,   r-   r   r   r?   r   r   r   ZAutoTuneCriterion_nq_getZAutoTuneCriterion_nq_setr   ZAutoTuneCriterion_nnn_getZAutoTuneCriterion_nnn_setZnnnZAutoTuneCriterion_gt_nnn_getZAutoTuneCriterion_gt_nnn_setr  ZAutoTuneCriterion_gt_D_getZAutoTuneCriterion_gt_D_setZgt_DZAutoTuneCriterion_gt_I_getZAutoTuneCriterion_gt_I_setZgt_Ir  rK  Zdelete_AutoTuneCriterionrT   r   r   r   r   r  +  s   r  c                   @   sJ   e Zd Zedd dd ddZeZeejej	Z
dd Zdd	 ZejZd
S )OneRecallAtRCriterionc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   0+  r4   zOneRecallAtRCriterion.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   0+  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZOneRecallAtRCriterion_swiginitZnew_OneRecallAtRCriterionr   r   r  r   r   r   r?   4+  s    zOneRecallAtRCriterion.__init__c                 C   s   t | ||S r#   )r   ZOneRecallAtRCriterion_evaluater  r   r   r   rK  7+  s    zOneRecallAtRCriterion.evaluateN)r   r   r,   r   r   r   r   r   ZOneRecallAtRCriterion_R_getZOneRecallAtRCriterion_R_setr  r?   rK  Zdelete_OneRecallAtRCriterionrT   r   r   r   r   r  /+  s   r  c                   @   sJ   e Zd Zedd dd ddZeZeejej	Z
dd Zdd	 ZejZd
S )IntersectionCriterionc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   >+  r4   zIntersectionCriterion.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   >+  r4   r7   r8   c                 C   s   t | t || d S r#   )r   ZIntersectionCriterion_swiginitZnew_IntersectionCriterionr  r   r   r   r?   B+  s    zIntersectionCriterion.__init__c                 C   s   t | ||S r#   )r   ZIntersectionCriterion_evaluater  r   r   r   rK  E+  s    zIntersectionCriterion.evaluateN)r   r   r,   r   r   r   r   r   ZIntersectionCriterion_R_getZIntersectionCriterion_R_setr  r?   rK  Zdelete_IntersectionCriterionrT   r   r   r   r   r  =+  s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZdd ZejZdS )OperatingPointa<  
    Maintains a list of experimental results. Each operating point is a
    (perf, t, key) triplet, where higher perf and lower t is
    better. The key field is an arbitrary identifier for the operating point.

    Includes primitives to extract the Pareto-optimal operating points in the
    (perf, t) space.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   U+  r4   zOperatingPoint.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   U+  r4   r7   r8   z, performance measure (output of a Criterion)z" corresponding execution time (ms)z key that identifies this op ptz integer identifierc                 C   s   t | t   d S r#   )r   ZOperatingPoint_swiginitZnew_OperatingPointr@   r   r   r   r?   \+  s    zOperatingPoint.__init__N)r   r   r,   r-   r   r   r   r   r   ZOperatingPoint_perf_getZOperatingPoint_perf_setperfZOperatingPoint_t_getZOperatingPoint_t_setrZ  ZOperatingPoint_key_getZOperatingPoint_key_setr}  ZOperatingPoint_cno_getZOperatingPoint_cno_setcnor?   Zdelete_OperatingPointrT   r   r   r   r   r  K+  s   	r  c                   @   s   e Zd Zedd dd ddZeZeejej	ddZ
eejejddZdd	 Zd
d Zdd ZdddZdd ZdddZdd Zdd ZejZdS )OperatingPointsc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   c+  r4   zOperatingPoints.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   c+  r4   r7   r8   z all operating pointsz) optimal operating points, sorted by perfc                 C   s   t | t   d S r#   )r   ZOperatingPoints_swiginitZnew_OperatingPointsr@   r   r   r   r?   h+  s    zOperatingPoints.__init__c                 G   s   t j| g|R  S )zC add operating points from other to this, with a prefix to the keys)r   ZOperatingPoints_merge_withrQ   r   r   r   
merge_withk+  s    zOperatingPoints.merge_withc                 C   s
   t | S r#   )r   ZOperatingPoints_clearr@   r   r   r   rY   o+  s    zOperatingPoints.clearr   c                 C   s   t | ||||S )zA add a performance measure. Return whether it is an optimal point)r   ZOperatingPoints_add)r   r  rZ  r}  r  r   r   r   ro  r+  s    zOperatingPoints.addc                 C   s   t | |S )z8 get time required to obtain a given performance measure)r   ZOperatingPoints_t_for_perf)r   r  r   r   r   
t_for_perfv+  s    zOperatingPoints.t_for_perfTc                 C   s   t | |S )z easy-to-read output)r   ZOperatingPoints_display)r   Zonly_optimalr   r   r   r  z+  s    zOperatingPoints.displayc                 C   s   t | |S )z- output to a format easy to digest by gnuplot)r   ZOperatingPoints_all_to_gnuplotr   fnamer   r   r   all_to_gnuplot~+  s    zOperatingPoints.all_to_gnuplotc                 C   s   t | |S r#   )r   Z"OperatingPoints_optimal_to_gnuplotr  r   r   r   optimal_to_gnuplot+  s    z"OperatingPoints.optimal_to_gnuplotN)r   )T)r   r   r,   r   r   r   r   r   ZOperatingPoints_all_pts_getZOperatingPoints_all_pts_setZall_ptsZOperatingPoints_optimal_pts_getZOperatingPoints_optimal_pts_setZoptimal_ptsr?   r  rY   ro  r  r  r  r  Zdelete_OperatingPointsrT   r   r   r   r   r  b+  s   

r  c                   @   sT   e Zd ZdZedd dd ddZeZeej	ej
ZeejejZdd ZejZd	S )
ParameterRangezM possible values of a parameter, sorted from least to most expensive/accuratec                 C   s
   | j  S r#   r0   r1   r   r   r   r3   +  r4   zParameterRange.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   +  r4   r7   r8   c                 C   s   t | t   d S r#   )r   ZParameterRange_swiginitZnew_ParameterRanger@   r   r   r   r?   +  s    zParameterRange.__init__N)r   r   r,   r-   r   r   r   r   r   ZParameterRange_name_getZParameterRange_name_setr   ZParameterRange_values_getZParameterRange_values_setvaluesr?   Zdelete_ParameterRangerT   r   r   r   r   r  +  s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZeejejddZeejejddZdd Zdd Zdd Zdd Zdd Zdd Z dd Z!dd Z"dd Z#dd  Z$d!d" Z%ej&Z'd#S )$ParameterSpacezL Uses a-priori knowledge on the Faiss indexes to extract tunable parameters.c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   +  r4   zParameterSpace.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   +  r4   r7   r8   z all tunable parametersz verbosity during explorationzA nb of experiments during optimization (0 = try all combinations)z/ maximum number of queries to submit at a time.z_
    use multithreading over batches (useful to benchmark
    independent single-searches)
    zm
    run tests several times until they reach at least this
    duration (to avoid jittering in MT mode)
    c                 C   s   t | t   d S r#   )r   ZParameterSpace_swiginitZnew_ParameterSpacer@   r   r   r   r?   +  s    zParameterSpace.__init__c                 C   s
   t | S )z. nb of combinations, = product of values sizes)r   ZParameterSpace_n_combinationsr@   r   r   r   n_combinations+  s    zParameterSpace.n_combinationsc                 C   s   t | ||S )z9 returns whether combinations c1 >= c2 in the tuple sense)r   ZParameterSpace_combination_ge)r   c1c2r   r   r   combination_ge+  s    zParameterSpace.combination_gec                 C   s   t | |S )z- get string representation of the combination)r   ZParameterSpace_combination_name)r   r  r   r   r   combination_name+  s    zParameterSpace.combination_namec                 C   s
   t | S )z print a description on stdout)r   ZParameterSpace_displayr@   r   r   r   r  +  s    zParameterSpace.displayc                 C   s   t | |S )z0 add a new parameter (or return it if it exists))r   ZParameterSpace_add_rangerX  r   r   r   	add_range+  s    zParameterSpace.add_rangec                 C   s   t | |S )z4 initialize with reasonable parameters for the index)r   ZParameterSpace_initializer  r   r   r   
initialize+  s    zParameterSpace.initializec                 G   s   t j| g|R  S )z
        *Overload 1:*
        set a combination of parameters on an index

        |

        *Overload 2:*
        set a combination of parameters described by a string
        )r   Z#ParameterSpace_set_index_parametersrQ   r   r   r   set_index_parameters+  s    
z#ParameterSpace.set_index_parametersc                 C   s   t | |||S )zB set one of the parameters, returns whether setting was successful)r   Z"ParameterSpace_set_index_parameter)r   r  r   r]  r   r   r   set_index_parameter+  s    z"ParameterSpace.set_index_parameterc                 C   s   t | ||||S )z
         find an upper bound on the performance and a lower bound on t
        for configuration cno given another operating point op
        )r   ZParameterSpace_update_bounds)r   r  opZupper_bound_perfZlower_bound_tr   r   r   update_bounds+  s    zParameterSpace.update_boundsc                 C   s   t | |||||S )a  
         explore operating points
        :type index: :py:class:`Index`
        :param index:   index to run on
        :type xq: float
        :param xq:      query vectors (size nq * index.d)
        :type crit: :py:class:`AutoTuneCriterion`
        :param crit:    selection criterion
        :type ops: :py:class:`OperatingPoints`
        :param ops:     resulting operating points
        )r   ZParameterSpace_explore)r   r  r   r  critopsr   r   r   explore+  s    zParameterSpace.exploreN)(r   r   r,   r-   r   r   r   r   r   Z#ParameterSpace_parameter_ranges_getZ#ParameterSpace_parameter_ranges_setZparameter_rangesZParameterSpace_verbose_getZParameterSpace_verbose_setr   Z ParameterSpace_n_experiments_getZ ParameterSpace_n_experiments_setZn_experimentsZParameterSpace_batchsize_getZParameterSpace_batchsize_setZ	batchsizeZ&ParameterSpace_thread_over_batches_getZ&ParameterSpace_thread_over_batches_setZthread_over_batchesZ$ParameterSpace_min_test_duration_getZ$ParameterSpace_min_test_duration_setZmin_test_durationr?   r  r  r  r  r  r  r  r  r  r  Zdelete_ParameterSpacerT   r   r   r   r   r  +  s*   r  c                  G   s
   t j|  S )z\
    Build and index with the sequence of processing steps described in
    the string.
    )r   index_factoryr   r   r   r   r  +  s    r  c                 C   s   t | |S r#   )r   index_binary_factory)r   descriptionr   r   r   r  +  s    r  c                   @   s   e Zd ZdZedd dd ddZeZdd Zee	j
e	jZee	je	jZee	je	jZee	je	jZee	je	jZee	je	jZee	je	jZee	je	j Z!ee	j"e	j#Z$ee	j%e	j&Z'ee	j(e	j)Z*ee	j+e	j,Z-ee	j.e	j/Z0d	d
 Z1e	j2Z3dS )MatrixStatsz
     Reports some statistics on a dataset and comments on them.

    It is a class rather than a function so that all stats can also be
    accessed from code
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   +  r4   zMatrixStats.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   +  r4   r7   r8   c                 C   s   t | t ||| d S r#   )r   ZMatrixStats_swiginitZnew_MatrixStats)r   rB   r   r2   r   r   r   r?    ,  s    zMatrixStats.__init__c                 C   s   t | |S r#   )r   ZMatrixStats_do_comment)r   fmtr   r   r   
do_comment,  s    zMatrixStats.do_commentN)4r   r   r,   r-   r   r   r   r   r?   r   ZMatrixStats_comments_getZMatrixStats_comments_setcommentsZMatrixStats_n_getZMatrixStats_n_setrB   ZMatrixStats_d_getZMatrixStats_d_setr   ZMatrixStats_n_collision_getZMatrixStats_n_collision_setZn_collisionZMatrixStats_n_valid_getZMatrixStats_n_valid_setZn_validZMatrixStats_n0_getZMatrixStats_n0_setr  ZMatrixStats_min_norm2_getZMatrixStats_min_norm2_setZ	min_norm2ZMatrixStats_max_norm2_getZMatrixStats_max_norm2_setZ	max_norm2ZMatrixStats_hash_value_getZMatrixStats_hash_value_set
hash_valueZMatrixStats_per_dim_stats_getZMatrixStats_per_dim_stats_setZper_dim_statsZMatrixStats_occurrences_getZMatrixStats_occurrences_setoccurrencesZMatrixStats_buf_getZMatrixStats_buf_setbufZMatrixStats_nbuf_getZMatrixStats_nbuf_setZnbufr  Zdelete_MatrixStatsrT   r   r   r   r   r  +  s&   r  c                   @   sX   e Zd Zedd dd ddZeZeejej	Z
eejejZdd Zdd	 ZejZd
S )PyCallbackIOWriterc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ,  r4   zPyCallbackIOWriter.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ,  r4   r7   r8   c                 G   s   t | t j|  dS )z
        Callback: Python function that takes a bytes object and
        returns the number of bytes successfully written.
        N)r   ZPyCallbackIOWriter_swiginitZnew_PyCallbackIOWriterrQ   r   r   r   r?   ,  s    zPyCallbackIOWriter.__init__c                 C   s   t | |||S r#   )r   ZPyCallbackIOWriter___call__r   Zptrvr[   r  r   r   r   r  #,  s    zPyCallbackIOWriter.__call__N)r   r   r,   r   r   r   r   r   ZPyCallbackIOWriter_callback_getZPyCallbackIOWriter_callback_setcallbackZPyCallbackIOWriter_bs_getZPyCallbackIOWriter_bs_setbsr?   r  Zdelete_PyCallbackIOWriterrT   r   r   r   r   r  ,  s   r  c                   @   sX   e Zd Zedd dd ddZeZeejej	Z
eejejZdd Zdd	 ZejZd
S )PyCallbackIOReaderc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   *,  r4   zPyCallbackIOReader.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   *,  r4   r7   r8   c                 G   s   t | t j|  dS )zy
         Callback: Python function that takes a size and returns a
        bytes object with the resulting read
        N)r   ZPyCallbackIOReader_swiginitZnew_PyCallbackIOReaderrQ   r   r   r   r?   /,  s    zPyCallbackIOReader.__init__c                 C   s   t | |||S r#   )r   ZPyCallbackIOReader___call__r  r   r   r   r  6,  s    zPyCallbackIOReader.__call__N)r   r   r,   r   r   r   r   r   ZPyCallbackIOReader_callback_getZPyCallbackIOReader_callback_setr  ZPyCallbackIOReader_bs_getZPyCallbackIOReader_bs_setr  r?   r  Zdelete_PyCallbackIOReaderrT   r   r   r   r   r  ),  s   r  c                   @   sJ   e Zd Zedd dd ddZeZeejej	Z
dd Zdd	 ZejZd
S )PyCallbackIDSelectorc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   =,  r4   zPyCallbackIDSelector.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   =,  r4   r7   r8   c                 C   s   t | t | d S r#   )r   ZPyCallbackIDSelector_swiginitZnew_PyCallbackIDSelector)r   r  r   r   r   r?   A,  s    zPyCallbackIDSelector.__init__c                 C   s   t | |S r#   )r   ZPyCallbackIDSelector_is_memberrM  r   r   r   rE  D,  s    zPyCallbackIDSelector.is_memberN)r   r   r,   r   r   r   r   r   Z!PyCallbackIDSelector_callback_getZ!PyCallbackIDSelector_callback_setr  r?   rE  Zdelete_PyCallbackIDSelectorrT   r   r   r   r   r  <,  s   r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZdd Zdd Zdd Zd ddZd!ddZd"ddZdd Zdd Zdd ZejZdS )#float_minheap_array_t
     a template structure for a set of [min|max]-heaps it is tailored
    so that the actual data of the heaps can just live in compact
    arrays.
    c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   Q,  r4   zfloat_minheap_array_t.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   Q,  r4   r7   r8    number of heaps allocated size per heap identifiers (size nh * k)0 values (distances or similarities), size nh * kc                 C   s   t | |S z% Return the list of values for a heap)r   Zfloat_minheap_array_t_get_valr   r}  r   r   r   get_valX,  s    zfloat_minheap_array_t.get_valc                 C   s   t | |S z Correspponding identifiers)r   Zfloat_minheap_array_t_get_idsr  r   r   r   r  \,  s    zfloat_minheap_array_t.get_idsc                 C   s
   t | S z$ prepare all the heaps before adding)r   Zfloat_minheap_array_t_heapifyr@   r   r   r   heapify`,  s    zfloat_minheap_array_t.heapifyr   r
  c                 C   s   t | |||||S a  
         add nj elements to heaps i0:i0+ni, with sequential ids

        :type nj: int
        :param nj:    nb of elements to add to each heap
        :type vin: float
        :param vin:   elements to add, size ni * nj
        :type j0: int, optional
        :param j0:    add this to the ids that are added
        :type i0: int, optional
        :param i0:    first heap to update
        :type ni: int, optional
        :param ni:    nb of elements to update (-1 = use nh)
        )r   Zfloat_minheap_array_t_addnr   njvinr  r  r  r   r   r   addnd,  s    zfloat_minheap_array_t.addnNc              	   C   s   t | ||||||S z
         same as addn

        :type id_in: int, optional
        :param id_in:     ids of the elements to add, size ni * nj
        :type id_stride: int, optional
        :param id_stride: stride for id_in
        )r   Z#float_minheap_array_t_addn_with_idsr   r  r  id_in	id_strider  r  r   r   r   addn_with_idsu,  s    	z#float_minheap_array_t.addn_with_idsc              	   C   s   t | ||||||S a  
         same as addn_with_ids, but for just a subset of queries

        :type nsubset: int
        :param nsubset:  number of query entries to update
        :type subset: int
        :param subset:   indexes of queries to update, in 0..nh-1, size nsubset
        )r   Z0float_minheap_array_t_addn_query_subset_with_idsr   r0  r/  r  r  r  r  r   r   r   addn_query_subset_with_ids,  s    	z0float_minheap_array_t.addn_query_subset_with_idsc                 C   s
   t | S z reorder all the heaps)r   Zfloat_minheap_array_t_reorderr@   r   r   r   reorder,  s    zfloat_minheap_array_t.reorderc                 C   s   t | ||S aC  
         this is not really a heap function. It just finds the per-line
          extrema of each line of array D
        :type vals_out: float
        :param vals_out:    extreme value of each line (size nh, or NULL)
        :type idx_out: int
        :param idx_out:     index of extreme value (size nh or NULL)
        )r   Z&float_minheap_array_t_per_line_extremar   Zvals_outZidx_outr   r   r   per_line_extrema,  s    	z&float_minheap_array_t.per_line_extremac                 C   s   t | t   d S r#   )r   Zfloat_minheap_array_t_swiginitZnew_float_minheap_array_tr@   r   r   r   r?   ,  s    zfloat_minheap_array_t.__init__)r   r   r
  )Nr   r   r
  )Nr   ) r   r   r,   r-   r   r   r   r   r   Zfloat_minheap_array_t_nh_getZfloat_minheap_array_t_nh_setnhZfloat_minheap_array_t_k_getZfloat_minheap_array_t_k_setr   Zfloat_minheap_array_t_ids_getZfloat_minheap_array_t_ids_setr'  Zfloat_minheap_array_t_val_getZfloat_minheap_array_t_val_setr]  r  r  r  r  r  r  r  r  r?   Zdelete_float_minheap_array_trT   r   r   r   r   r  J,  s"   


r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZdd Zdd Zdd Zd ddZd!ddZd"ddZdd Zdd Zdd ZejZdS )#int_minheap_array_tr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ,  r4   zint_minheap_array_t.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ,  r4   r7   r8   r  r  r  r  c                 C   s   t | |S r  )r   Zint_minheap_array_t_get_valr  r   r   r   r  ,  s    zint_minheap_array_t.get_valc                 C   s   t | |S r  )r   Zint_minheap_array_t_get_idsr  r   r   r   r  ,  s    zint_minheap_array_t.get_idsc                 C   s
   t | S r  )r   Zint_minheap_array_t_heapifyr@   r   r   r   r  ,  s    zint_minheap_array_t.heapifyr   r
  c                 C   s   t | |||||S a  
         add nj elements to heaps i0:i0+ni, with sequential ids

        :type nj: int
        :param nj:    nb of elements to add to each heap
        :type vin: int
        :param vin:   elements to add, size ni * nj
        :type j0: int, optional
        :param j0:    add this to the ids that are added
        :type i0: int, optional
        :param i0:    first heap to update
        :type ni: int, optional
        :param ni:    nb of elements to update (-1 = use nh)
        )r   Zint_minheap_array_t_addnr  r   r   r   r  ,  s    zint_minheap_array_t.addnNc              	   C   s   t | ||||||S r  )r   Z!int_minheap_array_t_addn_with_idsr  r   r   r   r  ,  s    	z!int_minheap_array_t.addn_with_idsc              	   C   s   t | ||||||S r  )r   Z.int_minheap_array_t_addn_query_subset_with_idsr  r   r   r   r  ,  s    	z.int_minheap_array_t.addn_query_subset_with_idsc                 C   s
   t | S r  )r   Zint_minheap_array_t_reorderr@   r   r   r   r  ,  s    zint_minheap_array_t.reorderc                 C   s   t | ||S aA  
         this is not really a heap function. It just finds the per-line
          extrema of each line of array D
        :type vals_out: int
        :param vals_out:    extreme value of each line (size nh, or NULL)
        :type idx_out: int
        :param idx_out:     index of extreme value (size nh or NULL)
        )r   Z$int_minheap_array_t_per_line_extremar  r   r   r   r  ,  s    	z$int_minheap_array_t.per_line_extremac                 C   s   t | t   d S r#   )r   Zint_minheap_array_t_swiginitZnew_int_minheap_array_tr@   r   r   r   r?   ,  s    zint_minheap_array_t.__init__)r   r   r
  )Nr   r   r
  )Nr   ) r   r   r,   r-   r   r   r   r   r   Zint_minheap_array_t_nh_getZint_minheap_array_t_nh_setr  Zint_minheap_array_t_k_getZint_minheap_array_t_k_setr   Zint_minheap_array_t_ids_getZint_minheap_array_t_ids_setr'  Zint_minheap_array_t_val_getZint_minheap_array_t_val_setr]  r  r  r  r  r  r  r  r  r?   Zdelete_int_minheap_array_trT   r   r   r   r   r  ,  s"   


r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZdd Zdd Zdd Zd ddZd!ddZd"ddZdd Zdd Zdd ZejZdS )#float_maxheap_array_tr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   ,  r4   zfloat_maxheap_array_t.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   ,  r4   r7   r8   r  r  r  r  c                 C   s   t | |S r  )r   Zfloat_maxheap_array_t_get_valr  r   r   r   r  -  s    zfloat_maxheap_array_t.get_valc                 C   s   t | |S r  )r   Zfloat_maxheap_array_t_get_idsr  r   r   r   r  -  s    zfloat_maxheap_array_t.get_idsc                 C   s
   t | S r  )r   Zfloat_maxheap_array_t_heapifyr@   r   r   r   r  -  s    zfloat_maxheap_array_t.heapifyr   r
  c                 C   s   t | |||||S r  )r   Zfloat_maxheap_array_t_addnr  r   r   r   r  -  s    zfloat_maxheap_array_t.addnNc              	   C   s   t | ||||||S r  )r   Z#float_maxheap_array_t_addn_with_idsr  r   r   r   r  !-  s    	z#float_maxheap_array_t.addn_with_idsc              	   C   s   t | ||||||S r  )r   Z0float_maxheap_array_t_addn_query_subset_with_idsr  r   r   r   r  ,-  s    	z0float_maxheap_array_t.addn_query_subset_with_idsc                 C   s
   t | S r  )r   Zfloat_maxheap_array_t_reorderr@   r   r   r   r  7-  s    zfloat_maxheap_array_t.reorderc                 C   s   t | ||S r  )r   Z&float_maxheap_array_t_per_line_extremar  r   r   r   r  ;-  s    	z&float_maxheap_array_t.per_line_extremac                 C   s   t | t   d S r#   )r   Zfloat_maxheap_array_t_swiginitZnew_float_maxheap_array_tr@   r   r   r   r?   F-  s    zfloat_maxheap_array_t.__init__)r   r   r
  )Nr   r   r
  )Nr   ) r   r   r,   r-   r   r   r   r   r   Zfloat_maxheap_array_t_nh_getZfloat_maxheap_array_t_nh_setr  Zfloat_maxheap_array_t_k_getZfloat_maxheap_array_t_k_setr   Zfloat_maxheap_array_t_ids_getZfloat_maxheap_array_t_ids_setr'  Zfloat_maxheap_array_t_val_getZfloat_maxheap_array_t_val_setr]  r  r  r  r  r  r  r  r  r?   Zdelete_float_maxheap_array_trT   r   r   r   r   r  ,  s"   


r  c                   @   s   e Zd ZdZedd dd ddZeZeej	ej
ddZeejejddZeejejd	dZeejejd
dZdd Zdd Zdd Zd ddZd!ddZd"ddZdd Zdd Zdd ZejZdS )#int_maxheap_array_tr  c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   S-  r4   zint_maxheap_array_t.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   S-  r4   r7   r8   r  r  r  r  c                 C   s   t | |S r  )r   Zint_maxheap_array_t_get_valr  r   r   r   r  Z-  s    zint_maxheap_array_t.get_valc                 C   s   t | |S r  )r   Zint_maxheap_array_t_get_idsr  r   r   r   r  ^-  s    zint_maxheap_array_t.get_idsc                 C   s
   t | S r  )r   Zint_maxheap_array_t_heapifyr@   r   r   r   r  b-  s    zint_maxheap_array_t.heapifyr   r
  c                 C   s   t | |||||S r  )r   Zint_maxheap_array_t_addnr  r   r   r   r  f-  s    zint_maxheap_array_t.addnNc              	   C   s   t | ||||||S r  )r   Z!int_maxheap_array_t_addn_with_idsr  r   r   r   r  w-  s    	z!int_maxheap_array_t.addn_with_idsc              	   C   s   t | ||||||S r  )r   Z.int_maxheap_array_t_addn_query_subset_with_idsr  r   r   r   r  -  s    	z.int_maxheap_array_t.addn_query_subset_with_idsc                 C   s
   t | S r  )r   Zint_maxheap_array_t_reorderr@   r   r   r   r  -  s    zint_maxheap_array_t.reorderc                 C   s   t | ||S r  )r   Z$int_maxheap_array_t_per_line_extremar  r   r   r   r  -  s    	z$int_maxheap_array_t.per_line_extremac                 C   s   t | t   d S r#   )r   Zint_maxheap_array_t_swiginitZnew_int_maxheap_array_tr@   r   r   r   r?   -  s    zint_maxheap_array_t.__init__)r   r   r
  )Nr   r   r
  )Nr   ) r   r   r,   r-   r   r   r   r   r   Zint_maxheap_array_t_nh_getZint_maxheap_array_t_nh_setr  Zint_maxheap_array_t_k_getZint_maxheap_array_t_k_setr   Zint_maxheap_array_t_ids_getZint_maxheap_array_t_ids_setr'  Zint_maxheap_array_t_val_getZint_maxheap_array_t_val_setr]  r  r  r  r  r  r  r  r  r?   Zdelete_int_maxheap_array_trT   r   r   r   r   r  L-  s"   


r  c                 C   s   t | |||||S z
     partitions the table into 0:q and q:n where all elements above q are >= all
    elements below q (for C = CMax, for CMin comparisons are reversed)

    Returns the partition threshold. The elements q:n are destroyed on output.
    )r   CMin_float_partition_fuzzyr1  r'  rB   Zq_minZq_maxZq_outr   r   r   r  -  s    r  c                 C   s   t | |||||S r  )r   CMax_float_partition_fuzzyr  r   r   r   r  -  s    r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZedd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd ZejZdS )AlignedTableUint8c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   -  r4   zAlignedTableUint8.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   -  r4   r7   r8   c                 C   s
   t | S r#   )r   Z AlignedTableUint8_round_capacityrB   r   r   r   round_capacity-  s    z AlignedTableUint8.round_capacityc                 G   s   t | t j|  d S r#   )r   ZAlignedTableUint8_swiginitZnew_AlignedTableUint8rQ   r   r   r   r?   -  s    zAlignedTableUint8.__init__c                 C   s
   t | S r#   )r   ZAlignedTableUint8_itemsizer@   r   r   r   itemsize-  s    zAlignedTableUint8.itemsizec                 C   s   t | |S r#   )r   ZAlignedTableUint8_resizerA   r   r   r   r]   -  s    zAlignedTableUint8.resizec                 C   s
   t | S r#   )r   ZAlignedTableUint8_clearr@   r   r   r   rY   -  s    zAlignedTableUint8.clearc                 C   s
   t | S r#   )r   ZAlignedTableUint8_sizer@   r   r   r   r[   -  s    zAlignedTableUint8.sizec                 C   s
   t | S r#   )r   ZAlignedTableUint8_nbytesr@   r   r   r   nbytes-  s    zAlignedTableUint8.nbytesc                 G   s   t j| g|R  S r#   )r   ZAlignedTableUint8_getrQ   r   r   r   rV  -  s    zAlignedTableUint8.getc                 G   s   t j| g|R  S r#   )r   ZAlignedTableUint8_datarQ   r   r   r   rZ   -  s    zAlignedTableUint8.dataN)r   r   r,   r   r   r   r   r   ZAlignedTableUint8_tab_getZAlignedTableUint8_tab_setr`  ZAlignedTableUint8_numel_getZAlignedTableUint8_numel_setr   r  r  r?   r  r]   rY   r[   r  rV  rZ   Zdelete_AlignedTableUint8rT   r   r   r   r   r  -  s   
r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZedd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd ZejZdS )AlignedTableUint16c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   -  r4   zAlignedTableUint16.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   -  r4   r7   r8   c                 C   s
   t | S r#   )r   Z!AlignedTableUint16_round_capacityr  r   r   r   r  -  s    z!AlignedTableUint16.round_capacityc                 G   s   t | t j|  d S r#   )r   ZAlignedTableUint16_swiginitZnew_AlignedTableUint16rQ   r   r   r   r?   -  s    zAlignedTableUint16.__init__c                 C   s
   t | S r#   )r   ZAlignedTableUint16_itemsizer@   r   r   r   r  -  s    zAlignedTableUint16.itemsizec                 C   s   t | |S r#   )r   ZAlignedTableUint16_resizerA   r   r   r   r]   -  s    zAlignedTableUint16.resizec                 C   s
   t | S r#   )r   ZAlignedTableUint16_clearr@   r   r   r   rY   -  s    zAlignedTableUint16.clearc                 C   s
   t | S r#   )r   ZAlignedTableUint16_sizer@   r   r   r   r[   -  s    zAlignedTableUint16.sizec                 C   s
   t | S r#   )r   ZAlignedTableUint16_nbytesr@   r   r   r   r  -  s    zAlignedTableUint16.nbytesc                 G   s   t j| g|R  S r#   )r   ZAlignedTableUint16_getrQ   r   r   r   rV  -  s    zAlignedTableUint16.getc                 G   s   t j| g|R  S r#   )r   ZAlignedTableUint16_datarQ   r   r   r   rZ   -  s    zAlignedTableUint16.dataN)r   r   r,   r   r   r   r   r   ZAlignedTableUint16_tab_getZAlignedTableUint16_tab_setr`  ZAlignedTableUint16_numel_getZAlignedTableUint16_numel_setr   r  r  r?   r  r]   rY   r[   r  rV  rZ   Zdelete_AlignedTableUint16rT   r   r   r   r   r  -  s   
r  c                   @   s   e Zd Zedd dd ddZeZeejej	Z
eejejZedd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd ZejZdS )AlignedTableFloat32c                 C   s
   | j  S r#   r0   r1   r   r   r   r3   -  r4   zAlignedTableFloat32.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   -  r4   r7   r8   c                 C   s
   t | S r#   )r   Z"AlignedTableFloat32_round_capacityr  r   r   r   r  .  s    z"AlignedTableFloat32.round_capacityc                 G   s   t | t j|  d S r#   )r   ZAlignedTableFloat32_swiginitZnew_AlignedTableFloat32rQ   r   r   r   r?   .  s    zAlignedTableFloat32.__init__c                 C   s
   t | S r#   )r   ZAlignedTableFloat32_itemsizer@   r   r   r   r  .  s    zAlignedTableFloat32.itemsizec                 C   s   t | |S r#   )r   ZAlignedTableFloat32_resizerA   r   r   r   r]   .  s    zAlignedTableFloat32.resizec                 C   s
   t | S r#   )r   ZAlignedTableFloat32_clearr@   r   r   r   rY   .  s    zAlignedTableFloat32.clearc                 C   s
   t | S r#   )r   ZAlignedTableFloat32_sizer@   r   r   r   r[   .  s    zAlignedTableFloat32.sizec                 C   s
   t | S r#   )r   ZAlignedTableFloat32_nbytesr@   r   r   r   r  .  s    zAlignedTableFloat32.nbytesc                 G   s   t j| g|R  S r#   )r   ZAlignedTableFloat32_getrQ   r   r   r   rV  .  s    zAlignedTableFloat32.getc                 G   s   t j| g|R  S r#   )r   ZAlignedTableFloat32_datarQ   r   r   r   rZ   .  s    zAlignedTableFloat32.dataN)r   r   r,   r   r   r   r   r   ZAlignedTableFloat32_tab_getZAlignedTableFloat32_tab_setr`  ZAlignedTableFloat32_numel_getZAlignedTableFloat32_numel_setr   r  r  r?   r  r]   rY   r[   r  rV  rZ   Zdelete_AlignedTableFloat32rT   r   r   r   r   r  -  s   
r  c                  G   s
   t j|  S r#   )r   CMin_uint16_partition_fuzzyr   r   r   r   r  $.  s    r  c                  G   s
   t j|  S r#   )r   CMax_uint16_partition_fuzzyr   r   r   r   r  '.  s    r  c                  G   s
   t j|  S r#   )r   merge_knn_results_CMinr   r   r   r   r  *.  s    r  c                  G   s
   t j|  S r#   )r   merge_knn_results_CMaxr   r   r   r   r  -.  s    r  c                   @   sZ   e Zd Zedd dd ddZeZeejej	Z
dd Zdd	 Zd
d Zdd ZejZdS )MapLong2Longc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   0.  r4   zMapLong2Long.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   0.  r4   r7   r8   c                 C   s   t | |||S r#   )r   ZMapLong2Long_addr   rB   rc  r1  r   r   r   ro  4.  s    zMapLong2Long.addc                 C   s   t | |S r#   )r   ZMapLong2Long_searchr  r   r   r   ru  7.  s    zMapLong2Long.searchc                 C   s   t | |||S r#   )r   ZMapLong2Long_search_multipler  r   r   r   search_multiple:.  s    zMapLong2Long.search_multiplec                 C   s   t | t   d S r#   )r   ZMapLong2Long_swiginitZnew_MapLong2Longr@   r   r   r   r?   =.  s    zMapLong2Long.__init__N)r   r   r,   r   r   r   r   r   ZMapLong2Long_map_getZMapLong2Long_map_setr  ro  ru  r  r?   Zdelete_MapLong2LongrT   r   r   r   r   r  /.  s   r  c                 C   s
   t | S r#   )r   omp_set_num_threads)Znum_threadsr   r   r   r  D.  s    r  c                   C   s   t  S r#   )r   omp_get_max_threadsr   r   r   r   r  G.  s    r  c                 C   s   t | ||S r#   )r   memcpy)destr  rB   r   r   r   r  J.  s    r  c                   @   sH   e Zd Zedd dd ddZeZdd Zedd	 Z	d
d Z
ejZdS )PythonInterruptCallbackc                 C   s
   | j  S r#   r0   r1   r   r   r   r3   M.  r4   z PythonInterruptCallback.<lambda>c                 C   s   | j |S r#   r0   r5   r   r   r   r3   M.  r4   r7   r8   c                 C   s
   t | S r#   )r   Z&PythonInterruptCallback_want_interruptr@   r   r   r   r7  P.  s    z&PythonInterruptCallback.want_interruptc                   C   s   t  S r#   )r   ZPythonInterruptCallback_resetr   r   r   r   r|   S.  s    zPythonInterruptCallback.resetc                 C   s   t | t   d S r#   )r   Z PythonInterruptCallback_swiginitZnew_PythonInterruptCallbackr@   r   r   r   r?   W.  s    z PythonInterruptCallback.__init__N)r   r   r,   r   r   r   r   r7  r  r|   r?   r   Zdelete_PythonInterruptCallbackrT   r   r   r   r   r  L.  s   
r  c                 C   s
   t | S r#   )r   swig_ptr)r   r   r   r   r  ^.  s    r  c                  G   s
   t j|  S r#   )r   rev_swig_ptrr   r   r   r   r  a.  s    r  c                 C   s
   t | S r#   )r   cast_integer_to_uint8_ptrr1   r   r   r   r  d.  s    r  c                 C   s
   t | S r#   )r   cast_integer_to_float_ptrr1   r   r   r   r  g.  s    r  c                 C   s
   t | S r#   )r   cast_integer_to_idx_t_ptrr1   r   r   r   r  j.  s    r  c                 C   s
   t | S r#   )r   cast_integer_to_int_ptrr1   r   r   r   r  m.  s    r  c                 C   s
   t | S r#   )r   cast_integer_to_void_ptrr1   r   r   r   r  p.  s    r  c                   C   s   t  S r#   )r   swig_versionr   r   r   r   r  s.  s    r  )T)Tr   )Fr   )r
  r
  r
  )r
  )r
  )N)N)r   )r
  r
  r
  )NN)NN)r
  )r   N)F)NN)NN)NN)r   (  sysr   Z_swig_python_version_info__package__r   r   r   builtinsr	   ImportErrorr   r   r"   r*   r   r+   objectr/   ZSwigPyIterator_swigregisterZSHARED_PTR_DISOWNrU   ZFloat32Vector_swigregisterra   ZFloat64Vector_swigregisterrb   ZInt8Vector_swigregisterrc   ZInt16Vector_swigregisterrd   ZInt32Vector_swigregisterre   ZInt64Vector_swigregisterrf   ZUInt8Vector_swigregisterrg   ZUInt16Vector_swigregisterrh   ZUInt32Vector_swigregisterri   ZUInt64Vector_swigregisterrj   Z Float32VectorVector_swigregisterrk   ZUInt8VectorVector_swigregisterrl   ZInt32VectorVector_swigregisterrm   ZInt64VectorVector_swigregisterrn   Z"VectorTransformVector_swigregisterro   Z!OperatingPointVector_swigregisterrp   Z#InvertedListsPtrVector_swigregisterrq   ZRepeatVector_swigregisterrr   Z+ClusteringIterationStatsVector_swigregisterrs   Z!ParameterRangeVector_swigregisterrt   Z OnDiskOneListVector_swigregisterru   rz   r{   ZPartitionStats_swigregisterr}   r   r   r   r   r   ZBitstringWriter_swigregisterZcvarr   ZBitstringReader_swigregisterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ZCodeSet_swigregisterZhamdis_tab_ham_bytesr   Z"CombinerRangeKNNfloat_swigregisterr   Z"CombinerRangeKNNint16_swigregisterr   r   r   r   r  r	  r  r  r  r  r  r  r  r  r   r!  r"  r#  r$  r&  r(  r)  r+  r,  r-  r.  r2  r3  r6  r7  r;  r>  r?  ZRandomGenerator_swigregisterrI  Z&SplitMix64RandomGenerator_swigregisterrK  rM  rN  rO  rP  rR  rU  rV  rW  rY  rZ  r]  r^  ra  rd  ZMETRIC_INNER_PRODUCTZ	METRIC_L2Z	METRIC_L1ZMETRIC_LinfZ	METRIC_LpZMETRIC_CanberraZMETRIC_BrayCurtisZMETRIC_JensenShannonZMETRIC_JaccardZMETRIC_NaNEuclideanZMETRIC_ABS_INNER_PRODUCTre  ZFAISS_VERSION_MAJORZFAISS_VERSION_MINORZFAISS_VERSION_PATCHZVERSION_STRINGrg  ZSearchParameters_swigregisterrh  ZIndex_swigregisterr  ZDistanceComputer_swigregisterr  Z%NegativeDistanceComputer_swigregisterr  Z&FlatCodesDistanceComputer_swigregisterr  ZIndexFlatCodes_swigregisterr  ZIndexFlat_swigregisterr  ZIndexFlatIP_swigregisterr  ZIndexFlatL2_swigregisterr  ZIndexFlat1D_swigregisterr  Z!ClusteringParameters_swigregisterr  Z%ClusteringIterationStats_swigregisterr  ZClustering_swigregisterr  ZClustering1D_swigregisterr  Z/ProgressiveDimClusteringParameters_swigregisterr  Z'ProgressiveDimIndexFactory_swigregisterr  Z%ProgressiveDimClustering_swigregisterr  r  r  r  r  ZQuantizer_swigregisterr  ZProductQuantizer_swigregisterr  ZPQEncoderGeneric_swigregisterr  ZPQEncoder8_swigregisterr  ZPQEncoder16_swigregisterr  ZPQDecoderGeneric_swigregisterr  ZPQDecoder8_swigregisterr   ZPQDecoder16_swigregisterr  ZAdditiveQuantizer_swigregisterr  r   r!  Z!RefineBeamMemoryPool_swigregisterr$  r+  Z$RefineBeamLUTMemoryPool_swigregisterr-  r0  Z3ComputeCodesAddCentroidsLUT0MemoryPool_swigregisterr1  r3  Z3ComputeCodesAddCentroidsLUT1MemoryPool_swigregisterr4  r5  ZResidualQuantizer_swigregisterr=  Z!LocalSearchQuantizer_swigregisterrQ  ZIcmEncoder_swigregisterrU  ZIcmEncoderFactory_swigregisterrW  ZLSQTimer_swigregisterr[  ZLSQTimerScope_swigregisterr`  Z%ProductAdditiveQuantizer_swigregisterrd  Z(ProductLocalSearchQuantizer_swigregisterre  Z%ProductResidualQuantizer_swigregisterrf  ZCodePacker_swigregisterrs  ZCodePackerFlat_swigregisterrt  ZVectorTransform_swigregisterr~  ZLinearTransform_swigregisterr  Z!RandomRotationMatrix_swigregisterr  ZPCAMatrix_swigregisterr  ZITQMatrix_swigregisterr  ZITQTransform_swigregisterr  ZOPQMatrix_swigregisterr  Z%RemapDimensionsTransform_swigregisterr  Z#NormalizationTransform_swigregisterr  ZCenteringTransform_swigregisterr  Z)SearchParametersPreTransform_swigregisterr  ZIndexPreTransform_swigregisterr  Z(IndexRefineSearchParameters_swigregisterr  ZIndexRefine_swigregisterr  ZIndexRefineFlat_swigregisterr  ZIndexLSH_swigregisterr  Z)SimulatedAnnealingParameters_swigregisterr  Z!PermutationObjective_swigregisterr  Z(ReproduceDistancesObjective_swigregisterr  Z(SimulatedAnnealingOptimizer_swigregisterr  ZPolysemousTraining_swigregisterr  ZIndexPQ_swigregisterr  ZSearchParametersPQ_swigregisterr  ZIndexPQStats_swigregisterr  Z MultiIndexQuantizer_swigregisterr  Z!MultiIndexQuantizer2_swigregisterr  Z#IndexAdditiveQuantizer_swigregisterr  Z#IndexResidualQuantizer_swigregisterr  Z&IndexLocalSearchQuantizer_swigregisterr  Z*IndexProductResidualQuantizer_swigregisterr  Z-IndexProductLocalSearchQuantizer_swigregisterr  Z$AdditiveCoarseQuantizer_swigregisterr  Z4SearchParametersResidualCoarseQuantizer_swigregisterr  Z$ResidualCoarseQuantizer_swigregisterr  Z'LocalSearchCoarseQuantizer_swigregisterr  ZIOReader_swigregisterr  ZIOWriter_swigregisterr  ZVectorIOReader_swigregisterr  ZVectorIOWriter_swigregisterr  ZFileIOReader_swigregisterr  ZFileIOWriter_swigregisterr  ZBufferedIOReader_swigregisterr  ZBufferedIOWriter_swigregisterr  r  r  r  Z"InvertedListsIterator_swigregisterr  ZInvertedLists_swigregisterr#  ZArrayInvertedLists_swigregisterr&  Z"ReadOnlyInvertedLists_swigregisterr'  Z HStackInvertedLists_swigregisterr,  ZSliceInvertedLists_swigregisterr/  Z VStackInvertedLists_swigregisterr0  Z MaskedInvertedLists_swigregisterr3  Z#StopWordsInvertedLists_swigregisterr5  Z InvertedListsIOHook_swigregisterrA  ZBlockInvertedLists_swigregisterrD  rE  rH  rI  ZDirectMap_swigregisterrS  ZDirectMapAdd_swigregisterrU  ZLevel1Quantizer_swigregisterr`  Z SearchParametersIVF_swigregisterre  ZIndexIVFInterface_swigregisterrn  ZIndexIVF_swigregisterr  Z InvertedListScanner_swigregisterr  ZIndexIVFStats_swigregisterr  r  r  r  r  r  r  ZSlidingIndexWindow_swigregisterr  r  r  r  r  r  r  ZScalarQuantizer_swigregisterr  Z!IndexScalarQuantizer_swigregisterr  Z$IndexIVFScalarQuantizer_swigregisterr  Z!IndexIVFSpectralHash_swigregisterr  Z&IndexIVFAdditiveQuantizer_swigregisterr  Z&IndexIVFResidualQuantizer_swigregisterr  Z)IndexIVFLocalSearchQuantizer_swigregisterr  Z-IndexIVFProductResidualQuantizer_swigregisterr  Z0IndexIVFProductLocalSearchQuantizer_swigregisterr  Z!SearchParametersHNSW_swigregisterr  ZHNSW_swigregisterr  ZHNSWStats_swigregisterr  r  r  r  r  ZIndexHNSW_swigregisterr  ZIndexHNSWFlat_swigregisterr  ZIndexHNSWPQ_swigregisterr  ZIndexHNSWSQ_swigregisterr  ZIndexHNSW2Level_swigregisterr
  ZIndexHNSWCagra_swigregisterr  r  r  ZNeighbor_swigregisterr  ZNhood_swigregisterr  ZNNDescent_swigregisterr%  ZIndexNNDescent_swigregisterr&  ZIndexNNDescentFlat_swigregisterr'  ZIndexIVFFlat_swigregisterr(  ZIndexIVFFlatDedup_swigregisterr)  r*  ZNSG_swigregisterr:  ZNSG_Graph_int_swigregisterr<  ZIndexNSG_swigregisterr?  ZIndexNSGFlat_swigregisterr@  ZIndexNSGPQ_swigregisterrA  ZIndexNSGSQ_swigregisterrB  ZOnDiskOneList_swigregisterrD  Z OnDiskInvertedLists_swigregisterrS  ZZnSphereSearch_swigregisterrV  ZEnumeratedVectors_swigregisterr\  ZRepeat_swigregisterr^  ZRepeats_swigregisterr`  ZZnSphereCodec_swigregisterrb  ZZnSphereCodecRec_swigregisterrj  ZZnSphereCodecAlt_swigregisterrk  ZIndexLattice_swigregisterrn  Z"IVFPQSearchParameters_swigregisterrr  ZIndexIVFPQ_swigregisterrz  r{  ZIndexIVFPQStats_swigregisterr|  ZIndexIVFPQR_swigregisterr}  ZIndex2Layer_swigregisterr  ZIndexFastScan_swigregisterr  ZFastScanStats_swigregisterr  Z+IndexAdditiveQuantizerFastScan_swigregisterr  Z+IndexResidualQuantizerFastScan_swigregisterr  Z.IndexLocalSearchQuantizerFastScan_swigregisterr  Z2IndexProductResidualQuantizerFastScan_swigregisterr  Z5IndexProductLocalSearchQuantizerFastScan_swigregisterr  ZIndexPQFastScan_swigregisterr  Zsimd16uint16_swigregisterr  ZSIMDResultHandler_swigregisterr  Z%SIMDResultHandlerToFloat_swigregisterr  ZDummyResultHandler_swigregisterr  ZStoreResultHandler_swigregisterr  ZIndexIVFFastScan_swigregisterr  ZIVFFastScanStats_swigregisterr  Z.IndexIVFAdditiveQuantizerFastScan_swigregisterr  Z1IndexIVFLocalSearchQuantizerFastScan_swigregisterr  Z.IndexIVFResidualQuantizerFastScan_swigregisterr  Z8IndexIVFProductLocalSearchQuantizerFastScan_swigregisterr  Z5IndexIVFProductResidualQuantizerFastScan_swigregisterr  Z)IndexIVFIndependentQuantizer_swigregisterr  ZIndexIVFPQFastScan_swigregisterr  r  r  r  r  r  ZIndexBinary_swigregisterr  ZIndexBinaryFlat_swigregisterr  ZIndexBinaryIVF_swigregisterr  Z&BinaryInvertedListScanner_swigregisterr  Z!IndexBinaryFromFloat_swigregisterr  ZIndexBinaryHNSW_swigregisterr  ZIndexBinaryHash_swigregisterr  Z!IndexBinaryHashStats_swigregisterr  Z!IndexBinaryMultiHash_swigregisterr  ZThreadedIndexBase_swigregisterr  Z$ThreadedIndexBaseBinary_swigregisterr  ZIndexShards_swigregisterr  ZIndexBinaryShards_swigregisterr  ZIndexShardsIVF_swigregisterr  ZIndexReplicas_swigregisterr  Z IndexBinaryReplicas_swigregisterr  ZIndexSplitVectors_swigregisterr  ZIndexRandom_swigregisterr  Z#IndexRowwiseMinMaxBase_swigregisterr	  Z#IndexRowwiseMinMaxFP16_swigregisterr
  ZIndexRowwiseMinMax_swigregisterr  ZLinear_swigregisterr  ZEmbedding_swigregisterr  ZFFN_swigregisterr  ZQINCoStep_swigregisterr  ZNeuralNetCodec_swigregisterr  ZQINCo_swigregisterr  ZTensor2D_swigregisterr"  ZInt32Tensor2D_swigregisterr#  Z IndexNeuralNetCodec_swigregisterr%  ZIndexQINCo_swigregisterr&  ZRangeSearchResult_swigregisterr)  ZBufferList_swigregisterr-  ZRangeQueryResult_swigregisterr/  Z%RangeSearchPartialResult_swigregisterr6  ZInterruptCallback_swigregisterr<  ZTimeoutCallback_swigregisterrA  ZVisitedTable_swigregisterrD  ZIDSelector_swigregisterrF  ZIDSelectorRange_swigregisterrK  ZIDSelectorArray_swigregisterrL  ZIDSelectorBatch_swigregisterrM  ZIDSelectorBitmap_swigregisterrO  ZIDSelectorNot_swigregisterrP  ZIDSelectorAll_swigregisterrQ  ZIDSelectorAnd_swigregisterrU  ZIDSelectorOr_swigregisterrV  ZIDSelectorXOr_swigregisterrW  Z!IDSelectorTranslated_swigregisterrX  ZIndexIDMap_swigregisterr^  ZIndexBinaryIDMap_swigregisterr_  ZIndexIDMap2_swigregisterrf  ZIndexBinaryIDMap2_swigregisterZ
EXACT_TOPKZAPPROX_TOPK_BUCKETS_B32_D2ZAPPROX_TOPK_BUCKETS_B8_D3ZAPPROX_TOPK_BUCKETS_B16_D2ZAPPROX_TOPK_BUCKETS_B8_D2rg  ri  rj  rk  rl  rn  ro  rp  rq  rr  rs  rt  ru  rv  rw  rx  ry  rz  ZCloner_swigregisterZIO_FLAG_SKIP_STORAGEZIO_FLAG_READ_ONLYZIO_FLAG_ONDISK_SAME_DIRZIO_FLAG_SKIP_IVF_DATAZIO_FLAG_SKIP_PRECOMPUTE_TABLEZIO_FLAG_PQ_SKIP_SDC_TABLEZIO_FLAG_MMAPr~  r  r  ZAutoTuneCriterion_swigregisterr  Z"OneRecallAtRCriterion_swigregisterr  Z"IntersectionCriterion_swigregisterr  ZOperatingPoint_swigregisterr  ZOperatingPoints_swigregisterr  ZParameterRange_swigregisterr  ZParameterSpace_swigregisterr  r  r  ZMatrixStats_swigregisterr  ZPyCallbackIOWriter_swigregisterr  ZPyCallbackIOReader_swigregisterr  Z!PyCallbackIDSelector_swigregisterr  Z"float_minheap_array_t_swigregisterr  Z int_minheap_array_t_swigregisterr  Z"float_maxheap_array_t_swigregisterr  Z int_maxheap_array_t_swigregisterr  r  r  ZAlignedTableUint8_swigregisterr  ZAlignedTableUint16_swigregisterr  Z AlignedTableFloat32_swigregisterr  r  r  r  r  ZMapLong2Long_swigregisterr  r  r  r  Z$PythonInterruptCallback_swigregisterr  r  r  r  r  r  r  r  r   r   r   r   <module>   s  	;

























(
(
-H#
)

  3
 


I
/
	


/

?




+
 %






 >
"


q  S?0+ 
\50$/I"
"
 	&%&$+(949(C  G
S&@"iI6"(63c*.h+)OE,	o:



4 7) ., %# ::tt^^%0
 %+"55##











+%T UUUU	$$$