B
    d5                 @   sX   d dl Z dgZG dd dZG dd dZG dd dZG d	d
 d
ZG dd dZdS )    NBSTc               @   s<   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZeZ	dS )MaxValuezL
    Represents an infinite value for purposes
    of tuple comparison.
    c             C   s   dS )NT )selfotherr   r   ^/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/table/bst.py__gt__   s    zMaxValue.__gt__c             C   s   dS )NTr   )r   r   r   r   r   __ge__   s    zMaxValue.__ge__c             C   s   dS )NFr   )r   r   r   r   r   __lt__   s    zMaxValue.__lt__c             C   s   dS )NFr   )r   r   r   r   r   __le__   s    zMaxValue.__le__c             C   s   dS )NMAXr   )r   r   r   r   __repr__   s    zMaxValue.__repr__N)
__name__
__module____qualname____doc__r   r	   r
   r   r   __str__r   r   r   r   r      s   r   c               @   s<   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZeZ	dS )MinValuezS
    The opposite of MaxValue, i.e. a representation of
    negative infinity.
    c             C   s   dS )NTr   )r   r   r   r   r   r
   %   s    zMinValue.__lt__c             C   s   dS )NTr   )r   r   r   r   r   r   (   s    zMinValue.__le__c             C   s   dS )NFr   )r   r   r   r   r   r   +   s    zMinValue.__gt__c             C   s   dS )NFr   )r   r   r   r   r   r	   .   s    zMinValue.__ge__c             C   s   dS )NZMINr   )r   r   r   r   r   1   s    zMinValue.__repr__N)
r   r   r   r   r
   r   r   r	   r   r   r   r   r   r   r      s   r   c               @   s<   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dS )Epsilona
  
    Represents the "next largest" version of a given value,
    so that for all valid comparisons we have
    x < y < Epsilon(y) < z whenever x < y < z and x, z are
    not Epsilon objects.

    Parameters
    ----------
    val : object
        Original value
    )valc             C   s
   || _ d S )N)r   )r   r   r   r   r   __init__E   s    zEpsilon.__init__c             C   s   | j |krdS | j |k S )NF)r   )r   r   r   r   r   r
   H   s    
zEpsilon.__lt__c             C   s   | j |krdS | j |kS )NT)r   )r   r   r   r   r   r   M   s    
zEpsilon.__gt__c             C   s   dS )NFr   )r   r   r   r   r   __eq__R   s    zEpsilon.__eq__c             C   s   t | jd S )Nz
 + epsilon)reprr   )r   r   r   r   r   U   s    zEpsilon.__repr__N)
r   r   r   r   	__slots__r   r
   r   r   r   r   r   r   r   r   7   s   r   c               @   st   e Zd ZdZdd Zdd Zdd Zdd Zdd Zdd Z	d	Z
d
d Zdd Zdd Zdd Zdd Zdd ZdS )Nodez
    An element in a binary search tree, containing
    a key, data, and references to children nodes and
    a parent node.

    Parameters
    ----------
    key : tuple
        Node key
    data : list or int
        Node data
    c             C   s   | j |j k S )N)key)xyr   r   r   <lambda>f       zNode.<lambda>c             C   s   | j |j kS )N)r   )r   r   r   r   r   r   g   r   c             C   s   | j |j kS )N)r   )r   r   r   r   r   r   h   r   c             C   s   | j |j kS )N)r   )r   r   r   r   r   r   i   r   c             C   s   | j |j kS )N)r   )r   r   r   r   r   r   j   r   c             C   s   | j |j kS )N)r   )r   r   r   r   r   r   k   r   )r   dataleftrightc             C   s,   || _ t|tr|n|g| _d | _d | _d S )N)r   
isinstancelistr    r!   r"   )r   r   r    r   r   r   r   o   s    zNode.__init__c             C   sD   | j dk	r| j |kr|| _ n$| jdk	r8| j|kr8|| _ntddS )z=
        Replace this node's child with a new child.
        Nz"Cannot call replace() on non-child)r!   r"   
ValueError)r   child	new_childr   r   r   replaceu   s
    zNode.replacec             C   s   |  |d dS )z)
        Remove the given child.
        N)r(   )r   r&   r   r   r   remove   s    zNode.removec             C   s   |j | _ |jdd | _dS )z&
        Copy the given node.
        N)r   r    )r   r   r   r   r   set   s    zNode.setc             C   s   t | j| jfS )N)strr   r    )r   r   r   r   r      s    zNode.__str__c             C   s   t | S )N)r+   )r   r   r   r   r      s    zNode.__repr__N)r   r   r   r   r
   r   r   r	   r   __ne__r   r   r(   r)   r*   r   r   r   r   r   r   r   Y   s   r   c               @   s  e Zd ZdZeZd>ddZd?ddZdd	 Zd
d Z	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dAd#d$Zd%d& Zd'd( ZdBd*d+ZdCd,d-Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zed8d9 Z d:d; Z!d<d= Z"dS )Dr   aj  
    A basic binary search tree in pure Python, used
    as an engine for indexing.

    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>   d | _ d| _|| _x&t||D ]\}}| t|| qW d S )Nr   )rootsizeuniquezipaddtuple)r   r    Z	row_indexr/   r   rowr   r   r   r      s
    zBST.__init__Nc             C   s   |dkr|}|  j d7  _ | ||}| j}|dkr>|| _dS xv||k rb|jdkrZ||_P |j}q@||kr|jdkr|||_P |j}q@| jrtdq@|j|j t	|j|_dS q@W dS )z'
        Add a key, data pair.
        N   zCannot insert non-unique value)
r.   	NodeClassr-   r!   r"   r/   r%   r    extendsorted)r   r   r    node	curr_noder   r   r   r1      s0    


zBST.addc             C   s    |  |\}}|dk	r|jS g S )a	  
        Return all data values corresponding to a given key.

        Parameters
        ----------
        key : tuple
            Input key

        Returns
        -------
        data_vals : list
            List of rows corresponding to the input key
        N)	find_noder    )r   r   r8   parentr   r   r   find   s    zBST.findc             C   s   | j dkrdS | || j dS )z>
        Find the node associated with the given key.
        N)NN)r-   _find_recursive)r   r   r   r   r   r:      s    
zBST.find_nodec                s,   x&|   D ]} fdd|jD |_q
W dS )z?
        Decrement all rows larger than the given row.
        c                s    g | ]}| kr|d  n|qS )r4   r   ).0r   )r3   r   r   
<listcomp>   s    z"BST.shift_left.<locals>.<listcomp>N)traverser    )r   r3   r8   r   )r3   r   
shift_left   s    zBST.shift_leftc                s,   x&|   D ]} fdd|jD |_q
W dS )zL
        Increment all rows greater than or equal to the given row.
        c                s    g | ]}| kr|d  n|qS )r4   r   )r>   r   )r3   r   r   r?      s    z#BST.shift_right.<locals>.<listcomp>N)r@   r    )r   r3   r8   r   )r3   r   shift_right   s    zBST.shift_rightc             C   sv   y\||j kr||fS ||j kr<|jd kr,dS | ||j|S |jd krJdS | ||j|S W n tk
rp   dS X d S )N)NN)r   r"   r=   r!   	TypeError)r   r   r8   r;   r   r   r   r=      s    



zBST._find_recursiveinorderc             C   sV   |dkr|  | jg S |dkr,| | jg S |dkrB| | jg S td| ddS )a  
        Return nodes of the BST in the given order.

        Parameters
        ----------
        order : str
            The order in which to recursively search the BST.
            Possible values are:
            "preorder": current node, left subtree, right subtree
            "inorder": left subtree, current node, right subtree
            "postorder": left subtree, right subtree, current node
        ZpreorderrD   Z	postorderzInvalid traversal method: ""N)	_preorderr-   _inorder
_postorderr%   )r   orderr   r   r   r@     s    zBST.traversec             C   s   dd |   D S )zA
        Return BST items in order as (key, data) pairs.
        c             S   s   g | ]}|j |jfqS r   )r   r    )r>   r   r   r   r   r?     s    zBST.items.<locals>.<listcomp>)r@   )r   r   r   r   items  s    z	BST.itemsc             C   sF   d}x<|   D ]0}t|j}dd t||| D |_||7 }qW dS )z6
        Make row order align with key order.
        r   c             S   s   g | ]}|qS r   r   )r>   r   r   r   r   r?   #  s    zBST.sort.<locals>.<listcomp>N)r@   lenr    range)r   ir8   Znum_rowsr   r   r   sort  s
    
zBST.sortc             C   s   dd |   D S )z7
        Return BST rows sorted by key values.
        c             S   s   g | ]}|j D ]}|qqS r   )r    )r>   r8   r   r   r   r   r?   *  s    z#BST.sorted_data.<locals>.<listcomp>)r@   )r   r   r   r   sorted_data&  s    zBST.sorted_datac             C   s6   |d kr|S | | | |j| | |j| |S )N)appendrF   r!   r"   )r   r8   lstr   r   r   rF   ,  s    
zBST._preorderc             C   s6   |d kr|S |  |j| || |  |j| |S )N)rG   r!   rP   r"   )r   r8   rQ   r   r   r   rG   4  s    
zBST._inorderc             C   s6   |d kr|S |  |j| |  |j| || |S )N)rH   r!   r"   rP   )r   r8   rQ   r   r   r   rH   <  s    
zBST._postorderc             C   s"   || j kr|| _ n||| d S )N)r-   r(   )r   r8   r;   new_noder   r   r   _substituteD  s    
zBST._substitutec             C   s  |  |\}}|dkrdS |dk	rT||jkr6tdnt|jdkrT|j| dS |jdkrx|jdkrx| ||d n|jdkr|jdk	r| |||j nd|jdkr|jdk	r| |||j n>|j}|}x|jdk	r|}|j}qW | |||j || |  j	d8  _	dS )a  
        Remove data corresponding to the given key.

        Parameters
        ----------
        key : tuple
            The key to remove
        data : int or None
            If None, remove the node corresponding to the given key.
            If not None, remove only the given data value from the node.

        Returns
        -------
        successful : bool
            True if removal was successful, false otherwise
        NFz$Data does not belong to correct noder4   T)
r:   r    r%   rK   r)   r!   r"   rS   r*   r.   )r   r   r    r8   r;   r9   r   r   r   r)   J  s0    



z
BST.removec             C   s   |  | jS )z6
        Returns whether this is a valid BST.
        )	_is_validr-   )r   r   r   r   is_validv  s    zBST.is_validc             C   sL   |d krdS |j d ks |j |koJ|jd ks4|j|koJ| |j oJ| |jS )NT)r!   r"   rT   )r   r8   r   r   r   rT   |  s
    zBST._is_validTTc             C   s   |  |||}dd |D S )a  
        Return all nodes with keys in the given range.

        Parameters
        ----------
        lower : tuple
            Lower bound
        upper : tuple
            Upper 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.
        c             S   s   g | ]}|j D ]}|qqS r   )r    )r>   r8   r   r   r   r   r?     s    zBST.range.<locals>.<listcomp>)range_nodes)r   lowerupperboundsnodesr   r   r   rL     s    z	BST.rangec             C   sL   | j dkrg S |d rtjntj}|d r0tjntj}| ||||| j g S )z2
        Return nodes in the given range.
        Nr   r4   )r-   operatorleltgegt_range)r   rX   rY   rZ   op1op2r   r   r   rW     s
    
zBST.range_nodesc             C   s,   | j dkrg S | || j g }dd |D S )z
        Assuming the given value has smaller length than keys, return
        nodes whose keys have this value as a prefix.
        Nc             S   s   g | ]}|j D ]}|qqS r   )r    )r>   r8   r   r   r   r   r?     s    z#BST.same_prefix.<locals>.<listcomp>)r-   _same_prefix)r   r   r[   r   r   r   same_prefix  s    
zBST.same_prefixc             C   sz   |||j r"|||j r"|| ||j krL|jd k	rL| |||||j| ||j k rv|jd k	rv| |||||j| |S )N)r   rP   r"   ra   r!   )r   rX   rY   rb   rc   r8   rQ   r   r   r   ra     s    
z
BST._rangec             C   sl   |j d t| }||kr$|| ||krF|jd k	rF| ||j| ||krh|jd k	rh| ||j| |S )N)r   rK   rP   r"   rd   r!   )r   r   r8   rQ   prefixr   r   r   rd     s    
zBST._same_prefixc             C   s   d| j j dS )N<>)	__class__r   )r   r   r   r   r     s    zBST.__repr__c             C   sX   d| t | d }|jd k	r4|| |j|d 7 }|jd k	rT|| |j|d 7 }|S )N	
r4   )r+   r!   _printr"   )r   r8   levelliner   r   r   rl     s    

z
BST._printc             C   s   |  | jS )z(
        Return the BST height.
        )_heightr-   )r   r   r   r   height  s    z
BST.heightc             C   s*   |d krdS t | |j| |jd S )Nr4   )maxro   r!   r"   )r   r8   r   r   r   ro     s    zBST._heightc                s4   x.|   D ]"\}} fdd|D |dd< q
W 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 nodes deleted.

        Parameters
        ----------
        row_map : dict
            Mapping of row numbers to new row numbers
        c                s   g | ]}| kr | qS r   r   )r>   r   )row_mapr   r   r?     s    z$BST.replace_rows.<locals>.<listcomp>N)rJ   )r   rs   r   r    r   )rs   r   replace_rows  s    zBST.replace_rows)F)N)rD   )N)rV   )rV   )#r   r   r   r   r   r5   r   r1   r<   r:   rA   rB   r=   r@   rJ   rN   rO   rF   rG   rH   rS   r)   rU   rT   rL   rW   re   ra   rd   r   rl   propertyrp   ro   rt   r   r   r   r   r      s<   




,


	
)r\   __all__r   r   r   r   r   r   r   r   r   <module>   s   ";