o
    2h                     @  s   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dZe
d,ddZe
d-ddZe
d.ddZe
d/d#dZd0d%dZd1d)d*ZdS )2zUtility functions for OpTree.    )annotations)IterableSequence)TYPE_CHECKINGAnyCallableoverload)STUNFkeyreverseiterableIterable[T]r   Callable[[T], Any] | Noner   boolreturnlist[T]c                 sz   t | }zt| |dW S  ty<    du rd
dd}nd
 fd	d}z
t|||dW  Y S  ty;   | Y  Y S w 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                 S  s   | j j d| j j | fS N.	__class__
__module____qualname__)r    r   G/var/www/html/chatgem/venv/lib/python3.10/site-packages/optree/utils.pykey_fn/   s   z"total_order_sorted.<locals>.key_fnc                   s"    | }|j j d|j j |fS r   r   )r   yr   r   r   r   4   s   )r   r
   r   r   )listsorted	TypeError)r   r   r   sequencer   r   r!   r   total_order_sorted   s   r&   iter1zip[tuple[T]]c                C     d S Nr   )r'   r   r   r   safe_zip@   s   r+   iter2Iterable[S]zip[tuple[T, S]]c                C  r)   r*   r   )r'   r,   r   r   r   r+   G   s   iter3Iterable[U]zip[tuple[T, S, U]]c                C  r)   r*   r   )r'   r,   r/   r   r   r   r+   O   s   Iterable[Any]iter4iterszip[tuple[Any, ...]]c                G  r)   r*   r   )r'   r,   r/   r3   r4   r   r   r   r+   X   s   argsc                  G  sD   dd | D }t ttt |dkrtdttt | t| S )z=Strict zip that requires all arguments to be the same length.c                 S  s"   g | ]}t |tr|nt|qS r   )
isinstancer   r"   ).0argr   r   r   
<listcomp>e   s   " zsafe_zip.<locals>.<listcomp>   zlength mismatch: )lensetmap
ValueErrorr"   zip)r6   seqsr   r   r   r+   c   s   xysIterable[tuple[T, S]]#tuple[tuple[T, ...], tuple[S, ...]]c                C  s:   g }g }| D ]\}}| | | | qt|t|fS )z2Unzip sequence of length-2 tuples into two tuples.)appendtuple)rB   xsysr   r    r   r   r   unzip2k   s   
rI   )r   r   r   r   r   r   r   r   )r'   r   r   r(   )r'   r   r,   r-   r   r.   )r'   r   r,   r-   r/   r0   r   r1   )r'   r2   r,   r2   r/   r2   r3   r2   r4   r2   r   r5   )r6   r2   r   r5   )rB   rC   r   rD   )__doc__
__future__r   collections.abcr   r   typingr   r   r   r   optree.typingr	   r
   r   r&   r+   rI   r   r   r   r   <module>   s&   %

