a
    d/                     @   s   d Z ddlmZmZ ddlZddlmZ ddlm	Z	m
Z
 zddlmZ edd	ZW n eyj   e ZY n0 ze W n ey   eZY n0 d
dgZG dd deZeZG dd deZdS )a  Python's builtin :class:`list` is a very fast and efficient
sequence type, but it could be better for certain access patterns,
such as non-sequential insertion into a large lists. ``listutils``
provides a pure-Python solution to this problem.

For utilities for working with iterables and lists, check out
:mod:`iterutils`. For the a :class:`list`-based version of
:class:`collections.namedtuple`, check out :mod:`namedutils`.
    )print_functiondivisionN)log)chainislice   )make_sentinel_MISSING)var_nameBList
BarrelListc                   @   s   e Zd ZdZdZd6ddZedd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zd7ddZd8ddZeZe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d0d1 Zd2d3 Zd4d5 Z dS )9r   a  The ``BarrelList`` is a :class:`list` subtype backed by many
    dynamically-scaled sublists, to provide better scaling and random
    insertion/deletion characteristics. It is a subtype of the builtin
    :class:`list` and has an identical API, supporting indexing,
    slicing, sorting, etc. If application requirements call for
    something more performant, consider the `blist module available on
    PyPI`_.

    The name comes by way of Kurt Rose, who said it reminded him of
    barrel shifters. Not sure how, but it's BList-like, so the name
    stuck. BList is of course a reference to `B-trees`_.

    Args:
        iterable: An optional iterable of initial values for the list.

    >>> blist = BList(xrange(100000))
    >>> blist.pop(50000)
    50000
    >>> len(blist)
    99999
    >>> len(blist.lists)  # how many underlying lists
    8
    >>> slice_idx = blist.lists[0][-1]
    >>> blist[slice_idx:slice_idx + 2]
    BarrelList([11637, 11638])

    Slicing is supported and works just fine across list borders,
    returning another instance of the BarrelList.

    .. _blist module available on PyPI: https://pypi.python.org/pypi/blist
    .. _B-trees: https://en.wikipedia.org/wiki/B-tree

    i  Nc                 C   s   g g| _ |r| | d S Nlistsextendselfiterable r   Z/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/boltons/listutils.py__init__k   s    zBarrelList.__init__c                 C   s*   t | | j }}tt|t|d d S )N   )len_size_factorintroundmath_log)r   len_selfZsize_factorr   r   r   _cur_size_limitp   s    zBarrelList._cur_size_limitc                 C   sf   |dk r|t | 7 }|| j }}tt |D ]$}t || }||k rH qR||8 }q,|dk r^dS ||fS )Nr   )NN)r   r   range)r   indexrel_idxr   list_idxZlen_listr   r   r   _translate_indexu   s    
zBarrelList._translate_indexc                 C   s   |dk r|t | j7 }| j| t |  }}| j}t ||kr|d }t ||kr~|d }| j||| d   || d = qDdS dS )Nr   r   r   TF)r   r   r   insert)r   r"   Zcur_listr   Z
size_limitZ
half_limitZnext_list_idxr   r   r   _balance_list   s    zBarrelList._balance_listc                 C   sh   t | jdkr,| jd || | d n8| |\}}|d u rHt | j| || | | d S Nr   r   )r   r   r$   r%   r#   
IndexError)r   r    itemr"   r!   r   r   r   r$      s    
zBarrelList.insertc                 C   s   | j d | d S N)r   appendr   r(   r   r   r   r+      s    zBarrelList.appendc                 C   s   | j d | d S r)   r   r   r   r   r   r      s    zBarrelList.extendc                 G   s   | j }t|dkr$|s$| j d  S |o.|d }|dksH|d u sH|dkrr|d  }t|dkr|d s|  n4| |\}}|d u rt || |}| | |S )Nr   r   r   r*   )r   r   popr#   r'   r%   )r   ar   r    retr"   r!   r   r   r   r-      s    

zBarrelList.popc                 C   s   | }|d u rd}|d u r t | }|d urP|dk rP| }| | d  }}t| }|dk rd|t | 7 }|dk rx|t | 7 }t||||S )Nr   r   )r   reversedr   )r   startstopstepr   r   r   r   
iter_slice   s    zBarrelList.iter_slicec           	      C   s  |d urRt |dkrRt| d||| |d |}|| jd d d < | d d S |d u r^d}|d u rnt| }| |\}}| |\}}|d u rt |d u rt ||kr| j| ||= nH||k r| j|d |= | j| |d = | j| d |= ndsJ dd S )Nr   r   FzGstart list index should never translate to greater than stop list index)absr   r4   r   r%   r   r#   r'   )	r   r1   r2   r3   Znew_listZstart_list_idxZstart_rel_idxZstop_list_idxZstop_rel_idxr   r   r   	del_slice   s0    
zBarrelList.del_slicec                 C   s   | |S r   r   )clsitr   r   r   from_iterable   s    zBarrelList.from_iterablec                 C   s   t | jS r   )r   r9   r   r   r   r   r   __iter__   s    zBarrelList.__iter__c                 C   s   t dd t| jD S )Nc                 s   s   | ]}t |V  qd S r   )r0   .0lr   r   r   	<genexpr>       z*BarrelList.__reversed__.<locals>.<genexpr>)r   r9   r0   r   r:   r   r   r   __reversed__   s    zBarrelList.__reversed__c                 C   s   t dd | jD S )Nc                 S   s   g | ]}t |qS r   )r   r<   r   r   r   
<listcomp>   r@   z&BarrelList.__len__.<locals>.<listcomp>sumr   r:   r   r   r   __len__   s    zBarrelList.__len__c                 C   s   | j D ]}||v r dS qdS )NTF)r   )r   r(   curr   r   r   __contains__   s    
zBarrelList.__contains__c           	      C   s~   z|j |j|j  }}}W n ty6   t|}Y n0 | |||}| |}|S | |\}}|d u rpt	 | j
| | S r   )r1   r2   r3   AttributeErroroperatorr    r4   r9   r#   r'   r   )	r   r    r1   r2   r3   r4   r/   r"   r!   r   r   r   __getitem__   s    
zBarrelList.__getitem__c                 C   sv   z|j |j|j  }}}W n ty6   t|}Y n0 | ||| d S | |\}}|d u rft | j	| |= d S r   )
r1   r2   r3   rH   rI   r    r6   r#   r'   r   )r   r    r1   r2   r3   r"   r!   r   r   r   __delitem__  s    zBarrelList.__delitem__c           	      C   s   z|j |j|j  }}}W n ty6   t|}Y nN0 t| jdkrV|| jd |< n t| }|||< |g| jd d < | 	d d S | 
|\}}|d u rt || j| |< d S r&   )r1   r2   r3   rH   rI   r    r   r   listr%   r#   r'   )	r   r    r(   r1   r2   r3   tmpr"   r!   r   r   r   __setitem__  s    
zBarrelList.__setitem__c                 C   s   |  ||d}| |S )Nr   )r4   r9   )r   r1   r2   r4   r   r   r   __getslice__%  s    zBarrelList.__getslice__c                 C   sT   t | jdkr"|| jd ||< n$t| }||||< |g| jd d < | d d S r&   )r   r   rL   r%   )r   r1   r2   sequencerM   r   r   r   __setslice__)  s    
zBarrelList.__setslice__c                 C   s   d| j jt| f S )Nz%s(%r))	__class____name__rL   r:   r   r   r   __repr__3  s    zBarrelList.__repr__c                 C   sf   t | jdkr| jd   nD| jD ]}|  q$tt| j}| jd d = || jd< | d d S r&   )r   r   sortsortedr   r9   r%   )r   liZ
tmp_sortedr   r   r   rU   6  s    


zBarrelList.sortc                 C   s"   | j D ]}|  q| j   d S r   )r   reverse)r   rF   r   r   r   rX   C  s    

zBarrelList.reversec                    s   t  fdd| jD S )Nc                    s   g | ]}|  qS r   )count)r=   rF   r(   r   r   rB   I  r@   z$BarrelList.count.<locals>.<listcomp>rC   r,   r   rZ   r   rY   H  s    zBarrelList.countc              	   C   sZ   d}| j D ]<}z||}|| W   S  tyD   |t|7 }Y q
0 q
td|f d S )Nr   z%r is not in list)r   r    
ValueErrorr   )r   r(   Z	len_accumrF   r!   r   r   r   r    K  s    

zBarrelList.index)N)N)N)!rS   
__module____qualname____doc__r   r   propertyr   r#   r%   r$   r+   r   r-   r4   r6   Z__delslice__classmethodr9   r;   rA   rE   rG   rJ   rK   rN   rO   rQ   rT   rU   rX   rY   r    r   r   r   r   r   E   s<   "





c                   @   s"   e Zd ZdZdddZdd ZdS )		SplayListzLike a `splay tree`_, the SplayList facilitates moving higher
    utility items closer to the front of the list for faster access.

    .. _splay tree: https://en.wikipedia.org/wiki/Splay_tree
    r   c                 C   s&   ||krd S |  |}| || d S r   )r-   r$   )r   
item_index
dest_indexr(   r   r   r   shift`  s    
zSplayList.shiftc                 C   s   | | | |  | |< | |< d S r   r   )r   rb   rc   r   r   r   swapf  s    zSplayList.swapN)r   )rS   r\   r]   r^   rd   re   r   r   r   r   ra   Y  s   
ra   )r^   
__future__r   r   rI   mathr   r   	itertoolsr   r   Z	typeutilsr   r	   ImportErrorobjectxrange	NameErrorr   __all__rL   r   r   ra   r   r   r   r   <module>!   s&   

  