
    fch8/                         S r SSKrSSKJr  SSKJrJr   SSKJ	r	  \	" SS9r
S	S
/r " S S
\5      r\r " S S\5      rg! \ a
    \" 5       r
 N,f = f)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`.
    N)log)chainislice   )make_sentinel_MISSING)var_nameBList
BarrelListc                       \ rS rSrSrSr SS jr\S 5       rS r	S r
S	 rS
 rS rS rSS jrSS jr\r\S 5       rS rS rS rS rS rS rS rS rS rS rS rS rS r S r!Sr"g) r   <   aU  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(range(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                 F    / /U l         U(       a  U R                  U5        g g Nlistsextendselfiterables     1lib/python3.13/site-packages/boltons/listutils.py__init__BarrelList.__init__b   s    T
KK!     c           	      v    [        U 5      U R                  p![        [        U[	        US-   S5      -  5      5      $ )N   )len_size_factorintroundmath_log)r   len_selfsize_factors      r   _cur_size_limitBarrelList._cur_size_limitg   s3     #D	4+<+<+5x1a'@@ABBr   c                     US:  a  U[        U 5      -  nXR                  p2[        [        U5      5       H  n[        X4   5      nX%:  a    OX%-  nM     US:  a  gWU4$ )Nr   )NN)r   r   range)r   indexrel_idxr   list_idxlen_lists         r   _translate_indexBarrelList._translate_indexl   sg    19SYE

c%j)H5?+H!G	 *
 Q;  r   c                 F   US:  a  U[        U R                  5      -  nU R                  U   [        U 5      p2U R                  n[        U5      U:  aO  US-  n[        U5      U:  a:  US-   nU R                  R                  XbU* S  5        X%* S 2	 [        U5      U:  a  M:  gg)Nr   r   r   TF)r   r   r#   insert)r   r)   cur_listr!   
size_limit
half_limitnext_list_idxs          r   _balance_listBarrelList._balance_listy   s    a<DJJ'H!ZZ13t9())
x=:%#qJh-*, (1

!!-:+,1GH[\* h-*, r   c                 4   [        U R                  5      S:X  a0  U R                  S   R                  X5        U R                  S5        g U R	                  U5      u  p4Uc
  [        5       eU R                  U   R                  XB5        U R                  U5        g Nr   r   )r   r   r.   r3   r+   
IndexError)r   r'   itemr)   r(   s        r   r.   BarrelList.insert   s    tzz?aJJqM  -q! 	 !% 5 5e <H l"JJx ''6x(r   c                 @    U R                   S   R                  U5        g N)r   append)r   r8   s     r   r=   BarrelList.append   s    

2d#r   c                 @    U R                   S   R                  U5        g r;   r   r   s     r   r   BarrelList.extend   s    

2h'r   c                    U R                   n[        U5      S:X  a$  U(       d  U R                   S   R                  5       $ U=(       a    US   nUS:X  d	  Ub  US:X  a>  US   R                  5       n[        U5      S:  a  US   (       d  UR                  5         U$ U R                  U5      u  pVUc
  [	        5       eX%   R                  U5      nU R                  U5        U$ )Nr   r    r<   )r   r   popr+   r7   r3   )r   ar   r'   retr)   r(   s          r   rC   BarrelList.pop   s    

u:?1::a=$$&&
adB;%-5B;)--/C5zA~eBi		 
 !% 5 5e <H l"/%%g.Cx(
r   c                     U nUc  SnUc  [        U 5      nUb  US:  a  U* nU* U* S-
  p![        U 5      nUS:  a  U[        U 5      -  nUS:  a  U[        U 5      -  n[        XAX#5      $ )Nr   r   )r   reversedr   )r   startstopstepr   s        r   
iter_sliceBarrelList.iter_slice   s    =E<t9Dq5D &4%!)4~H19SYE!8CIDht22r   c                     Ub_  [        U5      S:  aP  [        U R                  SX5      U R                  US U5      5      nX@R                  S   S S & U R	                  S5        g Uc  SnUc  [        U 5      nU R                  U5      u  pVU R                  U5      u  pxUc
  [        5       eUc
  [        5       eXW:X  a  U R                  U   Xh2	 g XW:  a7  U R                  US-   U2	 U R                  U   US 2	 U R                  U   S U2	 g  S5       e)Nr   r   zGstart list index should never translate to greater than stop list index)absr   rL   r   r3   r   r+   r7   )	r   rI   rJ   rK   new_liststart_list_idxstart_rel_idxstop_list_idxstop_rel_idxs	            r   	del_sliceBarrelList.del_slice   s$   D	AT__Q<!__T4>@H'JJqM!q!=E<t9D(,(=(=e(D%&*&;&;D&A#!, ,*

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

>A-m;<

>*=>:

=)-<-8< ; <5r   c                     U " U5      $ r   rB   )clsits     r   from_iterableBarrelList.from_iterable   s    2wr   c                 B    [         R                  " U R                  5      $ r   )r   rZ   r   r   s    r   __iter__BarrelList.__iter__   s    ""4::..r   c                 b    [         R                  " S [        U R                  5       5       5      $ )Nc              3   8   #    U  H  n[        U5      v   M     g 7fr   )rH   ).0ls     r   	<genexpr>*BarrelList.__reversed__.<locals>.<genexpr>   s     "M8L18A;;8Ls   )r   rZ   rH   r   r]   s    r   __reversed__BarrelList.__reversed__   s"    """M8L"MMMr   c                 j    [        U R                   Vs/ s H  n[        U5      PM     sn5      $ s  snf r   )sumr   r   )r   rc   s     r   __len__BarrelList.__len__   s'    DJJ/JqCFJ/00/s   0c                 8    U R                    H
  nX;   d  M
    g   g)NTFr   r   r8   curs      r   __contains__BarrelList.__contains__   s    ::C{  r   c                 D    UR                   UR                  UR                  pCnU R                  X#U5      nU R	                  U5      nU$ ! [
         a    [        R                  " U5      n Of = fU R                  U5      u  pxUc
  [        5       eU R                  U   U   $ r   )rI   rJ   rK   rL   rZ   AttributeErroroperatorr'   r+   r7   r   )	r   r'   rI   rJ   rK   rL   rE   r)   r(   s	            r   __getitem__BarrelList.__getitem__   s    	 %UZZE d;J$$Z0CJ  	*NN5)E	* !11%8,zz(#G,,s   #A
 
 A-,A-c                     UR                   UR                  UR                  pCnU R                  X#U5        g ! [         a    [
        R                  " U5      n Of = fU R                  U5      u  pVUc
  [        5       eU R                  U   U	 g r   )
rI   rJ   rK   rU   rs   rt   r'   r+   r7   r   )r   r'   rI   rJ   rK   r)   r(   s          r   __delitem__BarrelList.__delitem__   s    	 %UZZE NN5-	  	*NN5)E	*
 !11%8,JJx )s   #8  AAc                     UR                   UR                  UR                  pTn[        U R                  5      S:X  a  X R                  S   U'   O[        U 5      nX&U'   U/U R                  S S & U R                  S5        g ! [         a    [        R                  " U5      n Of = fU R                  U5      u  pxUc
  [        5       eX R                  U   U'   g r6   )rI   rJ   rK   r   r   listr3   rs   rt   r'   r+   r7   )	r   r'   r8   rI   rJ   rK   tmpr)   r(   s	            r   __setitem__BarrelList.__setitem__	  s    	 %UZZE 4::!#'+

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

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

8W%s   #B  B$#B$c                 H    U R                  XS5      nU R                  U5      $ )Nr   )rL   rZ   )r   rI   rJ   rL   s       r   __getslice__BarrelList.__getslice__  s#    __U!4
!!*--r   c                     [        U R                  5      S:X  a  X0R                  S   X& O[        U 5      nX4X& U/U R                  S S & U R                  S5        g r6   )r   r   r{   r3   )r   rI   rJ   sequencer|   s        r   __setslice__BarrelList.__setslice__   sR    tzz?a(0JJqM%%t*C&O EDJJqM1r   c                 N    U R                   R                   S[        U 5      < S3$ )N())	__class____name__r{   r]   s    r   __repr__BarrelList.__repr__*  s$    ..))*!DJ>;;r   c                 f   [        U R                  5      S:X  a  U R                  S   R                  5         g U R                   H  nUR                  5         M     [        [        R
                  " U R                  5      5      nU R                  S S 2	 X R                  S'   U R                  S5        g r6   )r   r   sortsortedr   rZ   r3   )r   li
tmp_sorteds      r   r   BarrelList.sort-  s|     tzz?aJJqM jj	 ! 3 3DJJ ?@J

1&JJqMq!r   c                 ~    U R                    H  nUR                  5         M     U R                   R                  5         g r   )r   reverse)r   ro   s     r   r   BarrelList.reverse:  s)    ::CKKM 

r   c                 t    [        U R                   Vs/ s H  o"R                  U5      PM     sn5      $ s  snf r   )ri   r   countrn   s      r   r   BarrelList.count?  s)    tzz:zIIdOz:;;:s   5c                     SnU R                    H  n UR                  U5      nX$-   s  $    [        U< S35      e! [         a    U[        U5      -  n MG  f = f)Nr   z is not in list)r   r'   
ValueErrorr   )r   r8   	len_accumro   r(   s        r   r'   BarrelList.indexB  sc    	::C&))D/ **  D8?344  &SX%	&s   ;AArm   r   )#r   
__module____qualname____firstlineno____doc__r   r   propertyr#   r+   r3   r.   r=   r   rC   rL   rU   __delslice__classmethodrZ   r^   rf   rj   rp   ru   rx   r}   r   r   r   r   r   r   r'   __static_attributes__rB   r   r   r   r   <   s     D LO"
 C C!
$("3&<8 L /N1-*-&.<"
<5r   c                   (    \ rS rSrSrSS jrS rSrg)	SplayListiP  zLike 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                 T    X:X  a  g U R                  U5      nU R                  X#5        g r   )rC   r.   )r   
item_index
dest_indexr8   s       r   shiftSplayList.shiftW  s%    #xx
#J%r   c                     X   X   sX'   X'   g r   rB   )r   r   r   s      r   swapSplayList.swap]  s    -1-=t?O*$*r   rB   N)r   )r   r   r   r   r   r   r   r   rB   r   r   r   r   P  s    &Pr   r   )r   rt   mathr   r    	itertoolsr   r   	typeutilsr   r   ImportErrorobject__all__r{   r   r
   r   rB   r   r   <module>r      sp   >    #(j1H
 L
!N5 N5b 	P P  xHs   ? AA