tisonkun commented on code in PR #23: URL: https://github.com/apache/datasketches-rust/pull/23#discussion_r2622647025
########## src/tdigest/sketch.rs: ########## @@ -0,0 +1,453 @@ +// 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 std::convert::identity; + +use crate::tdigest::{Centroid, TDigest}; + +const BUFFER_MULTIPLIER: usize = 4; + +impl Default for TDigest { + fn default() -> Self { + TDigest::new(Self::DEFAULT_K) + } +} + +impl TDigest { + /// The default value of K if one is not specified. + pub const DEFAULT_K: u16 = 200; + + /// Creates a tdigest instance with the given value of k. + /// + /// # Panics + /// + /// If k is less than 10 + pub fn new(k: u16) -> Self { + Self::make( + k, + false, + f64::INFINITY, + f64::NEG_INFINITY, + vec![], + 0, + vec![], + ) + } + + // for deserialization + pub(super) fn make( + k: u16, + reverse_merge: bool, + min: f64, + max: f64, + mut centroids: Vec<Centroid>, + centroids_weight: u64, + mut buffer: Vec<f64>, + ) -> Self { + assert!(k >= 10, "k must be at least 10"); + + let fudge = if k < 30 { 30 } else { 10 }; + let centroids_capacity = (k as usize * 2) + fudge; + + centroids.reserve(centroids_capacity); + buffer.reserve(centroids_capacity * BUFFER_MULTIPLIER); + + TDigest { + k, + reverse_merge, + min, + max, + centroids, + centroids_weight, + centroids_capacity, + buffer, + } + } + + /// Update this TDigest with the given value (`NaN` values are ignored). + pub fn update(&mut self, value: f64) { + if value.is_nan() { + return; + } + + if self.buffer.len() == self.centroids_capacity * BUFFER_MULTIPLIER { + self.compress(); + } + + self.buffer.push(value); + self.min = self.min.min(value); + self.max = self.max.max(value); + } + + /// Returns parameter k (compression) that was used to configure this TDigest. + pub fn k(&self) -> u16 { + self.k + } + + /// Returns true if TDigest has not seen any data. + pub fn is_empty(&self) -> bool { + self.centroids.is_empty() && self.buffer.is_empty() + } + + /// Returns minimum value seen by TDigest; `None` if TDigest is empty. + pub fn min_value(&self) -> Option<f64> { + if self.is_empty() { + None + } else { + Some(self.min) + } + } + + /// Returns maximum value seen by TDigest; `None` if TDigest is empty. + pub fn max_value(&self) -> Option<f64> { + if self.is_empty() { + None + } else { + Some(self.max) + } + } + + /// Returns total weight. + pub fn total_weight(&self) -> u64 { + self.centroids_weight + (self.buffer.len() as u64) + } + + /// Merge the given t-Digest into this one + pub fn merge(&mut self, other: &TDigest) { + if other.is_empty() { + return; + } + + let mut tmp = Vec::with_capacity( + self.centroids.len() + self.buffer.len() + other.centroids.len() + other.buffer.len(), Review Comment: Similarly, the related elements are pushed just below and in the first line of `do_merge`. But I do consider if we can move `buffer.extend(std::mem::take(&mut self.centroids));` to the caller so that we keep `tmp`'s init in the same place. We already copy `self.buffer` here anyway. -- 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]
