a
    zn„f�I  ã                   @  s`   d Z ddlmZ ddlZddlZddlZddlZe d¡ZG dd„ dej	j
ƒZG dd„ dƒZdS )	a	  
This module defines an :doc:`uproot.interpretation.Interpretation` and
temporary array for string data.

Note that :doc:`uproot.interpretation.strings.AsStrings` is an interpretation for
top-level strings, but :doc:`uproot.containers.AsString` can be nested within any
other :doc:`uproot.containers.AsContainer`.

The :doc:`uproot.interpretation.strings.StringArray` class only holds data while
an array is being built from ``TBaskets``. Its final form is determined by the
:doc:`uproot.interpretation.library.Library`.
é    )ÚannotationsNz>Ic                   @  s–   e Zd ZdZdd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edd„ ƒZd$dd„Zedd„ ƒZdd „ Zd!d"„ ZdS )%Ú	AsStringsa¬  
    Args:
        header_bytes (int): Number of bytes to skip at the beginning of each
            entry.
        length_bytes ("1-5" or "4"): Method used to determine the length of
            a string: "1-5" means one byte if the length is less than 256,
            otherwise the true length is in the next four bytes; "4" means
            always four bytes.
        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`.

    An :doc:`uproot.interpretation.Interpretation` for an array of strings.

    This cannot be nested within other
    :doc:`uproot.interpretation.Interpretation` objects; it can only represent
    a ``TBranch`` that only contains strings (not strings within ``std::vector``,
    for instance).

    Note that the :doc:`uproot.containers.AsString` class is for strings nested
    within other objects.

    (:ref:`uproot.interpretation.objects.AsObjects.simplify` converts an
    :doc:`uproot.interpretation.objects.AsObjects` of
    :doc:`uproot.containers.AsString` into a
    :doc:`uproot.interpretation.strings.AsStrings`.)
    a.  
            input stream

            output out-main uint8
            output out-offsets int64

            0 out-offsets <- stack

            begin
                stream !B-> stack dup 255 = if drop stream !I-> stack then dup out-offsets +<- stack stream #!B-> out-main
            again
        a  
            input stream

            output out-main uint8
            output out-offsets int64

            0 out-offsets <- stack

            begin
                stream I-> stack dup out-offsets <- stack stream #B-> out-main
            again
        ©ú1-5Ú4r   r   Nc                 C  s>   || _ |dv r|| _ntdƒ‚|| _|| _d | _t ¡ | _d S )Nr   z!length_bytes must be '1-5' or '4')	Ú_header_bytesÚ_length_bytesÚ
ValueErrorÚ	_typenameÚ	_originalZ_forth_codeÚ	threadingÚlocalÚ_threadlocal)ÚselfÚheader_bytesÚlength_bytesÚtypenameÚoriginal© r   ú`/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/interpretation/strings.pyÚ__init__W   s    zAsStrings.__init__c                 C  s   | j S )zM
        The number of bytes to skip at the beginning of each entry.
        )r   ©r   r   r   r   r   f   s    zAsStrings.header_bytesc                 C  s   | j S )zÙ
        Method used to determine the length of a string: "1-5" means one byte
        if the length is less than 256, otherwise the true length is in the
        next four bytes; "4" means always four bytes.
        )r   r   r   r   r   r   m   s    zAsStrings.length_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   v   s    zAsStrings.originalc                 C  sL   g }| j dkr | d| j › �¡ | jdkr<| d| j›�¡ d d |¡¡S )Nr   zheader_bytes=r   zlength_bytes=zAsStrings({})ú, )r   Úappendr   ÚformatÚjoin)r   Úargsr   r   r   Ú__repr__€   s    

zAsStrings.__repr__c                 C  s"   t |tƒo | j|jko | j|jkS ©N)Ú
isinstancer   r   r   )r   Úotherr   r   r   Ú__eq__ˆ   s
    

ÿ
ýzAsStrings.__eq__c                 C  s   | j d u rdS | j S d S )Nzchar*)r
   r   r   r   r   r   �   s    
zAsStrings.typenamec                 C  s
   t  t¡S r   )ÚnumpyÚdtypeÚobjectr   r   r   r   Únumpy_dtype–   s    zAsStrings.numpy_dtypeÚi64Fr   c                 C  sF   |   |||||¡}tj ¡ }|jj|d |jjdddid�ddid�S )NÚindex_formatZuint8Ú	__array__Úchar©Ú
parametersÚstring)Z_make_contextÚuprootÚextrasÚawkwardZformsZListOffsetFormZ	NumpyForm)r   ÚfileÚcontextr'   ÚheaderZtobject_headerZbreadcrumbsr/   r   r   r   Úawkward_formš   s    	
ÿ
ýzAsStrings.awkward_formc                 C  s    t | ƒj› d| j› d| j›d�S )Nú(ú,ú))ÚtypeÚ__name__r   r   r   r   r   r   Ú	cache_key­   s    zAsStrings.cache_keyc	                 C  s   | j ||||||||d� t|tjjjƒ�r|d u �rtj ¡  dd l}	| j	dks^| j	dk�rt
| jdƒs‚|	j | j| j	 ¡| j_| jj dt |¡i¡ | jjjdd� | jj d	¡}
| jj d
¡}| jj ¡  |	 |	jj|	j |
¡|	jj|ddid�ddid�¡S tt| j	ƒƒ‚�n¾|d u �r˜tjt|ƒtj d�}tjt|ƒ|j!d�}d}d}d}| j	dk�rð|t|ƒk�rt�q~|| }|d7 }|dk�r®t" #|||d … ¡\}|d7 }|||< |d7 }|||| … |||| …< ||7 }||7 }�qbnŽ| j	dk�rp|t|ƒk�r�q~t" #|||d … ¡\}|d7 }|||< |d7 }|||| … |||| …< ||7 }||7 }�qüntt| j	ƒƒ‚|d |… }|d |… }nü|d d… | j$ }|dd … }| j	dk�rðtj%t|ƒtj d�}||| dk  d7  < n2| j	dk�rtj&t|ƒdtj d�}ntt| j	ƒƒ‚||7 }tj't|ƒtj(d�}d|||t|ƒk  < tj) *|||t|ƒk  d¡ tj+||d� || ,tj-¡ }|| }tjt|ƒd tj d�}
d|
d< tj+||
dd … d� tj. /|¡}t0|
|ƒ}| j1|||||||||d�	 |S )N)ÚdataÚbyte_offsetsÚbasketÚbranchr1   Úcursor_offsetÚlibraryÚoptionsr   r   r   Úforth_vmÚstreamF)Zraise_read_beyondzout-offsetszout-mainr(   r)   r*   r,   )r#   é   éÿ   é   éÿÿÿÿ)Úout)	r:   r;   r<   r=   r1   Úoutputr>   r?   r@   )2Zhook_before_basket_arrayr   r-   Úinterpretationr?   ÚAwkwardr.   r/   Zawkward.forthr   Úhasattrr   ÚforthZForthMachine64Ú_forth_codesrA   Úbeginr"   ÚarrayÚresumerH   ÚresetZArrayÚcontentsZListOffsetArrayÚindexZIndex64Z
NumpyArrayÚAssertionErrorÚreprÚemptyÚlenZint32r#   Ú_string_4byte_sizeÚunpackr   ZonesÚfullÚzerosZint8ÚaddÚatZcumsumÚviewZbool_Ú_utilÚtobytesÚStringArrayZhook_after_basket_array)r   r:   r;   r<   r=   r1   r>   r?   r@   r/   ÚoffsetsÚcountsZoutdataÚposZ	entry_numZlen_outdataÚsizeZbyte_startsZ
byte_stopsZlength_header_sizeÚmaskrH   r   r   r   Úbasket_array±   sÔ    øÿþ

ÿ
ÿûÿ


ÿ
ÿÿ
÷zAsStrings.basket_arrayc              
   C  s˜  | j |||||||d� tdd„ | ¡ D ƒƒr´tj ||||||¡}tdd„ | ¡ D ƒƒr„t|tjj	j
ƒsnJ ‚|j}	|	j|ddd�}
| j|||||||
d� | |
|| |||¡}
�nÆi }i }| ¡ D ]\}}|j||< |j||< qÄ||k�rt| dtj¡d	ƒ}
�nJd
}|d
 }t|dd … ƒD ]¤\}}||k�rJ||k�rJ||| 7 }nt||  k�rb|k �rtn n||| 7 }nJ||  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�rD|k�r¾n nvd
}|| }|| ||  }}|||  |||d …  ||| d …< ||| ||  7 }| ||| || … ¡ nx||k �r6||k�r6|| ||  }}||d
  | ||| || d …< ||d |d
  7 }| ||d
 |d … ¡ |}�qút|d	 |¡ƒ}
| j|||||||
d� | |
|| |||¡}
| j|||||||
|d� |
S )N)Úbasket_arraysÚentry_startÚ
entry_stopÚentry_offsetsr?   r=   r@   c                 s  s   | ]}t |tƒ V  qd S r   )r   ra   ©Ú.0Úxr   r   r   Ú	<genexpr>V  ó    z(AsStrings.final_array.<locals>.<genexpr>c                 s  s   | ]}t j |d ¡V  qdS )r/   N)r-   r_   Zfrom_modulerl   r   r   r   ro   Z  s   F)Z	mergeboolZ	highlevel)rh   ri   rj   rk   r?   r=   rH   )rC   rp   r   rC   rF   )rh   ri   rj   rk   r?   r=   rH   r@   )Zhook_before_final_arrayÚanyÚvaluesr-   r_   Z
trim_finalÚallr   rI   r?   rJ   ZimportedZconcatenateZhook_before_library_finalizeÚfinalizeÚitemsrb   Úcontentra   r[   r"   Zint64Ú	enumeraterV   r   r   Zhook_after_final_array)r   rh   ri   rj   rk   r?   r=   r@   Ztrimmedr/   rH   Zbasket_offsetsZbasket_contentÚkÚvÚlengthÚstartÚ_Ústoprb   ÚbeforerR   Z
basket_numZlocal_startZ
local_stopÚoffZcntr   r   r   Úfinal_arrayB  s   
ù
ÿÿù	ÿ

þÿþÿ
þÿþÿþÿþÿþÿù
ÿøzAsStrings.final_array)r   r   NN)Nr&   FFr   )r8   Ú
__module__Ú__qualname__Ú__doc__rM   r   Úpropertyr   r   r   r   r!   r   r%   r3   r9   rg   r€   r   r   r   r   r      s:   !ó ÿ



	

     ù

 r   c                   @  sP   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S )ra   a  
    Args:
        offsets (array of ``numpy.int32``): Starting and stopping indexes for
            each string. The length of the ``offsets`` is one greater than the
            number of strings.
        content (array): Contiguous array of character data for all strings of
            the array.

    Temporary array filled by
    :ref:`uproot.interpretation.strings.AsStrings.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   )Ú_offsetsÚ_content)r   rb   rv   r   r   r   r   ó  s    zStringArray.__init__c                 C  s`   t | jƒdkrB| jd d… | jdd …  }}t|ƒd t|ƒ }n
t| jƒ}d| j› d|› d�S )Néd   é-   iÓÿÿÿz ... zStringArray(r   r6   )rW   r†   rU   r…   )r   ÚleftÚrightrv   r   r   r   r   ÷  s
    
zStringArray.__repr__c                 C  s   | j S )z‘
        Starting and stopping indexes for each string. The length of the
        ``offsets`` is one greater than the number of strings.
        )r…   r   r   r   r   rb   ÿ  s    zStringArray.offsetsc                 C  s   | j S )zR
        Contiguous array of character data for all strings of the array.
        )r†   r   r   r   r   rv     s    zStringArray.contentc                 C  s*   | j | j| | j|d  … }tj |¡S ©NrC   )r†   r…   r-   r_   Ú
ensure_str)r   Úwherer:   r   r   r   Ú__getitem__  s    zStringArray.__getitem__c                 C  s   t | jƒd S r‹   )rW   r…   r   r   r   r   Ú__len__  s    zStringArray.__len__c                 c  sB   | j d }| j}| j dd … D ]}tj |||… ¡V  |}qd S )Nr   rC   )r…   r†   r-   r_   rŒ   )r   r{   rv   r}   r   r   r   Ú__iter__  s
    
zStringArray.__iter__N)r8   r�   r‚   rƒ   r   r   r„   rb   rv   rŽ   r�   r�   r   r   r   r   ra   ä  s   

ra   )rƒ   Ú
__future__r   Ústructr   r"   r-   ÚStructrX   rI   ZInterpretationr   ra   r   r   r   r   Ú<module>   s   
   L