
    \h                     (    S SK Jr  S rS rS rS rg)    )OrderedDictc                    U (       d  S/$ [        U S   [        5      (       d'  [        U SS 5      nU Vs/ s H  o S   4U-   PM     sn$ [        U SS 5      nU VVs/ s H  o S     H  o34U-   PM
     M     snn$ s  snf s  snnf )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       T/var/www/auris/envauris/lib/python3.13/site-packages/sympy/multipledispatch/utils.pyr	   r	      s     t!e$$QqrU#%)*T1!T**QqrU#%);Tdd!dT;; + <s   A<Bc                   ^ [        U 5      mTR                  5        VVs0 s H  u  pU[        U5      _M     snnm[        R                  " U4S jU  5       5      n/ nU(       at  UR                  5       u  pVUR                  U5        U R                  US5       H2  nUTU   ;   d   eTU   R                  U5        TU   (       a  M.  SX7'   M4     U(       a  Mt  [        U4S jU  5       5      (       a  [        S5      eU$ s  snnf )a  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   6   >#    U  H  oT;  d  M
  Uv   M     g 7fNr   .0vincoming_edgess     r   	<genexpr>_toposort.<locals>.<genexpr>-   s     I1.1HQQs   		r   Nc              3   H   >#    U  H  nTR                  US 5      v   M     g 7fr   getr   s     r   r   r   8   s!     
61>a&&s   "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G0FfaaSk0FGNIIIA
A
yy{	1b!Aq))))1$$Q'!!$$	 " ! 
6
666+,,H Hs   D c                 b    0 nU  H&  nX    H  nUR                  US5      U4-   X'   M     M(     U$ )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   =   s?     F6C **S"-7FK   M    c                 d    0 nU H'  nU " U5      nXB;  a  / X$'   X$   R                  U5        M)     U$ )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   groupbyr4   S   s=      	A4j<AF	d	 
 Hr0   N)collectionsr   r	   r+   r   r4   r   r0   r   <module>r6      s    #<*!H,r0   