a
    yn„f´¦  ã                   @  s�  d dl mZ d dlZd dlmZmZ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 dlmZ d d	lmZmZ d d
lmZ d dlmZ d dlmZ d dlmZmZ d dlm Z  d dl!m"Z" d dl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* d dl+m,Z, d dl-m.Z.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z: e$�rbd dl!m;Z; d dl<m=Z= e >¡ Z?e >¡ Z@e*G dd„ dee/ e/ƒƒZAdS )é    )ÚannotationsN)ÚMappingÚMutableMappingÚSequence)ÚBackend)Úmaybe_posaxis)ÚIndexedMeta)Ú	ArrayLike)ÚNumpy)Ú	IndexTypeÚNumpyMetadata)ÚPlaceholderArray)Ú	ShapeItem)Ú
TypeTracer)Úparameters_intersectÚparameters_union)Úis_integer_like)ÚNO_HEAD)ÚTYPE_CHECKINGÚAnyÚCallableÚFinalÚSelfÚSupportsIndexÚfinal)ÚUNSET)ÚApplyActionOptionsÚContentÚImplementsApplyActionÚRemoveStructureOptionsÚToArrowOptions)Ú	AxisError)ÚForm)ÚIndexedForm)ÚIndex)Ú	SliceItem)ÚIndexedOptionArrayc                   @  sø  e Zd ZU dZddœdd„Zedd„ ƒZeZde	d	< e
e
fe
dœd
d„Zdd„ Zdd„ Zeddœdd„ƒZdddœdd„Zddddddœdd„Zdd d!œd"d#„Zdd$œd%d&„Zdd$œd'd(„Zed)d*œd+d,„ƒZd-d.„ Zd/d0„ Zd1d*œd2d3„Zd«dd5d6œd7d8„Zd9d:„ Zdd*œd;d<„Zd=d>œd?d@„Zd=d=dAdBœdCdD„Zd¬dFdGdAdHœdIdJ„Zd­dKdGdAdHœdLdM„ZdNdd dOœdPdQ„Z dRdS„ Z!dNdNdAdAdTœdUdV„Z"dWdXdYdAdZœd[d\„Z#d®d]d^„Z$d_d_d`daœdbdc„Z%dAddddœdedf„Z&dgdh„ Z'didj„ Z(dkdAdlœdmdn„Z)dAdAdoœdpdq„Z*drds„ Z+d¯dtdu„Z,dvdw„ Z-dxdy„ Z.dzd{„ Z/d|d}„ Z0d~d„ Z1d€d�„ Z2d‚dƒ„ Z3d„d…„ Z4d†d‡„ Z5dˆd‰„ Z6dŠd‹d‹d_dŒd�œdŽd�„Z7d�d‘„ Z8dd’d“d”œd•d–„Z9d—d_d˜d˜d™d‹dšœd›dœ„Z:d°dd d�œdždŸ„Z;d d¡„ Z<dd d¢œd£d¤„Z=d¥d*œd¦d§„Z>d ddddd¨œd©dª„Z?dS )±ÚIndexedArrayaÃ  
    IndexedArray is a general-purpose tool for *lazily* changing the order of
    and/or duplicating some `content` with a
    [np.take](https://docs.scipy.org/doc/numpy/reference/generated/numpy.take.html)
    over the integer buffer `index.

    It has many uses:

    * representing a lazily applied slice.
    * simulating pointers into another collection.
    * emulating the dictionary encoding of Apache Arrow and Parquet.

    If the `__array__` parameter is `"categorical"`, the contents must be unique.
    Some operations are optimized (for instance, `==` only compares `index` integers)
    and the array can be converted to and from Arrow/Parquet's dictionary encoding.

    To illustrate how the constructor arguments are interpreted, the following is a
    simplified implementation of `__init__`, `__len__`, and `__getitem__`:

        class IndexedArray(Content):
            def __init__(self, index, content):
                assert isinstance(index, (Index32, IndexU32, Index64))
                assert isinstance(content, Content)
                for x in index:
                    assert 0 <= x < len(content)  # index[i] must not be negative
                self.index = index
                self.content = content

            def __len__(self):
                return len(self.index)

            def __getitem__(self, where):
                if isinstance(where, int):
                    if where < 0:
                        where += len(self)
                    assert 0 <= where < len(self)
                    return self.content[self.index[where]]

                elif isinstance(where, slice) and where.step is None:
                    return IndexedArray(
                        self.index[where.start : where.stop], self.content
                    )

                elif isinstance(where, str):
                    return IndexedArray(self.index, self.content[where])

                else:
                    raise AssertionError(where)
    N©Ú
parametersc                C  sä   t |tƒr2|jt tj¡t tj¡t tj¡fv sJtt| ƒj	› d|›�ƒ‚t |t
ƒsltt| ƒj	› d|›�ƒ‚|d uo€| d¡dk}|jrŒ|r˜|js˜|jr´td t| ƒj	t|ƒj	¡ƒ‚|j|jju sÆJ ‚|| _|| _|  ||j¡ d S )NzD 'index' must be an Index with dtype in (int32, uint32, int64), not z* 'content' must be a Content subtype, not Ú	__array__Úcategoricalzy{0} cannot contain a union-type (unless categorical), option-type, or indexed 'content' ({1}); try {0}.simplified instead)Ú
isinstancer$   ÚdtypeÚnpZint32Zuint32Úint64Ú	TypeErrorÚtypeÚ__name__r   ÚgetÚis_unionÚ
is_indexedÚ	is_optionÚformatÚnplikeÚbackendÚindex_nplikeÚ_indexÚ_contentÚ_init)ÚselfÚindexÚcontentr)   Úis_cat© rB   ú`/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/contents/indexedarray.pyÚ__init__j   s:    ÿ


ýÿþ	ÿÿ
ÿÿÿzIndexedArray.__init__c                 C  s   | j S ©N)r;   ©r>   rB   rB   rC   r?   Š   s    zIndexedArray.indexr   Úform_clsc                C  s8   t |tu r| jn||tu r | jn||tu r0| jn|d�S ©Nr(   )r'   r   r;   r<   Ú_parameters)r>   r?   r@   r)   rB   rB   rC   Úcopy�   s
    ýzIndexedArray.copyc                 C  s   |   ¡ S rE   )rJ   rF   rB   rB   rC   Ú__copy__—   s    zIndexedArray.__copy__c                 C  s.   | j t  | j|¡t  | j|¡t  | j|¡d�S )N)r?   r@   r)   )rJ   Údeepcopyr;   r<   rI   )r>   ÚmemorB   rB   rC   Ú__deepcopy__š   s
    ýzIndexedArray.__deepcopy__c                C  s  |d uo|  d¡dk}|jr>|s>|j|dd�jt|j|ƒd�S |jsL|j�r |j}|jr`|j	}n
| 
¡ j	}tj	jj|j|jd�}| |d|jj|jj|jjf |j|j|j|j|jƒ¡ t|tjjƒrâtjj||jt|j|ƒd�S tjj||jt|j|ƒd�S n| |||d�S d S )Nr*   r+   F)Ú
allow_lazyr(   ©r8   Zawkward_IndexedArray_simplify)r3   r4   Ú_carryrJ   r   rI   r5   r6   r9   r?   Úto_IndexedOptionArray64ÚakÚIndex64ÚemptyÚlengthr:   Úmaybe_kernel_errorr-   r1   Údatar,   Úcontentsr'   r@   r&   )Úclsr?   r@   r)   rA   r9   ÚinnerÚresultrB   rB   rC   Ú
simplified¡   sL    

ÿ
ýÿöÿ
ý
ýzIndexedArray.simplifiedzCallable[[Content], str | None]r#   )ÚgetkeyÚreturnc                 C  s(   || ƒ}| j | jj| j |¡| j|d�S )N)r)   Úform_key)rG   r;   Úformr<   Ú_form_with_keyrI   )r>   r^   r`   rB   rB   rC   rb   Ð   s    
üzIndexedArray._form_with_keyr"   z#Callable[[Content, Form, str], str]zMutableMapping[str, ArrayLike]r   Ústr)ra   r^   Ú	containerr9   Ú	byteorderc                 C  sR   t || jƒsJ ‚|| |dƒ}tj | j |j¡|¡||< | j 	|j
||||¡ d S )Nr?   )r,   rG   rS   Z_utilZnative_to_byteorderr;   Úrawr:   r<   Ú_to_buffersr@   )r>   ra   r^   rd   r9   re   ÚkeyrB   rB   rC   rg   Ù   s    ÿzIndexedArray._to_buffersÚboolr   )Úforget_lengthr_   c                 C  s4   | j  t ¡ ¡}t|r| ¡ n|| j |¡| jd�S rH   )	r;   Ú	to_npliker   Úinstancer'   rj   r<   Ú_to_typetracerrI   )r>   rj   r?   rB   rB   rC   rm   è   s    
ýzIndexedArray._to_typetracer©Ú	recursivec                 C  s   | j  ¡  |r| j |¡ d S rE   )r;   Ú_touch_datar<   ©r>   ro   rB   rB   rC   rp   ð   s    
zIndexedArray._touch_datac                 C  s   | j  ¡  |r| j |¡ d S rE   )r;   Ú_touch_shaper<   rq   rB   rB   rC   rr   õ   s    
zIndexedArray._touch_shaper   )r_   c                 C  s   | j jS rE   )r;   rV   rF   rB   rB   rC   rV   ú   s    zIndexedArray.lengthc                 C  s   |   ddd¡S )NÚ )Ú_reprrF   rB   rB   rC   Ú__repr__þ   s    zIndexedArray.__repr__c                 C  sœ   ||dg}|  tt| jƒƒ¡ |  d¡ | |  |d ¡¡ |  d¡ |  | j |d dd¡¡ |  | j |d dd¡¡ |  |d	 ¡ |  |¡ d
 	|¡S )Nz<IndexedArray len=ú>z    Ú
z<index>z	</index>
z	<content>z</content>
z</IndexedArray>rs   )
ÚappendÚreprrc   rV   ÚextendZ_repr_extrar;   rt   r<   Újoin)r>   ÚindentÚpreÚpostÚoutrB   rB   rC   rt     s    



zIndexedArray._reprr&   c                 C  s   t jj| j| j| jd�S rH   )rS   rY   r&   r;   r<   rI   rF   rB   rB   rC   rR     s    ÿz$IndexedArray.to_IndexedOptionArray64Tr	   )Ú
valid_whenr_   c                 C  s    |r| j jdkS | j jdk S d S ©Nr   )r;   rX   )r>   r€   rB   rB   rC   Úmask_as_bool  s    zIndexedArray.mask_as_boolc                 C  s   | j  dd¡S r�   )r<   Ú_getitem_rangerF   rB   rB   rC   Ú_getitem_nothing  s    zIndexedArray._getitem_nothingc                 C  s   t | jtƒrdS | j ¡ S ©NT)r,   r;   r   r<   Ú_is_getitem_at_placeholderrF   rB   rB   rC   r†     s    z'IndexedArray._is_getitem_at_placeholderr   )Úwherec                 C  sv   | j jjs"| jdd� | j |¡S |dk r4|| j7 }| j jjrdd|  krT| jk sdn tj 	| |¡‚| j | j
| ¡S )NFrn   r   )Ú_backendr8   Ú
known_datarp   r<   Ú_getitem_atrV   rS   Ú_errorsÚindex_errorr;   )r>   r‡   rB   rB   rC   rŠ      s    

"zIndexedArray._getitem_atr   )ÚstartÚstopr_   c                 C  s6   | j jjs| jdd� | S t| j||… | j| jd�S )NFrn   r(   )rˆ   r8   r‰   rr   r'   r;   r<   rI   )r>   r�   rŽ   rB   rB   rC   rƒ   +  s    
ÿzIndexedArray._getitem_rangerB   zstr | SupportsIndexztuple[str, ...])r‡   Úonly_fieldsr_   c                 C  s   t j| j| j ||¡d d�S rH   )r'   r]   r;   r<   Ú_getitem_field©r>   r‡   r�   rB   rB   rC   r�   4  s
    ýzIndexedArray._getitem_fieldzlist[str | SupportsIndex]c                 C  s   t j| j| j ||¡d d�S rH   )r'   r]   r;   r<   Ú_getitem_fieldsr‘   rB   rB   rC   r’   =  s
    ýzIndexedArray._getitem_fieldsr$   )ÚcarryrO   r_   c              
   C  sr   t |tjjƒsJ ‚z| j|j }W n< ty^ } z$tj | |jt	|ƒ¡|‚W Y d }~n
d }~0 0 t
|| j| jd�S rH   )r,   rS   r?   r$   r;   rX   Ú
IndexErrorr‹   rŒ   rc   r'   r<   rI   )r>   r“   rO   Ú	nextindexÚerrrB   rB   rC   rQ   F  s    .zIndexedArray._carryc              
   C  sð   | j jjrR|j| jkrRtj | tjj|||d d�d|j› dt	| ƒj
› d| j› �¡‚tjj | j| j j¡}|j| j ju r†| jj| j ju sŠJ ‚| j| j d|jj	| jjj	f |j| jj| jj| jjƒtj |||¡d� | j |d¡}| ||||¡S )Nr(   z$cannot fit jagged slice with length z into z	 of size Ú&awkward_IndexedArray_getitem_nextcarry©ZslicerF)rˆ   r8   r‰   rV   rS   r‹   rŒ   rY   Z	ListArrayr1   r2   r?   rT   rU   r:   r;   Ú_maybe_index_errorr-   rX   r<   rQ   Ú_getitem_next_jagged)r>   ÚslicestartsÚ
slicestopsÚslicecontentÚtailÚ	nextcarryÚnextrB   rB   rC   Ú_getitem_next_jagged_genericP  s:    ÿûÿþþÿø
õz)IndexedArray._getitem_next_jagged_generic)r›   rœ   r�   r_   c                 C  s   |   ||||¡S rE   )r¡   )r>   r›   rœ   r�   rž   rB   rB   rC   rš   p  s    ÿz!IndexedArray._getitem_next_jaggedzSliceItem | tupleztuple[SliceItem, ...]zIndex | None)Úheadrž   Úadvancedr_   c                 C  s\  |t u r| S t|ƒs,t|ttjjtjjfƒrÎtj	 
|¡\}}tjj | jj| jj¡}|j| jju rr| jj| jju svJ ‚| j| jd|jj| jjjf |j| jj| jj| jjƒ|d� | j |d¡}| |||¡S t|tƒræ|  |||¡S t|tƒ�r |  |||¡S |tju �r|  ||¡S |tu �r.|   ||¡S t|tjj!ƒ�rL|  "|||¡S t#t$|ƒƒ‚d S )Nr—   r˜   F)%r   r   r,   ÚslicerS   r?   rT   rY   ÚListOffsetArrayZ_slicingZ	head_tailrU   r;   rV   rˆ   r:   r8   r™   r-   r1   rX   r<   rQ   Ú_getitem_nextrc   Z_getitem_next_fieldÚlistZ_getitem_next_fieldsr.   ZnewaxisZ_getitem_next_newaxisÚEllipsisZ_getitem_next_ellipsisr&   Z_getitem_next_missingÚAssertionErrorry   )r>   r¢   rž   r£   ZnextheadZnexttailrŸ   r    rB   rB   rC   r¦   w  sP    
ÿÿÿþþÿø
õ

zIndexedArray._getitem_nextc                 C  s�  |d urî| j jjrJ| jj|jkrJtd| ¡ › dt| ƒj› d| jj› d�ƒ‚tj	j
 | jj| j j¡}|j| j ju rŽ|j| j ju rŽ| jj| j ju s’J ‚| j  | j d|jj|jj| jjjf |j|j| jj| jjƒ¡ tjj|| j| jd�}| ¡ S tj	j
 | j| j j¡}|j| j ju �r&| jj| j ju �s*J ‚| j  | j d|jj| jjjf |j| jj| jj| jjƒ¡ | j |d¡}|jt|j| jd	d
�d�S d S )Nzmask length (z) is not equal to z	 length (ú)Z!awkward_IndexedArray_overlay_maskr(   r—   F))r*   r+   )Úexclude)rˆ   r8   r‰   r;   rV   Ú
ValueErrorr1   r2   rS   r?   rT   rU   r:   rW   r-   rX   rY   r&   r<   rI   ÚprojectrQ   rJ   r   )r>   Úmaskr•   r    rŸ   rB   rB   rC   r­   ¯  st    $ÿÿÿþýýÿ÷ÿ
ÿÿþþÿøÿýÿzIndexedArray.projectÚintztuple[Index, Content])ÚaxisÚdepthr_   c                 C  s>   t | ||ƒ}|d ur*|d |kr*tdƒ‚n|  ¡  ||¡S d S )Né   zaxis=0 not allowed for flatten)r   r!   r­   Ú_offsets_and_flattened©r>   r°   r±   ÚposaxisrB   rB   rC   r³   ê  s    
z#IndexedArray._offsets_and_flattened)ÚotherÚ	mergeboolr_   c                 C  s>   |j s|jrdS |js|jr,| j |j|¡S | j ||¡S d S r…   )Zis_identity_liker4   r6   r5   r<   Ú_mergeable_nextr@   )r>   r¶   r·   rB   rB   rC   r¸   ò  s
    zIndexedArray._mergeable_nextc                 C  sš   t |ƒdkrtdƒ‚| g}g }t|ƒ}|D ]6}t|tjjƒrV| |¡ | |¡  qbq*| |¡ q*t	dd„ || D ƒƒr’t
dd„ || D ƒƒs’t‚||fS )Nr   zFto merge this array with 'others', at least one other must be providedc                 s  s   | ]}|j jjV  qd S rE   )r9   r8   r‰   ©Ú.0ÚxrB   rB   rC   Ú	<genexpr>  ó    z1IndexedArray._merging_strategy.<locals>.<genexpr>)Úlenr¬   Úiterr,   rS   rY   Z
UnionArrayrx   rz   ÚanyÚallÚRuntimeError)r>   Úothersr¢   rž   Z	it_othersr¶   rB   rB   rC   Ú_merging_strategyü  s$    ÿ

ÿzIndexedArray._merging_strategyc                 C  s.  t |tjjƒr| S |jrD| d¡|  d¡  kr8dkrDn ntdƒ‚|j}| j}tjj	 
|| | jj¡}| | jg¡}|j| jju sˆJ ‚| j | jd|jjf |jd|dƒ¡ |j| jju sÀJ ‚| j | jd|jj| jjjf |j|| jj||ƒ¡ |j�s|j�rt| j|jƒ}n| j}tjjj|||d�S )Nr*   r+   ú„merging categorical arrays is currently not implemented. Use `ak.enforce_type` to drop the categorical type and use general merging.Úawkward_IndexedArray_fill_countr   Úawkward_IndexedArray_fillr(   )r,   rS   rY   Ú
EmptyArrayr5   Ú	parameterÚNotImplementedErrorrV   r?   rT   rU   rˆ   r:   Ú
_mergemanyr<   r8   rW   r-   r1   rX   r6   r   rI   r'   r]   )r>   r¶   ZtheirlengthZmylengthr?   r@   r)   rB   rB   rC   Ú_reverse_merge  s`    ÿýü
ÿÿüÿ
þÿ÷ÿÿzIndexedArray._reverse_mergezSequence[Content])rÃ   r_   c              	   C  sz  t |ƒdkr| S |  |¡\}}d}|D ]}||j7 }q&g }d}d}tjjj|| jjd�}	| j	}
|D �]V}t
|tjjƒrxqbt
|tjjtjjtjjfƒrœ| ¡ }t
|tjjtjjfƒ�r<t|
|j	ƒ}
| |j¡ |j}|	j| jju rî|j| jju sòJ ‚| j | jd|	jj|jjf |	j||j|j|ƒ¡ ||jj7 }||j7 }qb| |¡ |	j| jju �sZJ ‚| j | jd|	jjf |	j||j|ƒ¡ ||j7 }||j7 }|
d urb|
 d¡dkrbi |
¥}
|
d= qb|dd … }|d  |¡}|
d u�rø|
 d¡dk�røtdƒ‚td	d
„ |D ƒƒ�r tjj|	||
d�}ntjj|	||
d�}t |ƒdk�rD|S |d  |¡}t |ƒdk�rd|S | |dd … ¡S d S )Nr   rP   rÇ   rÆ   r*   r+   r²   rÅ   c                 s  s   | ]}|j V  qd S rE   )r6   r¹   rB   rB   rC   r¼   °  r½   z*IndexedArray._mergemany.<locals>.<genexpr>r(   ) r¾   rÄ   rV   rS   r?   rT   rU   rˆ   r:   rI   r,   rY   rÈ   ZByteMaskedArrayZBitMaskedArrayZUnmaskedArrayrR   r&   r'   r   rx   r@   r8   rW   r-   r1   rX   r3   rË   rÊ   rÀ   rÌ   )r>   rÃ   r¢   rž   Útotal_lengthÚarrayrY   Zcontentlength_so_farZlength_so_farr•   r)   Zarray_indexZtail_contentsÚnextcontentr    ÚreversedrB   rB   rC   rË   R  s¶    ÿ
ýþÿÿþþÿ÷ÿ
ÿÿùÿ

ÿþÿÿÿzIndexedArray._mergemany)Úvaluer_   c                 C  s@   |j jjr&|jdkr&td|j› d�ƒ‚t| j| j |¡| j	d�S )Nr²   zfill_none value length (z) is not equal to 1r(   )
r9   r8   r‰   rV   r¬   r'   r;   r<   Ú
_fill_nonerI   )r>   rÑ   rB   rB   rC   rÒ   Â  s    ÿÿzIndexedArray._fill_nonec                 C  s<   t | ||ƒ}|d ur(|d |kr(|  ¡ S |  ¡  ||¡S d S ©Nr²   )r   Z_local_index_axis0r­   Ú_local_indexr´   rB   rB   rC   rÔ   Ë  s    zIndexedArray._local_indexc                 C  s0  t jjj| j| jjd�}t jjjd| jjd�}|sÄ| j}t jj d| jj¡}d|d< |j|d< |j	| jju rz|j	| jju s~J ‚| j 
| jd|jj|jj|jjf |j|j|d |jd|d ddƒ¡ | jj	| jju rð|j	| jju rð|j	| jju sôJ ‚t jj| jj | j¡| jjd�}|jj|d< |d|d … S )NrP   r²   é   r   Zawkward_sortTF)rS   r?   rT   ZzerosrV   rˆ   r:   r;   rU   r8   rW   r-   r1   rX   Zunique_valuesÚsize)r>   r?   Úsortedr    rV   ÚoffsetsrB   rB   rC   Ú_unique_indexÒ  sR    
ÿþýÿóÿÿþýþzIndexedArray._unique_indexc                 C  s    t jj| j| j ||¡| jd�S rH   )rS   rY   r'   r;   r<   Ú_numbers_to_typerI   )r>   ÚnameZincluding_unknownrB   rB   rC   rÚ   ÿ  s
    ýzIndexedArray._numbers_to_typec                 C  sP   | j jdkrdS |  | j ¡}t|ƒt| j ƒkr2dS | j |d¡}| ||||¡S )Nr   TF)r;   rV   rÙ   r¾   r<   rQ   Ú
_is_unique)r>   ÚnegaxisÚstartsÚparentsÚ	outlengthr•   r    rB   rB   rC   rÜ     s    zIndexedArray._is_uniquec              	   C  sL  | j jdkr| S | j\}}| j j}|j}|}	tjj || jj¡}
tjj || jj¡}tjj || jj¡}|
j	| jju r°|j	| jju r°|j	| jju r°| j j	| jju r°|j	| jju s´J ‚| j 
| jd|
jj|jj|jj| j jj|jjf |
j|j|j| j j|j|ƒ¡ | j |
d¡}| ||||¡}|�s:|d u�rê||k�rêtjj || jj¡}|j	| jju �rŽ|j	| jju �rŽ|j	| jju �rŽ|j	| jju �s’J ‚| j 
| jd|jj|jj|jj|jjf |j|j|j||j|	ƒ¡ tjjj||| jd�S |�sþ||k�rþ|S t|tjjƒ�r| d¡}t|tjjƒ�rø|j	j�r`|jdk�r`|d dk�r`td|d › d�ƒ‚tjj |jd	 | jj¡}|j	| jju �rš|j	| jju �sžJ ‚| j 
| jd
|jj|jjf |j|j|j| j jƒ¡ tjj||jd d�}tjj||d d�S t|tjjƒ�rDtjj| jjj|jtjd�| jjd�}tjjj||| jd�S t ‚d S )Nr   Z#awkward_IndexedArray_reduce_next_64FZ)awkward_IndexedArray_local_preparenext_64r(   Tzfreduce_next with unbranching depth > negaxis expects a ListOffsetArray64 whose offsets start at zero (rª   r²   Z/awkward_IndexedArray_reduce_next_fix_offsets_64)r-   rP   )!r;   rV   Zbranch_depthrS   r?   rT   rU   rˆ   r:   r8   rW   r-   r1   rX   r<   rQ   Ú_uniquerY   r&   r]   rI   r,   ZRegularArrayZto_ListOffsetArray64r¥   r‰   r©   r'   Z
NumpyArrayZaranger.   r/   rÊ   )r>   rÝ   rÞ   rß   rà   Úbranchr±   Zindex_lengthZparents_lengthZnext_lengthrŸ   ZnextparentsZoutindexr    ÚuniqueZnextoutindexZ
outoffsetsÚtmprB   rB   rC   rá     sð    
ÿþýüûûÿóÿüÿÿþýüüÿôÿÿ
$ÿÿÿÿþþÿøÿÿþÿzIndexedArray._uniquec           	   	   C  s&   | j  | jd¡}| |||||||¡S ©NF)r<   rQ   r;   Ú_argsort_next)	r>   rÝ   rÞ   Úshiftsrß   rà   Ú	ascendingÚstabler    rB   rB   rC   ræ   ‘  s    ÿzIndexedArray._argsort_nextc                 C  s$   | j  | jd¡}| ||||||¡S rå   )r<   rQ   r;   Ú
_sort_next)r>   rÝ   rÞ   rß   rà   rè   ré   r    rB   rB   rC   rê   ™  s    zIndexedArray._sort_nextc                 C  sL   t | ||ƒ}|d ur0|d |kr0|  ||||¡S |  ¡  ||||||¡S d S rÓ   )r   Z_combinations_axis0r­   Ú_combinations)r>   ÚnÚreplacementZrecordlookupr)   r°   r±   rµ   rB   rB   rC   rë   �  s    ÿzIndexedArray._combinationsc
                 C  s*   | j  | jd¡}
|
 |||||||||	¡	S rå   )r<   rQ   r;   Ú_reduce_next)r>   ZreducerrÝ   rÞ   rç   rß   rà   r®   ZkeepdimsÚbehaviorr    rB   rB   rC   rî   ¦  s    ÷zIndexedArray._reduce_nextc              	   C  sÜ   |   d¡dkr2tj | j¡s2d|› dt| ƒ› d�S | jd| jjjf | jj	| jj
| jj
dƒ}|jd urÈ|jd u rvd}nd	|jjd
d� d¡ d¡ }|jjd
d�}d|› dt| ƒ› d|› d|j› |› �	S | j |d ¡S d S )Nr*   r+   zat z ("z<"): __array__ = "categorical" requires contents to be uniqueZawkward_IndexedArray_validityFrs   z (in compiled code: Úsurrogateescape)Úerrorsrw   ú(z"): z at i=z.content)rÉ   rS   Z_doZ	is_uniquer<   r1   rˆ   r?   r-   rX   rV   rc   ÚfilenameÚdecodeÚlstripÚidÚ_validity_error)r>   ÚpathÚerrorró   ÚmessagerB   rB   rC   r÷   ¿  s&    ÿ

ÿþþ&zIndexedArray._validity_errorc                 C  s   | j  ¡ | j ¡  S rE   )r?   Ú_nbytes_partr@   rF   rB   rB   rC   rû   Ó  s    zIndexedArray._nbytes_partc                 C  s€   t | ||ƒ}|d ur,|d |kr,|  ||¡S |d urX|d |d krX|  ¡  ||||¡S tjj| j| j ||||¡| j	d�S d S )Nr²   r(   )
r   Z_pad_none_axis0r­   Ú	_pad_nonerS   rY   r'   r;   r<   rI   )r>   Útargetr°   r±   Zcliprµ   rB   rB   rC   rü   Ö  s    ýzIndexedArray._pad_noner   zContent | Noner    )ÚpyarrowÚ	mask_nodeÚ
validbytesrV   Úoptionsc              	   C  s,  |d sJ|   d¡dkrJt| jƒ}|d= t| j| j|d�}| |||||¡S | j t¡}|   d¡dkrÖ| j |d d | jj	|¡}	|j
 ||	|d u r’d n| ¡}
|d rÐtjjj tjj |
j|d |d || ¡|
¡S |
S nR| jj	dkrê| j}n| j tj |¡d¡}|jt|j| jƒd�}| |||||¡S d S )	NZcategorical_as_dictionaryr*   r+   r(   ZextensionarrayZrecord_is_scalarr   F)rÉ   ÚdictrI   r'   r;   r<   Ú	_to_arrowrf   ÚnumpyrV   ZDictionaryArrayZfrom_arraysrS   Ú_connectrþ   ZAwkwardArrowArrayZfrom_storageZto_awkwardarrow_typer1   rQ   r?   r$   rJ   r   )r>   rþ   rÿ   r   rV   r  Znext_parametersr    r?   Ú
dictionaryr   Znext2rB   rB   rC   r  ã  sJ    	ÿþ
ÿý
ûøÿzIndexedArray._to_arrowc                 C  s   |   ¡  ||¡S rE   )r­   Ú_to_backend_array)r>   Zallow_missingr9   rB   rB   rC   r    s    zIndexedArray._to_backend_arrayr   zlist[Content])r9   r  r_   c                 C  s   |   ¡  ||¡S rE   )r­   Ú_remove_structure)r>   r9   r  rB   rB   rC   r    s    zIndexedArray._remove_structurer   zMapping[str, Any] | Noner   )Úactionr±   Údepth_contextÚlateral_contextr  r_   c           
   	     s  ˆj jjrfˆj jjrfˆjjdkrfˆjj}ˆj j |¡}tj	j
|| ˆj jd�‰ˆj|| ¡ d … ‰n$ˆj jjs|ˆjdd� ˆjˆj ‰‰ˆd rÄˆd r¢tj‰nt‰‡ ‡‡‡‡‡‡‡‡f	dd	„}n‡ ‡‡‡‡‡fd
d	„}ˆ ˆˆˆˆ|ˆj ˆd�}	t|	tƒ�r|	S |	d u �r|ƒ S t|	ƒ‚d S )Nr   rP   r²   Frn   Zreturn_arrayZreturn_simplifiedc                	     s0   ˆˆˆ  ˆ ˆt ˆ¡ˆˆ¡ˆd r(ˆjnd d�S )NZkeep_parametersr(   )Ú_recursively_applyrJ   rI   rB   ©	r	  r@   r±   r
  r?   r  Úmaker  r>   rB   rC   ÚcontinuationA  s    û÷z5IndexedArray._recursively_apply.<locals>.continuationc                     s   ˆ  ˆ ˆt ˆ¡ˆˆ¡ d S rE   )r  rJ   rB   )r	  r@   r±   r
  r  r  rB   rC   r  P  s    û)r±   r
  r  r  r9   r  )rˆ   r8   r‰   r;   rV   rX   r:   ÚminrS   r?   r$   r<   Úmaxrp   r'   r]   r,   r   r©   )
r>   r	  r±   r
  r  r  ZnpindexZindexminr  r\   rB   r  rC   r  #  sF    	ÿþ
ýÿ
	ù

zIndexedArray._recursively_apply)ro   r_   c                 C  sT   |   d¡dkr6|r| j d¡n| j}t| j|| jd�S |  ¡ }|rL| d¡S |S d S )Nr*   r+   Tr(   )rÉ   r<   Ú	to_packedr'   r;   rI   r­   )r>   ro   r@   Z	projectedrB   rB   rC   r  j  s
    zIndexedArray.to_packedc                 C  sX   | j jjstdƒ‚|  ||¡}|d ur*|S | j t¡}| j 	t
j |¡d¡}| ||¡S )Nz0cannot convert typetracer arrays to Python listsF)rˆ   r8   r‰   r0   Z_to_list_customr;   rf   r  r<   rQ   rS   r?   r$   Ú_to_list)r>   rï   Zjson_conversionsr   r?   rÏ   rB   rB   rC   r  r  s    
zIndexedArray._to_list)r9   r_   c                 C  s*   | j  |¡}| j |j¡}t||| jd�S rH   )r<   Z
to_backendr;   rk   r:   r'   rI   )r>   r9   r@   r?   rB   rB   rC   Ú_to_backend~  s    zIndexedArray._to_backendzSelf | ak.contents.RecordArrayc                   sP   ˆ j jrDtjj‡ fdd„ˆ j jD ƒˆ j jˆ jˆ jtˆ j j	ˆ j	ƒd�S ˆ  
¡ S d S )Nc                   s   g | ]}t jj ˆ j|¡‘qS rB   )rS   rY   r'   r]   r;   )rº   ÚcrF   rB   rC   Ú
<listcomp>†  s   ÿz?IndexedArray._push_inside_record_or_project.<locals>.<listcomp>)rY   ÚfieldsrV   r9   r)   )r@   Z	is_recordrS   rY   ZRecordArrayÚ_fieldsrV   rˆ   r   rI   r­   rF   rB   rF   rC   Ú_push_inside_record_or_projectƒ  s    
þøz+IndexedArray._push_inside_record_or_project)r¶   Úindex_dtypeÚ
numpyarrayÚall_parametersr_   c                 C  s2   |   ||¡o0| j |j||¡o0| j |j|||¡S rE   )Z_is_equal_to_genericr;   Zis_equal_tor?   r<   Ú_is_equal_tor@   )r>   r¶   r  r  r  rB   rB   rC   r  ’  s    ÿ
ÿýzIndexedArray._is_equal_to)T)rB   )rB   )N)T)T)@r2   Ú
__module__Ú__qualname__Ú__doc__rD   Úpropertyr?   r#   rG   Ú__annotations__r   rJ   rK   rN   Úclassmethodr]   rb   rg   rm   rp   rr   rV   ru   rt   rR   r‚   r„   r†   rŠ   rƒ   r�   r’   rQ   r¡   rš   r¦   r­   r³   r¸   rÄ   rÌ   rË   rÒ   rÔ   rÙ   rÚ   rÜ   rá   ræ   rê   rë   rî   r÷   rû   rü   r  r  r  r  r  r  r  r  r  rB   rB   rB   rC   r'   6   sz   
2 
.	
 ÿ
 ÿ	
 8
;
=p	
-	8Gr'   )BÚ
__future__r   rJ   Úcollections.abcr   r   r   ZawkwardrS   Zawkward._backends.backendr   Zawkward._layoutr   Zawkward._meta.indexedmetar   Zawkward._nplikes.array_liker	   Zawkward._nplikes.numpyr
   Zawkward._nplikes.numpy_liker   r   Zawkward._nplikes.placeholderr   Zawkward._nplikes.shaper   Zawkward._nplikes.typetracerr   Zawkward._parametersr   r   Zawkward._regularizer   Zawkward._slicingr   Zawkward._typingr   r   r   r   r   r   r   Zawkward._utilr   Zawkward.contents.contentr   r   r   r   r    Zawkward.errorsr!   Zawkward.forms.formr"   Zawkward.forms.indexedformr#   Zawkward.indexr$   r%   Z#awkward.contents.indexedoptionarrayr&   rl   r.   r  r'   rB   rB   rB   rC   Ú<module>   s:   $	