o
    =a+                     @   sJ   d Z ddlmZ ddlmZ g dZdd Zddd	Zd
d Zdd Z	dS )zTopological sorting algorithms.   )util)CircularDependencyError)sortsort_as_subsetsfind_cyclesc                 #   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   Q/var/www/html/gps/gps/lib/python3.10/site-packages/sqlalchemy/util/topological.py
<listcomp>'   s    z#sort_as_subsets.<locals>.<listcomp>)r   defaultdictsetaddlist
isdisjointappendr   r   
_gen_edgesdifference_update)tuplesallitemsedgesparentchildtodooutputnoder   r
   r   r      s,   


r   Tc                 c   s&    t | |D ]
}|D ]}|V  q
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   deterministic_orderset_sr   r   r   r   +   s   r   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   ;   s0   





r   c                    s   t  fdd D S )Nc                    s"   g | ]} | D ]}||fqqS r   r   )r   leftrightr   r   r   r   d   s   " z_gen_edges.<locals>.<listcomp>)r   r-   r   r-   r   r   c   s   r   N)T)
__doc__ r   excr   __all__r   r   r   r   r   r   r   r   <module>   s   
(