a
    yn„f€"  ã                
   @  s‚   d dl mZ d dlmZ d dlZd dlmZ d dlm	Z	m
Z
 d dlmZ dZe ¡ Zeƒ dddddd	ddd
œdd„ƒZdd„ ZdS )é    )Úannotations)ÚMappingN)Úhigh_level_function)ÚHighLevelContextÚensure_same_backend)ÚNumpyMetadata)ÚzipFT)Ú
parametersÚ	with_nameÚright_broadcastÚoptiontype_outside_recordÚ	highlevelÚbehaviorÚattrsc          	   
   c  s4   t | tƒr|  ¡ V  n| V  t| ||||||||ƒ	S )aâ  
    Args:
        arrays (mapping or sequence of arrays): Each value in this mapping or
            sequence can be any array-like data that #ak.to_layout recognizes.
        depth_limit (None or int): If None, attempt to fully broadcast the
            `array` to all levels. If an int, limit the number of dimensions
            that get broadcasted. The minimum value is `1`, for no
            broadcasting.
        parameters (None or dict): Parameters for the new
            #ak.contents.RecordArray node that is created by this operation.
        with_name (None or str): Assigns a `"__record__"` name to the new
            #ak.contents.RecordArray node that is created by this operation
            (overriding `parameters`, if necessary).
        right_broadcast (bool): If True, follow rules for implicit
            right-broadcasting, as described in #ak.broadcast_arrays.
        optiontype_outside_record (bool): If True, continue broadcasting past
            any option types before creating the new #ak.contents.RecordArray node.
        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.

    Combines `arrays` into a single structure as the fields of a collection
    of records or the slots of a collection of tuples. If the `arrays` have
    nested structure, they are broadcasted with one another to form the
    records or tuples as deeply as possible, though this can be limited by
    `depth_limit`.

    This operation may be thought of as the opposite of projection in
    #ak.Array.__getitem__, which extracts fields one at a time, or
    #ak.unzip, which extracts them all in one call.

    Consider the following arrays, `one` and `two`.

        >>> one = ak.Array([[1.1, 2.2, 3.3], [], [4.4, 5.5], [6.6]])
        >>> two = ak.Array([["a", "b", "c"], [], ["d", "e"], ["f"]])

    Zipping them together using a dict creates a collection of records with
    the same nesting structure as `one` and `two`.

        >>> ak.zip({"x": one, "y": two}).show()
        [[{x: 1.1, y: 'a'}, {x: 2.2, y: 'b'}, {x: 3.3, y: 'c'}],
         [],
         [{x: 4.4, y: 'd'}, {x: 5.5, y: 'e'}],
         [{x: 6.6, y: 'f'}]]

    Doing so with a list creates tuples, whose fields are not named.

        >>> ak.zip([one, two]).show()
        [[(1.1, 'a'), (2.2, 'b'), (3.3, 'c')],
         [],
         [(4.4, 'd'), (5.5, 'e')],
         [(6.6, 'f')]]

    Adding a third array with the same length as `one` and `two` but less
    internal structure is okay: it gets broadcasted to match the others.
    (See #ak.broadcast_arrays for broadcasting rules.)

        >>> three = ak.Array([100, 200, 300, 400])
        >>> ak.zip([one, two, three]).show()
        [[(1.1, 'a', 100), (2.2, 'b', 100), (3.3, 'c', 100)],
         [],
         [(4.4, 'd', 300), (5.5, 'e', 300)],
         [(6.6, 'f', 400)]]

    However, if arrays have the same depth but different lengths of nested
    lists, attempting to zip them together is a broadcasting error.

        >>> one = ak.Array([[[1, 2, 3], [], [4, 5], [6]], [], [[7, 8]]])
        >>> two = ak.Array([[[1.1, 2.2], [3.3], [4.4], [5.5]], [], [[6.6]]])
        >>> ak.zip([one, two])
        ValueError: while calling
            ak.zip(
                arrays = [<Array [[[1, 2, 3], [], [4, ...], [6]], ...] type='3 * var ...
                depth_limit = None
                parameters = None
                with_name = None
                right_broadcast = False
                optiontype_outside_record = False
                highlevel = True
                behavior = None
            )
        Error details: cannot broadcast nested list

    For this, one can set the `depth_limit` to prevent the operation from
    attempting to broadcast what can't be broadcasted.

        >>> ak.zip([one, two], depth_limit=1).show()
        [([[1, 2, 3], [], [4, ...], [6]], [[1.1, ...], ...]),
         ([], []),
         ([[7, 8]], [[6.6]])]

    As an extreme, `depth_limit=1` is a handy way to make a record structure
    at the outermost level, regardless of whether the fields have matching
    structure or not.

    When zipping together arrays with optional values, it can be useful to create
    the #ak.contents.RecordArray node after the option types. By default, #ak.zip
    does not do this:

        >>> one = ak.Array([1, 2, None])
        >>> two = ak.Array([None, 5, 6])
        >>> ak.zip([one, two])
        <Array [(1, None), (2, 5), (None, 6)] type='3 * (?int64, ?int64)'>

    If the `optiontype_outside_record` option is set to `True`, Awkward will continue to
    broadcast the arrays together at the depth_limit until it reaches non-option
    types. This effectively takes the union of the option mask:

        >>> ak.zip([one, two], optiontype_outside_record=True)
        <Array [None, (2, 5), None] type='3 * ?(int64, int64)'>
    )Ú
isinstancer   ÚvaluesÚ_impl)	ÚarraysÚdepth_limitr	   r
   r   r   r   r   r   © r   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/operations/ak_zip.pyr      s     
÷r   c	                   sn  ˆd urˆdkrt dƒ‚t||d��\‰t| tƒrXt‡fdd„|  ¡ D ƒŽ }	t|  ¡ ƒ‰nt‡fdd„| D ƒŽ }	d ‰W d   ƒ n1 s†0    Y  tdd„ |	D ƒdƒ‰ d	d
„ |	D ƒ}
‡ fdd
„|	D ƒ}	t	|
ƒ }|d urôˆd u räi ‰nt
ˆƒ‰|ˆd< ‡‡‡‡fdd„}tjj|	||d�}t|tƒ�r2t|ƒdk�s6J ‚|d }|�r`|d }t|tjjƒ�s`J ‚ˆj||d�S )Nr   z&depth_limit must be None or at least 1)r   r   c                 3  s"   | ]}ˆ j |d d ddd�V  qdS ©Fzpass-through)Zallow_recordZallow_unknownZnone_policyZprimitive_policyN©Úunwrap©Ú.0Úx©Úctxr   r   Ú	<genexpr>´   s   ùûz_impl.<locals>.<genexpr>c                 3  s"   | ]}ˆ j |d d ddd�V  qdS r   r   r   r   r   r   r   Ã   s   ùûc                 s  s   | ]}t |d ƒr|jV  qdS )ÚbackendN)Úhasattrr    )r   Úbr   r   r   r   Ñ   ó    Úcpuc                 S  s   g | ]}t |tjjƒ‘qS r   )r   ÚakÚcontentsÚContentr   r   r   r   Ú
<listcomp>Ò   r#   z_impl.<locals>.<listcomp>c                   s8   g | ]0}t |tjjtjjfƒr"|ntj |¡ ˆ ¡‘qS r   )	r   r%   r&   r'   ÚrecordÚRecordÚ
operationsZ	to_layoutZ
to_backendr   )r    r   r   r(   Ó   s   þÿZ
__record__c                   sR   ˆ |kst dd„ | D ƒƒrJˆr4tdd„ | D ƒƒr4d S tjj| ˆˆ|d�fS d S d S )Nc                 s  s   | ]}|j d kV  qdS )é   N)Zpurelist_depthr   r   r   r   r   ä   r#   z(_impl.<locals>.action.<locals>.<genexpr>c                 s  s   | ]}|j V  qd S )N)Z	is_optionr   r   r   r   r   æ   r#   )r	   r    )ÚallÚanyr%   r&   ZRecordArray)ÚinputsÚdepthr    Úignore)r   Úfieldsr   r	   r   r   Úactionã   s    ÿÿz_impl.<locals>.action)r   r,   )r   )Ú
ValueErrorr   r   r   r   r   ÚlistÚkeysÚnextr.   Údictr%   Z_broadcastingZbroadcast_and_applyÚtupleÚlenr)   r*   Úwrap)r   r   r	   r
   r   r   r   r   r   ZlayoutsZlayout_is_contentZ	to_recordr3   Úoutr   )r    r   r   r2   r   r	   r   r   ¤   sJ    

øÿ
øÿ"
ü
ÿr   )N)Ú
__future__r   Úcollections.abcr   Zawkwardr%   Zawkward._dispatchr   Zawkward._layoutr   r   Zawkward._nplikes.numpy_liker   Ú__all__ÚinstanceÚnpr   r   r   r   r   r   Ú<module>   s(    þö 