a
    zn„fQ:  ã                   @  sN   d Z ddlmZ ddlZddlZdd„ ZG dd„ dejjƒZG dd	„ d	ƒZ	dS )
aB  
This module defines an :doc:`uproot.interpretation.Interpretation` and
temporary array for jagged (variable-length list) data.

The :doc:`uproot.interpretation.jagged.JaggedArray` class only holds data while
an array is being built from ``TBaskets``. Its final form is determined by
:doc:`uproot.interpretation.library`.
é    )ÚannotationsNc                 C  sX   |dkr| S |dkr t  | d¡S |dkr4t  | d¡S |dkrHt  | d¡S t  | |¡S dS )z…
    Vectorized division function for a scalar divisor that is often a power of
    2. Usually bit-shifts, rather than dividing.
    é   é   é   é   é   N)ÚnumpyZright_shiftZfloor_divide)ÚarrayZdivisor© r
   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/interpretation/jagged.pyÚfast_divide   s    r   c                   @  sŒ   e Zd ZdZddd„Zedd„ ƒZedd	„ ƒZed
d„ ƒZdd„ Z	dd„ Z
edd„ ƒZd dd„Zedd„ ƒZedd„ ƒZdd„ Zdd„ ZdS )!ÚAsJaggeda‚  
    Args:
        content (:doc:`uproot.interpretation.numerical.AsDtype` or :doc:`uproot.interpretation.objects.AsStridedObjects`): Interpretation
            for data in the nested, variable-length lists.
        header_bytes (int): Number of bytes to skip at the beginning of each
            entry.
        typename (None or str): If None, construct a plausible C++ typename.
            Otherwise, take the suggestion as given.
        original (None, :doc:`uproot.model.Model`, or :doc:`uproot.containers.Container`): If
            this interpretation is derived from
            :ref:`uproot.interpretation.objects.AsObjects.simplify`, this is a
            reminder of the original
            :ref:`uproot.interpretation.objects.AsObjects.model`.

    Interpretation for any array that can be described as variable-length lists
    of :doc:`uproot.interpretation.numerical.AsDtype`.
    r   Nc                 C  s4   t |tjjjƒstdƒ‚|| _|| _|| _|| _	d S )Nz&AsJagged content can only be Numerical)
Ú
isinstanceÚuprootÚinterpretationZ	numericalZ	NumericalÚ	TypeErrorÚ_contentÚ_header_bytesÚ	_typenameÚ	_original)ÚselfÚcontentÚheader_bytesÚtypenameÚoriginalr
   r
   r   Ú__init__6   s    zAsJagged.__init__c                 C  s   | j S )zÈ
        The :doc:`uproot.interpretation.numerical.AsDtype` or
        :doc:`uproot.interpretation.objects.AsStridedObjects` that interprets
        data in the nested, variable-length lists.
        ©r   ©r   r
   r
   r   r   >   s    zAsJagged.contentc                 C  s   | j S )zM
        The number of bytes to skip at the beginning of each entry.
        )r   r   r
   r
   r   r   G   s    zAsJagged.header_bytesc                 C  s   | j S )a  
        If not None, this was the original
        :ref:`uproot.interpretation.objects.AsObjects.model` from an
        :doc:`uproot.interpretation.objects.AsObjects` that was simplified
        into this :doc:`uproot.interpretation.jagged.AsJagged`.
        )r   r   r
   r
   r   r   N   s    zAsJagged.originalc                 C  s2   | j dkrd| j›d�S d| j›d| j › d�S d S )Nr   z	AsJagged(ú)z, header_bytes=)r   r   r   r
   r
   r   Ú__repr__X   s    
zAsJagged.__repr__c                 C  s"   t |tƒo | j|jko | j|jkS ©N)r   r   r   r   )r   Úotherr
   r
   r   Ú__eq__^   s
    

ÿ
ýzAsJagged.__eq__c                 C  s
   t  t¡S r    )r   ÚdtypeÚobjectr   r
   r
   r   Únumpy_dtypee   s    zAsJagged.numpy_dtypeÚi64Fr
   c                 C  s<   |   |||||¡}tj ¡ }|j |d tj | j||¡¡S )NÚindex_format)	Z_make_contextr   ÚextrasÚawkwardZformsZListOffsetFormÚ_utilÚawkward_formr   )r   ÚfileÚcontextr'   ÚheaderZtobject_headerZbreadcrumbsr)   r
   r
   r   r+   i   s    	
ÿ
þzAsJagged.awkward_formc                 C  s"   t | ƒj› d| jj› d| j› d�S )Nú(ú,r   )ÚtypeÚ__name__r   Ú	cache_keyr   r   r
   r
   r   r3   {   s    zAsJagged.cache_keyc                 C  sb   | j d u rX| jj}z(| d¡}|d |… d ||d …  W S  tyT   |d  Y S 0 n| j S d S )Nú[z[])r   r   r   ÚindexÚ
ValueError)r   r   Úir
   r
   r   r      s    

zAsJagged.typenamec	                 C  sÖ  | j ||||||||d� |d u r–|j}	|	d u rJtj dd d ||jj¡‚nL| jjj	}
t
 |	|
¡}	t
jt|	ƒd t
jd�}d|d< t
j|	|dd … d� | jdkrÔt|| jj	ƒ}| j |d ||||||¡}t||ƒ}nâ|d d… | j }|dd … }t
 | j¡}|d d… |d d …t
jf   ¡ }t
jt|ƒdt
jd�}d	||< || }| j |d ||||||¡}|| }t|| jj	ƒ}	t
jt|	ƒd t
jd�}d|d< t
j|	|dd … d� t||ƒ}| j|||||||||d
�	 |S )N)ÚdataÚbyte_offsetsÚbasketÚbranchr-   Úcursor_offsetÚlibraryÚoptionsz;missing offsets (and missing count branch) for jagged arrayr   ©r#   r   )ÚoutéÿÿÿÿTF)	r8   r9   r:   r;   r-   Úoutputr<   r=   r>   )Zhook_before_basket_arrayÚcountsr   ZdeserializationZDeserializationErrorr,   Ú	file_pathr   Z
from_dtypeÚitemsizer   ÚmultiplyÚemptyÚlenÚint32Zcumsumr   r   Úbasket_arrayÚJaggedArrayÚarangeZnewaxisZravelÚfullZbool_Zhook_after_basket_array)r   r8   r9   r:   r;   r-   r<   r=   r>   rC   rE   Úoffsetsr   rB   Zbyte_startsZ
byte_stopsZheader_offsetsZheader_idxsÚmaskZbyte_countsr
   r
   r   rJ   ‹   sv    øû

ÿ"ÿ
÷zAsJagged.basket_arrayc              
   C  sJ  | j |||||||d� i }i }	| ¡ D ]\}
}|j||
< |j|	|
< q(||krz| dtj¡}t d| jj¡}t	||ƒ}�nžd}|d }t
|dd … ƒD ]˜\}}||kr¼||kr¼||| 7 }nn||  krÐ|k rân n||| 7 }nH||  krø|k�r
n n||| 7 }n ||k �r*||k�r*||| 7 }|}q–t |d ftj¡}d}|d }g }t
|dd … ƒD �]@\}}||k�rú||k�rú|| }|| }|| |	|  }}|||  |||d …  |d d …< ||| ||  7 }| ||| || … ¡ �n¦||  k�r|k �r–n n€|| }|| }|| |	|  }}|||  |||d …  |d || d …< ||| ||  7 }| ||| || … ¡ �n
||  k�r®|k�r(n nvd}|| }|| |	|  }}|||  |||d …  ||| d …< ||| ||  7 }| ||| || … ¡ nx||k �r ||k�r || |	|  }}||d  | ||| || d …< ||d |d  7 }| ||d |d … ¡ |}�qdt |f| jj¡}d}|D ]&}||||t|ƒ …< |t|ƒ7 }�qÂ| j |¡}t	||ƒ}| j|||||||d� | ||| |||¡}| j||||||||d� |S )N)Úbasket_arraysÚentry_startÚ
entry_stopÚentry_offsetsr=   r;   r>   )r   r   r   rA   )rP   rQ   rR   rS   r=   r;   rB   )rP   rQ   rR   rS   r=   r;   rB   r>   )Zhook_before_final_arrayÚitemsrN   r   Zzerosr   Úint64rG   Zto_dtyperK   Ú	enumerateÚappendrH   r   Z_wrap_almost_finalizedZhook_before_library_finalizeÚfinalizeZhook_after_final_array)r   rP   rQ   rR   rS   r=   r;   r>   Zbasket_offsetsZbasket_contentÚkÚvrN   r   rB   ÚlengthÚstartÚ_ÚstopÚbeforeÚcontentsZ
basket_numZlocal_startZ
local_stopÚoffZcntr
   r
   r   Úfinal_arrayÞ   sÀ    
ù

ÿ
ÿÿÿ
ù
ÿøzAsJagged.final_array)r   NN)Nr&   FFr
   )r2   Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r   r"   r%   r+   r3   r   rJ   rb   r
   r
   r
   r   r   #   s0   



	
     ù


Sr   c                   @  sX   e Zd ZdZdd„ Zdd„ Zedd„ ƒZedd	„ ƒZd
d„ Z	dd„ Z
dd„ Zdd„ ZdS )rK   a*  
    Args:
        offsets (array of ``numpy.int32``): Starting and stopping entries for
            each variable-length list. The length of the ``offsets`` is one
            greater than the number of lists.
        content (array): Contiguous array for data in all nested lists of the
            jagged array.

    Temporary array filled by
    :ref:`uproot.interpretation.jagged.AsJagged.basket_array`, which will be
    turned into a NumPy, Awkward, or other array, depending on the specified
    :doc:`uproot.interpretation.library.Library`.
    c                 C  s   || _ || _d S r    ©Ú_offsetsr   )r   rN   r   r
   r
   r   r   n  s    zJaggedArray.__init__c                 C  s   d| j › d| j› d�S )NzJaggedArray(z, r   rg   r   r
   r
   r   r   r  s    zJaggedArray.__repr__c                 C  s   | j S )z�
        Starting and stopping entries for each variable-length list. The length
        of the ``offsets`` is one greater than the number of lists.
        )rh   r   r
   r
   r   rN   u  s    zJaggedArray.offsetsc                 C  s   | j S )zT
        Contiguous array for data in all nested lists of the jagged array.
        r   r   r
   r
   r   r   }  s    zJaggedArray.contentc                 C  s   | j | j| | j|d  … S ©Nr   )r   rh   )r   Úwherer
   r
   r   Ú__getitem__„  s    zJaggedArray.__getitem__c                 C  s   t | jƒd S ri   )rH   rh   r   r
   r
   r   Ú__len__‡  s    zJaggedArray.__len__c                 c  s:   | j d }| j}| j dd … D ]}|||… V  |}qd S )Nr   r   rg   )r   r\   r   r^   r
   r
   r   Ú__iter__Š  s
    
zJaggedArray.__iter__c                 C  s¨   | j dd… | j dd…  }tjjr0| tj¡}|| t| j ƒd ksJJ ‚tjt| j ƒd tj	d�}t 
||¡}tj| j d | j d tj	d�}|| j | 8 }|| |fS )a}  
        Args:
            entry_start (int): First entry to include.
            entry_stop (int): FIrst entry to exclude (one greater than the last
                entry to include)

        Returns the "parents" and "localindex" of this jagged array, using
        Awkward 0 terminology.

        The "parents" is an array of integers with the same length as
        :ref:`uproot.interpretation.jagged.JaggedArray.content` that indicates
        which list each item belongs to.

        The "localindex" is an array of integers with the same length that
        indicates which subentry each item is, within its nested list.
        r   NrA   r?   r   )rh   r   r*   ÚwinZastyper   rI   rH   rL   rU   Úrepeat)r   rQ   rR   rC   ZindexesÚparentsZ
localindexr
   r
   r   Úparents_localindex‘  s    ÿzJaggedArray.parents_localindexN)r2   rc   rd   re   r   r   rf   rN   r   rk   rl   rm   rq   r
   r
   r
   r   rK   _  s   

rK   )
re   Ú
__future__r   r   r   r   r   ZInterpretationr   rK   r
   r
   r
   r   Ú<module>   s     >