From c7bf17933016e6cd0f8d9bb54ae7a662911058d4 Mon Sep 17 00:00:00 2001
From: Sven Gothel
Date: Wed, 2 Dec 2020 22:05:32 +0100
Subject: basic_algos.hpp: Add constexpr noexcept template functions: sign(T),
abs(T) and digits10(T)
Since above functions utilze nsize_t and snsize_t,
their definition is also moved to basic_algos.hpp from basic_types.hpp.
---
include/jau/basic_algos.hpp | 97 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 97 insertions(+)
(limited to 'include/jau/basic_algos.hpp')
diff --git a/include/jau/basic_algos.hpp b/include/jau/basic_algos.hpp
index 1121eda..39603ab 100644
--- a/include/jau/basic_algos.hpp
+++ b/include/jau/basic_algos.hpp
@@ -27,8 +27,105 @@
#define JAU_BASIC_ALGOS_HPP_
#include
+#include
namespace jau {
+ /**
+ // *************************************************
+ // *************************************************
+ // *************************************************
+ */
+
+ /**
+ * Natural 'size_t' alternative using 'unsigned int' as its natural sized type.
+ *
+ * The leading 'n' stands for natural.
+ *
+ *
+ * This is a compromise to indicate intend,
+ * but to avoid handling a multiple sized 'size_t' footprint where not desired.
+ *
+ */
+ typedef unsigned int nsize_t;
+
+ /**
+ * Natural 'ssize_t' alternative using 'signed int' as its natural sized type.
+ *
+ * The leading 'n' stands for natural.
+ *
+ *
+ * This is a compromise to indicate intend,
+ * but to avoid handling a multiple sized 'ssize_t' footprint where not desired.
+ *
+ */
+ typedef signed int snsize_t;
+
+ /**
+ // *************************************************
+ // *************************************************
+ // *************************************************
+ */
+
+ /**
+ * Returns the value of the sign function.
+ *
+ * -1 for x < 0
+ * 0 for x = 0
+ * 1 for x > 0
+ *
+ * Implementation is type safe.
+ * @tparam T an integral number type
+ * @param x the integral number
+ * @return function result
+ */
+ template
+ constexpr snsize_t sign(T x) noexcept
+ {
+ return (T(0) < x) - (x < T(0));
+ }
+
+ /**
+ * Returns the absolute value of an integral number
+ * @tparam T an integral number type
+ * @param x the integral number
+ * @return function result
+ */
+ template
+ constexpr T abs(T x) noexcept
+ {
+ return sign(x) < 0 ? -x : x;
+ }
+
+ /**
+ * Returns the number of decimal digits of the given integral value number using std::log10().
+ *
+ * x < 0: 2 + floor( log10( -x ) ) )
+ * x = 0: 1
+ * x > 0: 1 + (int)( log10( x ) ) )
+ *
+ * @tparam T an integral integer type
+ * @param x the integral integer
+ * @return digit count
+ */
+ template
+ constexpr nsize_t digits10(T x) noexcept
+ {
+ const snsize_t x_sign = jau::sign(x);
+ if( x_sign == 0 ) {
+ return 1;
+ }
+ if( x_sign < 0 ) {
+ return 2 + static_cast( std::floor( std::log10( -x ) ) );
+ } else {
+ return 1 + static_cast( std::log10( x ) );
+ }
+ }
+
+ /**
+ // *************************************************
+ // *************************************************
+ // *************************************************
+ */
/**
* Custom for_each template, using indices instead of iterators,
--
cgit v1.2.3