
    uYfD                     :   d dl mZ 	 d dlZd dlZd dlZej
                  rd dlmZ d dlmZ d dlZdZ	 G d de
      Z G d d	e
      Z e       Z e       Z G d
 d ej                  ej                               Z G d de      Z G d de      Z G d de      Zy)    )divisionN)List)Optional   c                        e Zd Zd ZeZd ZeZy)_NegativeIntInfinityc                      yNF selfxs     N/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/ddsketch/store.py__ge__z_NegativeIntInfinity.__ge__           c                      yNTr   r   s     r   __lt__z_NegativeIntInfinity.__lt__$       r   N__name__
__module____qualname__r   __gt__r   __le__r   r   r   r   r      s     F Fr   r   c                        e Zd Zd ZeZd ZeZy)_PositiveIntInfinityc                      yr   r   r   s     r   r   z_PositiveIntInfinity.__ge__+   r   r   c                      yr
   r   r   s     r   r   z_PositiveIntInfinity.__lt__0   r   r   Nr   r   r   r   r   r   *   s     F Fr   r   c                       e Zd ZdZd Zej                  d        Zej                  d        Zej                  d	d       Z	ej                  d
d       Z
ej                  d        Zy)StorezThe basic specification of a store

    Attributes:
        count (float): the sum of the counts for the bins
        min_key (int): the minimum key bin
        max_key (int): the maximum key bin
    c                 >    d| _         t        | _        t        | _        y Nr   )count_pos_infinitymin_key_neg_infinitymax_keyr   s    r   __init__zStore.__init__C   s    
$$r   c                      y)z%Copies the input store into this one.Nr   r   stores     r   copyz
Store.copyI       r   c                      y)Return the number of bins.Nr   r*   s    r   lengthzStore.lengthM   r0   r   c                      y)ziUpdates the counter at the specified index key, growing the number of bins if
        necessary.
        Nr   )r   keyweights      r   addz	Store.addR   r0   r   c                      y)av  Return the key for the value at given rank.

        E.g., if the non-zero bins are [1, 1] for keys a, b with no offset

        if lower = True:
             key_at_rank(x) = a for x in [0, 1)
             key_at_rank(x) = b for x in [1, 2)

        if lower = False:
             key_at_rank(x) = a for x in (-1, 0]
             key_at_rank(x) = b for x in (0, 1]
        Nr   )r   ranklowers      r   key_at_rankzStore.key_at_rankY   r0   r   c                      y)zMerge another store into this one. This should be equivalent as running the
        add operations that have been run on the other store on this one.
        Nr   r-   s     r   mergezStore.mergei   r0   r   Ng      ?T)r   r   r   __doc__r+   abcabstractmethodr/   r3   r7   r;   r=   r   r   r   r"   r"   :   s    % 	4 4 	) ) 	  	  	 r   r"   c                   t     e Zd ZdZef fd	Zd Zd Zd ZddZ	d Z
d Zdd	Zd
 Zd Zd ZddZd Z xZS )
DenseStorea  A dense store that keeps all the bins between the bin for the min_key and the
    bin for the max_key.

    Args:
        chunk_size (int, optional): the number of bins to grow by

    Attributes:
        count (int): the sum of the counts for the bins
        min_key (int): the minimum key bin
        max_key (int): the maximum key bin
        offset (int): the difference btw the keys and the index in which they are stored
        bins (List[float]): the bins
    c                 T    t         t        |           || _        d| _        g | _        y r$   )superrD   r+   
chunk_sizeoffsetbins)r   rG   	__class__s     r   r+   zDenseStore.__init__   s%    j$(*$	r   c                     d}t        | j                        D ]  \  }}||| j                  z   d|dz  } |d| j                  d| j                  d| j                  z  }|S )N{z: z, z}}, min_key:z
, max_key:z	, offset:)	enumeraterI   rH   r'   r)   )r   repr_strisbins       r   __repr__zDenseStore.__repr__   se     + 	=GAta$++ot<<H	=LLLLKK
 	

 r   c                     |j                   d d  | _         |j                  | _        |j                  | _        |j                  | _        |j                  | _        y N)rI   r%   r'   r)   rH   r-   s     r   r/   zDenseStore.copy   s=    JJqM	[[
}}}}llr   c                 ,    t        | j                        S )r2   )lenrI   r*   s    r   r3   zDenseStore.length   s     499~r   c                 ~    | j                  |      }| j                  |xx   |z  cc<   | xj                  |z  c_        y rS   )
_get_indexrI   r%   )r   r5   r6   idxs       r   r7   zDenseStore.add   s0    ooc"		#& 

f
r   c                     || j                   k  r| j                  |       n || j                  kD  r| j                  |       || j                  z
  S )FCalculate the bin index for the key, extending the range if necessary.)r'   _extend_ranger)   rH   r   r5   s     r   rW   zDenseStore._get_index   sG     s#4<<s#T[[  r   c                     ||z
  dz   }| j                   t        t        j                  || j                   z              z  S N   )rG   intmathceilr   new_min_keynew_max_keydesired_lengths       r   _get_new_lengthzDenseStore._get_new_length   s7    ${2Q6TYY~/O%P!QQQr   c                 B   ||}t        ||| j                        }t        ||| j                        }| j	                         dk(  r5dg| j                  ||      z  | _        || _        | j                  ||       y|| j                  k\  r/|| j                  | j	                         z   k  r|| _        || _        y| j                  ||      }|| j	                         kD  r0| j                  j                  dg|| j	                         z
  z         | j                  ||       y)z+Grow the bins as necessary and call _adjustNr           )
minr'   maxr)   r3   rg   rI   rH   _adjustextend)r   r5   
second_keyrd   re   
new_lengths         r   r[   zDenseStore._extend_range   s     J#z4<<8#z4<<8;;=A 4 4[+ NNDI%DKLLk2DLL([4;;;V-V&DL&DL --k;GJDKKM)		  #*t{{}*D!EFLLk2r   c                 D    | j                  ||       || _        || _        y)zAdjust the bins, the offset, the min_key, and max_key, without resizing the
        bins, in order to try making it fit the specified range.
        N)_center_binsr'   r)   )r   rd   re   s      r   rl   zDenseStore._adjust   s"    
 	+{3""r   c                    |dkD  r)| j                   d|  | _         dg|z  | j                   dd nE| j                   t        |      d | _         | j                   j                  dgt        |      z         | xj                  |z  c_        y)z(Shift the bins; this changes the offset.r   Nri   )rI   absrm   rH   )r   shifts     r   _shift_binszDenseStore._shift_bins   su     19		'E6*DI EEMDIIbqM		#e*,/DIIIcUSZ/0ur   c                     |||z
  dz   dz  z   }| j                  | j                  | j                         dz  z   |z
         y)z)Center the bins; this changes the offset.r_      N)ru   rH   r3   )r   rd   re   
middle_keys       r   rq   zDenseStore._center_bins   sD     !K+$=$Aa#GG
t{{}'99JFGr   c                     d}t        | j                        D ]-  \  }}||z  }|r||kD  s|r||dz   k\  s|| j                  z   c S  | j                  S )Nri   r_   )rM   rI   rH   r)   )r   r9   r:   
running_ctrO   bin_cts         r   r;   zDenseStore.key_at_rank   s_    
"499- 	'IAv& J*t+UzTTUX?U4;;&	'
 ||r   c                     |j                   dk(  ry | j                   dk(  r| j                  |       y |j                  | j                  k  s|j                  | j                  kD  r&| j	                  |j                  |j                         t        |j                  |j                  dz         D ]@  }| j                  || j                  z
  xx   |j                  ||j                  z
     z  cc<   B | xj                   |j                   z  c_         y )Nr   r_   )r%   r/   r'   r)   r[   rangerI   rH   )r   r.   r5   s      r   r=   zDenseStore.merge   s    ;;!::?IIe==4<<'5==4<<+Gu}}emm<(9: 	KCIIcDKK'(EJJsU\\7I,JJ(	K 	

ekk!
r   r>   rS   r?   )r   r   r   r@   
CHUNK_SIZEr+   rQ   r/   r3   r7   rW   rg   r[   rl   ru   rq   r;   r=   __classcell__rJ   s   @r   rD   rD   q   sP     #- 
#
!R
34#	H"r   rD   c                   H     e Zd ZdZef fd	Z fdZd Zd Zd Z	d Z
 xZS )CollapsingLowestDenseStoreaY  A dense store that keeps all the bins between the bin for the min_key and the
    bin for the max_key, but collapsing the left-most bins if the number of bins
    exceeds the bin_limit

    Args:
        bin_limit (int): the maximum number of bins
        chunk_size (int, optional): the number of bins to grow by

    Attributes:
        count (int): the sum of the counts for the bins
        min_key (int): the minimum key bin
        max_key (int): the maximum key bin
        offset (int): the difference btw the keys and the index in which they are stored
        bins (List[int]): the bins
    c                 F    t         t        |           || _        d| _        y r
   )rF   r   r+   	bin_limitis_collapsedr   r   rG   rJ   s      r   r+   z#CollapsingLowestDenseStore.__init__  s     ($8:"!r   c                 p    |j                   | _         |j                  | _        t        t        |   |       y rS   )r   r   rF   r   r/   r   r.   rJ   s     r   r/   zCollapsingLowestDenseStore.copy  s,    !..($4U;r   c           	          ||z
  dz   }t        | j                  t        t        j                  || j                  z              z  | j
                        S r^   )rj   rG   r`   ra   rb   r   rc   s       r   rg   z*CollapsingLowestDenseStore._get_new_length#  sH    ${2Q6OOc$))NT__,L"MNNNN
 	
r   c                     || j                   k  r+| j                  ry| j                  |       | j                  r!y|| j                  kD  r| j                  |       || j                  z
  S )rZ   r   )r'   r   r[   r)   rH   r\   s     r   rW   z%CollapsingLowestDenseStore._get_index+  s_       s#  4<<s#T[[  r   c                    ||z
  dz   | j                         kD  r9|| j                         z
  dz   }|| j                  k\  rI|| _        || _        dg| j                         z  | j                  dd | j
                  | j                  d<   n| j                  |z
  }|dk  r| j                  | j                  z
  }|| j                  z
  }t        | j                  ||       }dg|| j                  z
  z  | j                  || | j                  |xx   |z  cc<   || _        | j                  |       n|| _        | j                  |       || _        d| _        y| j                  ||       || _        || _        y)Override. Adjust the bins, the offset, the min_key, and max_key, without
        resizing the bins, in order to try making it fit the specified
        range. Collapse to the left if necessary.
        r_   ri   Nr   T)
r3   r)   rH   r'   rI   r%   sumru   r   rq   r   rd   re   rt   collapse_start_indexcollapse_end_indexcollapsed_counts          r   rl   z"CollapsingLowestDenseStore._adjust:  s_    $q(4;;=8%59Kdll*)* #ut{{}4		!#zz		!k119+/<<$+++E()4t{{)B&&)		"67IJ'O KN#dll2JDII23EF II01_D1#.DL$$U+#.DL$$U+&DL $Dk;7&DL&DLr   c                 &   |j                   dk(  ry| j                   dk(  r| j                  |       y|j                  | j                  k  s|j                  | j                  kD  r&| j	                  |j                  |j                         |j                  |j
                  z
  }t        | j                  |j                  dz         |j
                  z
  }||kD  r0t        |j                  ||       }| j                  dxx   |z  cc<   n|}t        ||j
                  z   |j                  dz         D ]@  }| j                  || j
                  z
  xx   |j                  ||j
                  z
     z  cc<   B | xj                   |j                   z  c_         y)	Override.r   Nr_   )
r%   r/   r'   r)   r[   rH   rj   r   rI   r}   )r   r.   collapse_start_idxcollapse_end_idxcollapse_countr5   s         r   r=   z CollapsingLowestDenseStore.mergee  s@    ;;!::?IIe==4<<'5==4<<+Gu}}emm<"]]U\\9t||U]]Q->?%,,N00 ,>?O!PQNIIaLN*L1)ELL8%--!:KL 	KCIIcDKK'(EJJsU\\7I,JJ(	K 	

ekk!
r   r   r   r   r@   r~   r+   r/   rg   rW   rl   r=   r   r   s   @r   r   r     s,      .8 "<
!)'V"r   r   c                   H     e Zd ZdZef fd	Z fdZd Zd Zd Z	d Z
 xZS )CollapsingHighestDenseStoreaZ  A dense store that keeps all the bins between the bin for the min_key and the
    bin for the max_key, but collapsing the right-most bins if the number of bins
    exceeds the bin_limit

    Args:
        bin_limit (int): the maximum number of bins
        chunk_size (int, optional): the number of bins to grow by

    Attributes:
        count (int): the sum of the counts for the bins
        min_key (int): the minimum key bin
        max_key (int): the maximum key bin
        offset (int): the difference btw the keys and the index in which they are stored
        bins (List[int]): the bins
    c                 F    t         t        |           || _        d| _        y r
   )rF   r   r+   r   r   r   s      r   r+   z$CollapsingHighestDenseStore.__init__  s     )49;"!r   c                 p    |j                   | _         |j                  | _        t        t        |   |       y rS   )r   r   rF   r   r/   r   s     r   r/   z CollapsingHighestDenseStore.copy  s,    !..)45e<r   c                     ||z
  dz   }t        t        | j                  t        t        j                  || j                  z              z  | j
                              S r^   )r`   rj   rG   ra   rb   r   rc   s       r   rg   z+CollapsingHighestDenseStore._get_new_length  sP    ${2Q6#dii0P&Q"RR
 	
r   c                    || j                   kD  rO| j                  r| j                         dz
  S | j                  |       | j                  r3| j                         dz
  S || j                  k  r| j                  |       || j
                  z
  S )zECalculate the bin index for the key, extending the range if necessaryr_   )r)   r   r3   r[   r'   rH   r\   s     r   rW   z&CollapsingHighestDenseStore._get_index  s{       {{}q((s#  {{}q((4<<s#T[[  r   c                    ||z
  dz   | j                         kD  rB|| j                         z   dz
  }|| j                  k  rI|| _        || _        dg| j                         z  | j                  dd | j
                  | j                  d<   n| j                  |z
  }|dkD  r|| j                  z
  dz   }| j                  | j                  z
  dz   }t        | j                  ||       }dg| j                  |z
  z  | j                  || | j                  |dz
  xx   |z  cc<   || _        | j                  |       n|| _        | j                  |       || _        d| _        y| j                  ||       || _        || _        y)r   r_   ri   Nr   T)
r3   r'   rH   r)   rI   r%   r   ru   r   rq   r   s          r   rl   z#CollapsingHighestDenseStore._adjust  sn    $q(4;;=8%59Kdll*)* #ut{{}4		! $

		"k119+6+Dq+H()-)Ca)G&&)		"67IJ'O KN{2JDII23EF II2Q67?J7#.DL$$U+#.DL$$U+&DL $Dk;7&DL&DLr   c                 &   |j                   dk(  ry| j                   dk(  r| j                  |       y|j                  | j                  k  s|j                  | j                  kD  r&| j	                  |j                  |j                         |j                  |j
                  z
  dz   }t        | j                  dz   |j                        |j
                  z
  }||kD  r0t        |j                  ||       }| j                  dxx   |z  cc<   n|}t        |j                  ||j
                  z         D ]@  }| j                  || j
                  z
  xx   |j                  ||j
                  z
     z  cc<   B | xj                   |j                   z  c_         y)r   r   Nr_   r   )
r%   r/   r'   r)   r[   rH   rk   r   rI   r}   )r   r.   r   r   r   r5   s         r   r=   z!CollapsingHighestDenseStore.merge  s@    ;;!::?IIe==4<<'5==4<<+Gu}}emm< ==5<<7!; !15==AELLP00 ,>?O!PQNIIbM^+M!1(:U\\(IJ 	KCIIcDKK'(EJJsU\\7I,JJ(	K 	

ekk!
r   r   r   s   @r   r   r     s,      .8 "
=	
!)'V"r   r   )
__future__r   rA   ra   typingTYPE_CHECKINGr   r   sixr~   r`   r   r   r(   r&   with_metaclassABCMetar"   rD   r   r   r   r   r   <module>r      s        
 
 
	3 		3 	 %&$&4Cs{{+ 4nR" R"jw" w"tx"* x"r   