o
    @@£i%  ã                   @   s   d Z ddlZG dd„ dƒZdS )uÍ   
    In computer science, a disjoint-set data structure, also called a unionâ€“find data structure or mergeâ€“find set,
    is a data structure that stores a collection of disjoint (non-overlapping) sets.
é    Nc                   @   s¤   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
ej	dd fdd„Z
d
ej	dej	fdd„Zd
ej	dd fdd„Zdejejej	  fdd„Zd
ej	dej	dd fdd„ZdS )Údisjointsetu  
    In computer science, a disjoint-set data structure, also called a unionâ€“find data structure or mergeâ€“find set,
    is a data structure that stores a collection of disjoint (non-overlapping) sets.
    Equivalently, it stores a partition of a set into disjoint subsets.
    It provides operations for adding new sets, merging sets (replacing them by their union),
    and finding a representative member of a set.
    The last operation allows to find out efficiently if any two elements are in the same or different sets.
    c                 C   s   i | _ i | _d S ©N©Ú_parentsÚ_ranks©Úself© r	   ú]/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/borb/datastructure/disjoint_set.pyÚ__init__   s   
zdisjointset.__init__c                 C   s
   || j v S r   )r   )r   Úitemr	   r	   r
   Ú__contains__!   ó   
zdisjointset.__contains__c                 C   s
   | j  ¡ S r   )r   Ú__iter__r   r	   r	   r
   r   $   r   zdisjointset.__iter__c                 C   s
   t | jƒS r   )Úlenr   r   r	   r	   r
   Ú__len__'   r   zdisjointset.__len__ÚxÚreturnc                 C   s   || j |< d| j|< | S )zy
        Add an element to this disjointset
        :param x:   the element to be added
        :return:    self
        r   r   ©r   r   r	   r	   r
   Úadd.   s   

zdisjointset.addc                 C   s"   | j | |kr	|S |  | j | ¡S )z¾
        Find the root of an element in this disjointset
        :param x:   the element for which to find the root element
        :return:    the root element of the given element
        )r   Úfindr   r	   r	   r
   r   8   s   zdisjointset.findc                 C   s   t ƒ ‚)z€
        Remove an element from this disjointset
        :param x:   the element to be removed
        :return:    self
        )ÚNotImplementedErrorr   r	   r	   r
   ÚpopC   s   zdisjointset.popc                 C   sR   i }| j  ¡ D ]\}}|  |¡}||vrg ||< ||  |¡ qdd„ | ¡ D ƒS )z
        This function returns all equivalence sets in this disjointset
        :return:    all equivalence sets of this disjointset
        c                 S   s   g | ]\}}|‘qS r	   r	   )Ú.0ÚkÚvr	   r	   r
   Ú
<listcomp>V   s    z$disjointset.sets.<locals>.<listcomp>)r   Úitemsr   Úappend)r   Úcluster_parentsr   Ú_Úpr	   r	   r
   ÚsetsK   s   
zdisjointset.setsÚyc                 C   s„   |   |¡}|   |¡}||u r| S | j| | j| kr!|| j|< | S | j| | j| kr2|| j|< | S || j|< | j|  d7  < | S )zë
        Mark two elements in this disjointset as equivalent,
        propagating the equivalence throughout the disjointset
        :param x:   the first element
        :param y:   the second element
        :return:    self
        é   )r   r   r   )r   r   r#   Úx_parentÚy_parentr	   r	   r
   ÚunionX   s   


û

þzdisjointset.unionN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   ÚtypingÚAnyr   r   r   ÚListr"   r'   r	   r	   r	   r
   r      s    
r   )r+   r,   r   r	   r	   r	   r
   Ú<module>   s   