ariesdevil commented on code in PR #145: URL: https://github.com/apache/datasketches-rust/pull/145#discussion_r3540416491
########## datasketches/src/theta/union.rs: ########## @@ -0,0 +1,233 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::common::ResizeFactor; +use crate::error::Error; +use crate::hash::DEFAULT_UPDATE_SEED; +use crate::theta::CompactThetaSketch; +use crate::theta::DEFAULT_LG_K; +use crate::theta::MAX_LG_K; +use crate::theta::MAX_THETA; +use crate::theta::MIN_LG_K; +use crate::theta::ThetaSketchView; +use crate::theta::hash_table::ThetaHashTable; + +/// Stateful union operator for Theta sketches. +#[derive(Debug)] +pub struct ThetaUnion { + table: ThetaHashTable, + union_theta: u64, +} + +impl ThetaUnion { + /// Create a new builder for ThetaUnion + /// + /// # Examples + /// + /// ``` + /// # use datasketches::theta::ThetaUnion; + /// let _union = ThetaUnion::builder().lg_k(12).build(); + /// ``` + pub fn builder() -> ThetaUnionBuilder { + ThetaUnionBuilder::default() + } + + /// Update this union with a given sketch. + pub fn update<S: ThetaSketchView>(&mut self, sketch: &S) -> Result<(), Error> { + if sketch.is_empty() { + return Ok(()); + } + + if self.table.seed_hash() != sketch.seed_hash() { + return Err(Error::invalid_argument(format!( + "incompatible seed hash: expected {}, got {}", + self.table.seed_hash(), + sketch.seed_hash(), + ))); + } + + self.table.set_empty(false); + self.union_theta = self.union_theta.min(sketch.theta64()); + + for hash in sketch.iter() { + if hash < self.union_theta && hash < self.table.theta() { + self.table.try_insert_hash(hash); + } else if sketch.is_ordered() { + break; + } + } + self.union_theta = self.union_theta.min(self.table.theta()); + + Ok(()) + } + + /// Return this union in compact form. + pub fn result(&self) -> CompactThetaSketch { + self.result_with_ordered(true) + } + + /// Return this union in compact form. + /// + /// If `ordered` is true, retained hash values are sorted in ascending order. + pub fn result_with_ordered(&self, ordered: bool) -> CompactThetaSketch { + let empty = self.table.is_empty(); + if empty { + return CompactThetaSketch::from_parts( + Vec::new(), + self.union_theta, + self.table.seed_hash(), + true, + true, + ); + } + + let mut theta = self.union_theta.min(self.table.theta()); + let mut entries = if self.union_theta >= self.table.theta() { + self.table.iter().collect::<Vec<_>>() + } else { + self.table + .iter() + .filter(|&hash| hash < theta) + .collect::<Vec<_>>() + }; + + let nominal_num = 1usize << self.table.lg_nom_size(); + if entries.len() > nominal_num { + let (_, kth, _) = entries.select_nth_unstable(nominal_num); + theta = *kth; + entries.truncate(nominal_num); + } + + let ordered = ordered || (entries.len() == 1 && theta == MAX_THETA); + + if ordered && entries.len() > 1 { Review Comment: seems no need check with `entries.len() > 1`, `sort_unstable()` no-op on single element array. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
