This is an automated email from the ASF dual-hosted git repository. tisonkun pushed a commit to branch codex/refine-theta-sketch-views in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git
commit 25b3f36393207a3f59c7503e72ea40f5a17e7509 Author: tison <[email protected]> AuthorDate: Sun Aug 9 16:26:41 2026 +0800 refactor: specialize sketch view iteration --- datasketches/src/thetafamily/common/a_not_b.rs | 36 ++++---- datasketches/src/thetafamily/common/hash_table.rs | 19 +++- .../src/thetafamily/common/intersection.rs | 9 +- .../src/thetafamily/common/jaccard_similarity.rs | 70 +++++++------- datasketches/src/thetafamily/common/mod.rs | 53 +---------- datasketches/src/thetafamily/common/union.rs | 9 +- datasketches/src/thetafamily/theta/a_not_b.rs | 8 +- datasketches/src/thetafamily/theta/intersection.rs | 3 +- .../src/thetafamily/theta/jaccard_similarity.rs | 18 +++- datasketches/src/thetafamily/theta/sketch.rs | 67 ++++++++++---- datasketches/src/thetafamily/theta/union.rs | 3 +- datasketches/src/thetafamily/tuple/a_not_b.rs | 8 +- datasketches/src/thetafamily/tuple/intersection.rs | 3 +- .../src/thetafamily/tuple/jaccard_similarity.rs | 18 +++- datasketches/src/thetafamily/tuple/sketch.rs | 101 ++++++++++++--------- datasketches/src/thetafamily/tuple/union.rs | 3 +- 16 files changed, 241 insertions(+), 187 deletions(-) diff --git a/datasketches/src/thetafamily/common/a_not_b.rs b/datasketches/src/thetafamily/common/a_not_b.rs index a3e7642..5be1961 100644 --- a/datasketches/src/thetafamily/common/a_not_b.rs +++ b/datasketches/src/thetafamily/common/a_not_b.rs @@ -22,7 +22,7 @@ use crate::error::ErrorKind; use crate::hash::check_seed_hash; use crate::hash::compute_seed_hash; use crate::thetacommon::RetainedEntry; -use crate::thetacommon::SketchInput; +use crate::thetacommon::SketchMetadata; use crate::thetacommon::constants::MAX_THETA; use crate::thetacommon::hash_table::CompactSketchParts; @@ -56,8 +56,10 @@ impl ANotBOperator { /// operator's seed. pub fn compute<E, A, B>( &self, - a: SketchInput<A>, - b: SketchInput<B>, + a: SketchMetadata, + a_entries: A, + b: SketchMetadata, + b_entries: B, ordered: bool, ) -> Result<CompactSketchParts<E>, Error> where @@ -68,7 +70,7 @@ impl ANotBOperator { // If A is empty the result is an (empty) copy of A. As with the union and intersection, an // empty input carries no keys, so its seed is not validated. if a.empty { - return Ok(Self::parts_from_input(a, ordered)); + return Ok(Self::parts_from_entries(a, a_entries, ordered)); } // A is non-empty, so its seed must be compatible. @@ -78,23 +80,21 @@ impl ANotBOperator { // "A is non-empty but has no retained keys" state: B's seed and theta must not influence // the result, so we return before touching them. if b.empty { - return Ok(Self::parts_from_input(a, ordered)); + return Ok(Self::parts_from_entries(a, a_entries, ordered)); } // B is non-empty, so its seed must be compatible. check_seed_hash(self.seed_hash, b.seed_hash, "B", ErrorKind::InvalidArgument)?; - let SketchInput { + let SketchMetadata { theta: a_theta, ordered: a_ordered, - entries: a_entries, .. } = a; - let SketchInput { + let SketchMetadata { theta: b_theta, ordered: b_ordered, num_retained: b_num_retained, - entries: b_entries, .. } = b; let theta = a_theta.min(b_theta); @@ -171,22 +171,26 @@ impl ANotBOperator { } /// Builds compact parts that are a copy of the view `a`. - fn parts_from_input<E, I>(a: SketchInput<I>, ordered: bool) -> CompactSketchParts<E> + fn parts_from_entries<E, I>( + metadata: SketchMetadata, + entries: I, + ordered: bool, + ) -> CompactSketchParts<E> where E: RetainedEntry, I: Iterator<Item = E>, { - let mut entries: Vec<E> = a.entries.collect(); - let out_ordered = ordered || a.ordered; - if ordered && !a.ordered && entries.len() > 1 { + let mut entries: Vec<E> = entries.collect(); + let out_ordered = ordered || metadata.ordered; + if ordered && !metadata.ordered && entries.len() > 1 { entries.sort_unstable_by_key(RetainedEntry::hash); } CompactSketchParts { entries, - theta: a.theta, - seed_hash: a.seed_hash, + theta: metadata.theta, + seed_hash: metadata.seed_hash, ordered: out_ordered, - empty: a.empty, + empty: metadata.empty, } } } diff --git a/datasketches/src/thetafamily/common/hash_table.rs b/datasketches/src/thetafamily/common/hash_table.rs index 6cc2a9c..0aac4d7 100644 --- a/datasketches/src/thetafamily/common/hash_table.rs +++ b/datasketches/src/thetafamily/common/hash_table.rs @@ -16,6 +16,7 @@ // under the License. use std::hash::Hash; +use std::slice; use crate::common::ResizeFactor; use crate::hash::MurmurHash3X64128; @@ -37,6 +38,20 @@ pub struct CompactSketchParts<E> { pub empty: bool, } +pub(crate) struct SketchHashTableIter<'a, E>(slice::Iter<'a, Option<E>>); + +impl<'a, E> Iterator for SketchHashTableIter<'a, E> { + type Item = &'a E; + + fn next(&mut self) -> Option<Self::Item> { + self.0.find_map(Option::as_ref) + } + + fn size_hint(&self) -> (usize, Option<usize>) { + (0, self.0.size_hint().1) + } +} + /// Generic hash-table mechanics shared by Theta and Tuple sketches. /// /// The entry type supplies the retained hash and any sketch-specific payload. The table owns all @@ -256,8 +271,8 @@ where } /// Get iterator over retained entries. - pub fn iter_entries(&self) -> impl Iterator<Item = &E> + '_ { - self.entries.iter().filter_map(Option::as_ref) + pub fn iter_entries(&self) -> SketchHashTableIter<'_, E> { + SketchHashTableIter(self.entries.iter()) } /// Returns the retained entries and theta as compact-sketch parts. diff --git a/datasketches/src/thetafamily/common/intersection.rs b/datasketches/src/thetafamily/common/intersection.rs index 2e7f3a8..92f789e 100644 --- a/datasketches/src/thetafamily/common/intersection.rs +++ b/datasketches/src/thetafamily/common/intersection.rs @@ -20,7 +20,7 @@ use crate::error::Error; use crate::error::ErrorKind; use crate::hash::check_seed_hash; use crate::thetacommon::RetainedEntry; -use crate::thetacommon::SketchInput; +use crate::thetacommon::SketchMetadata; use crate::thetacommon::constants::HASH_TABLE_REBUILD_THRESHOLD; use crate::thetacommon::constants::MAX_THETA; use crate::thetacommon::hash_table::CompactSketchParts; @@ -70,20 +70,19 @@ where /// /// The intersection can be viewed as starting from the "universe" set, and every update /// reduces the current set to the keys it shares with `sketch`. - pub fn update<I>(&mut self, sketch: SketchInput<I>) -> Result<(), Error> + pub fn update<I>(&mut self, metadata: SketchMetadata, entries: I) -> Result<(), Error> where I: Iterator<Item = E>, E: Clone, P: IntersectionMergePolicy<E>, { - let SketchInput { + let SketchMetadata { seed_hash, theta, empty, ordered, num_retained, - entries, - } = sketch; + } = metadata; let new_default_table = |table: &SketchHashTable<E>| { SketchHashTable::from_raw_parts( 0, diff --git a/datasketches/src/thetafamily/common/jaccard_similarity.rs b/datasketches/src/thetafamily/common/jaccard_similarity.rs index 9319d8c..8dfa314 100644 --- a/datasketches/src/thetafamily/common/jaccard_similarity.rs +++ b/datasketches/src/thetafamily/common/jaccard_similarity.rs @@ -21,7 +21,7 @@ use crate::error::ErrorKind; use crate::hash::check_seed_hash; use crate::hash::compute_seed_hash; use crate::thetacommon::RetainedEntry; -use crate::thetacommon::SketchInput; +use crate::thetacommon::SketchMetadata; use crate::thetacommon::binomial_bounds; use crate::thetacommon::constants::MAX_LG_K; use crate::thetacommon::constants::MAX_THETA; @@ -144,42 +144,42 @@ impl JaccardSimilarityOperator { pub(crate) fn compute<A, B, IA, IB>( &self, - sketch_a: A, - sketch_b: B, + metadata_a: SketchMetadata, + hashes_a: A, + metadata_b: SketchMetadata, + hashes_b: B, ) -> Result<JaccardSimilarity, Error> where - A: Fn() -> SketchInput<IA>, - B: Fn() -> SketchInput<IB>, + A: Fn() -> IA, + B: Fn() -> IB, IA: Iterator<Item = u64>, IB: Iterator<Item = u64>, { - let input_a = sketch_a(); - let input_b = sketch_b(); - if input_a.empty && input_b.empty { + if metadata_a.empty && metadata_b.empty { return Ok(JaccardSimilarity::exact(1.0)); } - if input_a.empty || input_b.empty { + if metadata_a.empty || metadata_b.empty { return Ok(JaccardSimilarity::exact(0.0)); } - let input_a_state = (input_a.num_retained, input_a.theta); - let input_b_state = (input_b.num_retained, input_b.theta); - let union = self.compute_union(input_a, input_b)?; - if !union.entries.is_empty() && identical_sets(input_a_state, input_b_state, &union) { + let sketch_a_state = (metadata_a.num_retained, metadata_a.theta); + let sketch_b_state = (metadata_b.num_retained, metadata_b.theta); + let union = self.compute_union(metadata_a, hashes_a(), metadata_b, hashes_b())?; + if !union.entries.is_empty() && identical_sets(sketch_a_state, sketch_b_state, &union) { return Ok(JaccardSimilarity::exact(1.0)); } let mut intersection = IntersectionState::new(self.seed, NoopMergePolicy); - intersection.update(sketch_a().map_entries(|hash| KeyEntry { hash }))?; - intersection.update(sketch_b().map_entries(|hash| KeyEntry { hash }))?; - intersection.update(SketchInput::new( + intersection.update(metadata_a, hashes_a().map(|hash| KeyEntry { hash }))?; + intersection.update(metadata_b, hashes_b().map(|hash| KeyEntry { hash }))?; + let union_metadata = SketchMetadata::new( union.seed_hash, union.theta, union.empty, union.ordered, union.entries.len(), - union.entries.iter().copied(), - ))?; + ); + intersection.update(union_metadata, union.entries.iter().copied())?; let intersection = intersection.result(false); JaccardSimilarity::ratio_bounds( @@ -191,48 +191,52 @@ impl JaccardSimilarityOperator { pub(crate) fn exactly_equal<A, B>( &self, - sketch_a: SketchInput<A>, - sketch_b: SketchInput<B>, + metadata_a: SketchMetadata, + hashes_a: A, + metadata_b: SketchMetadata, + hashes_b: B, ) -> Result<bool, Error> where A: Iterator<Item = u64>, B: Iterator<Item = u64>, { - if sketch_a.empty && sketch_b.empty { + if metadata_a.empty && metadata_b.empty { return Ok(true); } - if sketch_a.empty || sketch_b.empty { + if metadata_a.empty || metadata_b.empty { return Ok(false); } - let input_a_state = (sketch_a.num_retained, sketch_a.theta); - let input_b_state = (sketch_b.num_retained, sketch_b.theta); - let union = self.compute_union(sketch_a, sketch_b)?; - Ok(identical_sets(input_a_state, input_b_state, &union)) + let sketch_a_state = (metadata_a.num_retained, metadata_a.theta); + let sketch_b_state = (metadata_b.num_retained, metadata_b.theta); + let union = self.compute_union(metadata_a, hashes_a, metadata_b, hashes_b)?; + Ok(identical_sets(sketch_a_state, sketch_b_state, &union)) } fn compute_union<A, B>( &self, - sketch_a: SketchInput<A>, - sketch_b: SketchInput<B>, + metadata_a: SketchMetadata, + hashes_a: A, + metadata_b: SketchMetadata, + hashes_b: B, ) -> Result<CompactSketchParts<KeyEntry>, Error> where A: Iterator<Item = u64>, B: Iterator<Item = u64>, { let seed_hash = compute_seed_hash(self.seed); - check_seed_hash(seed_hash, sketch_a.seed_hash, "A", ErrorKind::InvalidData)?; - check_seed_hash(seed_hash, sketch_b.seed_hash, "B", ErrorKind::InvalidData)?; + check_seed_hash(seed_hash, metadata_a.seed_hash, "A", ErrorKind::InvalidData)?; + check_seed_hash(seed_hash, metadata_b.seed_hash, "B", ErrorKind::InvalidData)?; let mut union = UnionState::new( - union_lg_k(sketch_a.num_retained, sketch_b.num_retained), + union_lg_k(metadata_a.num_retained, metadata_b.num_retained), ResizeFactor::X8, 1.0, self.seed, NoopMergePolicy, ); - union.update(sketch_a.map_entries(|hash| KeyEntry { hash }))?; - union.update(sketch_b.map_entries(|hash| KeyEntry { hash }))?; + union.update(metadata_a, hashes_a.map(|hash| KeyEntry { hash }))?; + union.update(metadata_b, hashes_b.map(|hash| KeyEntry { hash }))?; Ok(union.to_compact_parts(false)) } } diff --git a/datasketches/src/thetafamily/common/mod.rs b/datasketches/src/thetafamily/common/mod.rs index 8545f43..6ef4365 100644 --- a/datasketches/src/thetafamily/common/mod.rs +++ b/datasketches/src/thetafamily/common/mod.rs @@ -32,24 +32,23 @@ pub(crate) trait RetainedEntry { fn hash(&self) -> u64; } -/// One-pass input assembled by a concrete sketch view and consumed by a shared algorithm. -pub(crate) struct SketchInput<I> { +/// Sketch properties inspected by Theta-family set operations. +#[derive(Clone, Copy, Debug)] +pub(crate) struct SketchMetadata { pub(crate) seed_hash: u16, pub(crate) theta: u64, pub(crate) empty: bool, pub(crate) ordered: bool, pub(crate) num_retained: usize, - pub(crate) entries: I, } -impl<I> SketchInput<I> { +impl SketchMetadata { pub(crate) fn new( seed_hash: u16, theta: u64, empty: bool, ordered: bool, num_retained: usize, - entries: I, ) -> Self { Self { seed_hash, @@ -57,50 +56,6 @@ impl<I> SketchInput<I> { empty, ordered, num_retained, - entries, - } - } - - pub(crate) fn map_entries<T, F>(self, f: F) -> SketchInput<impl Iterator<Item = T>> - where - I: Iterator, - F: FnMut(I::Item) -> T, - { - SketchInput::new( - self.seed_hash, - self.theta, - self.empty, - self.ordered, - self.num_retained, - self.entries.map(f), - ) - } -} - -/// Either of two concrete iterators without allocation or dynamic dispatch. -pub(crate) enum EitherIter<L, R> { - Left(L), - Right(R), -} - -impl<T, L, R> Iterator for EitherIter<L, R> -where - L: Iterator<Item = T>, - R: Iterator<Item = T>, -{ - type Item = T; - - fn next(&mut self) -> Option<Self::Item> { - match self { - Self::Left(iter) => iter.next(), - Self::Right(iter) => iter.next(), - } - } - - fn size_hint(&self) -> (usize, Option<usize>) { - match self { - Self::Left(iter) => iter.size_hint(), - Self::Right(iter) => iter.size_hint(), } } } diff --git a/datasketches/src/thetafamily/common/union.rs b/datasketches/src/thetafamily/common/union.rs index cfd1b92..f110537 100644 --- a/datasketches/src/thetafamily/common/union.rs +++ b/datasketches/src/thetafamily/common/union.rs @@ -20,7 +20,7 @@ use crate::error::Error; use crate::error::ErrorKind; use crate::hash::check_seed_hash; use crate::thetacommon::RetainedEntry; -use crate::thetacommon::SketchInput; +use crate::thetacommon::SketchMetadata; use crate::thetacommon::constants::MAX_THETA; use crate::thetacommon::hash_table::CompactSketchParts; use crate::thetacommon::hash_table::SketchHashTable; @@ -61,19 +61,18 @@ where } /// Incorporate a sketch into the union. - pub fn update<I>(&mut self, sketch: SketchInput<I>) -> Result<(), Error> + pub fn update<I>(&mut self, metadata: SketchMetadata, entries: I) -> Result<(), Error> where I: Iterator<Item = E>, P: UnionMergePolicy<E>, { - let SketchInput { + let SketchMetadata { seed_hash, theta, empty, ordered, - entries, .. - } = sketch; + } = metadata; if empty { return Ok(()); } diff --git a/datasketches/src/thetafamily/theta/a_not_b.rs b/datasketches/src/thetafamily/theta/a_not_b.rs index 544458c..aac74d2 100644 --- a/datasketches/src/thetafamily/theta/a_not_b.rs +++ b/datasketches/src/thetafamily/theta/a_not_b.rs @@ -83,9 +83,11 @@ impl ThetaANotB { b: impl Into<ThetaSketchView<'b>>, ordered: bool, ) -> Result<CompactThetaSketch, Error> { - let parts = self - .op - .compute(a.into().entries(), b.into().hashes(), ordered)?; + let a = a.into(); + let b = b.into(); + let parts = + self.op + .compute(a.metadata(), a.entries(), b.metadata(), b.hashes(), ordered)?; Ok(CompactThetaSketch::from_parts( parts .entries diff --git a/datasketches/src/thetafamily/theta/intersection.rs b/datasketches/src/thetafamily/theta/intersection.rs index 5279cdb..61aa563 100644 --- a/datasketches/src/thetafamily/theta/intersection.rs +++ b/datasketches/src/thetafamily/theta/intersection.rs @@ -59,7 +59,8 @@ impl ThetaIntersection { /// and every update can reduce the current set to leave the overlapping /// subset only. pub fn update<'a>(&mut self, sketch: impl Into<ThetaSketchView<'a>>) -> Result<(), Error> { - self.state.update(sketch.into().entries()) + let sketch = sketch.into(); + self.state.update(sketch.metadata(), sketch.entries()) } /// Returns whether this operator has received at least one update. diff --git a/datasketches/src/thetafamily/theta/jaccard_similarity.rs b/datasketches/src/thetafamily/theta/jaccard_similarity.rs index 06b3c9c..eb8f86e 100644 --- a/datasketches/src/thetafamily/theta/jaccard_similarity.rs +++ b/datasketches/src/thetafamily/theta/jaccard_similarity.rs @@ -74,8 +74,12 @@ impl ThetaJaccardSimilarity { ) -> Result<JaccardSimilarity, Error> { let sketch_a = sketch_a.into(); let sketch_b = sketch_b.into(); - self.op - .compute(move || sketch_a.hashes(), move || sketch_b.hashes()) + self.op.compute( + sketch_a.metadata(), + move || sketch_a.hashes(), + sketch_b.metadata(), + move || sketch_b.hashes(), + ) } /// Returns whether the two sketches are exactly equal. @@ -93,7 +97,13 @@ impl ThetaJaccardSimilarity { sketch_a: impl Into<ThetaSketchView<'a>>, sketch_b: impl Into<ThetaSketchView<'b>>, ) -> Result<bool, Error> { - self.op - .exactly_equal(sketch_a.into().hashes(), sketch_b.into().hashes()) + let sketch_a = sketch_a.into(); + let sketch_b = sketch_b.into(); + self.op.exactly_equal( + sketch_a.metadata(), + sketch_a.hashes(), + sketch_b.metadata(), + sketch_b.hashes(), + ) } } diff --git a/datasketches/src/thetafamily/theta/sketch.rs b/datasketches/src/thetafamily/theta/sketch.rs index c047f4c..79b1048 100644 --- a/datasketches/src/thetafamily/theta/sketch.rs +++ b/datasketches/src/thetafamily/theta/sketch.rs @@ -21,6 +21,7 @@ //! for cardinality estimation. use std::hash::Hash; +use std::slice; use crate::codec::SketchBytes; use crate::codec::SketchSlice; @@ -45,8 +46,7 @@ use crate::theta::serialization; use crate::theta::serialization::V2_PREAMBLE_EMPTY; use crate::theta::serialization::V2_PREAMBLE_ESTIMATE; use crate::theta::serialization::V2_PREAMBLE_PRECISE; -use crate::thetacommon::EitherIter; -use crate::thetacommon::SketchInput; +use crate::thetacommon::SketchMetadata; use crate::thetacommon::binomial_bounds; use crate::thetacommon::constants::DEFAULT_LG_K; use crate::thetacommon::constants::FLAGS_IS_COMPACT; @@ -56,6 +56,7 @@ use crate::thetacommon::constants::FLAGS_IS_READ_ONLY; use crate::thetacommon::constants::MAX_LG_K; use crate::thetacommon::constants::MAX_THETA; use crate::thetacommon::constants::MIN_LG_K; +use crate::thetacommon::hash_table::SketchHashTableIter; /// Read-only view for Theta sketches. /// @@ -84,7 +85,43 @@ enum ThetaSketchViewInner<'a> { Compact(&'a CompactThetaSketch), } -impl ThetaSketchView<'_> { +struct ThetaSketchIter<'a>(ThetaSketchIterState<'a>); + +enum ThetaSketchIterState<'a> { + Mutable(SketchHashTableIter<'a, ThetaEntry>), + Compact(slice::Iter<'a, u64>), +} + +impl Iterator for ThetaSketchIter<'_> { + type Item = ThetaEntry; + + fn next(&mut self) -> Option<Self::Item> { + match &mut self.0 { + ThetaSketchIterState::Mutable(iter) => iter.next().copied(), + ThetaSketchIterState::Compact(iter) => iter.next().map(|&hash| ThetaEntry::new(hash)), + } + } + + fn size_hint(&self) -> (usize, Option<usize>) { + match &self.0 { + ThetaSketchIterState::Mutable(iter) => iter.size_hint(), + ThetaSketchIterState::Compact(iter) => iter.size_hint(), + } + } +} + +impl<'a> ThetaSketchView<'a> { + fn into_iter(self) -> ThetaSketchIter<'a> { + match self.inner { + ThetaSketchViewInner::Mutable(sketch) => { + ThetaSketchIter(ThetaSketchIterState::Mutable(sketch.table.iter_entries())) + } + ThetaSketchViewInner::Compact(sketch) => { + ThetaSketchIter(ThetaSketchIterState::Compact(sketch.entries.iter())) + } + } + } + /// Returns the 16-bit seed hash. pub fn seed_hash(&self) -> u16 { match self.inner { @@ -118,11 +155,8 @@ impl ThetaSketchView<'_> { } /// Returns an iterator over retained entries. - pub fn iter(&self) -> impl Iterator<Item = ThetaEntry> + '_ { - match self.inner { - ThetaSketchViewInner::Mutable(sketch) => EitherIter::Left(sketch.iter()), - ThetaSketchViewInner::Compact(sketch) => EitherIter::Right(sketch.iter()), - } + pub fn iter(&self) -> impl Iterator<Item = ThetaEntry> + 'a { + (*self).into_iter() } /// Returns the number of retained entries. @@ -135,23 +169,22 @@ impl ThetaSketchView<'_> { } impl<'a> ThetaSketchView<'a> { - pub(crate) fn entries(self) -> SketchInput<impl Iterator<Item = ThetaEntry> + 'a> { - let entries = match self.inner { - ThetaSketchViewInner::Mutable(sketch) => EitherIter::Left(sketch.iter()), - ThetaSketchViewInner::Compact(sketch) => EitherIter::Right(sketch.iter()), - }; - SketchInput::new( + pub(crate) fn metadata(self) -> SketchMetadata { + SketchMetadata::new( self.seed_hash(), self.theta64(), self.is_empty(), self.is_ordered(), self.num_retained(), - entries, ) } - pub(crate) fn hashes(self) -> SketchInput<impl Iterator<Item = u64> + 'a> { - self.entries().map_entries(|entry| entry.hash()) + pub(crate) fn entries(self) -> impl Iterator<Item = ThetaEntry> + 'a { + self.into_iter() + } + + pub(crate) fn hashes(self) -> impl Iterator<Item = u64> + 'a { + self.into_iter().map(|entry| entry.hash()) } } diff --git a/datasketches/src/thetafamily/theta/union.rs b/datasketches/src/thetafamily/theta/union.rs index 7908c70..f7b60e5 100644 --- a/datasketches/src/thetafamily/theta/union.rs +++ b/datasketches/src/thetafamily/theta/union.rs @@ -43,7 +43,8 @@ impl UnionMergePolicy<ThetaEntry> for NoopUnionPolicy { impl ThetaUnion { /// Update this union with a given sketch. pub fn update<'a>(&mut self, sketch: impl Into<ThetaSketchView<'a>>) -> Result<(), Error> { - self.state.update(sketch.into().entries()) + let sketch = sketch.into(); + self.state.update(sketch.metadata(), sketch.entries()) } /// Return this union as a compact sketch. diff --git a/datasketches/src/thetafamily/tuple/a_not_b.rs b/datasketches/src/thetafamily/tuple/a_not_b.rs index ca0068a..de159d0 100644 --- a/datasketches/src/thetafamily/tuple/a_not_b.rs +++ b/datasketches/src/thetafamily/tuple/a_not_b.rs @@ -92,9 +92,11 @@ impl TupleANotB { S: Clone + 'a, T: 'b, { - let parts = self - .op - .compute(a.into().entries(), b.into().hashes(), ordered)?; + let a = a.into(); + let b = b.into(); + let parts = + self.op + .compute(a.metadata(), a.entries(), b.metadata(), b.hashes(), ordered)?; Ok(CompactTupleSketch::from_parts( parts.entries, parts.theta, diff --git a/datasketches/src/thetafamily/tuple/intersection.rs b/datasketches/src/thetafamily/tuple/intersection.rs index 3fbf777..73caca9 100644 --- a/datasketches/src/thetafamily/tuple/intersection.rs +++ b/datasketches/src/thetafamily/tuple/intersection.rs @@ -124,7 +124,8 @@ where where P::Summary: Clone + 'a, { - self.state.update(sketch.into().entries()) + let sketch = sketch.into(); + self.state.update(sketch.metadata(), sketch.entries()) } /// Returns whether this operator has received at least one update. diff --git a/datasketches/src/thetafamily/tuple/jaccard_similarity.rs b/datasketches/src/thetafamily/tuple/jaccard_similarity.rs index 3cd8f73..b978bf4 100644 --- a/datasketches/src/thetafamily/tuple/jaccard_similarity.rs +++ b/datasketches/src/thetafamily/tuple/jaccard_similarity.rs @@ -83,8 +83,12 @@ impl TupleJaccardSimilarity { { let sketch_a = sketch_a.into(); let sketch_b = sketch_b.into(); - self.op - .compute(move || sketch_a.hashes(), move || sketch_b.hashes()) + self.op.compute( + sketch_a.metadata(), + move || sketch_a.hashes(), + sketch_b.metadata(), + move || sketch_b.hashes(), + ) } /// Returns whether the two sketches are exactly equal. @@ -107,7 +111,13 @@ impl TupleJaccardSimilarity { S: 'a, T: 'b, { - self.op - .exactly_equal(sketch_a.into().hashes(), sketch_b.into().hashes()) + let sketch_a = sketch_a.into(); + let sketch_b = sketch_b.into(); + self.op.exactly_equal( + sketch_a.metadata(), + sketch_a.hashes(), + sketch_b.metadata(), + sketch_b.hashes(), + ) } } diff --git a/datasketches/src/thetafamily/tuple/sketch.rs b/datasketches/src/thetafamily/tuple/sketch.rs index fd8a6c1..8371155 100644 --- a/datasketches/src/thetafamily/tuple/sketch.rs +++ b/datasketches/src/thetafamily/tuple/sketch.rs @@ -23,6 +23,7 @@ //! implementations. use std::hash::Hash; +use std::slice; use crate::codec::SketchBytes; use crate::codec::SketchSlice; @@ -36,8 +37,7 @@ use crate::error::ErrorKind; use crate::hash::DEFAULT_UPDATE_SEED; use crate::hash::check_seed_hash; use crate::hash::compute_seed_hash; -use crate::thetacommon::EitherIter; -use crate::thetacommon::SketchInput; +use crate::thetacommon::SketchMetadata; use crate::thetacommon::binomial_bounds; use crate::thetacommon::constants::DEFAULT_LG_K; use crate::thetacommon::constants::FLAGS_IS_COMPACT; @@ -47,6 +47,7 @@ use crate::thetacommon::constants::FLAGS_IS_READ_ONLY; use crate::thetacommon::constants::MAX_LG_K; use crate::thetacommon::constants::MAX_THETA; use crate::thetacommon::constants::MIN_LG_K; +use crate::thetacommon::hash_table::SketchHashTableIter; use crate::tuple::hash_table::TupleEntry; use crate::tuple::hash_table::TupleHashTable; use crate::tuple::policy::SummaryPolicy; @@ -84,6 +85,35 @@ enum TupleSketchViewInner<'a, S> { Compact(&'a CompactTupleSketch<S>), } +struct TupleSketchIter<'a, S>(TupleSketchIterState<'a, S>); + +enum TupleSketchIterState<'a, S> { + Mutable(SketchHashTableIter<'a, TupleEntry<S>>), + Compact(slice::Iter<'a, TupleEntry<S>>), +} + +impl<'a, S> Iterator for TupleSketchIter<'a, S> { + type Item = (u64, &'a S); + + fn next(&mut self) -> Option<Self::Item> { + match &mut self.0 { + TupleSketchIterState::Mutable(iter) => { + iter.next().map(|entry| (entry.hash(), entry.summary())) + } + TupleSketchIterState::Compact(iter) => { + iter.next().map(|entry| (entry.hash(), entry.summary())) + } + } + } + + fn size_hint(&self) -> (usize, Option<usize>) { + match &self.0 { + TupleSketchIterState::Mutable(iter) => iter.size_hint(), + TupleSketchIterState::Compact(iter) => iter.size_hint(), + } + } +} + impl<S> Clone for TupleSketchView<'_, S> { fn clone(&self) -> Self { *self @@ -100,7 +130,18 @@ impl<S> Clone for TupleSketchViewInner<'_, S> { impl<S> Copy for TupleSketchViewInner<'_, S> {} -impl<S> TupleSketchView<'_, S> { +impl<'a, S> TupleSketchView<'a, S> { + fn into_iter(self) -> TupleSketchIter<'a, S> { + match self.inner { + TupleSketchViewInner::Mutable(table) => { + TupleSketchIter(TupleSketchIterState::Mutable(table.iter_entries())) + } + TupleSketchViewInner::Compact(sketch) => { + TupleSketchIter(TupleSketchIterState::Compact(sketch.entries.iter())) + } + } + } + /// Returns the 16-bit seed hash. pub fn seed_hash(&self) -> u16 { match self.inner { @@ -134,15 +175,12 @@ impl<S> TupleSketchView<'_, S> { } /// Returns an iterator over retained hashes and borrowed summaries. - pub fn iter(&self) -> impl Iterator<Item = (u64, &S)> + '_ { - match self.inner { - TupleSketchViewInner::Mutable(table) => EitherIter::Left(table.iter()), - TupleSketchViewInner::Compact(sketch) => EitherIter::Right(sketch.iter()), - } + pub fn iter(&self) -> impl Iterator<Item = (u64, &'a S)> + 'a { + (*self).into_iter() } /// Returns an iterator over retained hash keys. - pub fn iter_hashes(&self) -> impl Iterator<Item = u64> + '_ { + pub fn iter_hashes(&self) -> impl Iterator<Item = u64> + 'a { self.iter().map(|(hash, _)| hash) } @@ -156,50 +194,29 @@ impl<S> TupleSketchView<'_, S> { } impl<'a, S> TupleSketchView<'a, S> { - pub(crate) fn entries(self) -> SketchInput<impl Iterator<Item = TupleEntry<S>> + 'a> - where - S: Clone + 'a, - { - let entries = match self.inner { - TupleSketchViewInner::Mutable(table) => EitherIter::Left( - table - .iter() - .map(|(hash, summary)| TupleEntry::new(hash, summary.clone())), - ), - TupleSketchViewInner::Compact(sketch) => { - EitherIter::Right(sketch.entries.iter().cloned()) - } - }; - SketchInput::new( + pub(crate) fn metadata(self) -> SketchMetadata { + SketchMetadata::new( self.seed_hash(), self.theta64(), self.is_empty(), self.is_ordered(), self.num_retained(), - entries, ) } - pub(crate) fn hashes(self) -> SketchInput<impl Iterator<Item = u64> + 'a> + pub(crate) fn entries(self) -> impl Iterator<Item = TupleEntry<S>> + 'a + where + S: Clone + 'a, + { + self.into_iter() + .map(|(hash, summary)| TupleEntry::new(hash, summary.clone())) + } + + pub(crate) fn hashes(self) -> impl Iterator<Item = u64> + 'a where S: 'a, { - let entries = match self.inner { - TupleSketchViewInner::Mutable(table) => { - EitherIter::Left(table.iter().map(|(hash, _)| hash)) - } - TupleSketchViewInner::Compact(sketch) => { - EitherIter::Right(sketch.entries.iter().map(TupleEntry::hash)) - } - }; - SketchInput::new( - self.seed_hash(), - self.theta64(), - self.is_empty(), - self.is_ordered(), - self.num_retained(), - entries, - ) + self.into_iter().map(|(hash, _)| hash) } } diff --git a/datasketches/src/thetafamily/tuple/union.rs b/datasketches/src/thetafamily/tuple/union.rs index 8f8f733..fabf444 100644 --- a/datasketches/src/thetafamily/tuple/union.rs +++ b/datasketches/src/thetafamily/tuple/union.rs @@ -93,7 +93,8 @@ where where P::Summary: Clone + 'a, { - self.state.update(sketch.into().entries()) + let sketch = sketch.into(); + self.state.update(sketch.metadata(), sketch.entries()) } /// Returns the union as a [`CompactTupleSketch`]. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
