o
    g                     @  s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ ddlmZ ddlmZ ededZg dZd+ddZ	d,d-ddZd.d#d$Zd/d(d)Zd*S )0zTopological sorting algorithms.    )annotations)Any)
Collection)DefaultDict)Iterable)Iterator)Sequence)Set)Tuple)TypeVar   )util)CircularDependencyError_T)bound)sortsort_as_subsetsfind_cyclestuplesCollection[Tuple[_T, _T]]allitemsCollection[_T]returnIterator[Sequence[_T]]c                 #  s    t t}| D ]\}}|| | qt|}t|  rSg }|D ]} || r0|| q"|s>tdt| |t	| 
|  fdd|D }|V   sd S d S )NzCircular dependency detected.c                   s   g | ]}| v r|qS  r   ).0ttodo_setr   ]/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/util/topological.py
<listcomp>6   s    z#sort_as_subsets.<locals>.<listcomp>)r   defaultdictsetaddlist
isdisjointappendr   r   
_gen_edgesdifference_update)r   r   edgesparentchildtodooutputnoder   r   r   r      s,   


r   Tdeterministic_orderboolIterator[_T]c                 c  s     t | |D ]}|E dH  qdS )a,  sort the given list of items by dependency.

    'tuples' is a list of tuples representing a partial ordering.

    deterministic_order is no longer used, the order is now always
    deterministic given the order of "allitems".    the flag is there
    for backwards compatibility with Alembic.

    N)r   )r   r   r/   set_r   r   r   r   :   s   r   Iterable[Tuple[_T, _T]]Iterable[_T]Set[_T]c                 C  s   t t}| D ]\}}|| | qt|}t }|D ]D}|g}||}	|r`|d }
||
 D ])}||v rI|||d  }|	| || ||	v rY|| |		|  nq0|
 }|s(q|S )N)r   r!   r"   r#   
differenceindexr(   updater&   removepop)r   r   r)   r*   r+   nodes_to_testr-   r.   stackr,   topcycr   r   r   r   M   s0   





r   r)   DefaultDict[_T, Set[_T]]Set[Tuple[_T, _T]]c                   s    fdd D S )Nc                   s"   h | ]} | D ]}||fqqS r   r   )r   leftrightr)   r   r   	<setcomp>x   s   " z_gen_edges.<locals>.<setcomp>r   rD   r   rD   r   r'   w   s   r'   N)r   r   r   r   r   r   )T)r   r   r   r   r/   r0   r   r1   )r   r3   r   r4   r   r5   )r)   r@   r   rA   )__doc__
__future__r   typingr   r   r   r   r   r   r	   r
   r    r   excr   r   __all__r   r   r   r'   r   r   r   r   <module>   s(   

*