You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by ps...@apache.org on 2026/07/12 09:12:01 UTC
(datasketches-rust) branch main updated: chore: internal code tidy (#149)
This is an automated email from the ASF dual-hosted git repository.
PsiACE 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 eefe065 chore: internal code tidy (#149)
eefe065 is described below
commit eefe0656449620f2d23bcb8391f6aaf8a3e99396
Author: tison <wa...@gmail.com>
AuthorDate: Sun Jul 12 17:11:56 2026 +0800
chore: internal code tidy (#149)
Signed-off-by: tison <wa...@gmail.com>
---
datasketches/src/countmin/sketch.rs | 16 +++---
datasketches/src/countmin/value.rs | 108 ++++++++++++------------------------
xtask/src/main.rs | 26 +++++++--
3 files changed, 64 insertions(+), 86 deletions(-)
diff --git a/datasketches/src/countmin/sketch.rs b/datasketches/src/countmin/sketch.rs
index 48b7a3f..b898763 100644
--- a/datasketches/src/countmin/sketch.rs
+++ b/datasketches/src/countmin/sketch.rs
@@ -179,12 +179,12 @@ impl<T: CountMinValue> CountMinSketch<T> {
return;
}
let abs_weight = weight.abs();
- self.total_weight = self.total_weight.add(abs_weight);
+ self.total_weight = self.total_weight + abs_weight;
let num_buckets = self.num_buckets as usize;
for (row, seed) in self.hash_seeds.iter().enumerate() {
let bucket = self.bucket_index(&item, *seed);
let index = row * num_buckets + bucket;
- self.counts[index] = self.counts[index].add(weight);
+ self.counts[index] = self.counts[index] + weight;
}
}
@@ -220,8 +220,8 @@ impl<T: CountMinValue> CountMinSketch<T> {
/// Returns the upper bound on the true frequency of the given item.
pub fn upper_bound<I: Hash>(&self, item: I) -> T {
let estimate = self.estimate(item);
- let error = T::from_f64(self.relative_error() * self.total_weight.to_f64());
- estimate.add(error)
+ let error = self.total_weight.scale(self.relative_error());
+ estimate + error
}
/// Merges another sketch into this one.
@@ -253,9 +253,9 @@ impl<T: CountMinValue> CountMinSketch<T> {
assert_eq!(self.counts.len(), other.counts.len());
let counts_len = self.counts.len();
for i in 0..counts_len {
- self.counts[i] = self.counts[i].add(other.counts[i]);
+ self.counts[i] = self.counts[i] + other.counts[i];
}
- self.total_weight = self.total_weight.add(other.total_weight);
+ self.total_weight = self.total_weight + other.total_weight;
}
/// Serializes this sketch into the DataSketches Count-Min format.
@@ -452,9 +452,9 @@ impl<T: UnsignedCountMinValue> CountMinSketch<T> {
pub fn decay(&mut self, decay: f64) {
assert!(decay > 0.0 && decay <= 1.0, "decay must be within (0, 1]");
for c in &mut self.counts {
- *c = c.decay(decay)
+ *c = c.scale(decay)
}
- self.total_weight = self.total_weight.decay(decay);
+ self.total_weight = self.total_weight.scale(decay);
}
}
diff --git a/datasketches/src/countmin/value.rs b/datasketches/src/countmin/value.rs
index f236440..70fd542 100644
--- a/datasketches/src/countmin/value.rs
+++ b/datasketches/src/countmin/value.rs
@@ -17,77 +17,52 @@
use crate::error::Error;
-mod private {
- // Sealed trait to prevent external implementations of CountMinValue.
- pub trait Sealed {}
-}
-
-/// Value type supported in a Count-Min sketch.
-pub trait CountMinValue: private::Sealed + Copy + Ord {
- /// Zero value for counters and weights.
- const ZERO: Self;
-
- /// One value for unit updates.
- const ONE: Self;
-
- /// Maximum representable value for initializing minima.
- const MAX: Self;
-
- /// Performs the + operation.
- fn add(self, other: Self) -> Self;
-
- /// Computes the absolute value of `self`.
- fn abs(self) -> Self;
+/// Marker trait identifying the value types supported by
+/// [`CountMinSketch`](crate::countmin::CountMinSketch).
+pub trait CountMinValue: private::CountMinValue {}
- /// Converts into `f64`.
- fn to_f64(self) -> f64;
+/// Marker trait identifying the unsigned value types supported by
+/// [`CountMinSketch`](crate::countmin::CountMinSketch).
+///
+/// This marker enables unsigned-only operations such as halving and decay.
+pub trait UnsignedCountMinValue: CountMinValue + private::UnsignedCountMinValue {}
- /// Converts from `f64` by truncating toward zero.
- fn from_f64(value: f64) -> Self;
+mod private {
+ use std::ops::Add;
- /// Returns the raw transmutation in little-endian 8 bytes.
- fn to_bytes(self) -> [u8; 8];
+ use crate::error::Error;
- /// Constructs from the raw transmutation in little-endian 8 bytes.
- fn try_from_bytes(bytes: [u8; 8]) -> Result<Self, Error>;
-}
+ pub trait CountMinValue: Sized + Copy + Ord + Add<Output = Self> {
+ const ZERO: Self;
+ const ONE: Self;
+ const MAX: Self;
-/// Unsigned value type supported in a Count-Min sketch.
-pub trait UnsignedCountMinValue: CountMinValue {
- /// Divides the value by two, truncating toward zero.
- fn halve(self) -> Self;
+ fn abs(self) -> Self;
+ fn scale(self, factor: f64) -> Self;
+ fn to_bytes(self) -> [u8; 8];
+ fn try_from_bytes(bytes: [u8; 8]) -> Result<Self, Error>;
+ }
- /// Multiplies the value by decay and truncates back into `T`.
- fn decay(self, decay: f64) -> Self;
+ pub trait UnsignedCountMinValue: CountMinValue {
+ fn halve(self) -> Self;
+ }
}
macro_rules! impl_signed {
($name:ty, $min:expr, $max:expr) => {
- impl private::Sealed for $name {}
-
- impl CountMinValue for $name {
+ impl private::CountMinValue for $name {
const ZERO: Self = 0;
const ONE: Self = 1;
const MAX: Self = $max;
- #[inline(always)]
- fn add(self, other: Self) -> Self {
- self + other
- }
-
#[inline(always)]
fn abs(self) -> Self {
if self >= 0 { self } else { -self }
}
#[inline(always)]
- fn to_f64(self) -> f64 {
- self as f64
- }
-
- #[inline(always)]
- fn from_f64(value: f64) -> Self {
- value.trunc() as $name
+ fn scale(self, factor: f64) -> Self {
+ ((self as f64) * factor).trunc() as $name
}
#[inline(always)]
@@ -109,6 +84,8 @@ macro_rules! impl_signed {
Ok(value as $name)
}
}
+
+ impl CountMinValue for $name {}
};
}
@@ -119,31 +96,19 @@ impl_signed!(i64, i64::MIN, i64::MAX);
macro_rules! impl_unsigned {
($name:ty, $max:expr) => {
- impl private::Sealed for $name {}
-
- impl CountMinValue for $name {
+ impl private::CountMinValue for $name {
const ZERO: Self = 0;
const ONE: Self = 1;
const MAX: Self = $max;
- #[inline(always)]
- fn add(self, other: Self) -> Self {
- self + other
- }
-
#[inline(always)]
fn abs(self) -> Self {
self
}
#[inline(always)]
- fn to_f64(self) -> f64 {
- self as f64
- }
-
- #[inline(always)]
- fn from_f64(value: f64) -> Self {
- value.trunc() as $name
+ fn scale(self, factor: f64) -> Self {
+ ((self as f64) * factor).trunc() as $name
}
#[inline(always)]
@@ -166,18 +131,15 @@ macro_rules! impl_unsigned {
}
}
- impl UnsignedCountMinValue for $name {
+ impl private::UnsignedCountMinValue for $name {
#[inline(always)]
fn halve(self) -> Self {
self >> 1
}
-
- #[inline(always)]
- fn decay(self, decay: f64) -> Self {
- let value = self.to_f64() * decay;
- Self::from_f64(value)
- }
}
+
+ impl CountMinValue for $name {}
+ impl UnsignedCountMinValue for $name {}
};
}
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index 260f426..da33cd0 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -31,6 +31,7 @@ impl Command {
fn run(self) {
match self.sub {
SubCommand::Check(cmd) => cmd.run(),
+ SubCommand::Docs(cmd) => cmd.run(),
SubCommand::Lint(cmd) => cmd.run(),
SubCommand::Test(cmd) => cmd.run(),
}
@@ -41,7 +42,9 @@ impl Command {
enum SubCommand {
#[clap(about = "Check datasketches under the feature matrix.")]
Check(CommandCheck),
- #[clap(about = "Run format and clippy checks.")]
+ #[clap(about = "Generate documentation and open for preview")]
+ Docs(CommandDocs),
+ #[clap(about = "Run linter checks.")]
Lint(CommandLint),
#[clap(about = "Run unit tests.")]
Test(CommandTest),
@@ -63,6 +66,16 @@ impl CommandCheck {
}
}
+#[derive(Parser)]
+#[clap(name = "docs")]
+struct CommandDocs {}
+
+impl CommandDocs {
+ fn run(self) {
+ run_command(make_docs_cmd(true));
+ }
+}
+
#[derive(Parser)]
struct CommandTest {
#[arg(long, help = "Run tests serially and do not capture output.")]
@@ -112,7 +125,7 @@ impl CommandLint {
fn run(self) {
run_command(make_clippy_cmd(self.fix));
run_command(make_format_cmd(self.fix));
- run_command(make_docs_cmd());
+ run_command(make_docs_cmd(false));
run_command(make_taplo_cmd(self.fix));
run_command(make_typos_cmd());
run_command(make_hawkeye_cmd(self.fix));
@@ -202,17 +215,20 @@ fn make_clippy_cmd(fix: bool) -> StdCommand {
cmd
}
-fn make_docs_cmd() -> StdCommand {
+fn make_docs_cmd(open: bool) -> StdCommand {
let mut cmd = find_command("cargo");
- cmd.env("RUSTFLAGS", "--cfg docsrs");
- cmd.env("RUSTDOCFLAGS", "-D warnings");
+ cmd.env("RUSTDOCFLAGS", "--cfg docsrs -D warnings");
cmd.args([
"+nightly",
"doc",
"--package",
"datasketches",
"--all-features",
+ "--no-deps",
]);
+ if open {
+ cmd.args(["--open"]);
+ }
cmd
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org