ó
    Š*£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       ÚY/home/mande/repo/quber/.venv/lib/python3.13/site-packages/sympy/multipledispatch/utils.pyr	   r	      s‰   € ö ØˆtˆÜ˜˜!™œe×$Ñ$Ü˜Q˜q˜r˜UÓ#ˆÙ%)Ó*¢T �1‘�˜!”¡TÑ*Ð*ä˜Q˜q˜r˜UÓ#ˆÙ%)Ô;¢T ¸µd¨d�˜!”±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¸.Ñ1HŸQ™Qªù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ì   ø€ ô$ " %Ó(€NØ0>×0DÑ0DÔ0FÔGÒ0F¡f a�aœ˜S›’kÑ0FÒG€NÜ×ÒÔI©ÓIÓI€AØ
€Aæ
Ø�y‰y‹{‰ˆØ	�‰�ŒØ—‘˜1˜bÖ!ˆ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ÛˆØ”6ˆCØ Ÿ*™* S¨"Ó-°°Ñ7ˆF‹Kó ñ ð €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ÛˆÙ�4‹jˆØ‹<ØˆA‰FØ	‰�‰�dÖñ	 ð
 €Hr0   N)Úcollectionsr   r	   r+   r   r4   r   r0   r   Ú<module>r6      s   ðÝ #ò<ò*!òHó,r0   