
    	^c                         d Z ddlmZ ddlmZmZ dZedz  ZdZedz  Z	ee	z
  Z
e
dz  Zedz
  dz  Zd	 Zd
 Zd Zd Zd Zd  ed          D             Zd Zd Zd Zd Z G d d          ZdS )a3  
A "pvec" is a changeset property based on the theory of vector clocks
that can be compared to discover relatedness without consulting a
graph. This can be useful for tasks like determining how a
disconnected patch relates to a repository.

Currently a pvec consist of 448 bits, of which 24 are 'depth' and the
remainder are a bit vector. It is represented as a 70-character base85
string.

Construction:

- a root changeset has a depth of 0 and a bit vector based on its hash
- a normal commit has a changeset where depth is increased by one and
  one bit vector bit is flipped based on its hash
- a merge changeset pvec is constructed by copying changes from one pvec into
  the other to balance its depth

Properties:

- for linear changes, difference in depth is always <= hamming distance
- otherwise, changes are probably divergent
- when hamming distance is < 200, we can reliably detect when pvecs are near

Issues:

- hamming distance ceases to work over distances of ~ 200
- detecting divergence is less accurate when the common ancestor is very close
  to either revision or total distance is high
- this could probably be improved by modeling the relation between
  delta and hdist

Uses:

- a patch pvec can be used to locate the nearest available common ancestor for
  resolving conflicts
- ordering of patches can be established without a DAG
- two head pvecs can be compared to determine whether push/pull/merge is needed
  and approximately how many changesets are involved
- can be used to find a heuristic divergence measure between changesets on
  different branches
   )nullrev)pycompatutili              c                 >    d}| D ]}|dz  t          |          z   }|S )zconvert a bytestring to a long       )ord)bsvbs      0/usr/lib/python3/dist-packages/mercurial/pvec.py_binr   C   s1    	A  Gc!ffH    c                 l    d}t          |          D ]!}t          j        | dz            |z   }| dz  } "|S )Nr      r   )ranger   bytechr)r   lr   ps       r   _strr   K   sE    	B1XX  a#g&&+	aIr   c                 r    t          | dt                             t          | t          d                   fS )zdepth and bitvecN)r   _depthbytes)r   s    r   _splitr   T   s-    ,;,  $q"7"777r   c                 X    t          | t                    t          |t                    z   S N)r   r   	_vecbytes)depthbitvecs     r   _joinr#   Y   s!    {##d69&=&===r   c                 0    d}| r| dz  r|dz  }| dz  } | |S )Nr   r    )xcs     r   _hweightr(   ]   s>    	A
 q5 	FA	a   Hr   c                 ,    g | ]}t          |          S r%   )r(   ).0r&   s     r   
<listcomp>r+   f   s    )))!)))r   r   c                 L    | |z  }d}|r|t           |dz           z  }|dz  }||S )z+find the hamming distance between two longsr   r   r   )_htab)ar   dr'   s       r   _hammingr0   i   sD    	AA	A
 	U1t8_	a   Hr   c                    | \  }}|\  }}||k     r||||f\  }}}}t          ||          }||z
  }|}	||z  }
d}||k    r||z
  dz   dz  }nd}||z   }|
r|r|
|z  r
|	|z  }	|dz  }|dz  }|nt          |	|          }	||	fS )Nr   r	   )r0   _flipbit)r&   yr'   d1v1d2v2hdistddistr   michangesr!   s                 r   	_mergevecr=   s   s    
 FBFB	BwwRRBBREGE
A
RA	Au}} 5=1$* LE 	  	1u Q1!GA	  	 QNN!8Or   c                 F    t          |          dz  t          z  }| d|z  z  S )Nl    r   )hash_vecbits)r   nodebits      r   r2   r2      s%    ::
"h
.CS>r   c                    |                                  }t          j        |d          si |_        |j        }|                                 |vr|j        }t          |                                 dz             D ]}||vr|                    |          }|                    |          \  }}|t          k    r%dt          |dz  dt                             f||<   c|t          k    r$||         \  }}	|dz   t          |	|          f||<   t          ||         ||         |          ||<   t          ||                                           }
t          t          j        |
                    S )z3construct a pvec for ctx while filling in the cache
_pveccacher   r      N)repor   safehasattrrD   rev	changelogr   rA   
parentrevsr   r   r    r2   r=   r#   pvec	b85encode)ctxrpvcclnrA   p1p2r/   r   r   s              r   ctxpvecrT      sJ   

AA|,, 
,C
wwyy[swwyy1}%% 	? 	?A||wwqzzq))B==tax)&<!=!=>CFF7]]r7DAq!eXa%6%67CFF&s2wB>>CF	CGGII	Br""###r   c                   D    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )rK   c                     t          |t                    r7|| _        t          t	          j        |                    \  | _        | _        d S t          |          | _        d S r   )	
isinstancebytes_bsr   r   	b85decode_depth_vecrT   )self	hashorctxs     r   __init__zpvec.__init__   sR    i'' 	+ DH%+DN9,E,E%F%F"DK	**DIIIr   c                     | j         S r   )rY   )r]   s    r   __str__zpvec.__str__   s	    xr   c                 B    | j         |j         k    o| j        |j        k    S r   )r\   r[   r]   r   s     r   __eq__zpvec.__eq__   s    yAF">t{ah'>>r   c                 t    |j         | j         z
  }|dk     rdS t          | j        |j                  |k    rdS dS )Nr   FT)r[   r0   r\   r]   r   deltas      r   __lt__zpvec.__lt__   sA    4;&1995DIqv&&..5tr   c                     || k     S r   r%   rc   s     r   __gt__zpvec.__gt__   s    4xr   c                 ~    t          |j        | j        z
            }t          | j        |j                  |k    rdS dS )NFT)absr[   r0   r\   rf   s      r   __or__zpvec.__or__   s;    AHt{*++DIqv&&%//5tr   c                 H    | |z  rt          d          | j        |j        z
  S )Ns   concurrent pvecs)
ValueErrorr[   rc   s     r   __sub__zpvec.__sub__   s,    !8 	20111{QX%%r   c                     t          |j        | j        z
            }t          | j        |j                  }t	          ||          S r   )rl   r[   r0   r\   max)r]   r   r/   hs       r   distancezpvec.distance   s9    4;&''TY''1ayyr   c                     t          |j        | j        z
            }|t          k    s#t	          | j        |j                  t          k    rdS d S )NF)rl   r!   r[   _radiusr0   r\   )r]   r   dists      r   nearz	pvec.near   sG    17T[())'>>Xdi887BB5 CBr   N)__name__
__module____qualname__r_   ra   rd   rh   rj   rm   rp   rt   rx   r%   r   r   rK   rK      s        + + +  ? ? ?      & & &
  
    r   rK   N)__doc__rA   r    r   r   _size_bytes
_depthbitsr   r    r@   rv   r   r   r   r#   r(   r   r-   r0   r=   r2   rT   rK   r%   r   r   <module>r      sb  ) )X             
 		!
Ao[ 	q=b=Q
    8 8 8
> > >   	*)eeCjj)))  # # #L  $ $ $0, , , , , , , , , ,r   