tisonkun commented on code in PR #71:
URL: https://github.com/apache/datasketches-rust/pull/71#discussion_r2731052311


##########
datasketches/src/countmin/value.rs:
##########
@@ -0,0 +1,187 @@
+// 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::error::Error;
+
+mod private {
+    // Sealed trait to prevent external implementations of CountMinValue.
+    pub trait Sealed {}
+}
+
+/// Value type supported in a Count-Min sketch.
+pub trait CountMinValue: private::Sealed + Copy + Ord {
+    /// Zero value for counters and weights.
+    const ZERO: Self;
+
+    /// One value for unit updates.
+    const ONE: Self;
+
+    /// Maximum representable value for initializing minima.
+    const MAX: Self;
+
+    /// Performs the + operation.
+    fn add(self, other: Self) -> Self;
+
+    /// Computes the absolute value of `self`.
+    fn abs(self) -> Self;
+
+    /// Converts into `f64`.
+    fn to_f64(self) -> f64;
+
+    /// Converts from `f64` by truncating toward zero.
+    fn from_f64(value: f64) -> Self;
+
+    /// Returns the raw transmutation in little-endian 8 bytes.
+    fn to_bytes(self) -> [u8; 8];
+
+    /// Constructs from the raw transmutation in little-endian 8 bytes.
+    fn try_from_bytes(bytes: [u8; 8]) -> Result<Self, Error>;
+}
+
+/// Unsigned value type supported in a Count-Min sketch.
+pub trait UnsignedCountMinValue: CountMinValue {
+    /// Divides the value by two, truncating toward zero.
+    fn halve(self) -> Self;
+
+    /// Multiplies the value by decay and truncates back into `T`.
+    fn decay(self, decay: f64) -> Self;
+}
+
+macro_rules! impl_signed {
+    ($name:ty, $min:expr, $max:expr) => {
+        impl private::Sealed for $name {}
+
+        impl CountMinValue for $name {
+            const ZERO: Self = 0;
+            const ONE: Self = 1;
+            const MAX: Self = $max;
+
+            #[inline(always)]
+            fn add(self, other: Self) -> Self {
+                self + other
+            }
+
+            #[inline(always)]
+            fn abs(self) -> Self {
+                if self >= 0 { self } else { -self }
+            }

Review Comment:
   Change to align with C++/Java impl - no wrapping semantic.



-- 
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]

Reply via email to