a
    RG5d  ã                   @   s0   d dl mZ dd„ Zdd„ Zdd„ Zdd	„ Zd
S )é    )ÚOrderedDictc                    s`   ˆ s
dgS t ˆ d tƒs:tˆ dd… ƒ}‡ fdd„|D ƒS tˆ dd… ƒ}‡ fdd„|D ƒS dS )z¨
    >>> from sympy.multipledispatch.utils import expand_tuples
    >>> expand_tuples([1, (2, 3)])
    [(1, 2), (1, 3)]

    >>> expand_tuples([1, 2])
    [(1, 2)]
    © r   é   Nc                    s   g | ]}ˆ d  f| ‘qS ©r   r   )Ú.0Út©ÚLr   úX/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/multipledispatch/utils.pyÚ
<listcomp>   ó    z!expand_tuples.<locals>.<listcomp>c                    s$   g | ]}ˆ d  D ]}|f| ‘qqS r   r   )r   r   Úitemr   r   r
   r      r   )Ú
isinstanceÚtupleÚexpand_tuples)r	   Úrestr   r   r
   r      s    	r   c                    s´   t | ƒ‰ dd„ ˆ  ¡ D ƒ‰ t ‡ fdd„| D ƒ¡}g }|r’| ¡ \}}| |¡ |  |d¡D ]2}|ˆ | v spJ ‚ˆ |  |¡ ˆ | s\d||< q\q6t‡ fdd„| D ƒƒr°t	dƒ‚|S )	a2   Topological sort algorithm by Kahn [1] - O(nodes + vertices)

    inputs:
        edges - a dict of the form {a: {b, c}} where b and c depend on a
    outputs:
        L - an ordered list of nodes that satisfy the dependencies of edges

    >>> from sympy.multipledispatch.utils import _toposort
    >>> _toposort({1: (2, 3), 2: (3, )})
    [1, 2, 3]

    Closely follows the wikipedia page [2]

    [1] Kahn, Arthur B. (1962), "Topological sorting of large networks",
    Communications of the ACM
    [2] https://en.wikipedia.org/wiki/Toposort#Algorithms
    c                 S   s   i | ]\}}|t |ƒ“qS r   )Úset)r   ÚkÚvalr   r   r
   Ú
<dictcomp>,   r   z_toposort.<locals>.<dictcomp>c                 3   s   | ]}|ˆ vr|V  qd S ©Nr   ©r   Úv©Zincoming_edgesr   r
   Ú	<genexpr>-   r   z_toposort.<locals>.<genexpr>r   Nc                 3   s   | ]}ˆ   |d ¡V  qd S r   )Úgetr   r   r   r
   r   8   r   zInput has cycles)
Úreverse_dictÚitemsr   ÚfromkeysÚpopitemÚappendr   ÚremoveÚanyÚ
ValueError)ÚedgesÚSr	   ÚnÚ_Úmr   r   r
   Ú	_toposort   s    
r)   c                 C   s8   i }| D ]*}| | D ]}|  |tƒ ¡|f ||< qq|S )a›  Reverses direction of dependence dict

    >>> d = {'a': (1, 2), 'b': (2, 3), 'c':()}
    >>> reverse_dict(d)  # doctest: +SKIP
    {1: ('a',), 2: ('a', 'b'), 3: ('b',)}

    :note: dict order are not deterministic. As we iterate on the
        input dict, it make the output of this function depend on the
        dict order. So this function output order should be considered
        as undeterministic.

    )r   r   )ÚdÚresultÚkeyr   r   r   r
   r   =   s
    r   c                 C   s8   i }|D ]*}| |ƒ}||vr$g ||< ||   |¡ q|S )aÛ   Group a collection by a key function

    >>> from sympy.multipledispatch.utils import groupby
    >>> names = ['Alice', 'Bob', 'Charlie', 'Dan', 'Edith', 'Frank']
    >>> groupby(len, names)  # doctest: +SKIP
    {3: ['Bob', 'Dan'], 5: ['Alice', 'Edith', 'Frank'], 7: ['Charlie']}

    >>> iseven = lambda x: x % 2 == 0
    >>> groupby(iseven, [1, 2, 3, 4, 5, 6, 7, 8])  # doctest: +SKIP
    {False: [1, 3, 5, 7], True: [2, 4, 6, 8]}

    See Also:
        ``countby``
    )r    )ÚfuncÚseqr*   r   r,   r   r   r
   ÚgroupbyS   s    r/   N)Úcollectionsr   r   r)   r   r/   r   r   r   r
   Ú<module>   s   $