From 8058fac200e7184b3c337fa008e6cb851c0c9a53 Mon Sep 17 00:00:00 2001 From: saundersp Date: Sun, 9 Jun 2024 22:41:37 +0200 Subject: [PATCH] Added inline and constexpr when possible --- data.cpp | 16 ++++----- data.hpp | 100 ++++++++++++++++++++++++++-------------------------- toolbox.cpp | 91 ++++++++++++++++++++++++----------------------- toolbox.hpp | 8 ++--- 4 files changed, 108 insertions(+), 107 deletions(-) diff --git a/data.cpp b/data.cpp index ecc587d..730672a 100644 --- a/data.cpp +++ b/data.cpp @@ -4,7 +4,7 @@ #include "data.hpp" template -static bool is_arg_sorted(const asp::Array& a, const asp::Array& indices) noexcept { +constexpr static bool is_arg_sorted(const asp::Array& a, const asp::Array& indices) noexcept { for (size_t i = 1; i < a.length; ++i) if (a[indices[i - 1]] > a[indices[i]]) return false; @@ -12,7 +12,7 @@ static bool is_arg_sorted(const asp::Array& a, const asp::Array& indi } template -static bool is_sorted(const asp::Array& a) noexcept { +constexpr static bool is_sorted(const asp::Array& a) noexcept { for (size_t i = 1; i < a.length; ++i) if (a[i - 1] > a[i]) return false; @@ -20,9 +20,9 @@ static bool is_sorted(const asp::Array& a) noexcept { } template -static void test_sort(const asp::Array& original, void (* const fnc)(const asp::Array&), const char* title) noexcept { +constexpr static void test_sort(const asp::Array& original, void (* const fnc)(const asp::Array&), const char* const title) noexcept { #ifdef __DEBUG - printf("xxxxxxxxxxxxxxx INGORE COPY "); + printf("xxxxxxxxxxxxxxx IGNORE COPY "); #endif const asp::Array a(original); asp::measure_time_void(title, fnc, a); @@ -36,9 +36,9 @@ static void test_sort(const asp::Array& original, void (* const fnc)(const as } template -static void test_argsort(const asp::Array& original, asp::Array(* const fnc)(const asp::Array&), const char* title) noexcept { +constexpr static void test_argsort(const asp::Array& original, asp::Array(* const fnc)(const asp::Array&), const char* const title) noexcept { #ifdef __DEBUG - printf("xxxxxxxxxxxxxxx INGORE COPY "); + printf("xxxxxxxxxxxxxxx IGNORE COPY "); #endif const asp::Array a(original); const asp::Array indices = asp::measure_time>(title, fnc, a); @@ -56,8 +56,8 @@ static asp::Array create_random_array(const size_t& n) noexcept { asp::Array original(n); std::random_device rd; std::default_random_engine gen(rd()); - std::uniform_int_distribution distrib(std::numeric_limits::min(), std::numeric_limits::max()); - return std::move(asp::map(original, [& distrib, & gen](const size_t&, const T&) -> const T { return distrib(gen); })); + std::uniform_int_distribution distribution(std::numeric_limits::min(), std::numeric_limits::max()); + return std::move(asp::map(original, [&distribution, &gen](const size_t&, const T&) -> const T { return distribution(gen); })); } int32_t main(int32_t argc, char** argv) { diff --git a/data.hpp b/data.hpp index b762aa6..bfbaa3b 100644 --- a/data.hpp +++ b/data.hpp @@ -11,7 +11,7 @@ namespace asp { std::shared_ptr data; size_t length = 0; - Array() noexcept = delete; + Array(void) noexcept = delete; Array(const size_t& size) noexcept: data(std::shared_ptr(new T[size])), length(size) { // #ifdef __DEBUG // printf("Creating array of size %lu\n", size); @@ -33,7 +33,7 @@ namespace asp { other.length = 0; } - constexpr T& operator[](const size_t& index) const { + inline constexpr T& operator[](const size_t& index) const { #ifdef __DEBUG if (index > length) { fprintf(stderr, "Index %ld out of range in Array of length %ld !\n", index, length); @@ -45,7 +45,7 @@ namespace asp { }; template - int32_t print(const Array& a, const char* format) noexcept { + inline constexpr int32_t print(const Array& a, const char* const format) noexcept { int32_t num_written = 0; num_written += printf("["); char formatter[BUFSIZ] = { 0 }; @@ -57,33 +57,33 @@ namespace asp { return num_written; } - int32_t print(const Array& a) noexcept { + inline constexpr int32_t print(const Array& a) noexcept { return print(a, "%b"); } - int32_t print(const Array& a) noexcept { + inline constexpr int32_t print(const Array& a) noexcept { return print(a, "%i"); } - int32_t print(const Array& a) noexcept { + inline constexpr int32_t print(const Array& a) noexcept { return print(a, "%lu"); } - int32_t print(const Array& a) noexcept { + inline constexpr int32_t print(const Array& a) noexcept { //printf("%i\n", a[0]); return print(a, "%i"); } - int32_t print(const std::string& s) noexcept { + inline int32_t print(const std::string& s) noexcept { return printf("%s\n", s.c_str()); } - int32_t print(const char* s) noexcept { + inline constexpr int32_t print(const char* const s) noexcept { return printf("%s\n", s); } template - constexpr T& max(const Array& a) noexcept { + inline constexpr T& max(const Array& a) noexcept { T& max_el = a[0]; for (size_t i = 1; i < a.length; ++i) if (a[i] > max_el) @@ -92,7 +92,7 @@ namespace asp { } template - constexpr T& min(const Array& a) noexcept { + inline constexpr T& min(const Array& a) noexcept { T& max_el = a[0]; for (size_t i = 1; i < a.length; ++i) if (a[i] < max_el) @@ -101,19 +101,19 @@ namespace asp { } template - Array& map(Array& a, const F& fnc) noexcept { + inline constexpr Array& map(Array& a, const F& fnc) noexcept { for (size_t i = 0; i < a.length; ++i) a[i] = fnc(i, a[i]); return a; } template - void foreach(const Array& a, const F& fnc) noexcept { + inline constexpr void foreach(const Array& a, const F& fnc) noexcept { for (size_t i = 0; i < a.length; ++i) fnc(i, a[i]); } - Array range(const size_t& n) noexcept { + inline Array range(const size_t& n) noexcept { Array a(n); return std::move(map(a, [](const size_t& i, const size_t&) -> const size_t& { return i; @@ -121,15 +121,15 @@ namespace asp { } template - constexpr inline static void swap(T* a, T* b) noexcept { + inline constexpr void swap(T* const a, T* const b) noexcept { const T temp = *a; *a = *b; *b = temp; } template - void bubble_sort(const Array& a) noexcept { - size_t j; + inline constexpr void bubble_sort(const Array& a) noexcept { + size_t j = 0; for (size_t i = 0; i < a.length; ++i) for (j = i + 1; j < a.length; ++j) if (a[i] > a[j]) @@ -137,21 +137,21 @@ namespace asp { } template - Array bubble_sort_arg(const Array& a) noexcept { + inline Array bubble_sort_arg(const Array& a) noexcept { Array indices = range(a.length); size_t j; for (size_t i = 0; i < a.length; ++i) for (j = i + 1; j < a.length; ++j) if (a[i] > a[j]){ - swap(&indices[i], &indices[j]); - swap(&a[i], &a[j]); + swap(&indices[i], &indices[j]); + swap(&a[i], &a[j]); } return indices; } template - static size_t qs_partition(const Array& a, const size_t& l, const size_t& h) noexcept { + inline constexpr size_t qs_partition(const Array& a, const size_t& l, const size_t& h) noexcept { size_t i = l - 1; for (size_t j = l; j <= h; ++j) if (a[j] < a[h]) @@ -161,7 +161,7 @@ namespace asp { } template - static void quicksort(const Array& a, const size_t& l, const size_t& h) noexcept { + inline constexpr void quicksort(const Array& a, const size_t& l, const size_t& h) noexcept { if (l >= h) return; @@ -172,17 +172,17 @@ namespace asp { } template - void quicksort(const Array& a) noexcept { + inline constexpr void quicksort(const Array& a) noexcept { quicksort(a, 0, a.length - 1); } template - static void quicksort_iter(const Array& a, const size_t& l, const size_t& h) noexcept { + inline constexpr void quicksort_iter(const Array& a, const size_t& l, const size_t& h) noexcept { // Create an auxiliary stack const size_t total = h - l + 1; // push initial values of l and h to stack - size_t* stack = new size_t[total]{l, h}; + size_t* const stack = new size_t[total]{l, h}; // initialize top of stack size_t top = 1; @@ -220,12 +220,12 @@ namespace asp { } template - void quicksort_iter(const Array& a) noexcept { + inline constexpr void quicksort_iter(const Array& a) noexcept { quicksort_iter(a, 0, a.length - 1); } template - static size_t qs_arg_partition(const Array& a, const Array& indices, const size_t& l, const size_t& h) noexcept { + inline constexpr size_t qs_arg_partition(const Array& a, const Array& indices, const size_t& l, const size_t& h) noexcept { size_t i = l - 1; for (size_t j = l; j <= h; ++j) if (a[j] < a[h]){ @@ -238,7 +238,7 @@ namespace asp { } template - static void quicksort_arg(const Array& a, const Array& indices, const size_t& l, const size_t& h) noexcept { + inline constexpr void quicksort_arg(const Array& a, const Array& indices, const size_t& l, const size_t& h) noexcept { if (l >= h) return; @@ -249,24 +249,24 @@ namespace asp { } template - Array quicksort_arg(const Array& other, const size_t& l, const size_t& h) noexcept { + inline Array quicksort_arg(const Array& other, const size_t& l, const size_t& h) noexcept { Array indices = range(other.length); quicksort_arg(other, indices, l, h); return indices; } template - Array quicksort_arg(const Array& a) noexcept { + inline Array quicksort_arg(const Array& a) noexcept { return quicksort_arg(a, 0, a.length - 1); } template - void quicksort_arg_iter(const Array& a, const Array& indices, const size_t& l, const size_t& h) noexcept { + inline constexpr void quicksort_arg_iter(const Array& a, const Array& indices, const size_t& l, const size_t& h) noexcept { // Create an auxiliary stack const size_t total = h - l + 1; // push initial values of l and h to stack - size_t* stack = new size_t[total]{l,h}; + size_t* const stack = new size_t[total]{l,h}; // initialize top of stack size_t top = 1; @@ -304,7 +304,7 @@ namespace asp { } template - Array quicksort_arg_iter(const Array& a) noexcept { + inline Array quicksort_arg_iter(const Array& a) noexcept { Array indices = range(a.length); quicksort_arg_iter(a, indices, 0, a.length - 1); return indices; @@ -315,25 +315,25 @@ namespace asp { size_t indice; T val; - ArgVal() noexcept = default; + ArgVal(void) noexcept = default; ArgVal(const size_t& _i, const T& _v) noexcept : indice(_i), val(_v) {} - constexpr bool operator>(const ArgVal& other) const noexcept { + inline constexpr bool operator>(const ArgVal& other) const noexcept { return std::move(val > other.val); } - constexpr bool operator<(const ArgVal& other) const noexcept { + inline constexpr bool operator<(const ArgVal& other) const noexcept { return std::move(val < other.val); } - constexpr bool operator>=(const ArgVal& other) const noexcept { + inline constexpr bool operator>=(const ArgVal& other) const noexcept { return std::move(val >= other.val); } - constexpr bool operator<=(const ArgVal& other) const noexcept { + inline constexpr bool operator<=(const ArgVal& other) const noexcept { return std::move(val <= other.val); } }; template - static void merge(const Array& a, const size_t& l, const size_t& m, const size_t& r) noexcept { + inline constexpr void merge(const Array& a, const size_t& l, const size_t& m, const size_t& r) noexcept { Array left_arr(m - l + 1); memcpy(&left_arr.data[0], &a[l], left_arr.length * sizeof(T)); @@ -354,7 +354,7 @@ namespace asp { } template - void mergesort(const Array& a, const size_t& l, const size_t& r) noexcept { + inline constexpr void mergesort(const Array& a, const size_t& l, const size_t& r) noexcept { if (l >= r) return; @@ -365,12 +365,12 @@ namespace asp { } template - void mergesort(const Array& a) noexcept { + inline constexpr void mergesort(const Array& a) noexcept { mergesort(a, 0, a.length - 1); } template - Array mergesort_arg(const Array& a, const size_t& l, const size_t& r) noexcept { + inline Array mergesort_arg(const Array& a, const size_t& l, const size_t& r) noexcept { Array> temp_vals(a.length); map(temp_vals, [&a](const size_t& i, const ArgVal&) -> const ArgVal { @@ -386,11 +386,11 @@ namespace asp { } template - Array mergesort_arg(const Array& a) noexcept { + inline Array mergesort_arg(const Array& a) noexcept { return mergesort_arg(a, 0, a.length - 1); } - //static void count_sort(const Array& a, const int32_t& exp, const int32_t& d) noexcept { + //void count_sort(const Array& a, const int32_t& exp, const int32_t& d) noexcept { // Array output(a.length), count(d); // memset(&count[0], 0, d * sizeof(int32_t)); @@ -410,7 +410,7 @@ namespace asp { //} template - void counting_sort(const Array& a) noexcept { + inline constexpr void counting_sort(const Array& a) noexcept { Array output(a); map(a, [output](const size_t& i, const T&) -> const T& { @@ -419,14 +419,14 @@ namespace asp { } template - Array counting_sort_arg(const Array& a) noexcept { + inline Array counting_sort_arg(const Array& a) noexcept { Array indices = range(a.length); return indices; } template - inline void radix_sort_256(T* a, const size_t& n) noexcept { + inline constexpr void radix_sort_256(T* a, const size_t& n) noexcept { //template //void radix_sort(const Array& a) noexcept { if (n <= 1) @@ -434,7 +434,7 @@ namespace asp { return; T* output = new T[n]; // output array - size_t* count = new size_t[256]; + size_t* const count = new size_t[256]; T* originalArr = a; // So we know which was input for (size_t shift = 0, s = 0; shift < 4; shift++, s += 8) { @@ -479,12 +479,12 @@ namespace asp { } template - void radix_sort(const Array& a) noexcept { + inline constexpr void radix_sort(const Array& a) noexcept { radix_sort_256(a.data.get(), a.length); } template - Array radix_sort_arg(const Array& a) noexcept { + inline Array radix_sort_arg(const Array& a) noexcept { Array indices = range(a.length); return indices; diff --git a/toolbox.cpp b/toolbox.cpp index 047366c..0e62c73 100644 --- a/toolbox.cpp +++ b/toolbox.cpp @@ -5,14 +5,14 @@ #include "toolbox.hpp" namespace asp { - static const constexpr size_t N_TIMES = 10; - static const constexpr std::array time_formats = { "ns", "us", "ms", "s", "m", "h", "j", "w", "M", "y" }; - static const constexpr std::array time_numbers = { 1, 1000, 1000, 1000, 60, 60, 24, 7, 4, 12 }; - static const uint64_t total_time = std::accumulate(time_numbers.begin(), time_numbers.end(), (uint64_t)1, std::multiplies()); + static constexpr size_t N_TIMES = 10; + static constexpr std::array time_formats = { "ns", "us", "ms", "s", "m", "h", "j", "w", "M", "y" }; + static constexpr std::array time_numbers = { 1, 1000, 1000, 1000, 60, 60, 24, 7, 4, 12 }; + static const uint64_t total_time = std::accumulate(time_numbers.begin(), time_numbers.end(), uint64_t(1), std::multiplies()); - static const constexpr size_t N_BYTES = 7; - static const constexpr std::array bytes_formats = { "", "K", "M", "G", "P", "E", "Z" }; //, "Y" }; - static const constexpr uint64_t total_bytes = static_cast(1)<<(10 * (N_BYTES - 1)); + static constexpr size_t N_BYTES = 7; + static constexpr std::array bytes_formats = { "", "K", "M", "G", "P", "E", "Z" }; //, "Y" }; + static constexpr uint64_t total_bytes = uint64_t(1)<<(10 * (N_BYTES - 1)); /** * @brief Format the time in seconds in human readable format. @@ -43,7 +43,7 @@ namespace asp { bytes %= prod; s += std::to_string(res) + bytes_formats[i - 1] + "B "; } - prod /= static_cast(1)<<10; + prod /= uint64_t(1)<<10; } if (s.back() == ' ') @@ -84,49 +84,49 @@ namespace asp { * @brief Run some unitary tests on format functions * */ - void toolbox_unit_test() noexcept { - assert(std::string("0B") == format_byte_size(static_cast(0))); - assert(std::string("1B") == format_byte_size(static_cast(1))); - assert(std::string("1KB") == format_byte_size(static_cast(1)<<10)); - assert(std::string("1MB") == format_byte_size(static_cast(1)<<20)); - assert(std::string("1GB") == format_byte_size(static_cast(1)<<30)); - assert(std::string("1PB") == format_byte_size(static_cast(1)<<40)); - assert(std::string("1EB") == format_byte_size(static_cast(1)<<50)); - assert(std::string("1ZB") == format_byte_size(static_cast(1)<<60)); - //assert(std::string("1YB") == format_byte_size(static_cast(1)<<70)); + void toolbox_unit_test(void) noexcept { + assert(std::string("0B") == format_byte_size(uint64_t(0))); + assert(std::string("1B") == format_byte_size(uint64_t(1))); + assert(std::string("1KB") == format_byte_size(uint64_t(1)<<10)); + assert(std::string("1MB") == format_byte_size(uint64_t(1)<<20)); + assert(std::string("1GB") == format_byte_size(uint64_t(1)<<30)); + assert(std::string("1PB") == format_byte_size(uint64_t(1)<<40)); + assert(std::string("1EB") == format_byte_size(uint64_t(1)<<50)); + assert(std::string("1ZB") == format_byte_size(uint64_t(1)<<60)); + //assert(std::string("1YB") == format_byte_size(uint64_t(1)<<70)); // UINT64_MAX == 18446744073709551615I64u == -1 - assert(std::string("15ZB 1023EB 1023PB 1023GB 1023MB 1023KB 1023B") == format_byte_size(static_cast(-1))); + assert(std::string("15ZB 1023EB 1023PB 1023GB 1023MB 1023KB 1023B") == format_byte_size(uint64_t(-1))); - assert(std::string("0s") == format_time(static_cast(0))); - assert(std::string("1s") == format_time(static_cast(1))); - assert(std::string("1m") == format_time(static_cast(60))); - assert(std::string("1h") == format_time(static_cast(3600))); - assert(std::string("1j") == format_time(static_cast(86400))); - assert(std::string("1w") == format_time(static_cast(604800))); - assert(std::string("1M") == format_time(static_cast(2419200))); - assert(std::string("1y") == format_time(static_cast(29030400))); + assert(std::string("0s") == format_time(uint64_t(0))); + assert(std::string("1s") == format_time(uint64_t(1))); + assert(std::string("1m") == format_time(uint64_t(60))); + assert(std::string("1h") == format_time(uint64_t(3600))); + assert(std::string("1j") == format_time(uint64_t(86400))); + assert(std::string("1w") == format_time(uint64_t(604800))); + assert(std::string("1M") == format_time(uint64_t(2419200))); + assert(std::string("1y") == format_time(uint64_t(29030400))); - assert(std::string("0ns") == format_time_ns(static_cast(0))); - assert(std::string("1ns") == format_time_ns(static_cast(1))); - assert(std::string("1us") == format_time_ns(static_cast(1e3))); - assert(std::string("1ms") == format_time_ns(static_cast(1e6))); - assert(std::string("1s") == format_time_ns(static_cast(1e9))); - assert(std::string("1m") == format_time_ns(static_cast(6e10))); - assert(std::string("1h") == format_time_ns(static_cast(36e11))); - assert(std::string("1j") == format_time_ns(static_cast(864e11))); - assert(std::string("1w") == format_time_ns(static_cast(6048e11))); - assert(std::string("1M") == format_time_ns(static_cast(24192e11))); - assert(std::string("1y") == format_time_ns(static_cast(290304e11))); + assert(std::string("0ns") == format_time_ns(uint64_t(0))); + assert(std::string("1ns") == format_time_ns(uint64_t(1))); + assert(std::string("1us") == format_time_ns(uint64_t(1e3))); + assert(std::string("1ms") == format_time_ns(uint64_t(1e6))); + assert(std::string("1s") == format_time_ns(uint64_t(1e9))); + assert(std::string("1m") == format_time_ns(uint64_t(6e10))); + assert(std::string("1h") == format_time_ns(uint64_t(36e11))); + assert(std::string("1j") == format_time_ns(uint64_t(864e11))); + assert(std::string("1w") == format_time_ns(uint64_t(6048e11))); + assert(std::string("1M") == format_time_ns(uint64_t(24192e11))); + assert(std::string("1y") == format_time_ns(uint64_t(290304e11))); // UINT64_MAX == 18446744073709551615I64u == -1 - assert(std::string("635y 5M 3j 23h 34m 33s 709ms 551us 615ns") == format_time_ns(static_cast(-1))); + assert(std::string("635y 5M 3j 23h 34m 33s 709ms 551us 615ns") == format_time_ns(uint64_t(-1))); } std::string thousand_sep(const uint64_t& k, const char& sep) noexcept { std::string s = "", n = std::to_string(k); - int32_t c = 0; - for (int32_t i = static_cast(n.size()) - 1; i >= 0; --i) { - c++; + uint8_t c = 0; + for (int64_t i = int64_t(n.size()) - 1; i >= 0; --i) { + ++c; s.push_back(n[i]); if (c == 3) { s.push_back(sep); @@ -146,11 +146,12 @@ namespace asp { return thousand_sep(k, ','); } - void print_separator(const char* title) noexcept { -#define S(N) std::string(N, '-').c_str() - const constexpr size_t SEPARATOR_SIZE = W_NAME + W_TIME + W_FTIME + 6 + 6; + void print_separator(const char* const title) noexcept { + #define S(N) std::string(N, '-').c_str() + constexpr size_t SEPARATOR_SIZE = W_NAME + W_TIME + W_FTIME + 6 + 6; char separator[SEPARATOR_SIZE]; sprintf(separator, "|%s|%s|%s|\n", S(W_NAME + 2), S(W_TIME + 2), S(W_FTIME + 2)); printf("| %-" STR(W_NAME) "s | %-" STR(W_TIME) "s | %-" STR(W_FTIME) "s | \n%s", title, "Time spent(ns)", "Formatted time spent", separator); + #undef S } } diff --git a/toolbox.hpp b/toolbox.hpp index 70a9058..fc1e444 100644 --- a/toolbox.hpp +++ b/toolbox.hpp @@ -16,13 +16,13 @@ namespace asp { std::string format_byte_size(uint64_t) noexcept; std::string format_time(const uint64_t) noexcept; std::string format_time_ns(uint64_t) noexcept; - void toolbox_unit_test() noexcept; + void toolbox_unit_test(void) noexcept; std::string thousand_sep(const uint64_t&, const char&) noexcept; std::string thousand_sep(const uint64_t&) noexcept; - void print_separator(const char*) noexcept; + void print_separator(const char* const) noexcept; template - void measure_time_void(const char* step_name, const F& fnc, Args &&...args) noexcept { + inline constexpr void measure_time_void(const char* step_name, const F& fnc, Args &&...args) noexcept { #ifndef __DEBUG printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress"); #endif @@ -33,7 +33,7 @@ namespace asp { } template - T measure_time(const char* step_name, const F& fnc, Args &&...args) noexcept { + inline constexpr T measure_time(const char* step_name, const F& fnc, Args &&...args) noexcept { #ifndef __DEBUG printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress"); #endif