
    6h                       S r SSKJr  SSKJr  SSKJrJr  Sr\S-  r	Sr
\
S-  r\	\-
  r\S-  r\S	-
  S
-  rS rSS jrS rS rS r\" S5       V s/ s H  n \" U 5      PM     sn rS rS rS rS r " S S5      rgs  sn f )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
    )annotations   )nullrev)pycompatutili              c                >    SnU  H  nUS-  [        U5      -   nM     U$ )zconvert a bytestring to a longr      )ord)bsvbs      0/usr/lib/python3/dist-packages/mercurial/pvec.py_binr   D   s(    	AGc!f H    c                p    Sn[        U5       H$  n[        R                  " U S-  5      U-   nU S-  n M&     U$ )Nr      r   )ranger   bytechr)r   lr   ps       r   _strr   L   s>    	B1Xa#g&+	a  Ir   c                J    [        U S[         5      [        U [        S 5      4$ )zdepth and bitvecN)r   _depthbytes)r   s    r   _splitr   T   s#    ,; $q"777r   c                D    [        U [        5      [        U[        5      -   $ N)r   r   	_vecbytes)depthbitvecs     r   _joinr$   Y   s    {#d69&===r   c                R    SnU (       a  U S-  (       a  US-  nU S-  n U (       a  M  U$ )Nr   r    )xcs     r   _hweightr)   ]   s1    	A
q5FA	a ! Hr   r   c                Z    X-  nSnU(       a  U[         US-     -  nUS-  nU(       a  M  U$ )z+find the hamming distance between two longsr   r   r   )_htab)ar   dr(   s       r   _hammingr.   i   s:    	A	A
	U1t8_	a ! Hr   c                   U u  p4Uu  pVX5:  a  XSXd4u  p5pF[        XF5      nX5-
  nUn	XF-  n
SnXx:  a  Xx-
  S-   S-  nOSnX<-   nU
(       a*  U(       a   X-  (       a	  X-  n	US-  nUS-  nU(       a  M   X4$ [        X5      n	X4$ )Nr   r   )r.   _flipbit)r'   yr(   d1v1d2v2hdistddistr   michangesr"   s                 r   	_mergevecr;   s   s    
 FBFB	wREGE
A
A	A} =1$* LE 	u1!GA	 g 8O QN8Or   c                <    [        U5      S-  [        -  nU SU-  -  $ )Nl    r   )hash_vecbits)r   nodebits      r   r0   r0      s#    :
"h
.CS>r   c                l   U R                  5       n[        US5      (       d  0 Ul        UR                  nU R                  5       U;  a  UR                  n[        U R                  5       S-   5       H  nXB;  d  M
  UR                  U5      nUR                  U5      u  pgU[        :X  a  S[        US-  S[         5      4X$'   MS  U[        :X  a  X&   u  pUS-   [        X5      4X$'   Mw  [        X&   X'   U5      X$'   M     [        X R                  5          6 n
[        [        R                   " U
5      5      $ )z3construct a pvec for ctx while filling in the cache
_pveccacher   r      N)repohasattrrB   rev	changelogr   r?   
parentrevsr   r   r!   r0   r;   r$   pvecr   	b85encode)ctxrpvcclnr?   p1p2r-   r   r   s              r   ctxpvecrR      s    
A1l##
,,C
wwy[[swwy1}%A|wwqzq)=tax)&<!=>CF7]7DA!eXa%67CF&sw>CF & 
GGI	Br"##r   c                  J    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rSrg)rI      c                    [        U[        5      (       a3  Xl        [        [        R
                  " U5      5      u  U l        U l        g [        U5      U l        g r    )	
isinstancebytes_bsr   r   	b85decode_depth_vecrR   )self	hashorctxs     r   __init__pvec.__init__   s<    i'' H%+DNN9,E%F"DK	*DIr   c                    U R                   $ r    )rX   )r\   s    r   __str__pvec.__str__   s    xxr   c                t    U R                   UR                   :H  =(       a    U R                  UR                  :H  $ r    )r[   rZ   r\   r   s     r   __eq__pvec.__eq__   s'    yyAFF">t{{ahh'>>r   c                    UR                   U R                   -
  nUS:  a  g[        U R                  UR                  5      U:  a  gg)Nr   FT)rZ   r.   r[   r\   r   deltas      r   __lt__pvec.__lt__   s;    4;;&19DIIqvv&.r   c                
    X:  $ r    r&   rd   s     r   __gt__pvec.__gt__   s	    xr   c                    [        UR                  U R                  -
  5      n[        U R                  UR                  5      U::  a  gg)NFT)absrZ   r.   r[   rh   s      r   __or__pvec.__or__   s6    AHHt{{*+DIIqvv&%/r   c                \    X-  (       a  [        S5      eU R                  UR                  -
  $ )Ns   concurrent pvecs)
ValueErrorrZ   rd   s     r   __sub__pvec.__sub__   s&    8011{{QXX%%r   c                    [        UR                  U R                  -
  5      n[        U R                  UR                  5      n[	        X#5      $ r    )rp   rZ   r.   r[   max)r\   r   r-   hs       r   distancepvec.distance   s7    4;;&'TYY'1yr   c                    [        UR                  U R                  -
  5      nU[        :  d(  [	        U R
                  UR
                  5      [        :  a  gg )NF)rp   r"   rZ   _radiusr.   r[   )r\   r   dists      r   near	pvec.near   s?    177T[[()'>Xdii87B Cr   )rX   rZ   r[   N)__name__
__module____qualname____firstlineno__r^   ra   re   rj   rm   rq   ru   rz   r   __static_attributes__r&   r   r   rI   rI      s/    +?&

r   rI   N)r   intr   r   returnrW   )__doc__
__future__r   r?   r    r   r   _size_bytes
_depthbitsr   r!   r>   r}   r   r   r   r$   r)   r   r+   r.   r;   r0   rR   rI   )r'   s   0r   <module>r      s   )V # 
 		!
Ao[ 	q=b=Q
8
> $Cj)j!j)#L$0, ,c 	*s   A>