
    0Vh                       d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
 er
ddlmZmZmZ ddd	 	 	 	 	 	 	 dd	Ze
	 	 	 	 dd
       Ze
	 	 	 	 	 	 dd       Ze
	 	 	 	 	 	 	 	 dd       Ze
	 	 	 	 	 	 	 	 	 	 	 	 dd       ZddZddZy)zUtility functions for OpTree.    )annotations)IterableSequence)TYPE_CHECKINGAnyCallableoverload)STUNFkeyreversec                  t        |       }	 t        ||      S # t        $ r2 dd}ndfd}	 t        |||      cY S # t        $ r |cY cY S w xY ww xY w)zSort an iterable in a total order.

    This is useful for sorting objects that are not comparable, e.g., dictionaries with different
    types of keys.
    r   c                d    | j                   j                   d| j                   j                   | fS N.	__class__
__module____qualname__)xs    </home/dcms/DCMS/lib/python3.12/site-packages/optree/utils.pykey_fnz"total_order_sorted.<locals>.key_fn/   s/    ;;112!AKK4L4L3MNPQRR    c                v     |       }|j                   j                   d|j                   j                   |fS r   r   )r   yr   s     r   r   z"total_order_sorted.<locals>.key_fn4   s9    F;;112!AKK4L4L3MNPQRRr   )r   r   returnztuple[str, Any])listsorted	TypeError)iterabler   r   sequencer   s    `   r   total_order_sortedr$      sl     H~HhC99 ;S
S	 (@@ 	O	!s,    AA AAAAAc                    y N )iter1s    r   safe_zipr)   @   s     r   c                    y r&   r'   )r(   iter2s     r   r)   r)   G   s    
 r   c                    y r&   r'   )r(   r+   iter3s      r   r)   r)   O   s     r   c                    y r&   r'   )r(   r+   r-   iter4iterss        r   r)   r)   X   s     r   c            	        | D cg c]  }t        |t              r|n
t        |      ! }}t        t	        t        t        |                  dkD  r%t        dt        t        t        |                   t        | S c c}w )z=Strict zip that requires all arguments to be the same length.   zlength mismatch: )
isinstancer   r   lensetmap
ValueErrorzip)argsargseqss      r   r)   r)   c   sn    GKL:c8,C$s);LDL
3s3~!#,T#c4.-A,BCDD: Ms   $A=c                   g }g }| D ]'  \  }}|j                  |       |j                  |       ) t        |      t        |      fS )z2Unzip sequence of length-2 tuples into two tuples.)appendtuple)xysxsysr   r   s        r   unzip2rB   k   sL    
 
B	B 1
		!
		! 9eBir   )r"   Iterable[T]r   zCallable[[T], Any] | Noner   boolr   zlist[T])r(   rC   r   zzip[tuple[T]])r(   rC   r+   Iterable[S]r   zzip[tuple[T, S]])r(   rC   r+   rE   r-   zIterable[U]r   zzip[tuple[T, S, U]])r(   Iterable[Any]r+   rF   r-   rF   r/   rF   r0   rF   r   zip[tuple[Any, ...]])r9   rF   r   rG   )r?   zIterable[tuple[T, S]]r   z#tuple[tuple[T, ...], tuple[S, ...]])__doc__
__future__r   collections.abcr   r   typingr   r   r   r	   optree.typingr
   r   r   r$   r)   rB   r'   r   r   <module>rM      s7   $ " . 9 9 %% &*"" 
#	"
 " "J 
  
 
 	 
 
 
  
 
  	   

 r   