
    &Vf                        d Z ddlmZ ddlmZmZ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! Zd+d%ZdS ),zUtility functions for OpTree.    )annotations)TYPE_CHECKINGAnyCallableIterableSequenceoverload)STUNFkeyreverseiterableIterable[T]r   Callable[[T], Any] | Noner   boolreturnlist[T]c                   t          |           }	 t          ||          S # t          $ r7 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   Nxr   r   tuple[str, Any]c                <    | j         j         d| j         j         | fS N.	__class__
__module____qualname__)r   s    I/var/www/html/software/conda/lib/python3.11/site-packages/optree/utils.pykey_fnz"total_order_sorted.<locals>.key_fn-   s&    ;1NNAK4LNNPQRR    c                T     |           }|j         j         d|j         j         |fS r   r   )r   yr   s     r    r!   z"total_order_sorted.<locals>.key_fn2   s4    CFF;1NNAK4LNNPQRRr"   )r   r   r   r   )listsorted	TypeError)r   r   r   sequencer!   s    `   r    total_order_sortedr)      s     H~~HhC9999   ;S S S S S
S S S S S S	 (@@@@@@ 	 	 	OOOOO	!s,   $ A%AA%A!A% A!!A%__iter1zip[tuple[T]]c                    d S N )r*   s    r    safe_zipr/   >   s	     Cr"   __iter2Iterable[S]zip[tuple[T, S]]c                    d S r-   r.   )r*   r0   s     r    r/   r/   E   s	    
 Cr"   __iter3Iterable[U]zip[tuple[T, S, U]]c                    d S r-   r.   )r*   r0   r4   s      r    r/   r/   M   s	     Cr"   Iterable[Any]__iter4__iterszip[tuple[Any, ...]]c                    d S r-   r.   )r*   r0   r4   r9   r:   s        r    r/   r/   V   s	     Cr"   c            	         d | D             }t          t          t          t           |                              dk    r2t          dt	          t          t           |                               t          | S )z=Strict zip that requires all arguments to be the same length.c                Z    g | ](}t          |t                    r|nt          |          )S r.   )
isinstancer   r%   ).0args     r    
<listcomp>zsafe_zip.<locals>.<listcomp>c   s1    LLL:c8,,;CC$s))LLLr"      zlength mismatch: )lensetmap
ValueErrorr%   zip)argsseqss     r    r/   r/   a   si    LLtLLLD
3s3~~!##CT#c4..-A-ACCDDD:r"   xysIterable[tuple[T, S]]#tuple[tuple[T, ...], tuple[S, ...]]c                    g }g }| D ]/\  }}|                     |           |                     |           0t          |          t          |          fS )z2Unzip sequence of length-2 tuples into two tuples.)appendtuple)rK   xsysr   r$   s        r    unzip2rS   i   sX    
 
B	B  1
		!
		!99eBiir"   )r   r   r   r   r   r   r   r   )r*   r   r   r+   )r*   r   r0   r1   r   r2   )r*   r   r0   r1   r4   r5   r   r6   )r*   r8   r0   r8   r4   r8   r9   r8   r:   r8   r   r;   )rK   rL   r   rM   )__doc__
__future__r   typingr   r   r   r   r   r	   optree.typingr
   r   r   r)   r/   rS   r.   r"   r    <module>rX      sK   $ # " " " " " " M M M M M M M M M M M M M M M M  &%%%%%%%%%% &*	! ! ! ! ! !H 
   
 
   
 
   
 
   
  
  
  
  
  
  
 r"   