a
    yn„f1%  ã                   @  st   d dl mZ d dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ dZe	 ¡ Zeƒ dddd	œd
d„ƒZdd„ ZdS )é    )ÚannotationsN)Úhigh_level_function)ÚHighLevelContext)ÚNumpyMetadata)Úunknown_length)Úrun_lengthsT)Ú	highlevelÚbehaviorÚattrsc                c  s   | fV  t | |||ƒS )aü  
    Args:
        array: Array-like data (anything #ak.to_layout recognizes).
        highlevel (bool): If True, return an #ak.Array; otherwise, return
            a low-level #ak.contents.Content subclass.
        behavior (None or dict): Custom #ak.behavior for the output array, if
            high-level.
        attrs (None or dict): Custom attributes for the output array, if
            high-level.

    Computes the lengths of sequences of identical values at the deepest level
    of nesting, returning an array with the same structure but with `int64` type.

    For example,

        >>> array = ak.Array([1.1, 1.1, 1.1, 2.2, 3.3, 3.3, 4.4, 4.4, 5.5])
        >>> ak.run_lengths(array)
        <Array [3, 1, 2, 2, 1] type='5 * int64'>

    There are 3 instances of 1.1, followed by 1 instance of 2.2, 2 instances of 3.3,
    2 instances of 4.4, and 1 instance of 5.5.

    The order and uniqueness of the input data doesn't matter,

        >>> array = ak.Array([1.1, 1.1, 1.1, 5.5, 4.4, 4.4, 1.1, 1.1, 5.5])
        >>> ak.run_lengths(array)
        <Array [3, 1, 2, 2, 1] type='5 * int64'>

    just the difference between each value and its neighbors.

    The data can be nested, but runs don't cross list boundaries.

        >>> array = ak.Array([[1.1, 1.1, 1.1, 2.2, 3.3], [3.3, 4.4], [4.4, 5.5]])
        >>> ak.run_lengths(array)
        <Array [[3, 1, 1], [1, 1], [1, 1]] type='3 * var * int64'>

    This function recognizes strings as distinguishable values.

        >>> array = ak.Array([["one", "one"], ["one", "two", "two"], ["three", "two", "two"]])
        >>> ak.run_lengths(array)
        <Array [[2], [1, 2], [1, 2]] type='3 * var * int64'>

    Note that this can be combined with #ak.argsort and #ak.unflatten to compute
    a "group by" operation:

        >>> array = ak.Array([{"x": 1, "y": 1.1}, {"x": 2, "y": 2.2}, {"x": 1, "y": 1.1},
        ...                   {"x": 3, "y": 3.3}, {"x": 1, "y": 1.1}, {"x": 2, "y": 2.2}])
        >>> sorted = array[ak.argsort(array.x)]
        >>> sorted.x
        <Array [1, 1, 1, 2, 2, 3] type='6 * int64'>
        >>> ak.run_lengths(sorted.x)
        <Array [3, 2, 1] type='3 * int64'>
        >>> ak.unflatten(sorted, ak.run_lengths(sorted.x)).show()
        [[{x: 1, y: 1.1}, {x: 1, y: 1.1}, {x: 1, y: 1.1}],
         [{x: 2, y: 2.2}, {x: 2, y: 2.2}],
         [{x: 3, y: 3.3}]]

    Unlike a database "group by," this operation can be applied in bulk to many sublists
    (though the run lengths need to be fully flattened to be used as `counts` for
    #ak.unflatten, and you need to specify `axis=-1` as the depth).

        >>> array = ak.Array([[{"x": 1, "y": 1.1}, {"x": 2, "y": 2.2}, {"x": 1, "y": 1.1}],
        ...                   [{"x": 3, "y": 3.3}, {"x": 1, "y": 1.1}, {"x": 2, "y": 2.2}]])
        >>> sorted = array[ak.argsort(array.x)]
        >>> sorted.x
        <Array [[1, 1, 2], [1, 2, 3]] type='2 * var * int64'>
        >>> ak.run_lengths(sorted.x)
        <Array [[2, 1], [1, 1, 1]] type='2 * var * int64'>
        >>> counts = ak.flatten(ak.run_lengths(sorted.x), axis=None)
        >>> ak.unflatten(sorted, counts, axis=-1).show()
        [[[{x: 1, y: 1.1}, {x: 1, y: 1.1}], [{x: 2, y: 2.2}]],
         [[{x: 1, y: 1.1}], [{x: 2, y: 2.2}], [{x: 3, y: 3.3}]]]

    See also #ak.num, #ak.argsort, #ak.unflatten.
    )Ú_impl)Úarrayr   r	   r
   © r   úd/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/operations/ak_run_lengths.pyr      s    Nr   c                   sp   t ||d�� }|j| ddd�‰ W d   ƒ n1 s20    Y  ‡ fdd„‰‡fdd„}tj ˆ |¡}|j||d	�S )
N)r	   r
   FÚerror)Zallow_recordZprimitive_policyc                   s<  ˆ j }|j | ¡r| j}nt | ¡j}|turL|dkrL|jj	dt
jd�|fS |j | dd … | d d… k¡}|d ur¦|j d|k ||j |¡k ¡}|| }d||d < |j |¡d }|jj	|jd t
jd�}d|d< |j |¡|d< |d |dd…< |dd … |d d…  }	|d u �rd }
n|jj||dd�}
|	|
fS d S )	Nr   )Zdtypeé   éÿÿÿÿTé   Úleft)Zside)ÚbackendZnplikeZis_own_arrayÚsizeÚakZ	to_layoutÚlengthr   Zindex_nplikeÚemptyÚnpZint64ZasarrayÚlogical_andZshape_item_as_indexZnonzeroZsearchsorted)ÚdataÚoffsetsr   r   ZdiffsZis_interiorZinterior_offsetsZ	positionsZfull_positionsÚnextcontentÚnextoffsets)Úlayoutr   r   Ú
lengths_ofh   s8     þÿ
ÿz_impl.<locals>.lengths_ofc                   s"  | j dkrž| jr|  ¡ } |  d¡dks4|  d¡dkrVˆ tj | ¡d ƒ\}}tj |¡S | j	rj|  
tj¡} n| js‚tdt| ƒj ƒ‚ˆ | jd ƒ\}}tj |¡S | j dk�r| jr¸|  ¡ } | jsÐtdt| ƒj ƒ‚| j d¡dksò| j d¡dk�rj|  d¡}|j|jd |jd	 … }|j�r&| ¡ }|jj}ˆ tj |¡||d  ƒ\}}tj tj |¡tj |¡¡S |  d¡}|j|jd |jd	 … }|j�rž| ¡ }|j	�r´| 
tj¡}n(|j�sÜtdt| ƒj d
 t|ƒj ƒ‚|jj}ˆ |j||d  ƒ\}}tj tj |¡tj |¡¡S d S d S )N)Fr   Z	__array__ÚstringÚ
bytestringzrun_lengths on )Fr   Fr   r   z with content )Zbranch_depthZ
is_indexedÚprojectZ	parameterr   r   ZArrayÚcontentsZ
NumpyArrayZ
is_unknownZto_NumpyArrayr   Zfloat64Zis_numpyÚNotImplementedErrorÚtypeÚ__name__r   Zis_listÚcontentZto_ListOffsetArray64r   ZListOffsetArrayÚindexZIndex64)r   Úkwargsr   Ú_Zlistoffsetarrayr(   r   r   )r    r   r   Úaction—   sz    
ÿþÿþ
ÿÿÿ
ÿÿþýÿÿz_impl.<locals>.action)r   )r   Úunwrapr   Z_doZrecursively_applyÚwrap)r   r   r	   r
   Úctxr,   Úoutr   )r   r    r   r   d   s    ./Kr   )Ú
__future__r   Zawkwardr   Zawkward._dispatchr   Zawkward._layoutr   Zawkward._nplikes.numpy_liker   Zawkward._nplikes.shaper   Ú__all__Úinstancer   r   r   r   r   r   r   Ú<module>   s   S