Browse Source

Made in-place minimum and maximum template functions.

David Piuva 5 years ago
parent
commit
c69d36a9e5
1 changed files with 18 additions and 0 deletions
  1. 18 0
      Source/DFPSR/math/scalar.h

+ 18 - 0
Source/DFPSR/math/scalar.h

@@ -96,6 +96,8 @@ inline uint16_t absDiff(uint16_t a, uint16_t b) {
 	return (uint16_t)result;
 	return (uint16_t)result;
 }
 }
 
 
+// Allowing compilation on older C++ versions
+// Only use for trivial types if you want to avoid cloning and destruction
 template <typename T>
 template <typename T>
 inline void swap(T &a, T &b) {
 inline void swap(T &a, T &b) {
 	T temp = a;
 	T temp = a;
@@ -103,6 +105,22 @@ inline void swap(T &a, T &b) {
 	b = temp;
 	b = temp;
 }
 }
 
 
+// More compact than min(a, b) when reading from the target
+template <typename T>
+inline void replaceWithSmaller(T& target, T source) {
+	if (source < target) {
+		target = source;
+	}
+}
+
+// More compact than max(a, b) when reading from the target
+template <typename T>
+inline void replaceWithLarger(T& target, T source) {
+	if (source > target) {
+		target = source;
+	}
+}
+
 // True iff high and low bytes are equal
 // True iff high and low bytes are equal
 //   Equivalent to value % 257 == 0 because A + B * 256 = A * 257 when A = B.
 //   Equivalent to value % 257 == 0 because A + B * 256 = A * 257 when A = B.
 inline bool isUniformByteU16(uint16_t value) {
 inline bool isUniformByteU16(uint16_t value) {