
    uYfO                         d dl mZ 	 d dlmZ d dlmZ d dlZd dlZd dlZ G d d ej                  e            Z	 G d de	      Z
d	 Z G d
 de	      Z G d de	      Zy)    )division)ABCMeta)abstractmethodNc                   T    e Zd ZdZd	dZed        Zed        Zed        Z	d Z
d Zy)

KeyMappinga3  
    Args:
        relative_accuracy (float): the accuracy guarantee; referred to as alpha
        in the paper. (0. < alpha < 1.)
        offset (float): an offset that can be used to shift all bin keys
    Attributes:
        gamma (float): the base for the exponential buckets. gamma = (1 + alpha) / (1 - alpha)
        min_possible: the smallest value the sketch can distinguish from 0
        max_possible: the largest value the sketch can handle
        _multiplier (float): used for calculating log_gamma(value) initially, _multiplier = 1 / log(gamma)
    c                 d   |dk  s|dk\  rt        d|z        || _        || _        d|z  d|z
  z  }d|z   | _        dt	        j
                  |      z  | _        t        j                  j                  | j                  z  | _
        t        j                  j                  | j                  z  | _        y )Nr      z1Relative accuracy must be between 0 and 1, got %r   )
ValueErrorrelative_accuracy_offsetgammamathlog1p_multipliersys
float_infominmin_possiblemaxmax_possible)selfr   offsetgamma_mantissas       P/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/ddsketch/mapping.py__init__zKeyMapping.__init__+   s    !%6!%;CFWW  "3..!6G2GH'
tzz.99NN..;NN..;    c                 ,    |dz
  |dz   z  } | ||      S )zConstructor used by pb.proto      ?r    )clsr   r   r   s       r   from_gamma_offsetzKeyMapping.from_gamma_offset:   s%     #S[US[9$V44r   c                      y)zBReturn (an approximation of) the logarithm of the value base gammaNr!   r   values     r   
_log_gammazKeyMapping._log_gammaA       r   c                      y)z5Return (an approximation of) gamma to the power valueNr!   r%   s     r   
_pow_gammazKeyMapping._pow_gammaF   r(   r   c                 v    t        t        j                  | j                  |            | j                  z         S )z{
        Args:
            value (float)
        Returns:
            int: the key specifying the bucket for value
        )intr   ceilr'   r   r%   s     r   keyzKeyMapping.keyK   s*     499T__U34t||CDDr   c                 d    | j                  || j                  z
        dd| j                  z   z  z  S )z
        Args:
            key (int)
        Returns:
            float: the value represented by the bucket specified by the key
               @r	   )r*   r   r   )r   r.   s     r   r&   zKeyMapping.valueU   s.     sT\\12cQ^6LMMr   Ng        )__name__
__module____qualname____doc__r   classmethodr#   r   r'   r*   r.   r&   r!   r   r   r   r      sX    
< 5 5 Q Q D DENr   r   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )LogarithmicMappingzA memory-optimal KeyMapping, i.e., given a targeted relative accuracy, it
    requires the least number of keys to cover a given range of values. This is
    done by logarithmically mapping floating-point values to integers.
    c                     t         t        |   ||       | xj                  t	        j
                  d      z  c_        y )Nr    r
   )superr8   r   r   r   logr   r   r   	__class__s      r   r   zLogarithmicMapping.__init__f   s2     $01B60RDHHQK'r   c                 H    t        j                  |d      | j                  z  S )Nr
   )r   r;   r   r%   s     r   r'   zLogarithmicMapping._log_gammak   s    xxq!D$4$444r   c                 H    t        j                  d|| j                  z        S )Nr0   )r   powr   r%   s     r   r*   zLogarithmicMapping._pow_gammao   s    xxUT%5%5566r   r1   )r2   r3   r4   r5   r   r'   r*   __classcell__r=   s   @r   r8   r8   `   s    
(
57r   r8   c                 D    t        t        |       dz        }| dk  r| S |S )NgUUUUUU?r   )floatabs)xys     r   _cbrtrH   t   s(    c!f#$A1ur	Hr   c                   (    e Zd ZdZd Zd Zd Zd Zy)LinearlyInterpolatedMappinga  A fast KeyMapping that approximates the memory-optimal
    LogarithmicMapping by extracting the floor value of the logarithm to the
    base 2 from the binary representations of floating-point values and
    linearly interpolating the logarithm in-between.
    c                 R    t        j                  |      \  }}d|z  dz
  }||dz
  z   S )zApproximates log2 by s + f
        where v = (s+1) * 2 ** f  for s in [0, 1)

        frexp(v) returns m and e s.t.
        v = m * 2 ** e ; (m in [0.5, 1) or 0.0)
        so we adjust m and e accordingly
        r
   r	   )r   frexpr   r&   mantissaexponentsignificands        r   _log2_approxz(LinearlyInterpolatedMapping._log2_approx   s4     "ZZ.((lQ&hl++r   c                     t        t        j                  |      dz         }||z
  dz   dz  }t        j                  ||      S )zInverse of _log2_approxr	   r
   r0   )r,   r   floorldexp)r   r&   rO   rN   s       r   _exp2_approxz(LinearlyInterpolatedMapping._exp2_approx   sA     tzz%(1,-H$q(C/zz(H--r   c                 >    | j                  |      | j                  z  S N)rQ   r   r%   s     r   r'   z&LinearlyInterpolatedMapping._log_gamma   s      '$*:*:::r   c                 >    | j                  || j                  z        S rW   )rU   r   r%   s     r   r*   z&LinearlyInterpolatedMapping._pow_gamma   s      )9)9!9::r   N)r2   r3   r4   r5   rQ   rU   r'   r*   r!   r   r   rJ   rJ   |   s    ,.;;r   rJ   c                   H     e Zd ZdZdZdZdZd
 fd	Zd Zd Z	d Z
d	 Z xZS )CubicallyInterpolatedMappinga  A fast KeyMapping that approximates the memory-optimal LogarithmicMapping by
     extracting the floor value of the logarithm to the base 2 from the binary
     representations of floating-point values and cubically interpolating the
     logarithm in-between.

    More detailed documentation of this method can be found in:
    <a href="https://github.com/DataDog/sketches-java/">sketches-java</a>
    g__?g333333gm۶m?c                 n    t         t        |   ||       | xj                  | j                  z  c_        y )Nr    )r:   rZ   r   r   Cr<   s      r   r   z%CubicallyInterpolatedMapping.__init__   s4    *D:f 	; 	
 	DFF"r   c                     t        j                  |      \  }}d|z  dz
  }| j                  |z  | j                  z   |z  | j                  z   |z  |dz
  z   S )z*Approximates log2 using a cubic polynomialr
   r	   )r   rL   ABr\   rM   s        r   _cubic_log2_approxz/CubicallyInterpolatedMapping._cubic_log2_approx   sa     "ZZ.((lQ&VVk!DFF*k9DFFB#a<) 	)r   c                 j   t        t        j                  |            }| j                  | j                  z  d| j                  z  | j
                  z  z
  }d| j                  z  | j                  z  | j                  z  d| j                  z  | j                  z  | j
                  z  z
  d| j                  z  | j                  z  ||z
  z  z
  }t        |||z  d|z  |z  |z  z
  dz  z
  dz        }| j                  |z   ||z  z    d| j                  z  z  dz   }|d	z  }t        j                  ||d
z         S )N   r0   g      "@g      ;@   g      ?g      @r   r
   r	   )r,   r   rS   r_   r^   r\   rH   rT   )r   r&   rO   delta_0delta_1cardanosignificand_plus_onerN   s           r   _cubic_exp2_approxz/CubicallyInterpolatedMapping._cubic_exp2_approx   s2    tzz%()&&466/AJ$77$&&L466!DFF*DFFlTVV#dff,-TVVmdff$(89: 	
 7*Q[7-BW-LLQTTU

 ffw7!223sTVV|DsJ 	 (!+zz(HqL11r   c                 >    | j                  |      | j                  z  S rW   )r`   r   r%   s     r   r'   z'CubicallyInterpolatedMapping._log_gamma   s    &&u-0@0@@@r   c                 >    | j                  || j                  z        S rW   )rh   r   r%   s     r   r*   z'CubicallyInterpolatedMapping._pow_gamma   s    &&ut/?/?'?@@r   r1   )r2   r3   r4   r5   r^   r_   r\   r   r`   rh   r'   r*   rA   rB   s   @r   rZ   rZ      s4     	AAA#)2(AAr   rZ   )
__future__r   abcr   r   r   r   sixwith_metaclassr   r8   rH   rJ   rZ   r!   r   r   <module>ro      sg        
 
?N###G, ?ND7 7(!;* !;H8A: 8Ar   