From 3c8e96593bc31b65bd27ab0c53177a8ae29b7fda Mon Sep 17 00:00:00 2001 From: Edu Meneses Date: Tue, 25 Feb 2025 17:44:07 -0500 Subject: [PATCH] add discretizer (utils) --- include/puara/utils/discretizer.h | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 include/puara/utils/discretizer.h diff --git a/include/puara/utils/discretizer.h b/include/puara/utils/discretizer.h new file mode 100644 index 0000000..56dad32 --- /dev/null +++ b/include/puara/utils/discretizer.h @@ -0,0 +1,43 @@ +#pragma once + +template +class ValueMonitor { +private: + T latestValue; + bool firstValue; // Flag to track if it's the first value + +public: + // Constructor + ValueMonitor() : firstValue(true) { + // Initialize latestValue to a default value. Important to avoid + // undefined behavior on the first comparison. Using the minimum + // value for numeric types is usually a good choice. + latestValue = std::numeric_limits::min(); + } + + // Method to update the value and check for difference + bool updateAndCheck(const T& newValue) { + if (firstValue) { + latestValue = newValue; + firstValue = false; + return true; // It's the first value, so it's technically "different" + } + + if (newValue != latestValue) { + latestValue = newValue; + return true; // Values are different + } else { + return false; // Values are the same + } + } + + // Method to get the latest value (const version) + const T& getLatestValue() const { + return latestValue; + } + + // Method to get the latest value (non-const version) + T& getLatestValue() { + return latestValue; + } +}; \ No newline at end of file