U
    i2e                     @  s@   d dl mZ d dlZddddddddd	Zddd
ddZdS )    )annotationsNzint | tuple[int, int] | Nonez
int | Noneintztuple[int, int])
chunk_sizechunk_counttotal_chunk_countnynxreturnc                 C  s~  t | dk	|dk	|dk	gdkr&td|dk s6|dk rLtd| d| d|dk	r|d |d  }tt|d|}|dkrd} n4||krd	} n&t|}||kr|}n|d |d f}|dk	r(t|tr|\}}n| }}tt|d|d }tt|d|d }t|d | t|d | f} | dkr<d }	}
nt| trR| \}	}
n|  }	}
|
dk sn|	dk rvtd
|	|
fS )a1  Calculate chunk sizes.

    Args:
        chunk_size (int or tuple(int, int), optional): Chunk size in (y, x) directions, or the same
            size in both directions if only one is specified. Cannot be negative.
        chunk_count (int or tuple(int, int), optional): Chunk count in (y, x) directions, or the
            same count in both directions if only one is specified. If less than 1, set to 1.
        total_chunk_count (int, optional): Total number of chunks. If less than 1, set to 1.
        ny (int): Number of grid points in y-direction.
        nx (int): Number of grid points in x-direction.

    Return:
        tuple(int, int): Chunk sizes (y_chunk_size, x_chunk_size).

    Note:
        Zero or one of ``chunk_size``, ``chunk_count`` and ``total_chunk_count`` should be
        specified.
    N   zGOnly one of chunk_size, chunk_count and total_chunk_count should be set   z'(ny, nx) must be at least (2, 2), not (z, )r   )r
   r
   zchunk_size cannot be negative)	sum
ValueErrorminmaxtwo_factors
isinstancetuplemathceil)r   r   r   r   r   Zmax_chunk_countZfactorsZy_chunk_countZx_chunk_countZy_chunk_sizeZx_chunk_size r   3/tmp/pip-unpacked-wheel-_24pu9r8/contourpy/chunk.pycalc_chunk_sizes   s<    


$


r   )nr	   c                 C  s`   | dk rt d|  tt| }| | dkr<|d8 }q&| | }||krT||fS ||fS dS )aR  Split an integer into two integer factors.

    The two factors will be as close as possible to the sqrt of n, and are returned in decreasing
    order.  Worst case returns (n, 1).

    Args:
        n (int): The integer to factorize, must be positive.

    Return:
        tuple(int, int): The two factors of n, in decreasing order.
    r   z)two_factors expects positive integer not r
   N)r   r   r   sqrt)r   ijr   r   r   r   I   s    
r   )
__future__r   r   r   r   r   r   r   r   <module>   s   C