Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/tree.cpython-37.opt-1.pyc

B

�d{Tn)�@s�dZdZddlZddlmZddlmZmZmZGdd�de	�Z
dZd	Zd
Z
Gdd�de�ZGd
d�dee�ZGdd�de�Zejdd�dkr�eZnddlmZeZGdd�dee�Zefdd�Zefdd�ZGdd�de�ZGdd�de�ZdS)z.Base class to represent a tree structure.




zrestructuredtext en�N)�flatten)�VisitedMixIn�FilteredIterator�	no_filterc@seZdZdZdS)�NodeNotFoundz%raised when a node has not been foundN)�__name__�
__module__�__qualname__�__doc__�rr�D/opt/alt/python37/lib/python3.7/site-packages/logilab/common/tree.pyr!srzNo such sibling as '%s'zNo such child as '%s'zNo such node as '%s'c@s�eZdZdZd.dd�Zdd�Zd/dd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zd0dd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd1d*d+�Zd,d-�ZdS)2�Nodez)a basic tree node, characterized by an idNcCs||_d|_g|_dS)N)�id�parent�children)�self�nidrrr�__init__.sz
Node.__init__cCs
t|j�S)N)�iterr)rrrr�__iter__4sz
Node.__iter__rc	Cstdd||jj|jfg}|d7}xF|jD]<}y|�|�|��Wq*tk
rd|�|���Yq*Xq*Wd�|�S)Nz%s%s %s� ��
)�	__class__rrr�append�__str__�	TypeError�join)r�indent�s�childrrrr7szNode.__str__cCs|jS)N)r)rrrr�is_leafAszNode.is_leafcCs|j�|�||_dS)zadd a node to childrenN)rrr)rr rrrrDszNode.appendcCs|j�|�d|_dS)zremove a child nodeN)r�remover)rr rrrr"IszNode.removecCs|j�||�||_dS)zinsert a child nodeN)r�insertr)r�indexr rrrr#NszNode.insertcCs0|j�|�}|j�|�|j�||�||_dS)z!replace a child node with anotherN)rr$�popr#r)rZ	old_child�	new_child�irrr�replaceSszNode.replacecCs2y|j�|�Stk
r,tt|��YnXdS)z)return the sibling node that has given idN)r�get_child_by_idr�EX_SIBLING_NOT_FOUND)rrrrr�get_siblingZszNode.get_siblingcCsF|j}|dkrdS|j�|�}y|j|dStk
r@dSXdS)z>
        return the next sibling for this node if any
        N�)rrr$�
IndexError)rrr$rrr�next_siblingaszNode.next_siblingcCs8|j}|dkrdS|j�|�}|dkr4|j|dSdS)zB
        return the previous sibling for this node if any
        Nrr,)rrr$)rrr$rrr�previous_siblingoszNode.previous_siblingcCs:|��}y|�|d�Stk
r4tt|��YnXdS)zB
        return node in whole hierarchy that has given id
        r,N)�rootr)r�EX_NODE_NOT_FOUND)rrr0rrr�get_node_by_id|s
zNode.get_node_by_idc	Csd|j|kr|SxD|jD]:}|rBy|�|d�Stk
r@wYnX|j|kr|SqWtt|��dS)z*
        return child of given id
        r,N)rrr)r�EX_CHILD_NOT_FOUND)rrZrecurse�crrrr)�s

zNode.get_child_by_idc	Csrt|�dkrb|d|jkrbt|�dkr*|Sx6|jD],}y|�|dd��Stk
r\Yq2Xq2Wtt|��dS)zD
        return child of given path (path is a list of ids)
        rr,N)�lenrr�get_child_by_pathrr3)r�pathr4rrrr6�s
zNode.get_child_by_pathcCs |jdk	rd|j��SdSdS)z7
        return depth of this node in the tree
        Nr,r)r�depth)rrrrr8�s
z
Node.depthcCs"|jrdtdd�|jD��SdS)z9
        return depth of the tree from this node
        r,cSsg|]}|���qSr)�
depth_down)�.0r4rrr�
<listcomp>�sz#Node.depth_down.<locals>.<listcomp>)r�max)rrrrr9�szNode.depth_downcCst|���S)z=
        return the width of the tree from this node
        )r5�leaves)rrrr�width�sz
Node.widthcCs|jdk	r|j��S|S)z2
        return the root node of the tree
        N)rr0)rrrrr0�s

z	Node.rootcCs4g}|jr*x|jD]}||��7}qW|S|gSdS)zS
        return a list with all the leaves nodes descendant from this node
        N)rr=)rr=r rrrr=�szNode.leavescCs4|dkrg}|�|�x|jD]}|�|�qW|S)zL
        return a list with all the nodes descendant from this node
        N)rrr)rZ_listr4rrrr�s
zNode.flattencCs$|g}|jdk	r |�|j���|S)z8
        return list of parents up to root node
        N)r�extend�lineage)rZlstrrrr@�s
zNode.lineage)N)r)N)N)rrr	r
rrrr!rr"r#r(r+r.r/r2r)r6r8r9r>r0r=rr@rrrrr
+s,





	
r
c@seZdZdZdS)�VNodeza visitable node
    N)rrr	r
rrrrrA�srAc@s*eZdZdZd	dd�Zdd�Zdd�ZdS)
�
BinaryNodez*a binary node (i.e. only two children
    NcCs>t�|�|dk	s|dk	r:|r"|s&t�|�|�|�|�dS)N)rAr�AssertionErrorr)rZlhsZrhsrrrr�s


zBinaryNode.__init__cCs$|j�|�|j�||jd�dS)zDremove the child and replace this node with the other child
        rN)rr"rr()rr rrrr"�szBinaryNode.removecCs|jd|jdfS)zP
        return the left hand side and the right hand side of this node
        rr,)r)rrrr�	get_parts�szBinaryNode.get_parts)NN)rrr	r
rr"rDrrrrrB�s
rBr)rr)�UserListc@sJeZdZdZdd�Zddd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dS)�ListNodez&Used to manipulate Nodes as Lists
    cCst�|�t�|�||_dS)N)�
list_classrrAr)rrrrrs

zListNode.__init__rcCs&d|d|jjd�dd�|D��fS)Nz%s%s %srz, cSsg|]}t|��qSr)�str)r:�vrrrr;sz$ListNode.__str__.<locals>.<listcomp>)rrr)rrrrrrszListNode.__str__cCst�||�||_dS)zadd a node to childrenN)rGrr)rr rrrrszListNode.appendcCst�|||�||_dS)zadd a node to childrenN)rGr#r)rr$r rrrr#szListNode.insertcCst�||�d|_dS)zadd a node to childrenN)rGr"r)rr rrrr"szListNode.removecCst�||�}d|_dS)zadd a node to childrenN)rGr%r)rr$r rrrr%#szListNode.popcCs
t�|�S)N)rGr)rrrrr(szListNode.__iter__N)r)rrr	r
rrrr#r"r%rrrrrrFs
rFcCs�gg}}d\}}x�|r�||�r�|jrH|sH|�||f�d}|jd}q�|�|�|d7}y|ddj|}Wq�tk
r�d}Yq�Xnd}d}|dkr|r|��\}}d}qW|S)zq
    create a list with tree nodes for which the <filter> function returned true
    in a post order fashion
    )rrrr,���N)rrr-r%)�node�filter_func�l�stack�popedr$rrr�post_order_list-s(


rPcCs�gg}}d\}}x�|r�||�r�|s.|�|�|jrV|sV|�||f�d}|jd}q�|d7}y|ddj|}Wq�tk
r�d}Yq�Xnd}d}|dkrt|�dkr|��\}}d}qW|S)zp
    create a list with tree nodes for which the <filter> function returned true
    in a pre order fashion
    )rrrr,rJN)rrr-r5r%)rKrLrMrNrOr$rrr�pre_order_listIs*


rQc@seZdZdZddd�ZdS)�PostfixedDepthFirstIteratorzHa postfixed depth first iterator, designed to be used with visitors
    NcCst�||t|�dS)N)rrrP)rrKrLrrrrisz$PostfixedDepthFirstIterator.__init__)N)rrr	r
rrrrrrRfsrRc@seZdZdZddd�ZdS)�PrefixedDepthFirstIteratorzGa prefixed depth first iterator, designed to be used with visitors
    NcCst�||t|�dS)N)rrrQ)rrKrLrrrrosz#PrefixedDepthFirstIterator.__init__)N)rrr	r
rrrrrrSlsrS)r
Z
__docformat__�sysZlogilab.commonrZlogilab.common.visitorrrr�	Exceptionrr*r3r1�objectr
rArB�version_info�listrGrErFrPrQrRrSrrrr�<module>s*:%