probabilistic_machines 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b860b9aeb15b8f6ebdb6786cee404032b10637f4656f278be153268551d6b7e9
4
- data.tar.gz: 40586b97c8222825a608cbc96867710470fcdf1d12827b0235395e3c30dfebc8
3
+ metadata.gz: 6996defbe02f1e8db52383c761976ff45227c0b4f9a896f6794f2d80c09d175d
4
+ data.tar.gz: cd13df510bd689ce4bdc91694d3046c956559cd0e26d4d04376b874a5c7ba8a6
5
5
  SHA512:
6
- metadata.gz: 14a1850c6dfa15b0f0986fe9acf5bb5d57244710254300cf964027fc266a90823db2b7a1be9897fdd66989ad1203fbc330a0c1d7b38ea209510349457c35ad9d
7
- data.tar.gz: 9909b1ca8777def59303ef7e67a02a8996a6be92070247f1f2be5423c8c5c91641f8f5409b5e5f51cb39234dc72cd175fd749b979406f8843e0ecf0e20ffa3a2
6
+ metadata.gz: 955d1f7561fa4173e5f98c9b21bb26aad294f427cc0a6c65bf970bbd27bccb4c649981b145ad29ebcada2e3e7a145f73444ba0e7def03ad6106fdc8ee5c16638
7
+ data.tar.gz: 49da31e77f8a5581bd1b4f6986470228529ac30ae7e6de499e324c67485581e20150123d13feef0e92c7dc14ffe70cf0ec5970cfd18270ad51523f2bd9a8dc86
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.1.4](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.1.3...probabilistic_machines/v0.1.4) (2026-11-06)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * keep test modules out of the packaged crate ([e3c1a1f](https://github.com/seuros/probabilistic_machines/commit/e3c1a1f7376dbada6efe782c1d4d264273483432))
9
+ * share frame encode/decode helpers across sketches ([067d23d](https://github.com/seuros/probabilistic_machines/commit/067d23db55b8b9068fdef445a7f6dee483961a82))
10
+
3
11
  ## [0.1.3](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.1.2...probabilistic_machines/v0.1.3) (2026-11-01)
4
12
 
5
13
 
data/Cargo.lock CHANGED
@@ -158,11 +158,11 @@ dependencies = [
158
158
 
159
159
  [[package]]
160
160
  name = "probabilistic-machines"
161
- version = "0.3.0"
161
+ version = "0.3.1"
162
162
 
163
163
  [[package]]
164
164
  name = "probabilistic_machines_native"
165
- version = "0.1.2"
165
+ version = "0.1.3"
166
166
  dependencies = [
167
167
  "bytes",
168
168
  "magnus",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "probabilistic-machines"
3
- version = "0.3.0"
3
+ version = "0.3.1"
4
4
  edition.workspace = true
5
5
  rust-version.workspace = true
6
6
  authors = ["Abdelkader Boudih <oss@seuros.com>"]
@@ -10,7 +10,7 @@ repository = "https://github.com/seuros/probabilistic_machines"
10
10
  keywords = ["hyperloglog", "t-digest", "count-min", "sketch", "no-std"]
11
11
  categories = ["no-std", "algorithms", "data-structures"]
12
12
  readme = "README.md"
13
-
13
+ exclude = ["src/**/tests.rs", "tests/"]
14
14
  [lib]
15
15
  name = "probabilistic_machines"
16
16
  crate-type = ["lib"]
@@ -1,7 +1,7 @@
1
1
  //! Frequency estimation for a stream nobody wants to store.
2
2
 
3
3
  use crate::hash::{hash64, mix64, u32_low_u8, u64_low_u16, usize_low_u8};
4
- use crate::{CodecError, TAG_COUNTMIN, read8};
4
+ use crate::{CodecError, TAG_COUNTMIN, encode_frame, open_frame, read8};
5
5
 
6
6
  const HEADER_LEN: usize = 12;
7
7
  const COUNTER_LEN: usize = 4;
@@ -132,20 +132,14 @@ impl<const W: usize, const D: usize> CountMinSketch<W, D> {
132
132
  /// [`CodecError::Truncated`] when `out` is shorter than
133
133
  /// [`ENCODED_LEN`](Self::ENCODED_LEN).
134
134
  pub fn encode(&self, out: &mut [u8]) -> Result<usize, CodecError> {
135
- let Some(frame) = out.get_mut(..Self::ENCODED_LEN) else {
136
- return Err(CodecError::Truncated);
137
- };
138
- self.write_frame(frame);
139
- Ok(Self::ENCODED_LEN)
135
+ encode_frame(out, Self::ENCODED_LEN, |frame| self.write_frame(frame))
140
136
  }
141
137
 
142
138
  /// The sketch's frame, exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
143
139
  #[cfg(feature = "alloc")]
144
140
  #[must_use]
145
141
  pub fn encoded(&self) -> alloc::boxed::Box<[u8]> {
146
- let mut frame = alloc::vec![0_u8; Self::ENCODED_LEN].into_boxed_slice();
147
- self.write_frame(&mut frame);
148
- frame
142
+ crate::encoded_frame(Self::ENCODED_LEN, |frame| self.write_frame(frame))
149
143
  }
150
144
 
151
145
  /// Fills a frame of exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
@@ -185,12 +179,7 @@ impl<const W: usize, const D: usize> CountMinSketch<W, D> {
185
179
  ///
186
180
  /// The same as [`decode`](Self::decode).
187
181
  pub fn decode_into(&mut self, bytes: &[u8]) -> Result<(), CodecError> {
188
- let Some(frame) = bytes.get(..Self::ENCODED_LEN) else {
189
- return Err(CodecError::Truncated);
190
- };
191
- if frame[0] != TAG_COUNTMIN {
192
- return Err(CodecError::Tag);
193
- }
182
+ let frame = open_frame(bytes, Self::ENCODED_LEN, TAG_COUNTMIN)?;
194
183
  if frame[1] != u32_low_u8(W.trailing_zeros()) || frame[2] != usize_low_u8(D) {
195
184
  return Err(CodecError::Parameters);
196
185
  }
@@ -1,7 +1,7 @@
1
1
  //! Approximate membership with deletions, which a Bloom filter cannot do.
2
2
 
3
3
  use crate::hash::{hash64, mix64, u32_low_u8, u64_low_u8, u64_low_u16};
4
- use crate::{CodecError, TAG_CUCKOO};
4
+ use crate::{CodecError, TAG_CUCKOO, encode_frame, open_frame};
5
5
 
6
6
  const SLOTS: usize = 4;
7
7
  const EMPTY: u8 = 0;
@@ -167,20 +167,14 @@ impl<const B: usize> CuckooFilter<B> {
167
167
  /// [`CodecError::Truncated`] when `out` is shorter than
168
168
  /// [`ENCODED_LEN`](Self::ENCODED_LEN).
169
169
  pub fn encode(&self, out: &mut [u8]) -> Result<usize, CodecError> {
170
- let Some(frame) = out.get_mut(..Self::ENCODED_LEN) else {
171
- return Err(CodecError::Truncated);
172
- };
173
- self.write_frame(frame);
174
- Ok(Self::ENCODED_LEN)
170
+ encode_frame(out, Self::ENCODED_LEN, |frame| self.write_frame(frame))
175
171
  }
176
172
 
177
173
  /// The filter's frame, exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
178
174
  #[cfg(feature = "alloc")]
179
175
  #[must_use]
180
176
  pub fn encoded(&self) -> alloc::boxed::Box<[u8]> {
181
- let mut frame = alloc::vec![0_u8; Self::ENCODED_LEN].into_boxed_slice();
182
- self.write_frame(&mut frame);
183
- frame
177
+ crate::encoded_frame(Self::ENCODED_LEN, |frame| self.write_frame(frame))
184
178
  }
185
179
 
186
180
  /// Fills a frame of exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
@@ -216,12 +210,7 @@ impl<const B: usize> CuckooFilter<B> {
216
210
  ///
217
211
  /// The same as [`decode`](Self::decode).
218
212
  pub fn decode_into(&mut self, bytes: &[u8]) -> Result<(), CodecError> {
219
- let Some(frame) = bytes.get(..Self::ENCODED_LEN) else {
220
- return Err(CodecError::Truncated);
221
- };
222
- if frame[0] != TAG_CUCKOO {
223
- return Err(CodecError::Tag);
224
- }
213
+ let frame = open_frame(bytes, Self::ENCODED_LEN, TAG_CUCKOO)?;
225
214
  if frame[1] != u32_low_u8(B.trailing_zeros()) {
226
215
  return Err(CodecError::Parameters);
227
216
  }
@@ -1,7 +1,7 @@
1
1
  //! Distinct-count estimation in a fixed array of registers.
2
2
 
3
3
  use crate::hash::{hash64, u32_low_u8, u64_low_u16};
4
- use crate::{CodecError, TAG_HYPERLOGLOG};
4
+ use crate::{CodecError, TAG_HYPERLOGLOG, encode_frame, open_frame};
5
5
 
6
6
  /// Odd terms of `2 * atanh(t) = ln((1 + t) / (1 - t))`, in Horner order.
7
7
  const ATANH_COEFFICIENTS: [f64; 5] = [2.0, 2.0 / 3.0, 0.4, 2.0 / 7.0, 2.0 / 9.0];
@@ -145,20 +145,14 @@ impl<const M: usize> HyperLogLog<M> {
145
145
  /// [`CodecError::Truncated`] when `out` is shorter than
146
146
  /// [`ENCODED_LEN`](Self::ENCODED_LEN).
147
147
  pub fn encode(&self, out: &mut [u8]) -> Result<usize, CodecError> {
148
- let Some(frame) = out.get_mut(..Self::ENCODED_LEN) else {
149
- return Err(CodecError::Truncated);
150
- };
151
- self.write_frame(frame);
152
- Ok(Self::ENCODED_LEN)
148
+ encode_frame(out, Self::ENCODED_LEN, |frame| self.write_frame(frame))
153
149
  }
154
150
 
155
151
  /// The sketch's frame, exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
156
152
  #[cfg(feature = "alloc")]
157
153
  #[must_use]
158
154
  pub fn encoded(&self) -> alloc::boxed::Box<[u8]> {
159
- let mut frame = alloc::vec![0_u8; Self::ENCODED_LEN].into_boxed_slice();
160
- self.write_frame(&mut frame);
161
- frame
155
+ crate::encoded_frame(Self::ENCODED_LEN, |frame| self.write_frame(frame))
162
156
  }
163
157
 
164
158
  /// Fills a frame of exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
@@ -191,12 +185,7 @@ impl<const M: usize> HyperLogLog<M> {
191
185
  ///
192
186
  /// The same as [`decode`](Self::decode).
193
187
  pub fn decode_into(&mut self, bytes: &[u8]) -> Result<(), CodecError> {
194
- let Some(frame) = bytes.get(..Self::ENCODED_LEN) else {
195
- return Err(CodecError::Truncated);
196
- };
197
- if frame[0] != TAG_HYPERLOGLOG {
198
- return Err(CodecError::Tag);
199
- }
188
+ let frame = open_frame(bytes, Self::ENCODED_LEN, TAG_HYPERLOGLOG)?;
200
189
  if frame[1] != u32_low_u8(Self::PRECISION) {
201
190
  return Err(CodecError::Parameters);
202
191
  }
@@ -63,3 +63,37 @@ pub(crate) const fn read8(bytes: &[u8]) -> [u8; 8] {
63
63
  buffer.copy_from_slice(bytes);
64
64
  buffer
65
65
  }
66
+
67
+ /// Fills the first `len` bytes of `out` through `write`, for the fixed-size
68
+ /// frames every sketch encodes to.
69
+ pub(crate) fn encode_frame(
70
+ out: &mut [u8],
71
+ len: usize,
72
+ write: impl FnOnce(&mut [u8]),
73
+ ) -> Result<usize, CodecError> {
74
+ let Some(frame) = out.get_mut(..len) else {
75
+ return Err(CodecError::Truncated);
76
+ };
77
+ write(frame);
78
+ Ok(len)
79
+ }
80
+
81
+ /// A freshly allocated `len`-byte frame filled through `write`.
82
+ #[cfg(feature = "alloc")]
83
+ pub(crate) fn encoded_frame(len: usize, write: impl FnOnce(&mut [u8])) -> alloc::boxed::Box<[u8]> {
84
+ let mut frame = alloc::vec![0_u8; len].into_boxed_slice();
85
+ write(&mut frame);
86
+ frame
87
+ }
88
+
89
+ /// The first `len` bytes of `bytes`, once they are known to be a frame tagged
90
+ /// `tag`.
91
+ pub(crate) fn open_frame(bytes: &[u8], len: usize, tag: u8) -> Result<&[u8], CodecError> {
92
+ let Some(frame) = bytes.get(..len) else {
93
+ return Err(CodecError::Truncated);
94
+ };
95
+ if frame[0] != tag {
96
+ return Err(CodecError::Tag);
97
+ }
98
+ Ok(frame)
99
+ }
@@ -2,7 +2,7 @@
2
2
 
3
3
  use alloc::vec::Vec;
4
4
 
5
- use crate::{CodecError, TAG_TDIGEST, read8};
5
+ use crate::{CodecError, TAG_TDIGEST, open_frame, read8};
6
6
 
7
7
  const HEADER_LEN: usize = 31;
8
8
  const CENTROID_LEN: usize = 16;
@@ -323,12 +323,7 @@ impl TDigest {
323
323
  /// [`CodecError::Parameters`] when the clusters, the total weight and the
324
324
  /// extremes contradict each other.
325
325
  pub fn decode(bytes: &[u8]) -> Result<Self, CodecError> {
326
- let Some(header) = bytes.get(..HEADER_LEN) else {
327
- return Err(CodecError::Truncated);
328
- };
329
- if header[0] != TAG_TDIGEST {
330
- return Err(CodecError::Tag);
331
- }
326
+ let header = open_frame(bytes, HEADER_LEN, TAG_TDIGEST)?;
332
327
  let counted = u32::from_le_bytes([header[3], header[4], header[5], header[6]]);
333
328
  let Ok(clusters) = usize::try_from(counted) else {
334
329
  return Err(CodecError::Truncated);
@@ -1,7 +1,7 @@
1
1
  //! Which keys dominate a stream, in `K` counters instead of one per key.
2
2
 
3
3
  use crate::hash::usize_low_u16;
4
- use crate::{CodecError, TAG_TOPK, read8};
4
+ use crate::{CodecError, TAG_TOPK, encode_frame, open_frame, read8};
5
5
 
6
6
  const HEADER_LEN: usize = 13;
7
7
  const RECORD_LEN: usize = 24;
@@ -197,20 +197,14 @@ impl<const K: usize> HeavyHitters<u64, K> {
197
197
  /// [`CodecError::Truncated`] when `out` is shorter than
198
198
  /// [`ENCODED_LEN`](Self::ENCODED_LEN).
199
199
  pub fn encode(&self, out: &mut [u8]) -> Result<usize, CodecError> {
200
- let Some(frame) = out.get_mut(..Self::ENCODED_LEN) else {
201
- return Err(CodecError::Truncated);
202
- };
203
- self.write_frame(frame);
204
- Ok(Self::ENCODED_LEN)
200
+ encode_frame(out, Self::ENCODED_LEN, |frame| self.write_frame(frame))
205
201
  }
206
202
 
207
203
  /// The table's frame, exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
208
204
  #[cfg(feature = "alloc")]
209
205
  #[must_use]
210
206
  pub fn encoded(&self) -> alloc::boxed::Box<[u8]> {
211
- let mut frame = alloc::vec![0_u8; Self::ENCODED_LEN].into_boxed_slice();
212
- self.write_frame(&mut frame);
213
- frame
207
+ crate::encoded_frame(Self::ENCODED_LEN, |frame| self.write_frame(frame))
214
208
  }
215
209
 
216
210
  /// Fills a frame of exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
@@ -252,12 +246,7 @@ impl<const K: usize> HeavyHitters<u64, K> {
252
246
  ///
253
247
  /// The same as [`decode`](Self::decode).
254
248
  pub fn decode_into(&mut self, bytes: &[u8]) -> Result<(), CodecError> {
255
- let Some(frame) = bytes.get(..Self::ENCODED_LEN) else {
256
- return Err(CodecError::Truncated);
257
- };
258
- if frame[0] != TAG_TOPK {
259
- return Err(CodecError::Tag);
260
- }
249
+ let frame = open_frame(bytes, Self::ENCODED_LEN, TAG_TOPK)?;
261
250
  if frame[1..3] != Self::COUNTERS {
262
251
  return Err(CodecError::Parameters);
263
252
  }
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "probabilistic_machines_native"
3
- version = "0.1.2"
3
+ version = "0.1.3"
4
4
  edition.workspace = true
5
5
  rust-version.workspace = true
6
6
  authors = ["Abdelkader Boudih <oss@seuros.com>"]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ProbabilisticMachines
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: probabilistic_machines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih