
    %`i!                        d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	m
Z
mZ d dlmZ  e
d          Zededed	efd
            Ze	 	 	 	 ddededede	ee                  de	ee                  de	ee                  d	efd            Ze	 	 	 ddedededefd            Zed deeef         ded	eeef         fd            Zed deeef         ded	eeef         fd            Ze	 d dedeeef         ded	eeef         fd            ZdS )!    N)deque)MappingSequence)DictListOptionalTypeVarUnion)
DeprecatedTd1d2returnc                 R    t          j        |           }t          ||dg            |S )z
    Args:
        d1 (dict): Dict 1.
        d2 (dict): Dict 2.

    Returns:
         dict: A new dict that is d1 and d2 deep merged.
    T)copydeepcopydeep_update)r   r   mergeds      e/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/dict.pymerge_dictsr      s,     ]2FD"%%%M    Foriginalnew_dictnew_keys_allowedallow_new_subkey_listoverride_all_if_type_changesoverride_all_key_listc                    |pg }|pg }|pg }|                                 D ]\  }}|| vr$|s"t          d                    |                    t          |                     |          t
                    rt          |t
                    rl||vrh||v r,d|v r(d| |         v r|d         | |         d         k    r|| |<   ||v rt          | |         |d|           t          | |         |||           || |<   | S )a  Updates original dict with values from new_dict recursively.

    If new key is introduced in new_dict, then if new_keys_allowed is not
    True, an error will be thrown. Further, for sub-dicts, if the key is
    in the allow_new_subkey_list, then new subkeys can be introduced.

    Args:
        original: Dictionary with default values.
        new_dict: Dictionary with values to be updated
        new_keys_allowed: Whether new keys are allowed.
        allow_new_subkey_list: List of keys that
            correspond to dict values where new subkeys can be introduced.
            This is only at the top level.
        override_all_if_type_changes: List of top level
            keys with value=dict, for which we always simply override the
            entire value (dict), iff the "type" key in that value dict changes.
        override_all_key_list: List of top level keys
            for which we override the entire value if the key is in the new_dict.
    zUnknown config parameter `{}` typeT)r   )items	Exceptionformat
isinstancegetdictr   )r   r   r   r   r   r   kvalues           r   r   r      so   8 27R#?#E2 17RNN$$ %  % 5H%5<CCAFFGGG x||A-- 	 5$'' 	  ... 111eOOhqk))&MXa[%888#+++QK*?	     QK$*?	      HQKKOr   /dt	delimiterprevent_delimiterflatten_listc           
         fd}t          j         |           } |r%t          fd| D                       r
 |             |rt          t          fnt          t          fd|                                 D                       r]g }i }|                                 D ]\  }}t          |t                    rg|                                D ]<\  }	}
|r|	v r
 |             |
|                    |t          |	          g          <   =|	                    |           |rvt          |t                    rat          |          D ]<\  }}
|r|	v r
 |             |
|                    |t          |          g          <   =|	                    |           |                     |           |D ]}| |= t          fd|                                 D                       ]| S )zxFlatten dict.

    Output and input are of the same dict type.
    Input dict remains the same after the operation.
    c                  *    t          d  d          )NzFound delimiter `z^` in key when trying to flatten array. Please avoid using the delimiter in your specification.)
ValueError)r*   s   r   _raise_delimiter_exceptionz0flatten_dict.<locals>._raise_delimiter_exceptionp   s/    N	 N N N
 
 	
r   c              3       K   | ]}|v V  	d S N ).0keyr*   s     r   	<genexpr>zflatten_dict.<locals>.<genexpr>w   s(       @ @cc!1 @ @ @ @ @ @r   c              3   8   K   | ]}t          |          V  d S r2   )r#   )r4   vwhile_checks     r   r6   zflatten_dict.<locals>.<genexpr>}   s-      >>QjK((>>>>>>r   )r   anyr%   listvaluesr    r#   joinstrappend	enumerateupdate)r)   r*   r+   r,   r0   removeaddr5   r'   subkeyr8   ir&   r9   s    `           @r   flatten_dictrF   c   s<   
 
 
 
 
 
2B %S @ @ @ @R @ @ @@@ %""$$$".84,,DK
>>>>"))++>>>
>
> ((** 	# 	#JC%&& #!& @ @IFA( 5Y&-@-@22444>?C	S[['9::;;c"""" #*UD"9"9 #%e,, ; ;DAq( 5Y&-@-@224449:C	SVV}5566c"""
		# 	 	A1/ >>>>"))++>>>
>
> 0 Ir   c                 x   t          |           } |            }|                                 D ]\  }}|                    |          }|}|dd         D ]Y}|                    | |                      }t	          ||          s)t          d| d| d| dt          |           d	          Z|||d         <   |S )z4Unflatten dict. Does not support unflattening lists.Nz#Cannot unflatten dict due the key 'z' having a parent key 'z', which value is not of type z (got z0). Change the key names to resolve the conflict.)r   r    split
setdefaultr#   	TypeError)	r)   r*   	dict_typeoutr5   valpathitemr&   s	            r   unflatten_dictrQ      s    RI
)++CHHJJ  Syy##crc 	 	A??1iikk22DdI.. D# D D,-D D(D D04T

D D D   T"XJr   c                 ,   t          |           d                             |d          d                                         rt           nt          |           } |            }|                                 D ]\  }}|                    |          }|}t          |dd                   D ]\  }}	||dz                                            rt           nt          }
t          |t                    r|                    |	 |
                      }dt          |t                     rwt          |	          t          |          k    rB|                     |
                       t          |	          t          |          dz
  k    sJ |t          |	                   }t          |t                    r|||d         <   Gt          |t                     r@|                    |           t          |d                   t          |          dz
  k    sJ |S )aH  Unflatten nested dict and list.

    This function now has some limitations:
    (1) The keys of dt must be str.
    (2) If unflattened dt (the result) contains list, the index order must be
        ascending when accessing dt. Otherwise, this function will throw
        AssertionError.
    (3) The unflattened dt (the result) shouldn't contain dict with number
        keys.

    Be careful to use this function. If you want to improve this function,
    please also improve the unit test. See #14487 for more details.

    Args:
        dt: Flattened dictionary that is originally nested by multiple
            list and dict.
        delimiter: Delimiter of keys.

    Example:
        >>> dt = {"aaa/0/bb": 12, "aaa/1/cc": 56, "aaa/1/dd": 92}
        >>> unflatten_list_dict(dt)
        {'aaa': [{'bb': 12}, {'cc': 56, 'dd': 92}]}
    r      NrH   )r;   rI   isdigitr   r    r@   r%   r#   rJ   intlenr?   )r)   r*   out_typerM   r5   rN   rO   rP   rE   r&   	next_types              r   unflatten_list_dictrY      s   2 Bxx{((A66q9AACCQttbH
(**CHHJJ 2 2Syy##d3B3i(( 	$ 	$DAq $QU 3 3 5 5?4I$%% $q))++66D$'' $q66SYY&&KK		,,,q66SYY]2222CFF|dD!! 	2 DbNNd## 	2KKtBx==CIIM1111Jr   flat_keylookupc                    | |v r||          S t          |                     |                    }|}|r|                                }	 t          |t                    r	||         }n9t          |t
                    r|t          |                   }nt                      n%# t          $ r}d|v r|d         cY d}~S |d}~ww xY w||S )z
    Unflatten `flat_key` and iteratively look up in `lookup`. E.g.
    `flat_key="a/0/b"` will try to return `lookup["a"][0]["b"]`.
    defaultN)r   rI   popleftr#   r   r   rU   KeyError)rZ   r[   r*   kwargskeysbaser5   es           r   unflattened_lookuprd      s     6h	**++DD
 llnn
	$(( !CyD(++ !CHH~jj  	 	 	F""i(((((((G	   Ks%   AB   
C*B=5C;B==C)FNNN)r(   FF)r(   )r   collectionsr   collections.abcr   r   typingr   r   r   r	   r
   ray.util.annotationsr   r   r%   r   boolr>   r   rF   rQ   rY   rd   r3   r   r   <module>rj      s          - - - - - - - - 7 7 7 7 7 7 7 7 7 7 7 7 7 7 + + + + + +GCLL D d t      #158<15E EEE E $DI.	E
 #+49"5E $DI.E 
E E E EP  #	1 111 1 	1 1 1 1h  tCF|  d36l    ( - -DaL -S -4Q< - - - -` FI  (!23@C
7H     r   