a
    d                      @   s   d Z ddlmZ ddlmZmZ ddlZddl	Z	e
de
dfddZdd	d
ZdddZzeefZeZW n ey   efZeZY n0 G dd deZdS )z[This module provides useful math functions on top of Python's
built-in :mod:`math` module.
    )division)ceilfloorNz-infinfc                 C   s(   ||k rt d||f tt| ||S )a\  Limit a value to a given range.

    Args:
        x (int or float): Number to be clamped.
        lower (int or float): Minimum value for x.
        upper (int or float): Maximum value for x.

    The returned value is guaranteed to be between *lower* and
    *upper*. Integers, floats, and other comparable types can be
    mixed.

    >>> clamp(1.0, 0, 5)
    1.0
    >>> clamp(-1.0, 0, 5)
    0
    >>> clamp(101.0, 0, 5)
    5
    >>> clamp(123, upper=5)
    5

    Similar to `numpy's clip`_ function.

    .. _numpy's clip: http://docs.scipy.org/doc/numpy/reference/generated/numpy.clip.html

    z-expected upper bound (%r) >= lower bound (%r))
ValueErrorminmax)xlowerupper r   Z/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/boltons/mathutils.pyclamp+   s
    r   c                 C   sD   |du rt | S t|}t|| }|t|kr<td|  || S )a  Return the ceiling of *x*. If *options* is set, return the smallest
    integer or float from *options* that is greater than or equal to
    *x*.

    Args:
        x (int or float): Number to be tested.
        options (iterable): Optional iterable of arbitrary numbers
          (ints or floats).

    >>> VALID_CABLE_CSA = [1.5, 2.5, 4, 6, 10, 25, 35, 50]
    >>> ceil(3.5, options=VALID_CABLE_CSA)
    4
    >>> ceil(4, options=VALID_CABLE_CSA)
    4
    Nz,no ceil options greater than or equal to: %r)_ceilsortedbisectbisect_leftlenr   r	   optionsir   r   r   r   K   s    r   c                 C   s@   |du rt | S t|}t|| }|s4td|  ||d  S )a  Return the floor of *x*. If *options* is set, return the largest
    integer or float from *options* that is less than or equal to
    *x*.

    Args:
        x (int or float): Number to be tested.
        options (iterable): Optional iterable of arbitrary numbers
          (ints or floats).

    >>> VALID_CABLE_CSA = [1.5, 2.5, 4, 6, 10, 25, 35, 50]
    >>> floor(3.5, options=VALID_CABLE_CSA)
    2.5
    >>> floor(2.5, options=VALID_CABLE_CSA)
    2.5

    Nz*no floor options less than or equal to: %r   )_floorr   r   bisect_rightr   r   r   r   r   r   d   s    r   c                   @   s   e Zd ZdZ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d Zdd  Zed!d" Zed#d$ Zed%d& Zed.d'd(Zed)d* Zd+d, ZdS )/Bitsa  
    An immutable bit-string or bit-array object.
    Provides list-like access to bits as bools,
    as well as bitwise masking and shifting operators.
    Bits also make it easy to convert between many
    different useful representations:

    * bytes -- good for serializing raw binary data
    * int -- good for incrementing (e.g. to try all possible values)
    * list of bools -- good for iterating over or treating as flags
    * hex/bin string -- good for human readability

    )valr   r   Nc                 C   s  t |tvrt |tu r,ddd |D }t |tu rB|d}t |tu r|d u rtt|}|drt|d d }|drt	|d}n|rt	|d}nd	}t |tvrt
d
t |j|d	k rtd|d u rtd|}|d| krtd|||| _|| _d S )N c                 S   s   g | ]}|rd ndqS )10r   ).0er   r   r   
<listcomp>       z!Bits.__init__.<locals>.<listcomp>ascii0x         r   zinitialized with bad type: {0}z%Bits cannot represent negative valuesz{0:b}z-value {0} cannot be represented with {1} bits)type
_int_typeslistjoinbytesdecodeunicoder   
startswithint	TypeErrorformat__name__r   r   )selfr   len_r   r   r   __init__   s2    


zBits.__init__c                 C   sf   t |tu rt|  | S t |tu rV|| jkr:t|td| j| d > | j@ S t	t |d S )Nr   )
r(   slicer   as_binr0   r   
IndexErrorboolr   r1   )r4   kr   r   r   __getitem__   s    
zBits.__getitem__c                 C   s   | j S N)r   r4   r   r   r   __len__   s    zBits.__len__c                 C   s,   t | t |urtS | j|jko*| j|jkS r=   )r(   NotImplementedr   r   r4   otherr   r   r   __eq__   s    zBits.__eq__c                 C   s0   t | t |urtS t| j|jB t| j|jS r=   r(   r@   r   r   r   r   rA   r   r   r   __or__   s    zBits.__or__c                 C   s0   t | t |urtS t| j|j@ t| j|jS r=   rD   rA   r   r   r   __and__   s    zBits.__and__c                 C   s   t | j|> | j| S r=   r   r   r   rA   r   r   r   
__lshift__   s    zBits.__lshift__c                 C   s   t | j|? | j| S r=   rG   rA   r   r   r   
__rshift__   s    zBits.__rshift__c                 C   s
   t | jS r=   )hashr   r>   r   r   r   __hash__   s    zBits.__hash__c                 C   s   dd |   D S )Nc                 S   s   g | ]}|d kqS )r   r   )r   cr   r   r   r!      r"   z Bits.as_list.<locals>.<listcomp>)r8   r>   r   r   r   as_list   s    zBits.as_listc                 C   s   d | j | jS )Nz{{0:0{0}b}}r2   r   r   r>   r   r   r   r8      s    zBits.as_binc                 C   s0   d d| jd | jd dk  }|| j }|S )Nz%0{0}Xr%      r   rN   )r4   tmplretr   r   r   as_hex   s    "
zBits.as_hexc                 C   s   | j S r=   )r   r>   r   r   r   as_int   s    zBits.as_intc                 C   s   t |  S r=   )binascii	unhexlifyrR   r>   r   r   r   as_bytes   s    zBits.as_bytesc                 C   s   | |S r=   r   )clslist_r   r   r   	from_list   s    zBits.from_listc                 C   s   | |S r=   r   )rW   binr   r   r   from_bin   s    zBits.from_binc                 C   s.   t |tr|d}|ds&d| }| |S )Nr#   r$   )
isinstancer,   r-   r/   )rW   hexr   r   r   from_hex   s
    


zBits.from_hexc                 C   s
   | ||S r=   r   )rW   int_r5   r   r   r   from_int   s    zBits.from_intc                 C   s   |  t|S r=   )r^   rT   hexlify)rW   bytes_r   r   r   
from_bytes   s    zBits.from_bytesc                 C   s   | j j}d||  S )Nz
{0}('{1}'))	__class__r3   r2   r8   )r4   Zcnr   r   r   __repr__  s    zBits.__repr__)r   N)N)r3   
__module____qualname____doc__	__slots__r6   r<   r?   rC   rE   rF   rH   rI   rK   rM   r8   rR   rS   rV   classmethodrY   r[   r^   r`   rc   re   r   r   r   r   r      s6   
	



r   )N)N)rh   
__future__r   mathr   r   r   r   r   rT   floatr   r0   longr)   strr,   	NameErrorr.   objectr   r   r   r   r   <module>!   s    


