
    wg                     (    d dl mZ d Zd Zd Zd Zy)    )OrderedDictc                     | sdgS t        | d   t              s%t        | dd       }|D cg c]  }| d   f|z    c}S t        | dd       }|D cg c]  }| d   D ]  }|f|z   
  c}}S c c}w c c}}w )z
    >>> from sympy.multipledispatch.utils import expand_tuples
    >>> expand_tuples([1, (2, 3)])
    [(1, 2), (1, 3)]

    >>> expand_tuples([1, 2])
    [(1, 2)]
     r      N)
isinstancetupleexpand_tuples)Lresttitems       a/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/multipledispatch/utils.pyr	   r	      s     t!e$QqrU#%)*1!**QqrU#%);ad;d!;;; + <s   A,A1c                    t        |       j                         D ci c]  \  }}|t        |       c}}t        j                  fd| D              }g }|rf|j                         \  }}|j                  |       | j                  |d      D ]*  }||   v sJ |   j                  |       |   r&d||<   , |rft        fd| D              rt        d      |S c c}}w )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              3   ,   K   | ]  }|vs|  y wNr   .0vincoming_edgess     r   	<genexpr>z_toposort.<locals>.<genexpr>-   s     I1.1HQIs   	r   Nc              3   B   K   | ]  }j                  |d         y wr   getr   s     r   r   z_toposort.<locals>.<genexpr>8   s     
61>a&
6s   zInput has cycles)reverse_dictitemssetr   fromkeyspopitemappendr   removeany
ValueError)	edgeskvalSr
   n_mr   s	           @r   	_toposortr*      s    $ "%(N0>0D0D0FGfaaSkGNIIIA
A
yy{1	1b! 	Aq))))1$$Q'!!$!		  
6
66+,,H Hs   C#c                 ^    i }| D ]%  }| |   D ]  }|j                  |d      |fz   ||<    ' |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%   s       r   r   r   =   sM     F 8S6 	8C **S"-7F3K	88 M    c                 b    i }|D ]'  } | |      }||vrg ||<   ||   j                  |       ) |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.   s        r   groupbyr3   S   sH      	A 4ja<AcF	#d	
 Hr/   N)collectionsr   r	   r*   r   r3   r   r/   r   <module>r5      s    #<*!H,r/   