
    sh߂                         S r SSKrSSKJr  \R                  " SS5      rSrSrSr\R                  " S	S
5      r	\R                  " SS5      r
S rS rS rg)z
Timsort implementation.  Mostly adapted from CPython's listobject.c.

For more information, see listsort.txt in CPython's source tree.
    N)typesTimsortImplementation)compile	count_run
binarysortgallop_leftgallop_right
merge_initmerge_append	merge_popmerge_compute_minrunmerge_lomerge_himerge_atmerge_force_collapsemerge_collapserun_timsortrun_timsort_with_valuesU         
MergeState)
min_gallopkeysvaluespendingnMergeRun)startsizec                   ^^^^^^^	^
^^^^^^^^^^^^^^^^^^ U " T5      m[         R                  mT" S5      mU S 5       m
U UUU4S j5       mU UUU4S j5       mU S 5       mU S 5       mU U
U4S j5       mU U4S j5       mU S	 5       mU UU
4S
 j5       mU U4S j5       mU U4S j5       mU U4S j5       m	U S 5       mU U
4S j5       mU U
4S j5       mSmU UUUU	U
UUU4S j5       mU UUUU	U
UUUU4	S j5       mU UU	UUU4S j5       mU U4S j5       mU U4S j5       mU U
4S j5       mU UUUUUUUU4S j5       mU UU4S j5       nU UU4S j5       n[        U TTTT	TTTTTTTTTX#5      $ )Nr   c                     XL$ N )r   r   s     f/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/numba/misc/timsort.py
has_values%make_timsort_impl.<locals>.has_values?   s    !!    c                    > [        [        U 5      S-  S-   [        5      nT" X5      nUn[        TT5      /[        -  n[        T" [        5      X#UT5      $ )z/
Initialize a MergeState for a non-keyed sort.
      minlenMERGESTATE_TEMP_SIZEr   MAX_MERGE_PENDINGr   
MIN_GALLOP)r   	temp_size	temp_keystemp_valuesr   intpmake_temp_areazeros        r%   r
   %make_timsort_impl.<locals>.merge_initC   sY    
 D	Q*,@A	"43	D$'(+<<$z*IGTRRr(   c                    > [        [        U 5      S-  S-   [        5      nT" X5      nT" X5      n[        TT5      /[        -  n[        T" [        5      X4UT5      $ )z+
Initialize a MergeState for a keyed sort.
r*   r+   r,   )	r   r   r2   r3   r4   r   r5   r6   r7   s	         r%   merge_init_with_values1make_timsort_impl.<locals>.merge_init_with_valuesN   s^    
 D	Q*,@A	"43	$V7D$'(+<<$z*IGTRRr(   c                     U R                   nU[        :  d   eXR                  U'   [        U R                  U R
                  U R                  U R                  US-   5      $ )z"
Append a run on the merge stack.
r+   )r   r0   r   r   r   r   r   )msrunr   s      r%   r   'make_timsort_impl.<locals>.merge_appendY   sN    
 DD$$$$

1"--"))RZZQOOr(   c                     [        U R                  U R                  U R                  U R                  U R
                  S-
  5      $ )z'
Pop the top run from the merge stack.
r+   )r   r   r   r   r   r   )r=   s    r%   r   $make_timsort_impl.<locals>.merge_popc   s0    
 "--"))RZZPQRRr(   c                 L  > [        U R                  5      nX::  a  U $ X!:  a  US-  nX!:  a  M  T" U R                  U5      nT" U R                  U R                  5      (       a  T" U R                  U5      nOUn[        U R                  X4U R
                  U R                  5      $ )z:
Ensure enough temp memory for 'need' items is available.
r+   )r.   r   r   r   r   r   r   )r=   needallocedr3   r4   r&   r6   s        r%   merge_getmem'make_timsort_impl.<locals>.merge_getmemj   s    
 bgg,?InlG n #277G4	bggryy))(G<K#K"--RTTRRr(   c                 ~   > [        T" U5      U R                  U R                  U R                  U R                  5      $ )z%
Modify the MergeState's min_gallop.
)r   r   r   r   r   )r=   
new_gallopr5   s     r%   merge_adjust_gallop.make_timsort_impl.<locals>.merge_adjust_gallop~   s-    
 $z*BGGRYY

BDDQQr(   c                 
    X:  $ )zo
Trivial comparison function between two keys.  This is factored out to
make it clear where comparisons occur.
r$   )abs     r%   LTmake_timsort_impl.<locals>.LT   s     ur(   c                 h  > X$::  a  XC::  d   eT" X5      nX$:X  a  US-  nXC:  a  X   nUnUnXx:  a)  XxU-
  S-	  -   n	T" X`U	   5      (       a  U	nOU	S-   nXx:  a  M)  [        XGS5       H  n	X	S-
     X	'   M     X`U'   U(       a$  X   n
[        XGS5       H  n	XS-
     X'   M     XU'   US-  nXC:  a  M  gg)af  
binarysort is the best method for sorting small arrays: it does
few compares, but can do data movement quadratic in the number of
elements.
[lo, hi) is a contiguous slice of a list, and is sorted via
binary insertion.  This sort is stable.
On entry, must have lo <= start <= hi, and that [lo, start) is already
sorted (pass start == lo if you don't know!).
r+   Nrange)r   r   lohir   _has_valuespivotlrp	pivot_valrN   r&   s              r%   r   %make_timsort_impl.<locals>.binarysort   s     {u{** .;QJEjKE AA
 %a%A&e!W%%A!A % 5R(1u+ )G"M	u,A &1uFI -%q	QJEA jr(   c                 4  > X:  d   eUS-   U:X  a  gT" XS-      X   5      (       a9  [        US-   U5       H   nT" X   XS-
     5      (       a  M  X1-
  S4s  $    X!-
  S4$ [        US-   U5       H   nT" X   XS-
     5      (       d  M  X1-
  S4s  $    X!-
  S4$ )a~  
Return the length of the run beginning at lo, in the slice [lo, hi).
lo < hi is required on entry.  "A run" is the longest ascending sequence, with

    lo[0] <= lo[1] <= lo[2] <= ...

or the longest descending sequence, with

    lo[0] > lo[1] > lo[2] > ...

A tuple (length, descending) is returned, where boolean *descending*
is set to 0 in the former case, or to 1 in the latter.
For its intended use in a stable mergesort, the strictness of the defn of
"descending" is needed so that the caller can safely reverse a descending
sequence without violating stability (strict > ensures there are no equal
elements to get out of order).
r+   )r+   Fr*   TFrR   )r   rT   rU   krN   s       r%   r   $make_timsort_impl.<locals>.count_run   s    & ww6R<d6lDH%%262&$'4A;//64<' ' 7D=  262&dgtE{++65=( ' 7E>!r(   c                 
  > X2:  d   eXB:  a  XC:  d   eX2-
  nSnSnT
" X   U 5      (       aG  X4-
  nXx:  a.  T
" XU-      U 5      (       a  UnUS-  S-   nUS::  a  UnOOXx:  a  M.  Xx:  a  UnXd-  nXt-  nOGXB-
  S-   nXx:  a-  T
" XU-
     U 5      (       a  OUnUS-  S-   nUS::  a  UnXx:  a  M-  Xx:  a  UnXG-
  XF-
  pvUS-
  U::  a
  Xg:  a  Xs::  d   eUS-  nXg:  a)  XgU-
  S-	  -   n	T
" X   U 5      (       a  U	S-   nOU	nXg:  a  M)  U$ )aK  
Locate the proper position of key in a sorted vector; if the vector contains
an element equal to key, return the position immediately to the left of
the leftmost equal element.  [gallop_right() does the same except returns
the position to the right of the rightmost equal element (if any).]

"a" is a sorted vector with stop elements, starting at a[start].
stop must be > start.

"hint" is an index at which to begin the search, start <= hint < stop.
The closer hint is to the final result, the faster this runs.

The return value is the int k in start..stop such that

    a[k-1] < key <= a[k]

pretending that a[start-1] is minus infinity and a[stop] is plus infinity.
IOW, key belongs at index k; or, IOW, the first k elements of a should
precede key, and the last stop-start-k should follow key.

See listsort.txt for info on the method.
r   r+   r$   keyrL   r   stophintr   lastofsofsmaxofsmrN   s             r%   r   &make_timsort_impl.<locals>.gallop_left   ss   0 ||},,L ags [F,as
mS))!G!8q.Cax$  , |OGKC \A%F,as
mS)) "G!8q.Cax$ , |:t~SqyG##+EE 	1m'Ma/0A!$}}a%  m 
r(   c                   > X2:  d   eXB:  a  XC:  d   eX2-
  nSnSnT
" XU   5      (       aH  XB-
  S-   nXx:  a-  T
" XXG-
     5      (       a  UnUS-  S-   nUS::  a  UnOOXx:  a  M-  Xx:  a  UnXG-
  XF-
  pvODX4-
  nXx:  a,  T
" XXG-      5      (       a  OUnUS-  S-   nUS::  a  UnXx:  a  M,  Xx:  a  UnXd-  nXt-  nUS-
  U::  a
  Xg:  a  Xs::  d   eUS-  nXg:  a)  XgU-
  S-	  -   n	T
" XU	   5      (       a  U	nOU	S-   nXg:  a  M)  U$ )a  
Exactly like gallop_left(), except that if key already exists in a[start:stop],
finds the position immediately to the right of the rightmost equal value.

The return value is the int k in start..stop such that

    a[k-1] <= key < a[k]

The code duplication is massive, but this is enough different given that
we're sticking to "<" comparisons that it's much harder to follow if
written as one routine with yet another "left or right?" flag.
r   r+   r$   ra   s             r%   r	   'make_timsort_impl.<locals>.gallop_right;  so    ||},,L cT7 \A%F,cTZ=))!G!8q.Cax$  , |:t~S [F,cTZ=)) "G!8q.Cax$ , |OGKCqyG##+EE 	1m'Ma/0A#t}} a% m 
r(   c                 R    SnU S:  d   eU S:  a  XS-  -  nU S-  n U S:  a  M  X-   $ )a}  
Compute a good value for the minimum run length; natural runs shorter
than this are boosted artificially via binary insertion.

If n < 64, return n (it's too small to bother with fancy stuff).
Else if n is an exact power of 2, return 32.
Else return an int k, 32 <= k <= 64, such that n/k is close to, but
strictly less than, an exact power of 2.

See listsort.txt for more info.
r   @   r+   r$   )r   rY   s     r%   r   /make_timsort_impl.<locals>.merge_compute_minrun  sA     Avv2gQJA!GA 2g ur(   c                    > US:  d   eUS:  d   e[        U5       H  nX5U-      XU-   '   M     T" X45      (       a  [        U5       H  nXEU-      XU-   '   M     gg)z
Upwards memcpy().
r   NrR   		dest_keysdest_values
dest_startsrc_keys
src_values	src_startnitemsir&   s	           r%   sortslice_copy)make_timsort_impl.<locals>.sortslice_copy  o     A~~QvA(0Q(?I1n% h++6].8Q.GN+ # ,r(   c                    > US:  d   eUS:  d   e[        U5       H  nX5U-
     XU-
  '   M     T" X45      (       a  [        U5       H  nXEU-
     XU-
  '   M     gg)z
Downwards memcpy().
r   NrR   rp   s	           r%   sortslice_copy_down.make_timsort_impl.<locals>.sortslice_copy_down  r{   r(   r+   c           	        > US:  a  US:  a  XF::  d   eXSU-   :X  d   eT" X5      n T" U R                   U R                  SXUU5        U R                   nU R                  nUn	Un
UnSnT" Xx5      nU R                  nUS:  Ga  US:  Ga  SnSn T" X   Xs   5      (       a7  X   X'   U(       a  X   X+'   US-  nUS-  nUS-  nUS:X  a  OFUS-  nSnX:  a  O9O6Xs   X'   U(       a  X   X+'   US-  nUS-  nUS-  nUS:X  a  OUS-  nSnX:  a  OM  T(       Ga  US:  Ga  US:  Ga  US-  nU[        :  d
  U[        :  a  XS:  -  nT" X   XsX4-   U5      nUU-  nUnUS:  a"  T" XUXxUU5        UU-  nUU-  nUU-  nUS:X  a  OX   X'   U(       a  X   X+'   US-  nUS-  nUS-  nUS:X  a  OT" Xs   XXV-   U5      nUU-  nUnUS:  a"  T" XUXUU5        UU-  nUU-  nUU-  nUS:X  a  OAXs   X'   U(       a  X   X+'   US-  nUS-  nUS-  nUS:X  a  OU[        :  a  M  U[        :  a  M  US-  nUS:  a	  US:  a  GM  US:X  a  T" XUXxUU5        OUS:X  d   eX:X  d   eT" X5      $ )a  
Merge the na elements starting at ssa with the nb elements starting at
ssb = ssa + na in a stable way, in-place.  na and nb must be > 0,
and should have na <= nb. See listsort.txt for more info.

An updated MergeState is returned (with possibly a different min_gallop
or larger temp arrays).

NOTE: compared to CPython's timsort, the requirement that
    "Must also have that keys[ssa + na - 1] belongs at the end of the merge"

is removed. This makes the code a bit simpler and easier to reason about.
r   r+   r   r   r   r1   )r=   r   r   ssanassbnba_keysa_valuesb_keysb_valuesdestrV   r   acountbcountr^   	DO_GALLOPrN   r   r	   r&   rI   rE   ry   s                    r%   r   #make_timsort_impl.<locals>.merge_lo  sT    Av"q&RX--Bh"!rww		1S	 99 2]]
 1fa FFfk6;//!'DJ"'/}AID1HC!GBQwaKFF+ , "(DJ"'/}AID1HC!GBQwaKFF+9 D R!VQa

*f
.B q.0J %V[&sxMAHAF1u&tT'-'(* 	qa7!!'DJ"'/}AID1HC!GBQw $FKchLAHAF1u 'tT'-'(* 	qa7!!'DJ"'/}AID1HC!GBQww 
*f
.B| a
S 1faX 74!S 7N7;; #222r(   c           	      2  >	 US:  a  US:  a  XF:  d   eXSU-   :X  d   eT" X5      n T" U R                   U R                  SXUU5        UnUnU R                   n	U R                  n
XV-   S-
  nUS-
  nX4-   S-
  nT" X5      nU R                  nUS:  Ga  US:  Ga  SnSn T" X   Xs   5      (       a7  Xs   X'   U(       a  X   X+'   US-  nUS-  nUS-  nUS:X  a  OFUS-  nSnX:  a  O9O6X   X'   U(       a  X   X+'   US-  nUS-  nUS-  nUS:X  a  OUS-  nSnX:  a  OM  T(       Ga/  US:  Ga(  US:  Ga!  US-  nU[        :  d  U[        :  Ga  XS:  -  nT" X   XsU-
  S-   US-   U5      nUS-   U-
  nUnUS:  a"  T" XUXxUU5        UU-  nUU-  nUU-  nUS:X  a  OX   X'   U(       a  X   X+'   US-  nUS-  nUS-  nUS:X  a  OT" Xs   XU-
  S-   US-   U5      nUS-   U-
  nUnUS:  a"  T" XUXUU5        UU-  nUU-  nUU-  nUS:X  a  OBXs   X'   U(       a  X   X+'   US-  nUS-  nUS-  nUS:X  a  OU[        :  a  M  U[        :  a  GM  US-  nUS:  a	  US:  a  GM  US:X  a  T" XX-
  S-   XXV-
  S-   U5        OUS:X  d   eX:X  d   eT" X5      $ )a  
Merge the na elements starting at ssa with the nb elements starting at
ssb = ssa + na in a stable way, in-place.  na and nb must be > 0,
and should have na >= nb.  See listsort.txt for more info.

An updated MergeState is returned (with possibly a different min_gallop
or larger temp arrays).

NOTE: compared to CPython's timsort, the requirement that
    "Must also have that keys[ssa + na - 1] belongs at the end of the merge"

is removed. This makes the code a bit simpler and easier to reason about.
r   r+   r   )r=   r   r   r   r   r   r   r   r   r   r   r   rV   r   r   r   r^   r   rN   r   r	   r&   rI   rE   ry   r}   s                    r%   r   #make_timsort_impl.<locals>.merge_hiY  s    Av"q&RX--Bh"!rww		1S	 99 x!|1fhl 2]]
1fa FFfk6;//!'DJ"'/}AID1HC!GBQwaKFF+ , "(DJ"'/}AID1HC!GBQwaKFF+; F R!VQa

*f
.B q.0J %V[&(Q,aQTUAa!AF1u ,D$,2c,-/ 	qa7!!'DJ"'/}AID1HC!GBQw $FKrAsQwPSTAa!AF1u+D$,2c,-/ 	qa7!!'DJ"'/}AID1HC!GBQww 
*f
.B| a
U 1faZ 74Q!SX\ 7N7;; #222r(   c           	      &  > U R                   nUS:  d   eUS:  d   eX4S-
  :X  d
  X4S-
  :X  d   eU R                  U   u  pVU R                  US-      u  pxUS:  a  US:  d   eXV-   U:X  d   e[        XVU-   5      U R                  U'   X4S-
  :X  a"  U R                  US-      U R                  US-   '   T" U 5      n T" X   XXV-   U5      n	XiU-
  -  nU	nUS:X  a  U $ T
" XU-   S-
     XXx-   Xx-   S-
  5      n	X-
  nXh::  a  T" XX%XgU5      $ T" XX%XgU5      $ )zT
Merge the two runs at stack indices i and i+1.

An updated MergeState is returned.
r*   r      r+   )r   r   r   )r=   r   r   rx   r   r   r   r   r   r^   r   r	   r   r   r   s             r%   r   #make_timsort_impl.<locals>.merge_at  sO    DDAvvAvvEzQa%Z''**Q-**QU#Av"q&  x3
 !2g.

1A: "

1q5 1BJJq1ur] Dsx=
#g7I 2X\*DsxANW 8Bf2B??Bf2B??r(   c                 ,  > U R                   S:  Ga  U R                  nU R                   S-
  nUS:  a3  X4S-
     R                  X4   R                  X4S-      R                  -   ::  d9  US:  af  X4S-
     R                  X4S-
     R                  X4   R                  -   ::  a3  X4S-
     R                  X4S-      R                  :  a  US-  nT" XX$5      n O.X4   R                  X4S-      R                  :  a
  T" XX$5      n O U $ U R                   S:  a  GM  U $ )z
Examine the stack of runs waiting to be merged, merging adjacent runs
until the stack invariants are re-established:

1. len[-3] > len[-2] + len[-1]
2. len[-2] > len[-1]

An updated MergeState is returned.

See listsort.txt for more info.
r+   r*   r   r   r   r    r=   r   r   r   r   r   s        r%   r   )make_timsort_impl.<locals>.merge_collapse'  s     ddQhjjGqAQ7Q3<,,
'A#,BSBS0SSQ7Q3<,,!0A0AGJOO0SSq5>&&Q)<)<<FAb27q5>#6#66b2	 ddQh 	r(   c                    > U R                   S:  ae  U R                  nU R                   S-
  nUS:  a)  X4S-
     R                  X4S-      R                  :  a  US-  nT" XX$5      n U R                   S:  a  Me  U $ )z
Regardless of invariants, merge all runs on the stack until only one
remains.  This is used at the end of the mergesort.

An updated MergeState is returned.
r+   r*   r   r   r   s        r%   r   /make_timsort_impl.<locals>.merge_force_collapseC  sv     ddQhjjGqA1uq5>&&Q)<)<<FA"F.B ddQh 	r(   c                    > UnUS-
  nXE:  a  X   X   sX'   X'   US-  nUS-  nXE:  a  M  T" X5      (       a,  UnUS-
  nXE:  a  X   X   sX'   X'   US-  nUS-  nXE:  a  M  ggg)z
Reverse a slice, in-place.
r+   Nr$   )r   r   r   rc   rx   jr&   s         r%   reverse_slice(make_timsort_impl.<locals>.reverse_sliceV  s    
 1He#wDGTWFAFA e d##AqA%'-y&)$	69QQ % $r(   c                   > [        U5      nUS:  a  gT" U5      nTnUS:  al  T
" XXS-   5      u  pgU(       a  T" XXUU-   5        Xd:  a  [        XC5      nT	" XXUU-   XV-   5        UnT" U [        XV5      5      n T" XU5      n XV-  nX6-  nUS:  a  Ml  T" XU5      n U R                  S:X  d   eU R                  S   S[        U5      4:X  d   eg)z"
Run timsort with the mergestate.
r*   Nr   r+   )r.   r-   r   r   r   )r=   r   r   
nremainingminrunrT   r   descforcer   r   r   r   r   r   r   r7   s            r%   run_timsort_with_mergestate6make_timsort_impl.<locals>.run_timsort_with_mergestatej  s    
 Y
> &j11n"/:GAdBQ7zF/4%Z@b(2/2B&1BGBOJ 1n$ ""F3ttqyyzz!}CI...r(   c                 (   > U nT" T" U 5      X5        g)z"
Run timsort over the given keys.
Nr$   )r   r   r
   r   s     r%   r   &make_timsort_impl.<locals>.run_timsort  s    
 #Jt$4dCr(   c                 $   > T" T" X5      X5        g)z-
Run timsort over the given keys and values.
Nr$   )r   r   r:   r   s     r%   r   2make_timsort_impl.<locals>.run_timsort_with_values  s    
 	$$:4$H$(	2r(   )r   r5   r   )wrapr6   r   r   r   rN   r   r   r   r	   r&   r5   rI   r   r   r   r   r   rE   r   r
   r:   r   r   r   r   ry   r}   r7   s    `  @@@@@@@@@@@@@@@@@@@@@@@@@r%   make_timsort_implr   9   ss   .)N::D7D	" 
" 
S 
S 
S 
S 
P 
P 
S 
S 
S 
S& 
R 
R 
 
 
. 
.b 
!" 
!"H 
R 
Rj 
H 
HV 
 
( 
H 
H 
H 
H  I	W3 W3 
W3t 
Z3 Z3 
Z3z 
,@ ,@ 
,@^ 
 
6 
 
$ 
 
& 
!/ !/ 
!/H 
D 
D 
2 
2 !:{LL)h(n. .r(   c                      [        S /U Q76 $ )Nc                     U $ r#   r$   )fs    r%   <lambda>!make_py_timsort.<locals>.<lambda>  s    r(   )r   )argss    r%   make_py_timsortr     s    k2T22r(   c                  0   ^ SSK Jm  [        U4S j/U Q76 $ )Nr   )jitc                    > T" SS9" U 5      $ )NT)nopythonr$   )r   r   s    r%   r   "make_jit_timsort.<locals>.<lambda>  s    T(:1(=r(   )numbar   r   )r   r   s    @r%   make_jit_timsortr     s    = %#% %r(   )__doc__collections
numba.corer   
namedtupler   r0   r1   r/   r   r   r   r   r   r$   r(   r%   <module>r      s      $..	 *   
   ##BD
 !!*.?@m.`3%r(   