a
    yn„fn  ã                   @  s¨   d dl 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 d	Ze	 ¡ Zeƒ d
dddœdd„ƒZdd„ Zefdd„Zdd„ Zdd„ ZdS )é    )ÚannotationsN)Úattrs_of_obj)Ú
backend_of)ÚNumpyBackend)Úbehavior_of_obj)Úhigh_level_function)Úwrap_layout)Úbroadcast_fieldsT)Ú	highlevelÚbehaviorÚattrsc                 g  s   |V  t || ||ƒS )añ  
    Args:
        arrays: Array-like data (anything #ak.to_layout recognizes).
        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.

    Return a list of arrays whose types contain the same number of fields. Unlike
    #ak.broadcast_arrays, this function does not require record types to occur at the
    same depths. Where fields are missing from one record, they are inserted at the same
    position with an `option[unknown]` type. This type is easily erased by ufunc and
    concatenation operations.

        >>> x, y = ak.broadcast_fields(
        ...     [{"x": {"y": 1, "z": 2, "w": [1]}}],
        ...     [{"x": [{"y": 1}]}],
        ... )
        >>> x.type.show()
        1 * {
            x: {
                y: int64,
                z: int64,
                w: var * int64
            }
        }
        >>> y.type.show()
        1 * {
            x: var * {
                y: int64,
                z: ?unknown,
                w: ?unknown
            }
        }

    )Ú_impl)r
   r   r   Úarrays© r   úi/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/operations/ak_broadcast_fields.pyr	      s    )r	   c                 C  s   | S ©Nr   ©Úcontentr   r   r   Ú	_identityA   s    r   c                   s�   ˆ d usJ ‚t ˆ tjjƒr.tˆ j‡ fdd„ƒS ˆ js@ˆ js@ˆ jrHˆˆ fS ˆ j	sZˆ j
sZˆ jrt‡ ‡fdd„}tˆ j|ƒS ˆ jr„tdƒ‚ntdƒ‚d S )Nc                   s   t j | ˆ j¡S r   )ÚakÚrecordÚRecordÚat)Úx)Úlayoutr   r   Ú<lambda>N   ó    z,_descend_to_record_or_leaf.<locals>.<lambda>c                   s   ˆˆ j | d�ƒS )Nr   ©Úcopyr   ©r   Úpullbackr   r   Ú	next_pullT   s    z-_descend_to_record_or_leaf.<locals>.next_pullzunions are not supportedzunexpected content type)Ú
isinstancer   r   r   Ú_descend_to_record_or_leafÚarrayÚ	is_recordÚis_identity_likeZis_leafZ	is_optionZ
is_indexedZis_listr   Zis_unionÚ	TypeErrorÚAssertionError)r   r    r!   r   r   r   r#   J   s    ÿ
r#   c           
        s>  t dd„ | D ƒŽ \}}tdd„ |D ƒƒs<dd„ t ||ƒD ƒS tdd„ |D ƒƒsVtdƒ‚dd„ |D ƒ‰tj d	d„ ˆD ƒ¡‰ g }ˆ D ]p}g }g }ˆD ]:}| |¡r¼| d ¡ | | 	|¡¡ q’| | 
|¡¡ q’tt|ƒƒ‰| ‡fd
d„|D ƒ¡ q‚t |Ž }t‡ fdd„t ˆ|ƒD ƒƒ‰‡fdd„|D ƒ}	dd„ t ||	ƒD ƒS )Nc                 S  s   g | ]}t |ƒ‘qS r   )r#   ©Ú.0r   r   r   r   Ú
<listcomp>c   r   z_recurse.<locals>.<listcomp>c                 s  s   | ]}|j V  qd S r   ©r%   ©r*   Úcr   r   r   Ú	<genexpr>e   r   z_recurse.<locals>.<genexpr>c                 S  s   g | ]\}}||ƒ‘qS r   r   ©r*   Úpullr   r   r   r   r+   f   r   c                 s  s   | ]}|j p|jV  qd S r   )r%   r&   r-   r   r   r   r/   h   r   zCif any inputs are records, all inputs must be records or identitiesc                 S  s   g | ]}|j r|‘qS r   r,   )r*   Úrr   r   r   r+   n   r   c                 S  s   g | ]}|j D ]}|‘qqS r   )Úfields)r*   r   Úfr   r   r   r+   p   r   c                   s    g | ]}|d u rt ˆ ƒn|‘qS r   )Únext©r*   r   )Úrecursed_field_layoutsr   r   r+   Š   s   ÿc                   s   g | ]\}}|j ˆ |d �‘qS ))r3   Úcontentsr   )r*   r   r8   )Ú
all_fieldsr   r   r+   ”   s
   üþc                   s   g | ]}|j r|ntˆ ƒ‘qS r   )r&   r5   r6   )Únext_recordsr   r   r+   ž   s   ÿc                 S  s   g | ]\}}||ƒ‘qS r   r   r0   r   r   r   r+   ¤   r   )ÚzipÚanyÚallr(   r   Z_utilZunique_listZ	has_fieldÚappendr   Zmaybe_contentÚiterÚ_recurse)
ÚinputsZ	pullbacksZnext_inputsZlayouts_by_fieldÚfieldZlayouts_to_recurseZlayouts_for_fieldr   Zlayouts_by_recordZinner_layoutsr   )r9   r:   r7   r   r@   a   sH    ÿÿ


þÿ
ûÿ
þr@   c                   s^   t | ƒdkrg S t| dtiŽ‰‡fdd„| D ƒ}tdd„ |D ƒƒ}‡ ‡‡fdd„t|| ƒD ƒS )Nr   Údefaultc                   s    g | ]}t j|d d� ˆ ¡‘qS )T)Zallow_record)r   Z	to_layoutZ
to_backendr)   )Úbackendr   r   r+   ­   r   z_impl.<locals>.<listcomp>c                 S  s4   g | ],}t |tjjƒr,|j|j|jd  … n|‘qS )é   )r"   r   r   r   r$   r   )r*   r   r   r   r   r+   °   s   þÿc              
     s0   g | ](\}}t |t|ˆd �ˆt|ˆ d�d�‘qS ))r   )r   )r   r
   r   )r   r   r   )r*   Z
layout_outZarray_in)r   r   r
   r   r   r+   ¸   s   ú

ü)Úlenr   Úcpur@   r;   )r   r
   r   r   ZlayoutsZresult_layoutsr   )r   rD   r   r
   r   r   §   s    üÿ	ùr   )Ú
__future__r   Zawkwardr   Zawkward._attrsr   Zawkward._backends.dispatchr   Zawkward._backends.numpyr   Zawkward._behaviorr   Zawkward._dispatchr   Zawkward._layoutr   Ú__all__ÚinstancerG   r	   r   r#   r@   r   r   r   r   r   Ú<module>   s   .	F