a
    #©bû  ã                   @   s"   d Z ddlmZ G dd„ dƒZdS )z
Union-find data structure.
é    )Úgroupsc                   @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú	UnionFindaŒ  Union-find data structure.

    Each unionFind instance X maintains a family of disjoint sets of
    hashable objects, supporting the following two methods:

    - X[item] returns a name for the set containing the given item.
      Each set is named by an arbitrarily-chosen one of its members; as
      long as the set remains unchanged it will keep the same name. If
      the item is not yet part of a set in X, a new singleton set is
      created for it.

    - X.union(item1, item2, ...) merges the sets containing each item
      into a single larger set.  If any item is not yet part of a set
      in X, it is added to X as one of the members of the merged set.

      Union-find data structure. Based on Josiah Carlson's code,
      https://code.activestate.com/recipes/215912/
      with significant additional changes by D. Eppstein.
      http://www.ics.uci.edu/~eppstein/PADS/UnionFind.py

    Nc                 C   s:   |du rd}i | _ i | _|D ]}d| j|< || j |< qdS )z¾Create a new empty union-find structure.

        If *elements* is an iterable, this structure will be initialized
        with the discrete partition on the given set of elements.

        N© é   )ÚparentsÚweights)ÚselfÚelementsÚxr   r   úb/home/yuhw/wc/wire-cell-python-pr-11/pr11/lib/python3.9/site-packages/networkx/utils/union_find.pyÚ__init__   s    
zUnionFind.__init__c                 C   sl   || j vr"|| j |< d| j|< |S |g}| j | }||d krT| |¡ | j | }q2|D ]}|| j |< qX|S )z:Find and return the name of the set containing the object.r   éÿÿÿÿ)r   r   Úappend)r   ÚobjectÚpathÚrootZancestorr   r   r   Ú__getitem__.   s    




zUnionFind.__getitem__c                 C   s
   t | jƒS )zBIterate through all items ever found or unioned by this structure.)Úiterr   ©r   r   r   r   Ú__iter__C   s    zUnionFind.__iter__c                 c   s0   | j  ¡ D ]}| | }q
t| j ƒ ¡ E dH  dS )a]  Iterates over the sets stored in this structure.

        For example::

            >>> partition = UnionFind("xyz")
            >>> sorted(map(sorted, partition.to_sets()))
            [['x'], ['y'], ['z']]
            >>> partition.union("x", "y")
            >>> sorted(map(sorted, partition.to_sets()))
            [['x', 'y'], ['z']]

        N)r   Úkeysr   Úvalues)r   r
   Ú_r   r   r   Úto_setsG   s    
zUnionFind.to_setsc                    sz   t t‡ fdd„|D ƒ‡ fdd„dd�ƒ}zt|ƒ}W n tyH   Y dS 0 |D ]&}ˆ j|  ˆ j| 7  < |ˆ j|< qNdS )z8Find the sets containing the objects and merge them all.c                    s   h | ]}ˆ | ’qS r   r   )Ú.0r
   r   r   r   Ú	<setcomp>_   ó    z"UnionFind.union.<locals>.<setcomp>c                    s
   ˆ j |  S )N)r   )Úrr   r   r   Ú<lambda>_   r   z!UnionFind.union.<locals>.<lambda>T)ÚkeyÚreverseN)r   ÚsortedÚnextÚStopIterationr   r   )r   ZobjectsÚrootsr   r   r   r   r   ÚunionZ   s    ÿÿzUnionFind.union)N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r%   r   r   r   r   r      s   
r   N)r)   Znetworkx.utilsr   r   r   r   r   r   Ú<module>   s   