B
    od`                 @   s2  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 e j
dkrPd dlmZ nd dlmZ G dd deZee	e ee d	d
dZeee ee ee ee dddZdeee eee  ee dddZedkr.d dlmZ eG dd dZededddedddedddgZeee dS )    N)Fraction)ceil)castListOptionalSequence)      )Protocolc               @   s:   e Zd ZU dZdZee ed< dZeed< dZ	eed< dS )Edgez1Any object that defines an edge (such as Layout).Nsize   ratiominimum_size)
__name__
__module____qualname____doc__r   r   int__annotations__r   r    r   r   I/var/www/html/venv/lib/python3.7/site-packages/pip/_vendor/rich/_ratio.pyr      s   
r   )totaledgesreturnc             C   s   dd |D }t }xd|krdd tt||D }| tdd |D  }|dkrfdd t||D S ||td	d |D }xh|D ]$\}}||j |jkr|j||< P qW |d}	x0|D ](\}}t||j |	 d
\}
}	|
||< qW P qW ttt	 |S )a  Divide total space to satisfy size, ratio, and minimum_size, constraints.

    The returned list of integers should add up to total in most cases, unless it is
    impossible to satisfy all the constraints. For instance, if there are two edges
    with a minimum size of 20 each and `total` is 30 then the returned list will be
    greater than total. In practice, this would mean that a Layout object would
    clip the rows that would overflow the screen height.

    Args:
        total (int): Total number of characters.
        edges (List[Edge]): Edges within total space.

    Returns:
        List[int]: Number of characters for each edge.
    c             S   s   g | ]}|j pd qS )N)r   ).0edger   r   r   
<listcomp>%   s    z!ratio_resolve.<locals>.<listcomp>Nc             S   s$   g | ]\}\}}|d kr||fqS )Nr   )r   indexr   r   r   r   r   r   -   s   
c             s   s   | ]}|pd V  qdS )r   Nr   )r   r   r   r   r   	<genexpr>2   s    z ratio_resolve.<locals>.<genexpr>r   c             S   s&   g | ]\}}|d kr|j p dn|qS )Nr   )r   )r   r   r   r   r   r   r   6   s   c             s   s   | ]\}}|j pd V  qdS )r   N)r   )r   _r   r   r   r   r   ;   s    r   )
r   	enumeratezipsumr   r   divmodr   r   r   )r   r   sizes	_Fractionflexible_edges	remainingportionr   r   	remainderr   r   r   r   ratio_resolve   s*    

r+   )r   ratiosmaximumsvaluesr   c             C   s   dd t ||D }t|}|s,|dd S | }g }|j}xbt |||D ]R\}}	}
|r|dkrt|	t|| | }||
|  ||8 }||8 }qH||
 qHW |S )ad  Divide an integer total in to parts based on ratios.

    Args:
        total (int): The total to divide.
        ratios (List[int]): A list of integer ratios.
        maximums (List[int]): List of maximums values for each slot.
        values (List[int]): List of values

    Returns:
        List[int]: A list of integers guaranteed to sum to total.
    c             S   s   g | ]\}}|r|nd qS )r   r   )r   r   _maxr   r   r   r   _   s    z ratio_reduce.<locals>.<listcomp>Nr   )r"   r#   appendminround)r   r,   r-   r.   total_ratiototal_remainingresultr0   r   maximumvaluedistributedr   r   r   ratio_reduceQ   s    
r9   )r   r,   minimumsr   c             C   s   |rdd t ||D }t|}|dks0td| }g }|j}|dkrVdgt| }n|}xRt ||D ]D\}}	|dkrt|	t|| | }
n|}
||
 ||8 }||
8 }qfW |S )a<  Distribute an integer total in to parts based on ratios.

    Args:
        total (int): The total to divide.
        ratios (List[int]): A list of integer ratios.
        minimums (List[int]): List of minimum values for each slot.

    Returns:
        List[int]: A list of integers guaranteed to sum to total.
    c             S   s   g | ]\}}|r|nd qS )r   r   )r   r   _minr   r   r   r      s    z$ratio_distribute.<locals>.<listcomp>r   zSum of ratios must be > 0N)r"   r#   AssertionErrorr0   lenmaxr   )r   r,   r:   r3   r4   distributed_totalr0   	_minimumsr   minimumr8   r   r   r   ratio_distributeq   s$    rB   __main__)	dataclassc               @   s6   e Zd ZU dZee ed< dZeed< dZeed< dS )ENr   r   r   r   )	r   r   r   r   r   r   r   r   r   r   r   r   r   rE      s   
rE   n   r   )N)sys	fractionsr   mathr   typingr   r   r   r   version_infor
   pip._vendor.typing_extensionsr   r   r+   r9   rB   r   dataclassesrD   rE   resolvedprintr#   r   r   r   r   <module>   s"   
>"
(