tisonkun commented on code in PR #45: URL: https://github.com/apache/datasketches-rust/pull/45#discussion_r2652322572
########## datasketches/src/theta/sketch.rs: ########## @@ -0,0 +1,208 @@ +// 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. + +//! Theta sketch implementation +//! +//! This module provides ThetaSketch (mutable) and CompactThetaSketch (immutable) +//! for cardinality estimation. + +use std::hash::Hash; + +use crate::theta::hash_table::DEFAULT_LG_K; +use crate::theta::hash_table::DEFAULT_SEED; +use crate::theta::hash_table::MAX_LG_K; +use crate::theta::hash_table::MAX_THETA; +use crate::theta::hash_table::MIN_LG_K; +use crate::theta::hash_table::ResizeFactor; +use crate::theta::hash_table::ThetaHashTable; + +/// Mutable theta sketch for building from input data +#[derive(Debug)] +pub struct ThetaSketch { + table: ThetaHashTable, +} + +impl ThetaSketch { + /// Create a new builder for ThetaSketch + pub fn builder() -> ThetaSketchBuilder { + ThetaSketchBuilder::default() + } + + /// Update the sketch with a hashable value + pub fn update<T: Hash>(&mut self, value: T) { + let hash = self.table.hash_and_screen(value); + if hash != 0 { + self.table.try_insert(hash); + } + } + + /// Update the sketch with a f64 value + pub fn update_f64(&mut self, value: f64) { + // Canonicalize double for compatibility with Java + let canonical = canonical_double(value); + self.update(canonical); + } + + /// Update the sketch with a f32 value + pub fn update_f32(&mut self, value: f32) { + self.update_f64(value as f64); + } + + /// Get cardinality estimate + pub fn get_estimate(&self) -> f64 { Review Comment: Nope. The style check should be resolved by running `cargo x lint --fix` locally. It's a formatting issue where there are extra blank lines. -- 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]
