a
    ߙfbE$                     @   s$   d dl ZdddZG dd dZdS )    Nleftc                 C   s|   t | dr| j||dS d}t| }||k rx|| d }|| | krN|d }q$|| | k r`|}q$|dkrr|d }q$|}q$|S )zN
    Call np.searchsorted or use a custom binary
    search if necessary.
    searchsortedsider         right)hasattrr   len)arrayvalr   beginendZmid r   9lib/python3.9/site-packages/astropy/table/sorted_array.py_searchsorted   s    


r   c                   @   s   e Zd ZdZd$ddZedd Zdd Zd	d
 Zd%ddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#S )&SortedArrayaa  
    Implements a sorted array container using
    a list of numpy arrays.

    Parameters
    ----------
    data : Table
        Sorted columns of the original table
    row_index : Column object
        Row numbers corresponding to data columns
    unique : bool
        Whether the values of the index must be unique.
        Defaults to False.
    Fc                 C   s(   || _ || _tt|dg | _|| _d S )NZcolnames)data	row_indexr
   getattrnum_colsunique)selfr   r   r   r   r   r   __init__,   s    zSortedArray.__init__c                 C   s   t | jj S N)listr   columnsvaluesr   r   r   r   cols2   s    zSortedArray.colsc                    s     |jrbd  kr,tjk rbn n2t fddtt D rbtd  dj  j	|_dS )z
        Add a new entry to the sorted array.

        Parameters
        ----------
        key : tuple
            Column values at the given row
        row : int
            Row number
        r   c                 3   s$   | ]}j  |  | kV  qd S r   )r   ).0ikeyposr   r   r   	<genexpr>D       z"SortedArray.add.<locals>.<genexpr>zCannot add duplicate value "z" in a unique indexN)
find_posr   r
   r   allrange
ValueErrorr   Z
insert_rowinsert)r   r#   rowr   r"   r   add6   s    $ zSortedArray.addc                 C   s.   || j k r| j| || S | j|| S dS )zW
        Retrieve the ith slice of the sorted array
        from begin to end.
        N)r   r   r   )r   r!   r   r   r   r   r   _get_key_sliceJ   s    
zSortedArray._get_key_slicec           
      C   s   d}t | j}| j}| js,||f }|d7 }t|D ]}| |||}t||| }	|rz|	t |kst||	 || krz dS |	t |ks|	dkrt |dkr|| |d k r||	   S |t||| dd }||	7 }|t | jkr4|  S q4|S )au  
        Return the index of the largest key in data greater than or
        equal to the given key, data pair.

        Parameters
        ----------
        key : tuple
            Column key
        data : int
            Row number
        exact : bool
            If True, return the index of the given key in data
            or -1 if the key is not present.
        r   r   r   r   )r
   r   r   r   r)   r.   r   )
r   r#   r   exactr   r   r   r!   	key_slicetr   r   r   r'   T   s(    

  
zSortedArray.find_posc                 C   s   d}t | j}t| jD ]}| |||}t||| }|t |ksT|| || kr\g   S |dkrt |dkr|| |d k rg   S |t||| dd }||7 }|t | jkrg   S q| j|| S )z
        Find all rows matching the given key.

        Parameters
        ----------
        key : tuple
            Column values

        Returns
        -------
        matching_rows : list
            List of rows matching the input key
        r   r   r   )r
   r   r)   r   r.   r   )r   r#   r   r   r!   r1   r2   r   r   r   find~   s    
$
zSortedArray.findc                    s   |  |d |  |d t| jkr*g S t fdd| jD }|d sZ||krZ d7  t| jk rtfdd| jD }|d s||krd8 n||krd8 | j d  S )a  
        Find values in the given range.

        Parameters
        ----------
        lower : tuple
            Lower search bound
        upper : tuple
            Upper search bound
        bounds : (2,) tuple of bool
            Indicates whether the search should be inclusive or
            exclusive with respect to the endpoints. The first
            argument corresponds to an inclusive lower bound,
            and the second argument to an inclusive upper bound.
        r   c                    s   g | ]}|  qS r   r   r    col)	lower_posr   r   
<listcomp>   r&   z%SortedArray.range.<locals>.<listcomp>r   c                    s   g | ]}|  qS r   r   r4   )	upper_posr   r   r7      r&   )r'   r
   r   tupler   )r   lowerupperZboundsZlower_boundZupper_boundr   )r6   r8   r   r)      s    
zSortedArray.rangec                 C   sT   | j ||dd}|dkrdS | j| tjt| jtd}d||< | j| | _dS )a0  
        Remove the given entry from the sorted array.

        Parameters
        ----------
        key : tuple
            Column values
        data : int
            Row number

        Returns
        -------
        successful : bool
            Whether the entry was successfully removed
        T)r0   r/   FZdtype)r'   r   Z
remove_rownpZonesr
   r   bool)r   r#   r   r$   Z	keep_maskr   r   r   remove   s    zSortedArray.removec                 C   s   | j | j |k  d8  < dS )z
        Decrement all row numbers greater than the input row.

        Parameters
        ----------
        row : int
            Input row number
        r   Nr   r   r,   r   r   r   
shift_left   s    	zSortedArray.shift_leftc                 C   s   | j | j |k  d7  < dS )z
        Increment all row numbers greater than or equal to the input row.

        Parameters
        ----------
        row : int
            Input row number
        r   Nr@   rA   r   r   r   shift_right   s    	zSortedArray.shift_rightc                    s   t  }tjt | jtd}d}t| jD ],\}}| v r*d||< |d7 }||kr* qXq*| j| | _t fdd| j| D | _dS )a+  
        Replace all rows with the values they map to in the
        given dictionary. Any rows not present as keys in
        the dictionary will have their entries deleted.

        Parameters
        ----------
        row_map : dict
            Mapping of row numbers to new row numbers
        r<   r   Tr   c                    s   g | ]} | qS r   r   )r    xrow_mapr   r   r7     r&   z,SortedArray.replace_rows.<locals>.<listcomp>N)r
   r=   Zzerosr   r>   	enumerater   r   )r   rF   Znum_rowsZ	keep_rowsZtaggedr!   r,   r   rE   r   replace_rows   s    zSortedArray.replace_rowsc                 C   sd   g }d}t t| jj  D ]B\}}| j| }||krJ|d d | q|}|||gf q|S )zs
        Retrieve all array items as a list of pairs of the form
        [(key, [row 1, row 2, ...]), ...]
        Nr/   r   )rG   zipr   r   r   r   append)r   r   Zlast_keyr!   r#   r,   r   r   r   items  s    
zSortedArray.itemsc                 C   s   t t| j| _dS )z6
        Make row order align with key order.
        N)r=   Zaranger
   r   r   r   r   r   sort  s    zSortedArray.sortc                 C   s   | j S )z.
        Return rows in sorted order.
        r@   r   r   r   r   sorted_data#  s    zSortedArray.sorted_datac                 C   s   t | j| | j| S )z
        Return a sliced reference to this sorted array.

        Parameters
        ----------
        item : slice
            Slice to use for referencing
        )r   r   r   )r   itemr   r   r   __getitem__)  s    	zSortedArray.__getitem__c                 C   s2   | j  }| j|d< d| jj dt| d| S )NZrows<z length=z>
)r   copyr   	__class____name__r
   )r   r2   r   r   r   __repr__4  s    

zSortedArray.__repr__N)F)F)rS   
__module____qualname____doc__r   propertyr   r-   r.   r'   r3   r)   r?   rB   rC   rH   rK   rL   rM   rO   rT   r   r   r   r   r      s$   



*##r   )r   )Znumpyr=   r   r   r   r   r   r   <module>   s   
