This is an automated email from the ASF dual-hosted git repository.
tison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git
The following commit(s) were added to refs/heads/main by this push:
new e053fae chore: define static lookup table as static variables (#83)
e053fae is described below
commit e053fae5178f0ecd3f2fb45323f0f3eb98c81b30
Author: tison <[email protected]>
AuthorDate: Tue Feb 3 22:55:46 2026 +0800
chore: define static lookup table as static variables (#83)
Signed-off-by: tison <[email protected]>
---
datasketches/src/common/binomial_bounds.rs | 6 +++---
datasketches/src/common/inv_pow2_table.rs | 2 +-
datasketches/src/common/num_std_dev.rs | 2 +-
datasketches/src/cpc/estimator.rs | 10 +++++-----
datasketches/src/cpc/kxp_byte_lookup.rs | 2 +-
datasketches/src/cpc/sketch.rs | 6 +++---
datasketches/src/hll/aux_map.rs | 2 +-
datasketches/src/hll/composite_interpolation.rs | 19 +++++++++----------
datasketches/src/hll/estimator.rs | 8 ++++----
datasketches/src/hll/harmonic_numbers.rs | 2 +-
10 files changed, 29 insertions(+), 30 deletions(-)
diff --git a/datasketches/src/common/binomial_bounds.rs
b/datasketches/src/common/binomial_bounds.rs
index 682aed7..f0b90c7 100644
--- a/datasketches/src/common/binomial_bounds.rs
+++ b/datasketches/src/common/binomial_bounds.rs
@@ -20,7 +20,7 @@ use crate::error::Error;
#[rustfmt::skip]
#[allow(clippy::excessive_precision)]
-const LB_EQUIV_TABLE: [f64; 363] = [
+static LB_EQUIV_TABLE: [f64; 363] = [
1.0, 2.0, 3.0, // fake values for k = 0
0.78733703534118149, 3.14426768537558132, 13.56789685109913535, // k = 1
0.94091379266077979, 2.64699271711145911, 6.29302733018320737, // k = 2
@@ -146,7 +146,7 @@ const LB_EQUIV_TABLE: [f64; 363] = [
#[rustfmt::skip]
#[allow(clippy::excessive_precision)]
-const UB_EQUIV_TABLE: [f64; 363] = [
+static UB_EQUIV_TABLE: [f64; 363] = [
1.0, 2.0, 3.0, // fake values for k = 0
0.99067760836669549, 1.75460517119302040, 2.48055626001627161, // k = 1
0.99270518097577565, 1.78855957509907171, 2.53863835259832626, // k = 2
@@ -604,7 +604,7 @@ mod tests {
}
#[allow(clippy::excessive_precision)]
- const STD: [[f64; 5]; 9] = [
+ static STD: [[f64; 5]; 9] = [
// max_num_samples=20, ci=1,2,3, min_p=1e-3
[
7.083330682531043e+04,
diff --git a/datasketches/src/common/inv_pow2_table.rs
b/datasketches/src/common/inv_pow2_table.rs
index 580a2fc..dd19f1e 100644
--- a/datasketches/src/common/inv_pow2_table.rs
+++ b/datasketches/src/common/inv_pow2_table.rs
@@ -17,7 +17,7 @@
#[rustfmt::skip]
#[allow(clippy::excessive_precision)]
-pub(crate) const INVERSE_POWERS_OF_2: [f64; 256] = [
+pub(crate) static INVERSE_POWERS_OF_2: [f64; 256] = [
1.0, 0.5, 0.25, 0.125,
0.0625, 0.03125, 0.015625, 0.0078125,
0.00390625, 0.001953125, 0.0009765625, 0.00048828125,
diff --git a/datasketches/src/common/num_std_dev.rs
b/datasketches/src/common/num_std_dev.rs
index 92cbddd..441a07b 100644
--- a/datasketches/src/common/num_std_dev.rs
+++ b/datasketches/src/common/num_std_dev.rs
@@ -21,7 +21,7 @@
//! upper and lower bounds for sketch estimates.
#[allow(clippy::excessive_precision)]
-const DELTA_OF_NUM_STD_DEVS: [f64; 4] = [
+static DELTA_OF_NUM_STD_DEVS: [f64; 4] = [
0.5000000000000000000, // = 0.5 (1 + erf(0))
0.1586553191586026479, // = 0.5 (1 + erf((-1/sqrt(2))))
0.0227502618904135701, // = 0.5 (1 + erf((-2/sqrt(2))))
diff --git a/datasketches/src/cpc/estimator.rs
b/datasketches/src/cpc/estimator.rs
index 6d37e6e..81a2252 100644
--- a/datasketches/src/cpc/estimator.rs
+++ b/datasketches/src/cpc/estimator.rs
@@ -21,7 +21,7 @@ use crate::common::NumStdDev;
const ICON_ERROR_CONSTANT: f64 = LN_2;
-const ICON_LOW_SIDE_DATA: [u16; 33] = [
+static ICON_LOW_SIDE_DATA: [u16; 33] = [
//1, 2, 3, kappa
// lgK num trials
6037, 5720, 5328, // 4 1000000
@@ -37,7 +37,7 @@ const ICON_LOW_SIDE_DATA: [u16; 33] = [
6919, 6897, 6842, // 14 1000297
];
-const ICON_HIGH_SIDE_DATA: [u16; 33] = [
+static ICON_HIGH_SIDE_DATA: [u16; 33] = [
//1, 2, 3, kappa
// lgK num trials
8031, 8559, 9309, // 4 1000000
@@ -56,7 +56,7 @@ const ICON_HIGH_SIDE_DATA: [u16; 33] = [
#[allow(clippy::excessive_precision)]
const HIP_ERROR_CONSTANT: f64 = 0.588705011257737332; // (LN_2 / 2.0).sqrt()
-const HIP_LOW_SIDE_DATA: [u16; 33] = [
+static HIP_LOW_SIDE_DATA: [u16; 33] = [
//1, 2, 3, kappa
// lgK num trials
5871, 5247, 4826, // 4 1000000
@@ -72,7 +72,7 @@ const HIP_LOW_SIDE_DATA: [u16; 33] = [
5881, 5853, 5842, // 14 1000297
];
-const HIP_HIGH_SIDE_DATA: [u16; 33] = [
+static HIP_HIGH_SIDE_DATA: [u16; 33] = [
//1, 2, 3, kappa
// lgK num trials
5855, 6688, 7391, // 4 1000000
@@ -208,7 +208,7 @@ const ICON_TABLE_SIZE: usize =
#[rustfmt::skip]
#[allow(clippy::excessive_precision)]
-const ICON_POLYNOMIAL_COEFFICIENTS: [f64; ICON_TABLE_SIZE] = [
+static ICON_POLYNOMIAL_COEFFICIENTS: [f64; ICON_TABLE_SIZE] = [
// log K = 4
0.9895027971889700513, 0.3319496644645180128, 0.1242818722715769986,
-0.03324149686026930256, -0.2985637298081619817,
1.366555923595830002, -4.705499366260569971, 11.61506432505530029,
-21.11254986175579873, 28.89421695078809904,
diff --git a/datasketches/src/cpc/kxp_byte_lookup.rs
b/datasketches/src/cpc/kxp_byte_lookup.rs
index 25fddf6..a4118f2 100644
--- a/datasketches/src/cpc/kxp_byte_lookup.rs
+++ b/datasketches/src/cpc/kxp_byte_lookup.rs
@@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.
-pub(super) const KXP_BYTE_TABLE: [f64; 256] = [
+pub(super) static KXP_BYTE_TABLE: [f64; 256] = [
0.99609375, 0.49609375, 0.74609375, 0.24609375, 0.87109375, 0.37109375,
0.62109375, 0.12109375,
0.93359375, 0.43359375, 0.68359375, 0.18359375, 0.80859375, 0.30859375,
0.55859375, 0.05859375,
0.96484375, 0.46484375, 0.71484375, 0.21484375, 0.83984375, 0.33984375,
0.58984375, 0.08984375,
diff --git a/datasketches/src/cpc/sketch.rs b/datasketches/src/cpc/sketch.rs
index 6af8bfc..d8df959 100644
--- a/datasketches/src/cpc/sketch.rs
+++ b/datasketches/src/cpc/sketch.rs
@@ -449,8 +449,10 @@ impl CpcSketch {
// that were equally spaced over values of the quantity C/K between
3.0 and 8.0.
// This table does not include the worst-case space for the preamble,
which is added
// by the function.
+ const MAX_PREAMBLE_SIZE_BYTES: usize = 40;
const EMPIRICAL_SIZE_MAX_LGK: usize = 19;
- const EMPIRICAL_MAX_SIZE_BYTES: [usize; 16] = [
+ const EMPIRICAL_MAX_SIZE_FACTOR: f64 = 0.6; // 0.6 = 4.8 / 8.0
+ static EMPIRICAL_MAX_SIZE_BYTES: [usize; 16] = [
24, // lg_k = 4
36, // lg_k = 5
56, // lg_k = 6
@@ -468,8 +470,6 @@ impl CpcSketch {
157516, // lg_k = 18
314656, // lg_k = 19
];
- const EMPIRICAL_MAX_SIZE_FACTOR: f64 = 0.6; // 0.6 = 4.8 / 8.0
- const MAX_PREAMBLE_SIZE_BYTES: usize = 40;
if lg_k <= EMPIRICAL_SIZE_MAX_LGK {
return EMPIRICAL_MAX_SIZE_BYTES[lg_k - MIN_LG_K] +
MAX_PREAMBLE_SIZE_BYTES;
diff --git a/datasketches/src/hll/aux_map.rs b/datasketches/src/hll/aux_map.rs
index f3e5b5d..c4e8ca9 100644
--- a/datasketches/src/hll/aux_map.rs
+++ b/datasketches/src/hll/aux_map.rs
@@ -79,7 +79,7 @@ impl PartialEq for AuxMap {
/// This determines the initial size of the auxiliary hash map
/// based on the sketch size.
fn lg_aux_arr_ints(lg_config_k: u8) -> u8 {
- const LG_AUX_ARR_INTS: &[u8] = &[
+ static LG_AUX_ARR_INTS: &[u8] = &[
0, 2, 2, 2, 2, 2, 2, 3, 3, 3, // 0-9
4, 4, 5, 5, 6, 7, 8, 9, 10, 11, // 10-19
12, 13, 14, 15, 16, 17, 18, // 20-26
diff --git a/datasketches/src/hll/composite_interpolation.rs
b/datasketches/src/hll/composite_interpolation.rs
index 30f8b2c..da501a2 100644
--- a/datasketches/src/hll/composite_interpolation.rs
+++ b/datasketches/src/hll/composite_interpolation.rs
@@ -21,22 +21,22 @@
//! cardinality estimates when the HLL sketch is in out-of-order mode
//! (after deserialization or merging).
//!
-//! NOTE: Currently this module contains tables for common lg_k values (4-12).
-//! The full C++ implementation has tables for lg_k 4-21. Additional tables
-//! can be added from
datasketches-cpp/hll/include/CompositeInterpolationXTable-internal.hpp
+//! Currently, this module contains tables for common lg_k values (4-12). The
full C++
+//! implementation has tables for lg_k 4-21. Additional tables can be found at:
+//!
https://github.com/apache/datasketches-cpp/blob/5a055521/hll/include/CompositeInterpolationXTable-internal.hpp
const NUM_X_VALUES: usize = 257;
/// Y stride values for each lg_k (index 0 = lg_k 4, index 17 = lg_k 21)
-const Y_STRIDES: [u32; 18] = [
+static Y_STRIDES: [u32; 18] = [
1, 2, 3, 5, 10, 20, 40, 80, 160, 320, 640, 1280, 2560, 5120, 10240, 20480,
40960, 81920,
];
-/// Get Y stride for a given lg_k
+/// Get Y stride for a given lg_k.
///
/// # Panics
///
-/// If lg_k is not in range [4, 21]
+/// Panics if lg_k is not in range `[4, 21]`.
pub fn get_y_stride(lg_k: u8) -> u32 {
if !(4..=21).contains(&lg_k) {
panic!("lg_k must be in range [4, 21], got: {}", lg_k);
@@ -49,13 +49,13 @@ pub const fn get_x_arr_length() -> usize {
NUM_X_VALUES
}
-/// Get X array for a given lg_k
+/// Get X array for a given lg_k.
///
/// Returns a reference to the pre-computed X values for cubic interpolation.
///
/// # Panics
///
-/// If lg_k is not in range [4, 21]
+/// Panics if lg_k is not in range `[4, 21]`.
pub fn get_x_arr(lg_k: u8) -> &'static [f64; NUM_X_VALUES] {
if !(4..=21).contains(&lg_k) {
panic!("lg_k must be in range [4, 21], got: {}", lg_k);
@@ -63,8 +63,7 @@ pub fn get_x_arr(lg_k: u8) -> &'static [f64; NUM_X_VALUES] {
&ARRAYS[(lg_k - 4) as usize]
}
-// Arrays extracted from
datasketches-cpp/hll/include/CompositeInterpolationXTable-internal.hpp
-
+/// This is extracted from
https://github.com/apache/datasketches-cpp/blob/5a055521/hll/include/CompositeInterpolationXTable-internal.hpp#L54
static ARRAYS: [[f64; NUM_X_VALUES]; 18] = [
[
10.767999803534,
diff --git a/datasketches/src/hll/estimator.rs
b/datasketches/src/hll/estimator.rs
index df3d45e..a9bd63b 100644
--- a/datasketches/src/hll/estimator.rs
+++ b/datasketches/src/hll/estimator.rs
@@ -375,7 +375,7 @@ fn get_rel_err(lg_config_k: u8, upper_bound: bool, ooo:
bool, num_std_dev: NumSt
/// HIP (in-order) Lower Bound errors for lg_k 4-12, std_dev 1-3
/// Q(.84134), Q(.97725), Q(.99865) quantiles
-const HIP_LB: [f64; 27] = [
+static HIP_LB: [f64; 27] = [
0.207316195,
0.502865572,
0.882303765, //4
@@ -407,7 +407,7 @@ const HIP_LB: [f64; 27] = [
/// HIP (in-order) Upper Bound errors for lg_k 4-12, std_dev 1-3
/// Q(.15866), Q(.02275), Q(.00135) quantiles
-const HIP_UB: [f64; 27] = [
+static HIP_UB: [f64; 27] = [
-0.207805347,
-0.355574279,
-0.475535095, //4
@@ -439,7 +439,7 @@ const HIP_UB: [f64; 27] = [
/// Non-HIP (out-of-order) Lower Bound errors for lg_k 4-12, std_dev 1-3
/// Q(.84134), Q(.97725), Q(.99865) quantiles
-const NON_HIP_LB: [f64; 27] = [
+static NON_HIP_LB: [f64; 27] = [
0.254409839,
0.682266712,
1.304022158, //4
@@ -471,7 +471,7 @@ const NON_HIP_LB: [f64; 27] = [
/// Non-HIP (out-of-order) Upper Bound errors for lg_k 4-12, std_dev 1-3
/// Q(.15866), Q(.02275), Q(.00135) quantiles
-const NON_HIP_UB: [f64; 27] = [
+static NON_HIP_UB: [f64; 27] = [
-0.256980172,
-0.411905944,
-0.52651057, // lg_k=4
diff --git a/datasketches/src/hll/harmonic_numbers.rs
b/datasketches/src/hll/harmonic_numbers.rs
index 4ee5894..cdc4161 100644
--- a/datasketches/src/hll/harmonic_numbers.rs
+++ b/datasketches/src/hll/harmonic_numbers.rs
@@ -24,7 +24,7 @@ const NUM_EXACT: usize = 25;
const EULER_MASCHERONI: f64 = 0.577_215_664_901_532_9;
/// Exact harmonic numbers H(n) for n = 0..24
-const EXACT_HARMONIC: [f64; NUM_EXACT] = [
+static EXACT_HARMONIC: [f64; NUM_EXACT] = [
0.0, // H(0)
1.0, // H(1)
1.5, // H(2)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]