
    [g                     0    S SK Jr  S SKJr   " S S5      rg)    )sqrtBaseLunrExceptionc                   l    \ rS rSrSrSS jrS rS rS rS r	SS	 jr
S
 rS r\S 5       rS rS rSrg)Vector   a  A vector is used to construct the vector space of documents and queries.
These vectors support operations to determine the similarity between two
documents or a document and a query.

Normally no parameters are required for initializing a vector, but in the
case of loading a previously dumped vector the raw elements can be provided
to the constructor.

For performance reasons vectors are implemented with a flat array, where an
elements index is immediately followed by its value.
E.g. [index, value, index, value].

TODO: consider implemetation as 2-tuples.

This allows the underlying array to be as sparse as possible and still
offer decent performance when being used for vector calculations.
Nc                 2    SU l         U=(       d    / U l        g )Nr   
_magnitudeelements)selfr   s     -/usr/lib/python3/dist-packages/lunr/vector.py__init__Vector.__init__   s     B    c                 8    SR                  U R                  5      $ )Nz<Vector magnitude={}>)format	magnituder   s    r   __repr__Vector.__repr__   s    &--dnn==r   c                 ,    [        U R                  5      $ N)iterr   r   s    r   __iter__Vector.__iter__    s    DMM""r   c                 x   U R                   (       d  gSn[        [        U R                   5      S-  5      nX2-
  n[        US-  5      nU R                   US-     nUS:  a@  Xa:  a  UnO	Xa:  a  UnOO/X2-
  nU[        US-  5      -   nU R                   US-     nUS:  a  M@  Xa:X  a  US-  $ Xa:  a  US-  $ US-   S-  $ )a;  Calculates the position within the vector to insert a given index.

This is used internally by insert and upsert. If there are duplicate
indexes then the position is returned as if the value for that index
were to be updated, but it is the callers responsibility to check
whether there is a duplicate at that index
r         )r   intlen)r   indexstartendslice_lengthpivot_pointpivot_indexs          r   position_for_indexVector.position_for_index#   s     }}#dmm$q(){,*+mmK!O4Q"#$!;L#lQ&6"77K--a8K Q ?" ?"!Oq((r   c                 .    S nU R                  XU5        g)zInserts an element at an index within the vector.

Does not allow duplicates, will throw an error if there is already an
entry for this index.
c                     [        S5      e)NzDuplicate indexr   )r"   vals     r   prevent_duplicates)Vector.insert.<locals>.prevent_duplicatesN   s    #$566r   N)upsert)r   insert_indexr,   r-   s       r   insertVector.insertG   s    	7 	L'9:r   c                 d   U=(       d    S nSU l         U R                  U5      nU[        U R                  5      :  a=  U R                  U   U:X  a*  U" U R                  US-      U5      U R                  US-   '   gU R                  R	                  XB5        U R                  R	                  XA5        g)a  Inserts or updates an existing index within the vector.

Args:
    - insert_index (int): The index at which the element should be
        inserted.
    - val (int|float): The value to be inserted into the vector.
    - fn (callable, optional): An optional callable taking two
        arguments, the current value and the passed value to generate
        the final inserted value at the position in case of collision.
c                     U$ r    )currentpasseds     r   <lambda>Vector.upsert.<locals>.<lambda>^   s    Fr   r   r   N)r   r(   r!   r   r1   )r   r0   r,   fnpositions        r   r/   Vector.upsertS   s     32**<8c$--((T]]8-D-T*,T]]8a<-H#*NDMM(Q,'MM  /MM  8r   c                     / n[        S[        U R                  5      S5       H!  nUR                  U R                  U   5        M#     U$ )zAConverts the vector to an array of the elements within the vectorr   r   )ranger!   r   append)r   outputis      r   to_listVector.to_listg   s=    q#dmm,a0AMM$--*+ 1r   c                 Z    U R                    Vs/ s H  n[        US5      PM     sn$ s  snf )N   )r   round)r   elements     r   	serializeVector.serializen   s'     26?ggq!???s   (c                     U R                   (       dO  Sn[        S[        U R                  5      S5       H  nU R                  U   nXU-  -  nM     [	        U5      U l         U R                   $ )Nr   r   r   )r   r>   r!   r   r   )r   sum_of_squaresrA   values       r   r   Vector.magnitudes   s\    N1c$--0!4a(%-/ 5 #>2DOr   c                    SnU R                   nUR                   n[        U5      n[        U5      nS=pxXu:  aK  X:  aF  X7   n	XH   n
X:  a  US-  nO'X:  a  US-  nOX#US-      XHS-      -  -  nUS-  nUS-  nXu:  a  X:  a  MF  U$ )z=Calculates the dot product of this vector and another vector.r   r   r   )r   r!   )r   otherdot_productaba_lenb_lenrA   ja_valb_vals              r   dot
Vector.dot   s    MMNNAA	iAIDEDE}QQQx!E(22QQ iAI r   c                     U R                   S:X  d  UR                   S:X  a  gU R                  U5      U R                   -  $ )zHCalculates the cosine similarity between this vector and another
vector.r   )r   rX   )r   rO   s     r   
similarityVector.similarity   s5     >>Q%//Q"6xx//r   r
   r   )__name__
__module____qualname____firstlineno____doc__r   r   r   r(   r1   r/   rB   rH   propertyr   rX   r[   __static_attributes__r5   r   r   r   r      sO    $'>#")H
;9(@
 	 	.0r   r   N)mathr   lunr.exceptionsr   r   r5   r   r   <module>rf      s     -V0 V0r   