a
    yn„fT  ã                   @  s‚   d dl mZ d dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ dZe ¡ Zeƒ ddddd	œd
d„ƒZdd„ ZdS )é    )ÚannotationsN)Úhigh_level_function)ÚHighLevelContextÚensure_same_backend)ÚNumpyMetadata)Úis_non_string_like_sequence)Ú
with_fieldT)Ú	highlevelÚbehaviorÚattrsc                c  s   | |fV  t | |||||ƒS )aÂ  
    Args:
        array: Array-like data (anything #ak.to_layout recognizes).
        what: Array-like data (anything #ak.to_layout recognizes) to add as a new field.
        where (None or str or non-empy sequence of str): If None, the new field
            has no name (can be accessed as an integer slot number in a
            string); If str, the name of the new field. If a sequence, it is
            interpreted as a path where to add the field in a nested record.
        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.

    Returns an #ak.Array or #ak.Record (or low-level equivalent, if
    `highlevel=False`) with a new field attached. This function does not
    change the array in-place.

    See #ak.Array.__setitem__ and #ak.Record.__setitem__ for a variant that
    changes the high-level object in-place. (These methods internally use
    #ak.with_field, so performance is not a factor in choosing one over the
    other.)
    )Ú_impl)ÚarrayÚwhatÚwherer	   r
   r   © r   úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/operations/ak_with_field.pyr      s    
r   c           	        s|  ˆd u s4t ˆtƒs4tˆƒr,tdd„ ˆD ƒƒs4tdƒ‚tˆƒr|tˆƒdkr|t| t| ˆd  |ˆdd … |||ƒˆd |||ƒS tˆƒrŒˆd ‰t||d��<}t|j	| ddd	�|j	|dd
dddd�ƒ\} }W d   ƒ n1 sÚ0    Y  t
 
| j¡‰ ˆ| jv �rˆ  ˆ¡ ‡fdd„‰ˆ| ƒ�s$tdƒ‚‡ ‡fdd„}tjj| |g|d
d�}t |tƒ�rbt|ƒdk�sfJ ‚|j|d |d�S d S )Nc                 s  s   | ]}t |tƒV  qd S ©N)Ú
isinstanceÚstr©Ú.0Úxr   r   r   Ú	<genexpr>9   ó    z_impl.<locals>.<genexpr>zeNew fields may only be assigned by field name(s) or as a new integer slot by passing None for 'where'é   r   )r
   r   TÚerror)Úallow_recordÚprimitive_policyFzpass-throughZpromote)r   Zallow_unknownZnone_policyr   Zstring_policyc                   s(   d‰ ‡‡ fdd„}t jj| |dd� ˆ S )NFc                   s8   | j rd‰| S | jr0t‡ fdd„| jD ƒƒ‰| S d S d S )NTc                 3  s   | ]}ˆ |ƒV  qd S r   r   r   ©Úpurelist_is_recordr   r   r   j   r   zN_impl.<locals>.purelist_is_record.<locals>.action_is_record.<locals>.<genexpr>)Z	is_recordZis_unionÚallÚcontents)ÚinputÚkwargs)r   Úresultr   r   Úaction_is_recordc   s    z;_impl.<locals>.purelist_is_record.<locals>.action_is_record)Zreturn_array)ÚakZ_doZrecursively_apply)Zlayoutr%   r   )r$   r   r   `   s    z!_impl.<locals>.purelist_is_recordz5no tuples or records in array; cannot add a new fieldc                   s  | \‰ }ˆ j }tˆ tjjƒ�r|d u r\tj tjj|jj	ˆ j
dtjd�|jd�tj ¡ ¡}n6t|tjjƒs’tj |j |j |¡|j ˆ j
¡¡¡}ˆ jr´ˆd u r¦d }qÞg ˆ¢ˆ‘}n*ˆd u rÒg ˆ¢ttˆƒƒ‘}ng ˆ¢ˆ‘}tjj‡ fdd„ˆD ƒ|g |ˆ jd�}|fS d S d S )Néÿÿÿÿ)Zdtype)Únplikec                   s   g | ]}ˆ | ‘qS r   r   )r   Úk©Úbaser   r   Ú
<listcomp>–   r   z)_impl.<locals>.action.<locals>.<listcomp>)Ú
parameters)Úbackendr   r&   r!   ZRecordArrayZIndexedOptionArrayÚindexZIndex64Zindex_nplikeÚfullÚlengthÚnpZint64Z
EmptyArrayZContentZ
NumpyArrayr(   ÚrepeatZasarrayZshape_item_as_indexÚis_tupler   Úlenr-   )Úinputsr#   r   r.   ÚfieldsÚout)Úkeysr   r*   r   Úactionu   s@    þû
þÿýz_impl.<locals>.action)Zright_broadcast)r	   )r   r   r   r    Ú	TypeErrorr5   r   r   r   ÚunwrapÚcopyr7   ÚremoveÚ
ValueErrorr&   Z_broadcastingZbroadcast_and_applyÚtupleÚwrap)	r+   r   r   r	   r
   r   Úctxr:   r8   r   )r9   r   r   r   r   3   s\    ÿþüûÿ ú
úþ&

)
ÿr   )N)Ú
__future__r   r=   Zawkwardr&   Zawkward._dispatchr   Zawkward._layoutr   r   Zawkward._nplikes.numpy_liker   Zawkward._regularizer   Ú__all__Úinstancer2   r   r   r   r   r   r   Ú<module>   s    