o
    [h
                     @   sL   d Z ddlmZ ddlmZ g dZd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_cyclesFc           
      c   s    t t}| D ]\}}|| | q|rt jnt}||}|rN| }|D ]}	|||	 r4||	 q&|sBtdt| |t||	| |V  |s!d S d S )NzCircular dependency detected.)
r   defaultdictsetaddZ
OrderedSet
isdisjointr   r   
_gen_edgesdifference_update)
tuplesallitemsdeterministic_orderedgesparentchildSettodooutputnode r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/sqlalchemy/util/topological.pyr      s*   


r   c                 c   s(    t | ||D ]
}|D ]}|V  qqdS )zsort the given list of items by dependency.

    'tuples' is a list of tuples representing a partial ordering.
    'deterministic_order' keeps items within a dependency tier in list order.
    N)r   )r   r   r   Z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   updateappendremovepop)r   r   r   r   r   Znodes_to_testr   r   stackr   topZcycr   r   r   r   8   s0   





r   c                    s   t  fdd D S )Nc                    s"   g | ]} | D ]}||fqqS r   r   ).0leftrightr   r   r   
<listcomp>a   s   " z_gen_edges.<locals>.<listcomp>)r   r&   r   r&   r   r   `   s   r   N)F)
__doc__ r   excr   __all__r   r   r   r   r   r   r   r   <module>   s   

(