
    yiBe/                         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
 	 ddlmZ  ed	      Z	 e d
dgZ G d de      ZeZ G d de      Zy# e$ r
  e       ZY .w xY w# e$ r eZY 8w xY w)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                       e Zd ZdZdZ	 ddZed        Zd Zd Z	d Z
d	 Zd
 Zd ZddZddZeZed        Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z y)r   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                 <    g g| _         |r| j                  |       y y Nlistsextendselfiterables     1lib/python3.12/site-packages/boltons/listutils.py__init__zBarrelList.__init__k   s    T
KK!     c           	      x    t        |       | j                  }}t        t        |t	        |dz   d      z              S )N   )len_size_factorintroundmath_log)r   len_selfsize_factors      r   _cur_size_limitzBarrelList._cur_size_limitp   s5     #D	4+<+<+5x1a'@@ABBr   c                     |dk  r|t        |       z  }|| j                  }}t        t        |            D ]  }t        ||         }||k  r n||z  } |dk  ry|fS )Nr   )NN)r   r   range)r   indexrel_idxr   list_idxlen_lists         r   _translate_indexzBarrelList._translate_indexu   sv    19SYE

c%j) 	 H5?+H!xG		 
 Q;  r   c                 B   |dk  r|t        | j                        z  }| j                  |   t        |       }}| j                  }t        |      |kD  rN|dz  }t        |      |kD  r:|dz   }| j                  j                  ||| d         || d = t        |      |kD  r:yy)Nr   r   r   TF)r   r   r#   insert)r   r(   cur_listr!   
size_limit
half_limitnext_list_idxs          r   _balance_listzBarrelList._balance_list   s    a<DJJ'H!ZZ13t9())
x=:%#qJh-*, (1

!!-:+,1GHj[\* h-*, r   c                 6   t        | j                        dk(  r1| j                  d   j                  ||       | j                  d       y | j	                  |      \  }}|
t               | j                  |   j                  ||       | j                  |       y Nr   r   )r   r   r,   r1   r*   
IndexError)r   r&   itemr(   r'   s        r   r,   zBarrelList.insert   s    tzz?aJJqM  -q! 	 !% 5 5e <Hg l"JJx ''6x(r   c                 @    | j                   d   j                  |       y N)r   append)r   r5   s     r   r9   zBarrelList.append   s    

2d#r   c                 @    | j                   d   j                  |       y r7   r   r   s     r   r   zBarrelList.extend   s    

2h'r   c                    | j                   }t        |      dk(  r|s| j                   d   j                         S |xr |d   }|dk(  s||dk(  r8|d   j                         }t        |      dkD  r|d   s|j                          |S | j                  |      \  }}|
t	               ||   j                  |      }| j                  |       |S )Nr   r    r8   )r   r   popr*   r4   r1   )r   ar   r&   retr(   r'   s          r   r=   zBarrelList.pop   s    

u:?1::a=$$&&
adB;%-5B;)--/C5zA~eBi		 
 !% 5 5e <Hg l"/%%g.Cx(
r   c                     | }|d}|t        |       }||dk  r| }| | dz
  }}t        |       }|dk  r|t        |       z  }|dk  r|t        |       z  }t        ||||      S )Nr   r   )r   reversedr   )r   startstopstepr   s        r   
iter_slicezBarrelList.iter_slice   s    =E<t9Dq5D &4%!)4E~H19SYE!8CIDhtT22r   c                    |`t        |      dkD  rRt        | j                  d||      | j                  |d |            }|| j                  d   d d  | j	                  d       y |d}|t        |       }| j                  |      \  }}| j                  |      \  }}|
t               |
t               ||k(  r| j                  |   ||= y ||k  r7| j                  |dz   |= | j                  |   |d = | j                  |   d |= y J d       )Nr   r   zGstart list index should never translate to greater than stop list index)absr   rE   r   r1   r   r*   r4   )	r   rB   rC   rD   new_liststart_list_idxstart_rel_idxstop_list_idxstop_rel_idxs	            r   	del_slicezBarrelList.del_slice   s0   D	AT__Qt<!__T4>@H'DJJqM!q!=E<t9D(,(=(=e(D%&*&;&;D&A#|!, ,]*

>*=+EFm+

>A-m;<

>*=>:

=)-<-8< ; <r   c                      | |      S r   r<   )clsits     r   from_iterablezBarrelList.from_iterable   s    2wr   c                 @    t        j                  | j                        S r   )r   rQ   r   r   s    r   __iter__zBarrelList.__iter__   s    ""4::..r   c                 `    t        j                  d t        | j                        D              S )Nc              3   2   K   | ]  }t        |        y wr   )rA   ).0ls     r   	<genexpr>z*BarrelList.__reversed__.<locals>.<genexpr>   s     "M18A;"Ms   )r   rQ   rA   r   rS   s    r   __reversed__zBarrelList.__reversed__   s"    """M8L"MMMr   c                 d    t        | j                  D cg c]  }t        |       c}      S c c}w r   )sumr   r   )r   rX   s     r   __len__zBarrelList.__len__   s#    DJJ/qCF/00/s   -c                 2    | j                   D ]  }||v s y y)NTF)r   r   r5   curs      r   __contains__zBarrelList.__contains__   s$    :: 	Cs{	 r   c                 D   	 |j                   |j                  |j                  }}}| j                  |||      }| j	                  |      }|S # t
        $ r t        j                  |      }Y nw xY w| j                  |      \  }}|
t               | j                  |   |   S r   )rB   rC   rD   rE   rQ   AttributeErroroperatorr&   r*   r4   r   )	r   r&   rB   rC   rD   rE   r?   r(   r'   s	            r   __getitem__zBarrelList.__getitem__   s    	 %UZZ4E d;J$$Z0CJ  	*NN5)E	* !11%8',zz(#G,,s   $A A-,A-c                    	 |j                   |j                  |j                  }}}| j                  |||       y # t        $ r t        j                  |      }Y nw xY w| j                  |      \  }}|
t               | j                  |   |= y r   )
rB   rC   rD   rM   rc   rd   r&   r*   r4   r   )r   r&   rB   rC   rD   r(   r'   s          r   __delitem__zBarrelList.__delitem__  s    	 %UZZ4E NN5$-	  	*NN5)E	*
 !11%8',JJx )s   $: AAc                    	 |j                   |j                  |j                  }}}t        | j                        dk(  r|| j                  d   |<   n t        |       }|||<   |g| j                  d d  | j                  d       y # t        $ r t        j                  |      }Y nw xY w| j                  |      \  }}|
t               || j                  |   |<   y r3   )rB   rC   rD   r   r   listr1   rc   rd   r&   r*   r4   )	r   r&   r5   rB   rC   rD   tmpr(   r'   s	            r   __setitem__zBarrelList.__setitem__  s    	 %UZZ4E 4::!#'+

1e$4j!E
!$

1q!  	*NN5)E	* !11%8',(,

8W%s   $B B$#B$c                 J    | j                  ||d      }| j                  |      S )Nr   )rE   rQ   )r   rB   rC   rE   s       r   __getslice__zBarrelList.__getslice__%  s%    __UD!4
!!*--r   c                     t        | j                        dk(  r|| j                  d   || n t        |       }|||| |g| j                  d d  | j                  d       y r3   )r   r   ri   r1   )r   rB   rC   sequencerj   s        r   __setslice__zBarrelList.__setslice__)  sZ    tzz?a(0DJJqM%%t*C&CdO EDJJqM1r   c                 L    | j                   j                  dt        |       dS )N())	__class____name__ri   rS   s    r   __repr__zBarrelList.__repr__3  s    >>22DJ??r   c                 `   t        | j                        dk(  r| j                  d   j                          y | j                  D ]  }|j                           t        t	        j
                  | j                              }| j                  d d = || j                  d<   | j                  d       y r3   )r   r   sortsortedr   rQ   r1   )r   li
tmp_sorteds      r   rx   zBarrelList.sort6  s     tzz?aJJqM jj 	 3 3DJJ ?@J

1&DJJqMq!r   c                 z    | j                   D ]  }|j                           | j                   j                          y r   )r   reverse)r   r`   s     r   r}   zBarrelList.reverseC  s.    :: 	CKKM	

r   c                 p    t        | j                  D cg c]  }|j                  |       c}      S c c}w r   )r\   r   countr_   s      r   r   zBarrelList.countH  s'    tzz:CIIdO:;;:s   3c                     d}| j                   D ]  }	 |j                  |      }||z   c S  t        |d      # t        $ r |t        |      z  }Y Dw xY w)Nr   z is not in list)r   r&   
ValueErrorr   )r   r5   	len_accumr`   r'   s        r   r&   zBarrelList.indexK  sf    	:: 	&C&))D/ 7**	& 677  &SX%	&s   :AAr   )!ru   
__module____qualname____doc__r   r   propertyr#   r*   r1   r,   r9   r   r=   rE   rM   __delslice__classmethodrQ   rT   rZ   r]   ra   re   rg   rk   rm   rp   rv   rx   r}   r   r&   r<   r   r   r   r   E   s     D LO"
 C C!
$("3&<8 L /N1-*-&.@"
<8r   c                       e Zd ZdZddZd Zy)	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
    c                 V    ||k(  ry | j                  |      }| j                  ||       y r   )r=   r,   )r   
item_index
dest_indexr5   s       r   shiftzSplayList.shift`  s)    #xx
#J%r   c                 &    | |   | |   c| |<   | |<   y r   r<   )r   r   r   s      r   swapzSplayList.swapf  s"    -1*-=tJ?O*Z$z*r   N)r   )ru   r   r   r   r   r   r<   r   r   r   r   Y  s    &Pr   r   )r   
__future__r   r   rd   mathr   r    	itertoolsr   r   	typeutilsr	   r
   ImportErrorobjectxrange	NameErrorr%   __all__ri   r   r   r   r<   r   r   <module>r      s   B 0    #(j1H
 L
!N8 N8b 	P PK	  xH
  Fs"   A A AAA)(A)