
    3js$              
      V   d Z ddlmZ ddlmZ ddlZddlm	Z	 ddl
mZ  G d dej                        Z ej                  d	d
efde	fdefdefdefdefg      Z G d d	e      Z edddddd      Z edde ddd      Z eddeddd      Zedk(  rddl
Z
 e
j,                          yy)a  
This module defines a single class, SortTuple, which is a named tuple that can
sort against bare offsets and other SortTuples.

This is a performance-critical object.

It also defines three singleton instances of the SortTupleLow class as ZeroSortTupleDefault,
ZeroSortTupleLow and
ZeroSortTupleHigh which are sortTuple at
offset 0.0, priority [0, -inf, inf] respectively:

>>> sorting.ZeroSortTupleDefault
SortTuple(atEnd=0, offset=0.0, priority=0, classSortOrder=0, isNotGrace=1, insertIndex=0)
>>> sorting.ZeroSortTupleLow
SortTuple(atEnd=0, offset=0.0, priority=-inf, classSortOrder=0, isNotGrace=1, insertIndex=0)
>>> sorting.ZeroSortTupleHigh
SortTuple(atEnd=0, offset=0.0, priority=inf, classSortOrder=0, isNotGrace=1, insertIndex=0)
    )annotations)infN)OffsetQL)exceptions21c                      e Zd Zy)SortingExceptionN)__name__
__module____qualname__     </DATA/.local/lib/python3.12/site-packages/music21/sorting.pyr   r   $   s    r   r   	SortTupleatEndoffsetpriorityclassSortOrder
isNotGraceinsertIndexc                  j     e Zd ZdZ fdZ fdZ fdZ fdZd Zd Z	d Z
d	 Zd
 Zd Zd Z xZS )r   a  
    Derived class of namedTuple which allows for comparisons with pure ints/fractions.

    >>> n = note.Note()
    >>> s = stream.Stream()
    >>> s.insert(4, n)
    >>> st = n.sortTuple()
    >>> st
    SortTuple(atEnd=0, offset=4.0, priority=0, classSortOrder=20, isNotGrace=1, insertIndex=...)
    >>> st.shortRepr()
    '4.0 <0.20...>'
    >>> st.atEnd
    0
    >>> st.offset
    4.0

    >>> st < 5.0
    True
    >>> 5.0 > st
    True
    >>> st > 3.0
    True
    >>> 3.0 < st
    True

    >>> st == 4.0
    True

    >>> ts = bar.Barline('double')
    >>> s2 = stream.Stream()
    >>> s2.storeAtEnd(ts)
    >>> ts_st = ts.sortTuple()
    >>> ts_st
    SortTuple(atEnd=1, offset=0.0, priority=0, classSortOrder=-5, isNotGrace=1, insertIndex=...)
    >>> st < ts_st
    True
    >>> ts_st > 999999
    True
    >>> import math
    >>> ts_st == math.inf
    True

    Construct one w/ keywords:

    >>> st = sorting.SortTuple(atEnd=0, offset=1.0, priority=0, classSortOrder=20,
    ...           isNotGrace=1, insertIndex=323)
    >>> st.shortRepr()
    '1.0 <0.20.323>'

    or as tuple:

    >>> st = sorting.SortTuple(0, 1.0, 0, 20, 1, 323)
    >>> st.shortRepr()
    '1.0 <0.20.323>'

    c                2    t        t        | 
  | g|i |S N)superr   __new__)clstupElskeywords	__class__s      r   r   zSortTuple.__new__j   s    Y,SF6FXFFr   c                    t        |t              rt        |   |      S 	 | j                  dk(  r
|t
        k7  ry| j                  dk(  ry| j                  |k(  S # t        $ r	 t        cY S w xY w)N   FT)	
isinstancetupler   __eq__r   INFINITYr   
ValueErrorNotImplementedselfotherr   s     r   r#   zSortTuple.__eq__n   sh    eU#7>%((	"zzQ5H#4q{{e++ 	"!!	"   A A A A,+A,c                    t        |t              rt        |   |      S 	 | j                  dk(  ry| j
                  |k  S # t        $ r	 t        cY S w xY w)Nr    F)r!   r"   r   __lt__r   r   r%   r&   r'   s     r   r,   zSortTuple.__lt__{   sS    eU#7>%((	"zzQ{{U** 	"!!	"s   A A AAc                    t        |t              rt        |   |      S 	 | j                  dk(  r
|t
        k7  ry| j                  dk(  ry| j                  |kD  S # t        $ r	 t        cY S w xY w)Nr    TF)	r!   r"   r   __gt__r   r$   r   r%   r&   r'   s     r   r.   zSortTuple.__gt__   sh    eU#7>%((	"zzQ5H#4q{{U** 	"!!	"r*   c                &    | j                  |       S r   )r#   r(   r)   s     r   __ne__zSortTuple.__ne__   s    ;;u%%%r   c                J    | j                  |      xs | j                  |      S r   )r,   r#   r0   s     r   __le__zSortTuple.__le__       {{5!7T[[%77r   c                J    | j                  |      xs | j                  |      S r   )r.   r#   r0   s     r   __ge__zSortTuple.__ge__   r4   r   c                L   g }| j                   r|j                  d       n$|j                  t        | j                               |j                  d       |j                  t        | j                               |j                  d       |j                  t        | j
                               | j                  dk(  r|j                  d       |j                  d       |j                  t        | j                               |j                  d       dj                  |      S )a  
        Returns a nice representation of a SortTuple

        >>> st = sorting.SortTuple(atEnd=0, offset=1.0, priority=0, classSortOrder=20,
        ...           isNotGrace=1, insertIndex=323)
        >>> st.shortRepr()
        '1.0 <0.20.323>'

        >>> st = sorting.SortTuple(atEnd=1, offset=1.0, priority=4, classSortOrder=7,
        ...           isNotGrace=0, insertIndex=200)
        >>> st.shortRepr()
        'End <4.7.[Grace].200>'
        Endz <.r   z.[Grace]> )	r   appendstrr   r   r   r   r   join)r(   	reprPartss     r   	shortReprzSortTuple.shortRepr   s     	::U#S-.T]]+,T0012??aZ(T--./wwy!!r   c           
         | j                   D cg c]  }|j                  |t        | |              }} | j                  | S c c}w )a  
        return a new SortTuple identical to the previous, except with
        the given keyword modified.  Works only with keywords.

        >>> st = sorting.SortTuple(atEnd=0, offset=1.0, priority=0, classSortOrder=20,
        ...           isNotGrace=1, insertIndex=32)
        >>> st2 = st.modify(offset=2.0)
        >>> st2.shortRepr()
        '2.0 <0.20.32>'
        >>> st2
        SortTuple(atEnd=0, offset=2.0, priority=0, classSortOrder=20, isNotGrace=1, insertIndex=32)

        >>> st3 = st2.modify(atEnd=1, isNotGrace=0)
        >>> st3.shortRepr()
        'End <0.20.[Grace].32>'

        The original tuple is never modified (hence tuple):

        >>> st.offset
        1.0

        Changing offset, but nothing else, helps in creating .flatten() positions.
        )_fieldsgetgetattrr   )r(   r   attroutLists       r   modifyzSortTuple.modify   sG    2 HL||T|t8<<gdD&9:|Tt~~w'' Us   #Ac           
        t        || j                        st        d      | j                  D cg c]?  }|dv r t	        t        | |      t        ||            nt        | |      t        ||      z   A }} | j                  | S c c}w )a  
        Add all attributes from one sortTuple to another,
        returning a new one.

        >>> n = note.Note()
        >>> n.offset = 10
        >>> s = stream.Stream()
        >>> s.offset = 10
        >>> n.sortTuple()
        SortTuple(atEnd=0, offset=10.0, priority=0, classSortOrder=20, isNotGrace=1, insertIndex=0)
        >>> s.sortTuple()
        SortTuple(atEnd=0, offset=10.0, priority=0, classSortOrder=-20, isNotGrace=1, insertIndex=0)
        >>> s.sortTuple().add(n.sortTuple())
        SortTuple(atEnd=0, offset=20.0, priority=0, classSortOrder=0, isNotGrace=1, insertIndex=0)

        Note that atEnd and isNotGrace are equal to other's value. are upper bounded at 1 and
        take the maxValue of either.
        ,Cannot add attributes from a different classr   r   )r!   r   r   rB   maxrD   r(   r)   rE   rF   s       r   addzSortTuple.add   s    & %0"#QRR
 !%. !- 66 wtT*GE4,@A!$-t0DDF !- 	 .
 t~~w''.   ABc           
        t        || j                        st        d      | j                  D cg c]?  }|dv r t	        t        | |      t        ||            nt        | |      t        ||      z
  A }} | j                  | S c c}w )a  
        Subtract all attributes from to another.  atEnd and isNotGrace take the min value of either.

        >>> n = note.Note()
        >>> n.offset = 10
        >>> s = stream.Stream()
        >>> s.offset = 10
        >>> n.sortTuple()
        SortTuple(atEnd=0, offset=10.0, priority=0, classSortOrder=20, isNotGrace=1, insertIndex=0)
        >>> s.sortTuple()
        SortTuple(atEnd=0, offset=10.0, priority=0, classSortOrder=-20, isNotGrace=1, insertIndex=0)
        >>> s.sortTuple().sub(n.sortTuple())
        SortTuple(atEnd=0, offset=0.0, priority=0, classSortOrder=-40, isNotGrace=1, insertIndex=0)

        Note that atEnd and isNotGrace are lower bounded at 0.
        rI   rJ   )r!   r   r   rB   minrD   rL   s       r   subzSortTuple.sub   s    " %0"#QRR
  $||-  ,t 55 wtT*GE4,@A t,wud/CCE  , 	 -
 t~~w''-rN   )r	   r
   r   __doc__r   r#   r,   r.   r1   r3   r6   r@   rG   rM   rQ   __classcell__)r   s   @r   r   r   1   sA    7pG"	""&88">(8(:(r   g        r    )r   r   r   r   r   r   __main__)rR   
__future__r   mathr   r$   typingtmusic21.common.typesr   music21r   Music21Exceptionr   
NamedTupleint_SortTupleBaser   ZeroSortTupleDefaultZeroSortTupleLowZeroSortTupleHighr	   mainTestr   r   r   <module>rc      s   $ #    )  	|44 	 kcNxs3C, \( \(~ !qqQR,-1>  1SH9UV()q:  AcHUV)*; 
 zG r   