a
    yn„f‘O  ã                   @  sŒ  d dl mZ d dl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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.m/Z/m0Z0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 e#�r^d dl m:Z: d dl;m<Z< e =¡ Z>e =¡ Z?e)G dd„ dee. e.ƒƒZ@dS )é    )ÚannotationsN)ÚMappingÚMutableMappingÚSequence)ÚBackend)Úmaybe_posaxis)ÚUnmaskedMeta)Ú	ArrayLike)ÚNumpy)Ú	IndexTypeÚNumpyMetadata)Ú	ShapeItem)Ú	MaybeNone)Ú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)ÚUnmaskedForm)ÚIndex)Ú	SliceItem)ÚIndexedOptionArrayc                   @  sî  e Zd ZU dZddœdd„ZeZded< 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-d.„ Zd/d(œd0d1„Zd2d3„ Zd4d5„ Zd©dd7d8œd9d:„Zd;d<„ Zdd(œd=d>„Zd?d@œdAdB„Zd?d?dCdDœdEdF„ZdªdHdIdCdJœdKdL„Zd«dMdIdCdJœdNdO„Z dPddCdQœdRdS„Z!dTd(œdUdV„Z"dPdPdCdCdWœdXdY„Z#dZd[d\dCd]œd^d_„Z$d¬d`da„Z%dbdbdcddœdedf„Z&dCdddgœdhdi„Z'djdk„ Z(dldCdmœdndo„Z)dCdCdpœdqdr„Z*dsdt„ Z+dudv„ Z,dwdx„ Z-dydz„ Z.d{d|„ Z/d}d~„ Z0dd€„ Z1d�d‚„ Z2dƒd„„ Z3d…d†„ Z4d‡dˆ„ Z5d‰dŠdŠdbd‹dŒœd�dŽ„Z6d�d�„ Z7dd‘d’d“œd”d•„Z8dCd(œd–d—„Z9d˜dbd™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dd¦œd§d¨„Z>dS )®ÚUnmaskedArraya¼  
    UnmaskedArray implements an #ak.types.OptionType for which the values are
    never, in fact, missing. It exists to satisfy systems that formally require this
    high-level type without the overhead of generating an array of all True or all
    False values.

    This is like NumPy's
    [masked arrays](https://docs.scipy.org/doc/numpy/reference/maskedarray.html)
    with `mask=None`.

    It is also like Apache Arrow's
    [validity bitmaps](https://arrow.apache.org/docs/format/Columnar.html#validity-bitmaps)
    because the bitmap can be omitted when all values are valid.

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

        class UnmaskedArray(Content):
            def __init__(self, content):
                assert isinstance(content, Content)
                self.content = content

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

            def __getitem__(self, where):
                if isinstance(where, int):
                    return self.content[where]
                else:
                    return UnmaskedArray(self.content[where])
    N©Ú
parametersc                C  sh   t |tƒs"tt| ƒj› d|›�ƒ‚|js4|js4|jrPtd t| ƒjt|ƒj¡ƒ‚|| _	|  
||j¡ d S )Nz* 'content' must be a Content subtype, not zd{0} cannot contain a union-type, option-type, or indexed 'content' ({1}); try {0}.simplified instead)Ú
isinstancer   Ú	TypeErrorÚtypeÚ__name__Úis_unionÚ
is_indexedÚ	is_optionÚformatÚ_contentÚ_initÚbackend©ÚselfÚcontentr(   © r7   úa/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/contents/unmaskedarray.pyÚ__init__X   s    
ÿÿÿzUnmaskedArray.__init__r   Úform_clsc                C  s(   t |tu r| jn||tu r | jn|d�S ©Nr'   )r&   r   r1   Ú_parametersr4   r7   r7   r8   Úcopyh   s    þzUnmaskedArray.copyc                 C  s   |   ¡ S ©N)r=   ©r5   r7   r7   r8   Ú__copy__n   s    zUnmaskedArray.__copy__c                 C  s"   | j t  | j|¡t  | j|¡d�S )N)r6   r(   )r=   Údeepcopyr1   r<   )r5   Úmemor7   r7   r8   Ú__deepcopy__q   s    þzUnmaskedArray.__deepcopy__c                  s`   |j r,|j‡ fdd„|jD ƒt|j|ƒd�S |js8|jrP| ¡ jt|j|ƒd�S ˆ ||d�S d S )Nc                   s   g | ]}ˆ   |¡‘qS r7   )Ú
simplified©Ú.0Úx©Úclsr7   r8   Ú
<listcomp>{   ó    z,UnmaskedArray.simplified.<locals>.<listcomp>)Úcontentsr(   r'   )r-   r=   rL   r   r<   r.   r/   Úto_IndexedOptionArray64)rI   r6   r(   r7   rH   r8   rD   w   s    
þ
ÿzUnmaskedArray.simplifiedzCallable[[Content], str | None]r"   )ÚgetkeyÚreturnc                 C  s"   || ƒ}| j | j |¡| j|d�S )N)r(   Úform_key)r:   r1   Ú_form_with_keyr<   )r5   rN   rP   r7   r7   r8   rQ   …   s    
ýzUnmaskedArray._form_with_keyr!   z#Callable[[Content, Form, str], str]zMutableMapping[str, ArrayLike]r   Ústr)ÚformrN   Ú	containerr3   Ú	byteorderc                 C  s*   t || jƒsJ ‚| j |j||||¡ d S r>   )r)   r:   r1   Ú_to_buffersr6   )r5   rS   rN   rT   r3   rU   r7   r7   r8   rV   �   s    zUnmaskedArray._to_buffersÚboolr   )Úforget_lengthrO   c                 C  s   t | j |¡| jd�S r;   )r&   r1   Ú_to_typetracerr<   )r5   rX   r7   r7   r8   rY   ˜   s    
þzUnmaskedArray._to_typetracer©Ú	recursivec                 C  s   |r| j  |¡ d S r>   )r1   Ú_touch_data©r5   r[   r7   r7   r8   r\   ž   s    zUnmaskedArray._touch_datac                 C  s   |r| j  |¡ d S r>   )r1   Ú_touch_shaper]   r7   r7   r8   r^   ¢   s    zUnmaskedArray._touch_shaper   )rO   c                 C  s   | j jS r>   )r1   Úlengthr?   r7   r7   r8   r_   ¦   s    zUnmaskedArray.lengthc                 C  s   |   ddd¡S )NÚ )Ú_reprr?   r7   r7   r8   Ú__repr__ª   s    zUnmaskedArray.__repr__c                 C  s‚   ||dg}|  tt| jƒƒ¡ |  d¡ | |  |d ¡¡ |  d¡ |  | j |d dd¡¡ |  |d ¡ |  |¡ d |¡S )	Nz<UnmaskedArray len=ú>z    Ú
z	<content>z</content>
z</UnmaskedArray>r`   )	ÚappendÚreprrR   r_   ÚextendZ_repr_extrar1   ra   Újoin)r5   ÚindentÚpreÚpostÚoutr7   r7   r8   ra   ­   s    



zUnmaskedArray._reprr%   c                 C  s>   | j jj| jjtjd�}tjj	tj
j|| j jd�| j| jd�S )N©Zdtype©Únpliker'   )Ú_backendÚindex_nplikeÚaranger1   r_   ÚnpZint64ÚakrL   r%   ÚindexÚIndex64r<   )r5   rr   r7   r7   r8   rM   ¸   s    ýz%UnmaskedArray.to_IndexedOptionArray64c                 C  s6   t jjt jj|  |¡ tj¡| j	j
d�| j|| jd�S )Nrn   r'   )rt   rL   ÚByteMaskedArrayru   ZIndex8Úmask_as_boolÚviewrs   Zint8rp   rq   r1   r<   ©r5   Ú
valid_whenr7   r7   r8   Úto_ByteMaskedArrayÀ   s    þùz UnmaskedArray.to_ByteMaskedArrayc                 C  sr   t t | jjd ¡ƒ}|r8| jjj|t 	d¡tj	d�}n| jjj
|tj	d�}tjjtj |¡| j|| j|| jd�S )Ng       @éÿ   rm   r'   )ÚintÚmathÚceilr1   r_   rp   rq   Úfullrs   Zuint8Úzerosrt   rL   ZBitMaskedArrayru   ZIndexU8r<   )r5   r{   Z	lsb_orderZ	bitlengthZbitmaskr7   r7   r8   Úto_BitMaskedArrayË   s    ÿ
úzUnmaskedArray.to_BitMaskedArrayTr	   )r{   rO   c                 C  s8   |r| j jj| jjtjd�S | j jj| jjtjd�S d S )Nrm   )rp   rq   Zonesr1   r_   rs   Zbool_r‚   rz   r7   r7   r8   rx   Ý   s
    
ÿzUnmaskedArray.mask_as_boolc                 C  s   | j  dd¡S ©Nr   )r1   Ú_getitem_ranger?   r7   r7   r8   Ú_getitem_nothingå   s    zUnmaskedArray._getitem_nothingc                 C  s
   | j  ¡ S r>   )r1   Ú_is_getitem_at_placeholderr?   r7   r7   r8   r‡   è   s    z(UnmaskedArray._is_getitem_at_placeholderr   )Úwherec                 C  s2   | j jjs&| jdd� t| j |¡ƒS | j |¡S )NFrZ   )rp   ro   Ú
known_datar\   r   r1   Ú_getitem_at)r5   rˆ   r7   r7   r8   rŠ   ë   s    
zUnmaskedArray._getitem_atr   )ÚstartÚstoprO   c                 C  s2   | j jjs| jdd� | S t| j ||¡| jd�S )NFrZ   r'   )rp   ro   r‰   r^   r&   r1   r…   r<   )r5   r‹   rŒ   r7   r7   r8   r…   ò   s    
þzUnmaskedArray._getitem_ranger7   zstr | SupportsIndexztuple[str, ...])rˆ   Úonly_fieldsrO   c                 C  s   t j| j ||¡d d�S r;   )r&   rD   r1   Ú_getitem_field©r5   rˆ   r�   r7   r7   r8   rŽ   ü   s    ÿzUnmaskedArray._getitem_fieldzlist[str | SupportsIndex]c                 C  s   t j| j ||¡d d�S r;   )r&   rD   r1   Ú_getitem_fieldsr�   r7   r7   r8   r�     s    ÿzUnmaskedArray._getitem_fieldsr#   )ÚcarryÚ
allow_lazyrO   c                 C  s   t j| j ||¡| jd�S r;   )r&   rD   r1   Ú_carryr<   )r5   r‘   r’   r7   r7   r8   r“   
  s    ÿzUnmaskedArray._carryz.tuple[int, ak.index.Index64, ak.index.Index64]c                 C  sD   | j j | jj¡}tjj|| j jd�}tjj|| j jd�}d||fS )Nrn   r   )rp   rq   rr   r1   r_   rt   ru   rv   )r5   ZcountingZ	nextcarryZoutindexr7   r7   r8   Ú_nextcarry_outindex  s    z!UnmaskedArray._nextcarry_outindex)ÚslicestartsÚ
slicestopsÚslicecontentrO   c                 C  s   t | j ||||¡| jd�S r;   )r&   r1   Ú_getitem_next_jaggedr<   )r5   r•   r–   r—   Útailr7   r7   r8   r˜     s    ÿüz"UnmaskedArray._getitem_next_jaggedzSliceItem | tupleztuple[SliceItem, ...]zIndex | None)Úheadr™   ÚadvancedrO   c                 C  sÎ   |t u r| S t|ƒs,t|ttjjtjjfƒrHt	j
| j |||¡| jd�S t|tƒr`|  |||¡S t|tƒrx|  |||¡S |tju rŽ|  ||¡S |tu r¢|  ||¡S t|tjjƒr¾|  |||¡S tt|ƒƒ‚d S r;   )r   r   r)   Úslicert   ru   rv   rL   ZListOffsetArrayr&   rD   r1   Ú_getitem_nextr<   rR   Z_getitem_next_fieldÚlistZ_getitem_next_fieldsrs   ZnewaxisZ_getitem_next_newaxisÚEllipsisZ_getitem_next_ellipsisr%   Z_getitem_next_missingÚAssertionErrorrf   )r5   rš   r™   r›   r7   r7   r8   r�     s(    
ÿþ


zUnmaskedArray._getitem_nextc                 C  s.   |d ur$t jj|| jd| jd� ¡ S | jS d S )NFr'   )rt   rL   rw   r1   r<   Úproject)r5   Úmaskr7   r7   r8   r¡   B  s
    ÿ
zUnmaskedArray.projectr~   ztuple[Index, Content])ÚaxisÚdepthrO   c                 C  sd   t | ||ƒ}|d ur*|d |kr*tdƒ‚n6| j ||¡\}}|jdkrX|t|| jd�fS ||fS d S )Né   zaxis=0 not allowed for flattenr   r'   )r   r    r1   Ú_offsets_and_flattenedr_   r&   r<   )r5   r£   r¤   ÚposaxisÚoffsetsZ	flattenedr7   r7   r8   r¦   J  s    

þz$UnmaskedArray._offsets_and_flattened)ÚotherÚ	mergeboolrO   c                 C  s>   |j s|jrdS |js|jr,| j |j|¡S | j ||¡S d S )NT)Zis_identity_liker-   r/   r.   r1   Ú_mergeable_nextr6   )r5   r©   rª   r7   r7   r8   r«   Y  s
    zUnmaskedArray._mergeable_nextc                 C  s   |   ¡  |¡S r>   )rM   Ú_reverse_merge)r5   r©   r7   r7   r8   r¬   c  s    zUnmaskedArray._reverse_mergezSequence[Content])ÚothersrO   c                 C  st   t |ƒdkr| S tdd„ |D ƒƒrb| j}g }|D ]}t||jƒ}| |j¡ q0t| j |¡|d�S |  ¡  |¡S d S )Nr   c                 s  s   | ]}t |tƒV  qd S r>   )r)   r&   rE   r7   r7   r8   Ú	<genexpr>j  rK   z+UnmaskedArray._mergemany.<locals>.<genexpr>r'   )	ÚlenÚallr<   r   re   r1   r&   Ú
_mergemanyrM   )r5   r­   r(   Ztail_contentsrG   r7   r7   r8   r±   f  s    ÿzUnmaskedArray._mergemany)ÚvaluerO   c                 C  s   | j  |¡S r>   )r1   Ú
_fill_none)r5   r²   r7   r7   r8   r³   x  s    zUnmaskedArray._fill_nonec                 C  sD   t | ||ƒ}|d ur(|d |kr(|  ¡ S t| j ||¡| jd�S d S ©Nr¥   r'   )r   Z_local_index_axis0r&   r1   Ú_local_indexr<   )r5   r£   r¤   r§   r7   r7   r8   rµ   {  s    ÿzUnmaskedArray._local_indexc                 C  s   t jj| j ||¡| jd�S r;   )rt   rL   r&   r1   Ú_numbers_to_typer<   )r5   ÚnameZincluding_unknownr7   r7   r8   r¶   „  s    þzUnmaskedArray._numbers_to_typec                 C  s"   | j jdkrdS | j  ||||¡S )Nr   T)r1   r_   Ú
_is_unique©r5   ÚnegaxisÚstartsÚparentsÚ	outlengthr7   r7   r8   r¸   Š  s    zUnmaskedArray._is_uniquec                 C  s"   | j jdkr| S | j  ||||¡S r„   )r1   r_   Ú_uniquer¹   r7   r7   r8   r¾   �  s    zUnmaskedArray._uniquec           
   	   C  sZ   | j  |||||||¡}t|tjjƒrRtjjj|j d d�}	tjj|	|j|j	d d�S |S d S r;   )
r1   Ú_argsort_nextr)   rt   rL   ÚRegularArrayr&   rD   Ú_sizeÚ_length)
r5   rº   r»   Úshiftsr¼   r½   Ú	ascendingÚstablerl   Útmpr7   r7   r8   r¿   ”  s    ÿÿzUnmaskedArray._argsort_nextc           	      C  s\   | j  ||||||¡}t|tjjƒrTtjjj|j | jd�}tjj||j	|j
| jd�S |S d S r;   )r1   Ú
_sort_nextr)   rt   rL   rÀ   r&   rD   r<   rÁ   rÂ   )	r5   rº   r»   r¼   r½   rÄ   rÅ   rl   rÆ   r7   r7   r8   rÇ   ¤  s    ÿÿÿzUnmaskedArray._sort_nextc              	   C  sX   t | ||ƒ}|d ur0|d |kr0|  ||||¡S tjj| j ||||||¡| jd�S d S r´   )r   Z_combinations_axis0rt   rL   r&   r1   Ú_combinationsr<   )r5   ÚnÚreplacementZrecordlookupr(   r£   r¤   r§   r7   r7   r8   rÈ   µ  s    ÿüzUnmaskedArray._combinationsc
           
      C  s   | j  |||||||||	¡	S r>   )r1   Ú_reduce_next)
r5   Zreducerrº   r»   rÃ   r¼   r½   r¢   ZkeepdimsÚbehaviorr7   r7   r8   rË   Á  s    ÷zUnmaskedArray._reduce_nextc                 C  s   | j  |d ¡S )Nz.content)r1   Ú_validity_error)r5   Úpathr7   r7   r8   rÍ   Ù  s    zUnmaskedArray._validity_errorc                 C  s
   | j  ¡ S r>   )r6   Ú_nbytes_partr?   r7   r7   r8   rÏ   Ü  s    zUnmaskedArray._nbytes_partc                 C  sz   t | ||ƒ}|d ur,|d |kr,|  ||¡S |d urV|d |d krV| j ||||¡S tjj| j ||||¡| jd�S d S r´   )r   Z_pad_none_axis0r1   Ú	_pad_nonert   rL   r&   r<   )r5   Útargetr£   r¤   Zclipr§   r7   r7   r8   rÐ   ß  s    þzUnmaskedArray._pad_noner   zContent | Noner   )ÚpyarrowÚ	mask_nodeÚ
validbytesr_   Úoptionsc                 C  s   | j  || d ||¡S r>   )r1   Ú	_to_arrow)r5   rÒ   rÓ   rÔ   r_   rÕ   r7   r7   r8   rÖ   ë  s    zUnmaskedArray._to_arrowc                 C  s(   | j  ||¡}|r |jj |¡S |S d S r>   )r6   Ú_to_backend_arrayro   ÚmaZMaskedArray)r5   Zallow_missingr3   r6   r7   r7   r8   r×   õ  s    zUnmaskedArray._to_backend_arrayr   zlist[Content])r3   rÕ   rO   c                 C  s8   | j \}}|s|d s|dkr.|  ¡  ||¡S | gS d S )NZ
drop_nonesr¥   )Zbranch_depthr¡   Ú_remove_structure)r5   r3   rÕ   Úbranchr¤   r7   r7   r8   rÙ   ü  s    
zUnmaskedArray._remove_structurec                 C  s   | j S r>   )r6   r?   r7   r7   r8   Ú
_drop_none  s    zUnmaskedArray._drop_noner   zMapping[str, Any] | Noner   )Úactionr¤   Údepth_contextÚlateral_contextrÕ   rO   c              	     sŒ   ˆd r6ˆd rt j‰nt ‰‡ ‡‡‡‡‡‡fdd„}n‡ ‡‡‡‡‡fdd„}ˆ ˆˆˆˆ|ˆjˆd�}t|tƒrr|S |d u r€|ƒ S t|ƒ‚d S )NZreturn_arrayZreturn_simplifiedc                     s0   ˆˆj  ˆ ˆt ˆ¡ˆˆ¡ˆd r(ˆjnd d�S )NZkeep_parametersr'   )r1   Ú_recursively_applyr=   r<   r7   ©rÜ   r¤   rÝ   rÞ   ÚmakerÕ   r5   r7   r8   Úcontinuation  s    ûøz6UnmaskedArray._recursively_apply.<locals>.continuationc                     s   ˆj  ˆ ˆt ˆ¡ˆˆ¡ d S r>   )r1   rß   r=   r7   )rÜ   r¤   rÝ   rÞ   rÕ   r5   r7   r8   râ   $  s    û)r¤   rÝ   rÞ   râ   r3   rÕ   )r&   rD   rp   r)   r   r    )r5   rÜ   r¤   rÝ   rÞ   rÕ   râ   Úresultr7   rà   r8   rß     s(    	ù

z UnmaskedArray._recursively_apply)r[   rO   c                 C  s    t |r| j d¡n| j| jd�S )NTr'   )r&   r1   Ú	to_packedr<   r]   r7   r7   r8   rä   >  s    þzUnmaskedArray.to_packedc                 C  s8   | j jjstdƒ‚|  ||¡}|d ur*|S | j ||¡S )Nz0cannot convert typetracer arrays to Python lists)rp   ro   r‰   r*   Z_to_list_customr1   Ú_to_list)r5   rÌ   Zjson_conversionsrl   r7   r7   r8   rå   D  s    
zUnmaskedArray._to_list)r3   rO   c                 C  s   | j  |¡}t|| jd�S r;   )r1   Z
to_backendr&   r<   )r5   r3   r6   r7   r7   r8   Ú_to_backendN  s    zUnmaskedArray._to_backend)r©   Úindex_dtypeÚ
numpyarrayÚall_parametersrO   c                 C  s   | j  |j|||¡S r>   )r1   Ú_is_equal_tor6   )r5   r©   rç   rè   ré   r7   r7   r8   rê   R  s    
ÿzUnmaskedArray._is_equal_to)T)r7   )r7   )N)T)?r,   Ú
__module__Ú__qualname__Ú__doc__r9   r"   r:   Ú__annotations__r   r=   r@   rC   ÚclassmethodrD   rQ   rV   rY   r\   r^   Úpropertyr_   rb   ra   rM   r|   rƒ   rx   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Û   rß   rä   rå   ræ   rê   r7   r7   r7   r8   r&   6   sv   
  ÿ ÿ
#

	
	6
r&   )AÚ
__future__r   r=   r   Úcollections.abcr   r   r   Zawkwardrt   Zawkward._backends.backendr   Zawkward._layoutr   Zawkward._meta.unmaskedmetar   Zawkward._nplikes.array_liker	   Zawkward._nplikes.numpyr
   Zawkward._nplikes.numpy_liker   r   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.unmaskedformr"   Zawkward.indexr#   r$   Zawkward.contentsr%   Úinstancers   Únumpyr&   r7   r7   r7   r8   Ú<module>   s:   $	