§
    cÌc‚8  ã                   ót   — d Z ddlmZ ddlmZmZ  G d„ d¦  «        Z G d„ d¦  «        Z G d	„ d
¦  «        ZdS )zKImplementation of Graph algorithms when we have already loaded everything.
é    )Údequeé   )ÚerrorsÚrevisionc                   ó"   — e Zd ZdZdZd„ Zd„ ZdS )Ú_KnownGraphNodez.Represents a single object in the known graph.©ÚkeyÚparent_keysÚ
child_keysÚgdfoc                 ó>   — || _         || _        g | _        d | _        d S ©Nr	   )Úselfr
   r   s      ú8/usr/lib/python3/dist-packages/breezy/_known_graph_py.pyÚ__init__z_KnownGraphNode.__init__    s#   € ØˆŒØ&ˆÔØˆŒàˆŒ	ˆ	ˆ	ó    c                 óp   — d                      | j        j        | j        | j        | j        | j        ¦  «        S )Nz{}({}  gdfo:{} par:{} child:{}))ÚformatÚ	__class__Ú__name__r
   r   r   r   ©r   s    r   Ú__repr__z_KnownGraphNode.__repr__'   s4   € Ø0×7Ò7ØŒNÔ# T¤X¨t¬yØÔ˜dœoñ/ô /ð 	/r   N)r   Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   © r   r   r   r      s=   € € € € € Ø8Ð8à<€Iðð ð ð/ð /ð /ð /ð /r   r   c                   ó   — e Zd ZdZdZd„ ZdS )Ú_MergeSortNodez5Information about a specific node in the merge graph.©r
   Úmerge_depthÚrevnoÚend_of_mergec                 ó>   — || _         || _        || _        || _        d S r   r!   )r   r
   r"   r#   r$   s        r   r   z_MergeSortNode.__init__2   s&   € ØˆŒØ&ˆÔØˆŒ
Ø(ˆÔÐÐr   N)r   r   r   r   r   r   r   r   r   r    r    -   s.   € € € € € Ø?Ð?à?€Ið)ð )ð )ð )ð )r   r    c                   ó\   — e Zd ZdZdd„Zd„ Zd„ Zd„ Zd„ Zd„ Z	d	„ Z
d
„ Zd„ Zd„ Zd„ Zd„ ZdS )Ú
KnownGraphz=This is a class which assumes we already know the full graph.Tc                 ó‚   — i | _         i | _        || _        |                      |¦  «         |                      ¦   «          dS )znCreate a new KnownGraph instance.

        :param parent_map: A dictionary mapping key => parent_keys
        N)Ú_nodesÚ_known_headsÚdo_cacheÚ_initialize_nodesÚ
_find_gdfo)r   Ú
parent_mapr+   s      r   r   zKnownGraph.__init__<   sD   € ð
 ˆŒàˆÔØ ˆŒØ×Ò˜zÑ*Ô*Ð*Ø�ŠÑÔÐÐÐr   c                 ó6  — | j         }|                     ¦   «         D ]|\  }}||v r||         }||_        nt          ||¦  «        }|||<   |D ]K}	 ||         }n%# t          $ r t          |d¦  «        }|||<   Y nw xY w|j                             |¦  «         ŒLŒ}dS )a  Populate self._nodes.

        After this has finished:
        - self._nodes will have an entry for every entry in parent_map.
        - ghosts will have a parent_keys = None,
        - all nodes found will also have .child_keys populated with all known
          child_keys,
        N)r)   Úitemsr   r   ÚKeyErrorr   Úappend)r   r.   Únodesr
   r   ÚnodeÚ
parent_keyÚparent_nodes           r   r,   zKnownGraph._initialize_nodesH   så   € ð ”ˆØ *× 0Ò 0Ñ 2Ô 2ð 	3ð 	3ÑˆC�Ø�eˆ|ˆ|Ø˜S”z�Ø#.�Ô Ð å& s¨KÑ8Ô8�Ø!��c‘
Ø)ð 3ð 3�
ð4Ø"'¨
Ô"3�K�KøÝð 4ð 4ð 4Ý"1°*¸dÑ"CÔ"C�KØ(3�E˜*Ñ%Ð%Ð%ð4øøøð Ô&×-Ò-¨cÑ2Ô2Ð2Ð2ð3ð	3ð 	3s   ÁAÁA:Á9A:c                 óH   — d„ | j                              ¦   «         D ¦   «         S )Nc                 ó    — g | ]}|j         °	|‘ŒS r   )r   ©Ú.0r4   s     r   ú
<listcomp>z*KnownGraph._find_tails.<locals>.<listcomp>b   s/   € ð )ð )ð )˜ØÔ'ð)�ð )ð )ð )r   ©r)   Úvaluesr   s    r   Ú_find_tailszKnownGraph._find_tailsa   s0   € ð)ð ) ¤×!3Ò!3Ñ!5Ô!5ð )ñ )ô )ð 	)r   c                 óH   — d„ | j                              ¦   «         D ¦   «         S )Nc                 ó    — g | ]}|j         °	|‘ŒS r   )r   r9   s     r   r;   z)KnownGraph._find_tips.<locals>.<listcomp>f   s.   € ð (ð (ð (˜Ø”ð(�ð (ð (ð (r   r<   r   s    r   Ú
_find_tipszKnownGraph._find_tipse   s0   € ð(ð ( ¤×!3Ò!3Ñ!5Ô!5ð (ñ (ô (ð 	(r   c                 óÈ  — | j         }i }g }|                      ¦   «         D ]}d|_        |                     |¦  «         Œ|r¡|                     ¦   «         }|j        D ]�}||         }||v r||         dz   }d}nd}d}|j        �|j        dz   |j        k    r|j        dz   |_        |t          |j        ¦  «        k    r|                     |¦  «         |r||= Œ||||<   Œ‚|°Ÿd S d S )Nr   TF)r)   r>   r   r2   Úpopr   Úlenr   )	r   r3   Úknown_parent_gdfosÚpendingr4   Ú	child_keyÚchildÚ
known_gdfoÚpresents	            r   r-   zKnownGraph._find_gdfoi   s7  € Ø”ˆØÐØˆà×$Ò$Ñ&Ô&ð 	!ð 	!ˆDØˆDŒIØ�NŠN˜4Ñ Ô Ð Ð àð 	?Ø—;’;‘=”=ˆDØ!œ_ð ?ð ?�	Ø˜iÔ(�ØÐ 2Ð2Ð2Ø!3°IÔ!>ÀÑ!B�JØ"�G�Gà!"�JØ#�GØ”:Ð%¨¬°Q©¸¼Ò)CÐ)CØ!%¤¨Q¡�E”JØ¥ UÔ%6Ñ!7Ô!7Ò7Ð7ð —N’N 5Ñ)Ô)Ð)Øð :Ø.¨yÐ9øð 5?Ð& yÑ1Ð1ð) ð 	?ð 	?ð 	?ð 	?ð 	?r   c                 óì  — | j         }||v rs||         }|j        €!||_        | j                             ¦   «          nXt	          |¦  «        }t	          |j        ¦  «        }||k    rdS t          d|›d|›d|›�¦  «        ‚t          ||¦  «        }|||<   d}|D ]d}	 ||         }n,# t          $ r t          |d¦  «        }d|_        |||<   Y nw xY w||j        k     r|j        }|j	         
                    |¦  «         Œe|dz   |_        t          |g¦  «        }	|	r[|	                     ¦   «         }|j        dz   }
|j	        D ]1}||         }|j        |
k     r|
|_        |	 
                    |¦  «         Œ2|	°YdS dS )aj  Add a new node to the graph.

        If this fills in a ghost, then the gdfos of all children will be
        updated accordingly.

        :param key: The node being added. If this is a duplicate, this is a
            no-op.
        :param parent_keys: The parents of the given node.
        :return: None (should we return if this was a ghost, etc?)
        Nz#Parent key mismatch, existing node z has parents of z not r   r   )r)   r   r*   ÚclearÚlistÚ
ValueErrorr   r1   r   r   r2   r   Úpopleft)r   r
   r   r3   r4   Úexisting_parent_keysÚparent_gdfor5   r6   rF   Ú	next_gdforG   rH   s                r   Úadd_nodezKnownGraph.add_nodeˆ   sý  € ð ”ˆØ�%ˆ<ˆ<Ø˜”:ˆDØÔÐ'Ø#.�Ô ð Ô!×'Ò'Ñ)Ô)Ð)Ð)õ # ;Ñ/Ô/�Ý'+¨DÔ,<Ñ'=Ô'=Ð$ØÐ"6Ò6Ð6Ø�Få$˜*ð ˜3˜3Ð 4Ð 4Ð 4°k°kðCñDô Dð Dõ
 # 3¨Ñ4Ô4ˆDØˆE�#‰JØˆØ%ð 
	/ð 
	/ˆJð0Ø# JÔ/��øÝð 0ð 0ð 0Ý-¨j¸$Ñ?Ô?�à#$�Ô Ø$/��jÑ!Ð!Ð!ð	0øøøð
 ˜[Ô-Ò-Ð-Ø)Ô.�ØÔ"×)Ò)¨#Ñ.Ô.Ð.Ð.Ø !‘OˆŒ	õ ˜˜‘-”-ˆØð 		*Ø—?’?Ñ$Ô$ˆDØœ	 A™ˆIØ!œ_ð *ð *�	Ø˜iÔ(�Ø”: 	Ò)Ð)ð "+�E”JØ—N’N 5Ñ)Ô)Ð)øð ð 		*ð 		*ð 		*ð 		*ð 		*s   ÂB$Â$&CÃCc                 ó<  ‡ — ˆ fd„|D ¦   «         }t           j        |v r;|                     t           j        ¦  «         |st          t           j        g¦  «        S t	          |¦  «        dk     rt          |¦  «        S t          |¦  «        }	 ‰ j        |         }|S # t          $ r Y nw xY wt          ¦   «         }g }d}|                     ¦   «         D ]7}|j	        r| 
                    |j	        ¦  «         |�|j        |k     r|j        }Œ8‰ j        }	|re|                     ¦   «         }
|
|v rŒ|                     |
¦  «         |	|
         }|j        |k    rŒD|j	        r| 
                    |j	        ¦  «         |°e|                     |¦  «        }‰ j        r
|‰ j        |<   |S )a+  Return the heads from amongst keys.

        This is done by searching the ancestries of each key.  Any key that is
        reachable from another key is not returned; all the others are.

        This operation scales with the relative depth between any two keys. It
        uses gdfo to avoid walking all ancestry.

        :param keys: An iterable of keys.
        :return: A set of the heads. Note that as a set there is no ordering
            information. Callers will need to filter their input to create
            order if they need it.
        c                 ó,   •— i | ]}|‰j         |         “ŒS r   )r)   )r:   r
   r   s     €r   ú
<dictcomp>z$KnownGraph.heads.<locals>.<dictcomp>Ø   s"   ø€ ÐAÐAÐA°S˜3 ¤¨CÔ 0ÐAÐAÐAr   é   N)r   ÚNULL_REVISIONrC   Ú	frozensetrD   r*   r1   Úsetr=   r   Úextendr   r)   ÚaddÚ
differencer+   )r   ÚkeysÚcandidate_nodesÚ	heads_keyÚheadsÚseenrF   Úmin_gdfor4   r3   Únode_keys   `          r   ra   zKnownGraph.headsÊ   sß  ø€ ð BÐAÐAÐA¸DÐAÑAÔAˆÝÔ! _Ð4Ð4à×Ò¥Ô 6Ñ7Ô7Ð7Ø"ð ;Ý ¥(Ô"8Ð!9Ñ:Ô:Ð:ÝˆÑÔ !Ò#Ð#å˜_Ñ-Ô-Ð-Ý˜oÑ.Ô.ˆ	ð	ØÔ% iÔ0ˆEØˆLøÝð 	ð 	ð 	ØˆDð	øøøõ ‰uŒuˆØˆØˆØ#×*Ò*Ñ,Ô,ð 	%ð 	%ˆDØÔð 1Ø—’˜tÔ/Ñ0Ô0Ð0ØÐ 4¤9¨xÒ#7Ð#7Øœ9�øØ”ˆØð 
	1Ø—{’{‘}”}ˆHØ˜4ÐÐàØ�HŠH�XÑÔÐØ˜”?ˆDØŒy˜HÒ$Ð$ØØÔð 1Ø—’˜tÔ/Ñ0Ô0Ð0ð ð 
	1ð ×$Ò$ TÑ*Ô*ˆØŒ=ð 	1Ø+0ˆDÔ˜iÑ(Øˆs   ÂB Â
B'Â&B'c                 óú  — | j                              ¦   «         D ]"}|j        €t          j        | j         ¦  «        ‚Œ#|                      ¦   «         }|j        }|j        }g }|j        }t           	                    | j         d¦  «        }|rq |¦   «         }|j
        � ||j        ¦  «         |j        D ]F}| j         |         }	||         dz   }
|
t          |	j
        ¦  «        k    r ||	¦  «         ||= ŒA|
||<   ŒG|°q|S )zdReturn the nodes in topological order.

        All parents must occur before all children.
        Nr   r   )r)   r=   r   r   ÚGraphCycleErrorr>   rC   r2   ÚdictÚfromkeysr   r
   r   rD   )r   r4   rF   Úpending_popÚpending_appendÚ
topo_orderÚtopo_order_appendÚnum_seen_parentsrG   Ú
child_nodeÚseen_parentss              r   Ú	topo_sortzKnownGraph.topo_sort  s0  € ð
 ”K×&Ò&Ñ(Ô(ð 	:ð 	:ˆDØŒyÐ ÝÔ,¨T¬[Ñ9Ô9Ð9ð !à×"Ò"Ñ$Ô$ˆØ”kˆØ œˆàˆ
Ø&Ô-ÐåŸ=š=¨¬°aÑ8Ô8ÐØð 	?Ø�;‘=”=ˆDØÔÐ+à!Ð! $¤(Ñ+Ô+Ð+Ø!œ_ð 	?ð 	?�	Ø!œ[¨Ô3�
Ø/°	Ô:¸QÑ>�Ø¥3 zÔ'=Ñ#>Ô#>Ò>Ð>à"�N :Ñ.Ô.Ð.à(¨Ð3Ð3à2>Ð$ YÑ/Ð/ð ð 	?ð  Ðr   c                 óà  — |                       ¦   «         }i }|D ]f}|j        j        t          u st	          |j        ¦  «        dk    rd}n|j        d         }|                     |g ¦  «                             |¦  «         Œgt                               | j	        d¦  «        }g }t          |¦  «        D ]¼}t          ||         d„ d¬¦  «        }|rŸ|                     ¦   «         }|j        €Œ|                     |j        ¦  «         t          |j        d¬¦  «        D ]P}| j	        |         }	||         dz   }
|
t	          |	j        ¦  «        k    r|                     |	¦  «         ||= ŒK|
||<   ŒQ|°ŸŒ½|S )	aØ  Return a reverse topological ordering which is 'stable'.

        There are a few constraints:
          1) Reverse topological (all children before all parents)
          2) Grouped by prefix
          3) 'stable' sorting, so that we get the same result, independent of
             machine, or extra data.
        To do this, we use the same basic algorithm as topo_sort, but when we
        aren't sure what node to access next, we sort them lexicographically.
        r   Ú r   c                 ó   — | j         S r   )r
   )Úns    r   ú<lambda>z$KnownGraph.gc_sort.<locals>.<lambda>=  s   € ÀÄ€ r   T)r
   ÚreverseN)rv   )rA   r
   r   ÚstrrD   Ú
setdefaultr2   rg   rh   r)   ÚsortedrC   r   r   )r   ÚtipsÚprefix_tipsr4   ÚprefixÚnum_seen_childrenÚresultrF   r5   r6   Úseen_childrens              r   Úgc_sortzKnownGraph.gc_sort$  sž  € ð �ŠÑ Ô ˆàˆØð 	<ð 	<ˆDØŒxÔ!¥SÐ(Ð(­C°´©M¬M¸QÒ,>Ð,>Ø��àœ !œ�Ø×"Ò" 6¨2Ñ.Ô.×5Ò5°dÑ;Ô;Ð;Ð;å ŸMšM¨$¬+°qÑ9Ô9ÐàˆÝ˜[Ñ)Ô)ð 	Fð 	FˆFÝ˜[¨Ô0°o°oØ%)ð+ñ +ô +ˆGàð FØ—{’{‘}”}�ØÔ#Ð+àØ—’˜dœhÑ'Ô'Ð'Ý"(¨Ô)9À4Ð"HÑ"HÔ"Hð 	Fð 	F�JØ"&¤+¨jÔ"9�KØ$5°jÔ$AÀAÑ$E�MØ$­¨KÔ,BÑ(CÔ(CÒCÐCàŸš {Ñ3Ô3Ð3à-¨jÐ9Ð9à8EÐ)¨*Ñ5Ð5ð ð Føð  ˆr   c                 óš   — ddl m} d„ | j                             ¦   «         D ¦   «         }d„ |                     ||dd¬¦  «        D ¦   «         S )z&Compute the merge sorted graph output.r   )Útsortc                 ó6   — i | ]}|j         ®	|j        |j         “ŒS r   )r   r
   r9   s     r   rV   z)KnownGraph.merge_sort.<locals>.<dictcomp>T  s2   € ð >ð >ð >Ø!%Ø $Ô 0Ð <ð œ 4Ô#3à <Ð <Ð <r   c                 ó>   — g | ]\  }}}}}t          ||||¦  «        ‘ŒS r   )r    )r:   Ú_r
   r"   r#   r$   s         r   r;   z)KnownGraph.merge_sort.<locals>.<listcomp>Z  s@   € ð :ð :ð :Ù<�A�s˜K¨°õ ˜s K°¸ÑEÔEð :ð :ð :r   NT)Úmainline_revisionsÚgenerate_revno)Úbreezyr‚   r)   r=   Ú
merge_sort)r   Útip_keyr‚   Úas_parent_maps       r   r‰   zKnownGraph.merge_sortQ  s€   € à Ð Ð Ð Ð Ð ð>ð >Ø)-¬×);Ò);Ñ)=Ô)=ð>ñ >ô >ˆð:ð :à×#Ò# M°7Ø7;Ø37ð $ñ 9ô 9ð:ñ :ô :ð 	:r   c                 ó&   — | j         |         j        S )a  Get the parents for a key

        Returns a list containg the parents keys. If the key is a ghost,
        None is returned. A KeyError will be raised if the key is not in
        the graph.

        :param keys: Key to check (eg revision_id)
        :return: A list of parents
        )r)   r   ©r   r
   s     r   Úget_parent_keyszKnownGraph.get_parent_keys`  s   € ð Œ{˜3ÔÔ+Ð+r   c                 ó&   — | j         |         j        S )zñGet the children for a key

        Returns a list containg the children keys. A KeyError will be raised
        if the key is not in the graph.

        :param keys: Key to check (eg revision_id)
        :return: A list of children
        )r)   r   r�   s     r   Úget_child_keyszKnownGraph.get_child_keysl  s   € ð Œ{˜3ÔÔ*Ð*r   N)T)r   r   r   r   r   r,   r>   rA   r-   rS   ra   rp   r€   r‰   rŽ   r�   r   r   r   r'   r'   9   sÚ   € € € € € ØGÐGð
ð 
ð 
ð 
ð3ð 3ð 3ð2)ð )ð )ð(ð (ð (ð?ð ?ð ?ð>@*ð @*ð @*ðD6ð 6ð 6ðp ð  ð  ðD+ð +ð +ðZ:ð :ð :ð
,ð 
,ð 
,ð	+ð 	+ð 	+ð 	+ð 	+r   r'   N)	r   Úcollectionsr   rr   r   r   r   r    r'   r   r   r   ú<module>r’      s×   ðð"ð ð Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð/ð /ð /ð /ð /ñ /ô /ð /ð$	)ð 	)ð 	)ð 	)ð 	)ñ 	)ô 	)ð 	)ð|+ð |+ð |+ð |+ð |+ñ |+ô |+ð |+ð |+ð |+r   