U
    ‰dÓ  ã                   @   s,   d dl mZ d
dd„Zddd„Zdd„ Zd	S )é    )Úhsm_pb2Únodec                 C   s(   t  ¡ }||_| D ]}|j |¡ q|S ©N)r   Ú	NodeProtoÚnameÚword_idsÚappend)Úwordsr   r   Úword© r   ú:/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/hsm_util.pyÚcreate_node_with_words   s
    r   c                 C   s0   t  ¡ }||_| D ]}|j ¡ }| |¡ q|S r   )r   r   r   ÚchildrenÚaddÚ	MergeFrom)Znodesr   r   Z
child_nodeZnew_child_noder   r   r   Úcreate_node_with_nodes   s    
r   c                    s>   d}dd„ ‰ ‡ ‡fdd„‰| j }t ¡ }g }ˆ||||ƒ}|S )Nr   c                 S   sD   t  ¡ }||_| D ],}|j ¡ }|d |_|d |_|d |_q|S )Nr   é   é   )r   Z	PathProtoZword_idZ
path_nodesr   ÚindexÚlengthÚtarget)Úpathr
   Z
path_protoÚentryZnew_path_noder   r   r   Úcreate_path%   s    


z%create_hierarchy.<locals>.create_pathc           	         sÎ   || _ | |t| jƒt| jƒ dg¡ |t| jƒt| jƒ 7 }|j|k rN||_t| jƒD ]"\}}||d d< ˆ||||ƒ}qXt| jƒD ]<\}}|t| jƒ |d d< ˆ ||ƒ}|j ¡ }| 	|¡ q†|d= |S )Nr   éÿÿÿÿr   )
Úoffsetr   Úlenr   r   ÚsizeÚ	enumerateÚpathsr   r   )	Z
node_protor   Úhierarchy_protoÚ	max_indexr   r   r
   Z
path_entryZnew_path_entry©r   Úrecursive_path_builderr   r   r#   /   s(     ÿ
ÿ

z0create_hierarchy.<locals>.recursive_path_builder)Z	root_noder   ZHierarchyProto)Z
tree_protor!   r   r    r   r   r"   r   Úcreate_hierarchy"   s    
r$   N)r   )r   )Zcaffe2.protor   r   r   r$   r   r   r   r   Ú<module>   s   	

	