
    d?                        d Z ddlZddlZd Zd Zd Zd Zd Zd.d
Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Z ej        dd          dz  Z ej        dd          Zddgddgddgddgddgdd gdd!gd"d#gdd$ge ege egd%Z eed&          r. ej        ej                    ej        ej                  ged&<    eed'          r. ej        ej                    ej        ej                  ged'<    eed(          r. ej        ej                    ej        ej                  ged(<   eZeZd/d)Zd* Zd+ Z d, Z!d- Z"dS )0z/Utilities to be used mainly by the Index class.    Nc                 6    ddt          j        |           z  z  S )z6Return the fitted chunksize (a float value) for nrows.@      )mathlog10)nrowss    /lib/python3.11/site-packages/tables/idxutils.py	csformular
      s     4:e$$$$$    c                 (    | dk     rd} n| dk    rd} | S )zDProtection against creating too small or too large chunks or slices.i l    J) )expectedrowss    r	   limit_err      s,     e			r   c                     t          |           } t          t          j        |                     }d|z  }t          t	          |                    S )z0Get the optimum chunksize based on expectedrows.
   )r   intr   r   r
   )r   zoner   s      r	   computechunksizer   '   sG     L))Ltz,''((DHEy   r   c                     t          |           } t          |           }t          |           }t          ||dz  z            }||z  |z  }|dz  }|dk    rd}|S )z@Get the optimum slicesize based on expectedrows and memorylevel.r      i   @)r   r
   r   r   )r   memlevelcs	chunksizesss        r	   computeslicesizer   0   sm     L))L	<	 	 B ..I	R(A+		B
	/Y	&B!GB 
EzzIr   c                 `    | |z  dz   }|dk    rd}t          ||z            }||z  dz   |z  }|S )zCalculate the optimum number of superblocks made from compounds blocks.

    This is useful for computing the sizes of both blocks and
    superblocks (using the PyTables terminology for blocks in indexes).

       i   )r   )r   compoundsizelowercompoundsizenlowerblockssizes        r	   computeblocksizer"   J   sR     !$55:Le </00D \!Q&,6DKr      r   c                    t          |           }t          | |          }|P|j        j        d         |j        j        d         z  }||j        j        z  |k    r|dz  }||j        j        z  |k    |dk    rt          |||          \  }}nP|dk    rt          |||          \  }}n5|dk    rt          |||          \  }}n|dk    rt          |||          \  }}t          | ||          }t          | ||          }	|	|||f}
|
S )a  Calculate the HDF5 chunk size for index and sorted arrays.

    The logic to do that is based purely in experiments playing with
    different chunksizes and compression flag. It is obvious that using
    big chunks optimizes the I/O speed, but if they are too large, the
    uncompressor takes too much time. This might (should) be further
    optimized by doing more experiments.

    NBUFFER_TIMESIO_BUFFER_SIZEr   r   r      )r   r   _v_fileparamsdtypeitemsizeccs_ultralight	ccs_light
ccs_mediumccs_fullr"   )r   optlevelindsizer   noder   	slicesizemaxsize	blocksizesuperblocksizesizess              r	   calc_chunksizer8   ]   s7    !..I x88I ,%n58KL\8]]4:..'99!QI 4:..'99 !||-h	9MM	99	A(9iHH	99	A)(IyII	99	A')YGG	9 !y)DDI%lIyIINY	9=ELr   c                 ~    | dv r|dz  }|| |z  z  }n(| dv r	|| dz
  z  }n| dv r	|| dz
  z  }n| dk    r|| dz
  z  }||fS ):Correct the slicesize and the chunksize based on optlevel.r   r   r   r      r      r   r#      r'   	   r   r0   r   r3   s      r	   r,   r,      s     9a	X	))			Y		X\!			Y		X\!			QX\!	ir   c                 \    | dv r|dz  }n| dv rn| dv r|dz  }n| dk    r
|dz  }|dz  }||fS r:   r;   r   r<   r?   rA   r   rB   s      r	   r-   r-      f     9a			Y			Y		a			Q 	a	Q	ir   c                 \    | dv r|dz  }n| dv rn| dv r|dz  }n| dk    r
|dz  }|dz  }||fS rD   r   rB   s      r	   r.   r.      rE   r   c                 \    | dv r|dz  }n| dv rn| dv r|dz  }n| dk    r
|dz  }|dz  }||fS rD   r   rB   s      r	   r/   r/      rE   r   c                     |dk    rt          | |          S |dk    rt          | |          S |dk    rt          | |          S dS )zCompute the optimizations to be done.

    The calculation is based on the number of blocks, optlevel and
    indexing mode.

    r   r   r'   N)	col_light
col_mediumcol_full)nblocksr0   r1   s      r	   calcoptlevelsrM      sV     !||(+++	A'8,,,	A*** 
r   c                     d\  }}}}d|cxk     rdk    rn nd}n,d|cxk     rdk    r	n nd\  }}nd|cxk     rdk    r	n nd\  }}}||||fS )	z7Compute the optimizations to be done for light indexes.FFFFr   r=   Tr#   TTrA   )TTTr   rL   r0   	optmedian	optstartsoptstopsoptfulls         r	   rI   rI      s     /;+Iy(G8q			
X									+	99	
X									);&	9hi722r   c                    d\  }}}}| dk    r<d|cxk     rdk    rn nd}nad|cxk     rdk    r	n nd\  }}nKd|cxk     rdk    rn n;d}n8d|cxk     rdk    rn nd}n%d|cxk     rdk    rn nd	}nd|cxk     rdk    rn nd}||||fS )
z8Compute the optimizations to be done for medium indexes.rO   r   r   r=   Tr#   rP   rA   r   r   rQ   s         r	   rJ   rJ      $    /;+Iy(G !||x1IIQ#/ IyyQGx1GGQGGQGi722r   c                    d\  }}}}| dk    r<d|cxk     rdk    rn nd}nad|cxk     rdk    r	n nd\  }}nKd|cxk     rdk    rn n;d}n8d|cxk     rdk    rn nd}n%d|cxk     rdk    rn nd	}nd|cxk     rdk    rn nd}||||fS )
z6Compute the optimizations to be done for full indexes.rO   r   r   r=   Tr#   rP   rA   r   r   rQ   s         r	   rK   rK      rW   r   c                     g dg dg dg dg}ddddd	}|||                   |         }||k    rd}|||z  k    rd}| d
k    r|dk    sJ |S )z:Compute the reduction level based on indsize and optlevel.)
r'   r'   r'   r'   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   r   r   r   r=   )r   r   r   r'   r'   r   )r1   r0   r3   r   rlevelsisizesrlevels          r	   get_reduction_levelr]     s     	'&&&&&&&&&&&	G qQ1%%FVG_%h/FI&&&!|| {{{{Mr   g      ?i  r      r   i      i i  i  i   il    l         l    l    )boolint8uint8int16uint16int32uint32int64uint64float32float64float16float96float128c                     |dv sJ | j         dk    r|dk     rd|z  S d|z  S 	 t          | j                 |dk             S # t          $ r t	          d| j        z            w xY w)z<Return a superior limit for maximum representable data type.r   Sr          zType %s is not supported)kindinfinitymapnameKeyError	TypeError)r*   r+   signs      r	   inftyper{   o  s     8zS!88X%%X%%A5:&tqy11 A A A2UZ?@@@As	   ? "A!c                    |dv sJ |t          |           z
  }|dk    r| d|z  z  } d | D             }|                                 d}|dk    rx|d|z  k    rd                    |          S |D ]V}t          |          dk     r7t	          t          |          dz                                 d	          ||<    nd||<   |dz  }Wnw|d|z  k    rd                    |          S |D ]V}t          |          dk    r7t	          t          |          dz
                                d	          ||<    nd||<   |dz  }W|                                 d                    |          S )
MReturn the next representable neighbor of x in the appropriate
    direction.rp   r   rs   c                 .    g | ]}t          |g          S r   )bytes).0is     r	   
<listcomp>z%string_next_after.<locals>.<listcomp>  s     ###AUA3ZZ###r   rt   r   r`   r   ascii)lenreversejoinordchrencode)x	directionr+   padsizexlistr   xchars          r	   string_next_afterr     s         QG{{	Ww $####E	MMOOO	A1}}Gh&&&88E??" 	 	E5zzD  s5zzA~..55g>>a"aFAAGh&&&88E??" 	 	E5zzD  s5zzA~..55g>>a"aFAA	MMOOO88E??r   c                    |dv sJ |dk     r?t          | t                    r| dz
  S t          t          j        | | dz
                      S t          | t                    r| dz   S t          t          j        | | dz                       dz   S )r}   rp   r   r   )
isinstancer   np	nextafterr   r   r+   s      r	   int_type_next_afterr     s          1}}a 	/q5L r|Aq1u--...a 	3q5L r|Aq1u--..22r   c                 "    |dv sJ |dk     rdS dS )r}   rp   r   FTr   r   s      r	   bool_type_next_afterr     s*          1}}utr   c                    |dv sJ |j         dk    s%t          |           t          t          t          fv sJ |dk    r| S |j         dk    rt          | ||          S |j         dv rt          | ||          S |j         dv rt          | ||          S |j         dk    r6|dk     rt          j	        | | dz
            S t          j	        | | dz             S t          d|z            )	r}   )rq   r   r   rr   r   )b)r   ufr   z!data type ``%s`` is not supported)ru   typera   floatr   r   r   r   r   r   ry   )r   r   r*   r+   s       r	   r   r     s    ####:QD%+= = = = =A~~zS Ix888zU#Ay(;;;	z	!	!"1i:::	s		q==<1q5)))<1q5))) 7%?
@
@@r   )r#   r   r   N)r   )#__doc__r   numpyr   r
   r   r   r   r"   r8   r,   r-   r.   r/   rM   rI   rJ   rK   r]   ldexpinfinity	infinityfrv   hasattrrl   infrm   rn   infinityMap	infinityFr{   r   r   r   r   r   r   r	   <module>r      s   5 5     	% 	% 	%  ! ! !  4  &# # # #L          "     "     "+ + + 3 3 33 3 303 3 30  Z 4:c4  1$DJsC  	 FH]i )ni )ni )n
I&	8$  72y 2)rz"&111(bj002K	
72y 2)rz"&111(bj002K	
72z 4 +BF 3 33*r{26224K
 	
A A A A & & &R3 3 3*
 
 
"A "A "A "A "Ar   