Modernized code and fancier code printing

This commit is contained in:
saundersp
2025-07-08 02:45:49 +02:00
parent 36b96f1836
commit 14e3997aca
4 changed files with 491 additions and 282 deletions

View File

@ -1,51 +1,51 @@
#include <assert.h> #include <cassert>
#include <random> #include <random>
#include "toolbox.hpp" #include "toolbox.hpp"
#include "data.hpp" #include "data.hpp"
template<typename T> template<typename T>
constexpr static bool is_arg_sorted(const asp::Array<T>& a, const asp::Array<size_t>& indices) noexcept { static constexpr bool is_arg_sorted(const asp::Array<T>& a, const asp::Array<size_t>& indices) noexcept {
for (size_t i = 1; i < a.length; ++i) for (size_t i = 1; i < a.length; ++i)
if (a[indices[i - 1]] > a[indices[i]]) if (indices[i - 1] == indices[i] || a[indices[i - 1]] > a[indices[i]])
return false; return false;
return true; return true;
} }
template<typename T> template<typename T>
constexpr static bool is_sorted(const asp::Array<T>& a) noexcept { static constexpr bool is_sorted(const asp::Array<T>& a) noexcept {
for (size_t i = 1; i < a.length; ++i) for (size_t i = 1; i < a.length; ++i)
if (a[i - 1] > a[i]) if (a[i - 1] > a[i])
return false; return false;
return true; return true;
} }
template<typename T> template<typename T, size_t N>
constexpr static void test_sort(const asp::Array<T>& original, void (* const fnc)(const asp::Array<T>&), const char* const title) noexcept { static constexpr void test_sort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, void (* const fnc)(const asp::Array<T>&), const char* const title) noexcept {
#ifdef __DEBUG #ifdef __DEBUG
printf("xxxxxxxxxxxxxxx IGNORE COPY "); printf("xxxxxxxxxxxxxxx IGNORE COPY ");
#endif #endif
const asp::Array<T> a(original); const asp::Array<T> a(original);
asp::measure_time_void(title, fnc, a); asp::measure_time_void(gaps, title, fnc, a);
#ifdef __DEBUG #ifdef __DEBUG
// assert(asp::measure_time<bool>("=> Unit test", is_sorted, a)); const bool result = asp::measure_time<bool>(gaps, "=> Unit test", is_sorted<T>, a);
const bool result = asp::measure_time<bool>("=> Unit test", is_sorted<T>, a); // asp::print(original);
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", result ? "Success" : "Failure", "-", "-"); // asp::print(a);
asp::formatted_row(gaps, { result ? "Success" : "Failure", "-", "-" });
#else #else
assert(is_sorted(a)); assert(is_sorted(a));
#endif #endif
} }
template<typename T> template<typename T, size_t N>
constexpr static void test_argsort(const asp::Array<T>& original, asp::Array<size_t>(* const fnc)(const asp::Array<T>&), const char* const title) noexcept { static void test_argsort(const std::array<int32_t, N>& gaps, const asp::Array<T>& original, asp::Array<size_t>(* const fnc)(const asp::Array<T>&), const char* const title) noexcept {
#ifdef __DEBUG #ifdef __DEBUG
printf("xxxxxxxxxxxxxxx IGNORE COPY "); printf("xxxxxxxxxxxxxxx IGNORE COPY ");
#endif #endif
const asp::Array<T> a(original); const asp::Array<T> a(original);
const asp::Array<size_t> indices = asp::measure_time<asp::Array<size_t>>(title, fnc, a); const asp::Array<size_t> indices = asp::measure_time<asp::Array<size_t>>(gaps, title, fnc, a);
#ifdef __DEBUG #ifdef __DEBUG
// assert(asp::measure_time<bool>("=> Unit test", is_arg_sorted, a, indices)); const bool result = asp::measure_time<bool>(gaps, "=> Unit test", is_arg_sorted<T>, original, indices);
const bool result = asp::measure_time<bool>("=> Unit test", is_arg_sorted<T>, original, indices); asp::formatted_row(gaps, { result ? "Success" : "Failure", "-", "-" });
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", result ? "Success" : "Failure", "-", "-");
#else #else
assert(is_arg_sorted(original, indices)); assert(is_arg_sorted(original, indices));
#endif #endif
@ -64,7 +64,7 @@ int32_t main(int32_t argc, char** argv) {
asp::toolbox_unit_test(); asp::toolbox_unit_test();
using array_type = uint16_t; using array_type = uint16_t;
size_t N = static_cast<size_t>(1)<<16; size_t N = (static_cast<size_t>(1)<<15) - 1;
// size_t N = std::numeric_limits<array_type>::max(); // size_t N = std::numeric_limits<array_type>::max();
if (argc > 2) { if (argc > 2) {
fprintf(stderr, "Too many arguments\nUsage: ./data <ARRAY_SIZE>\n"); fprintf(stderr, "Too many arguments\nUsage: ./data <ARRAY_SIZE>\n");
@ -72,29 +72,33 @@ int32_t main(int32_t argc, char** argv) {
} else if (argc == 2) } else if (argc == 2)
N = std::strtoul(argv[1], argv + strlen(argv[1]) + 1, 10); N = std::strtoul(argv[1], argv + strlen(argv[1]) + 1, 10);
asp::print("Estimating memory footprint at : " + asp::format_byte_size(2 * N * sizeof(array_type))); char title[64];
sprintf(title, "Sorting %s elements of %s", asp::thousand_sep(N).c_str(), asp::format_byte_size(2 * N * sizeof(array_type)).c_str());
const std::array<int32_t, 3> gaps = { 48, -17, 25 };
asp::header(gaps, { title, "Time spent (ns)", "Formatted time spent" });
char buff[64]; const asp::Array<array_type> original = asp::measure_time<asp::Array<array_type>>(gaps, "Creating random array", create_random_array<array_type>, N);
sprintf(buff, "Sorting algorithm for %s elements", asp::thousand_sep(N).c_str()); // const asp::Array<array_type> original = { 2, 5, 3, 0, 2, 3, 0, 3 };
asp::print_separator(buff);
const asp::Array<array_type> original = asp::measure_time<asp::Array<array_type>>("Creating random array", create_random_array<array_type>, N);
// std::cout << asp::min(original) << "<=>" << asp::max(original) << std::endl;;
// asp::print(original); // asp::print(original);
test_sort(original, asp::bubble_sort<array_type>, "Bubble sort"); test_sort(gaps, original, asp::bubble_sort<array_type>, "Bubble sort");
test_argsort(original, asp::bubble_sort_arg<array_type>, "Bubble sort (arg)"); test_argsort(gaps, original, asp::bubble_sort_arg<array_type>, "Bubble sort (arg)");
test_sort(original, asp::quicksort<array_type>, "Quicksort"); test_sort(gaps, original, asp::quicksort<array_type>, "Quicksort");
test_argsort(original, asp::quicksort_arg<array_type>, "Quicksort (arg)"); test_argsort(gaps, original, asp::quicksort_arg<array_type>, "Quicksort (arg)");
test_sort(original, asp::quicksort_iter<array_type>, "Quicksort (iterative)"); test_sort(gaps, original, asp::quicksort_iter<array_type>, "Quicksort (iterative)");
test_argsort(original, asp::quicksort_arg_iter<array_type>, "Quicksort (iterative) (arg)"); test_argsort(gaps, original, asp::quicksort_arg_iter<array_type>, "Quicksort (iterative) (arg)");
test_sort(original, asp::mergesort<array_type>, "Mergesort"); test_sort(gaps, original, asp::mergesort<array_type>, "Mergesort");
test_argsort(original, asp::mergesort_arg<array_type>, "Mergesort (arg)"); test_argsort(gaps, original, asp::mergesort_arg<array_type>, "Mergesort (arg)");
test_sort(gaps, original, asp::insertion_sort<array_type>, "Insertion sort");
// test_argsort(gaps, original, asp::insertion_argsort<array_type>, "Insertion argsort");
// W.I.P // W.I.P
// test_sort(original, asp::counting_sort<array_type>, "Counting sort"); // test_sort(gaps, original, asp::counting_sort<array_type>, "Counting sort");
// test_argsort(original, asp::counting_sort_arg<array_type>, "Counting sort (arg)"); // test_argsort(gaps, original, asp::counting_sort_arg<array_type>, "Counting sort (arg)");
// test_sort(original, asp::radix_sort<array_type>, "Radix sort"); // test_sort(gaps, original, asp::radix_sort<array_type>, "Radix sort");
// test_argsort(original, asp::radix_sort_arg<array_type>, "Radix sort (arg)"); // test_argsort(gaps, original, asp::radix_sort_arg<array_type>, "Radix sort (arg)");
asp::footer(gaps);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

362
data.hpp
View File

@ -1,6 +1,9 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <string_view>
#include <cstring> #include <cstring>
#include <cstdio>
#include <cstdint>
#ifdef __DEBUG #ifdef __DEBUG
#include <stdexcept> #include <stdexcept>
#endif #endif
@ -11,29 +14,39 @@ namespace asp {
std::shared_ptr<T[]> data; std::shared_ptr<T[]> data;
size_t length = 0; size_t length = 0;
Array(void) noexcept = delete; constexpr Array(void) noexcept = delete;
Array(const size_t& size) noexcept: data(std::shared_ptr<T[]>(new T[size])), length(size) { constexpr Array(const size_t& size) noexcept : data(std::shared_ptr<T[]>(new T[size])), length(size) {
// #ifdef __DEBUG #ifdef __DEBUG
// printf("Creating array of size %lu\n", size); printf("Creating array of size %lu\n", size);
// #endif #endif
} }
Array(const Array<T>& other) noexcept: data(std::shared_ptr<T[]>(new T[other.length])), length(other.length) {
constexpr Array(const std::initializer_list<size_t>& other) noexcept : data(std::shared_ptr<T[]>(new T[other.size()])), length(other.size()) {
#ifdef __DEBUG
printf("Copying array of size %lu\n", other.size());
#endif
memcpy(data.get(), other.begin(), length * sizeof(T));
}
constexpr Array(const Array<T>& other) noexcept : data(std::shared_ptr<T[]>(new T[other.length])), length(other.length) {
#ifdef __DEBUG #ifdef __DEBUG
printf("Copying array of size %lu\n", other.length); printf("Copying array of size %lu\n", other.length);
#endif #endif
memcpy(data.get(), other.data.get(), length * sizeof(T)); memcpy(data.get(), other.data.get(), length * sizeof(T));
} }
Array(Array&& other) noexcept {
// #ifdef __DEBUG constexpr Array(Array&& other) noexcept {
// printf("Moving array of size %lu\n", other.length); #ifdef __DEBUG
// #endif printf("Moving array of size %lu\n", other.length);
#endif
data = other.data; data = other.data;
length = other.length; length = other.length;
other.data = nullptr; other.data = nullptr;
other.length = 0; other.length = 0;
} }
inline constexpr T& operator[](const size_t& index) const { constexpr T& operator[](const size_t& index) const {
#ifdef __DEBUG #ifdef __DEBUG
if (index > length) { if (index > length) {
fprintf(stderr, "Index %ld out of range in Array of length %ld !\n", index, length); fprintf(stderr, "Index %ld out of range in Array of length %ld !\n", index, length);
@ -45,10 +58,10 @@ namespace asp {
}; };
template<typename T> template<typename T>
inline constexpr int32_t print(const Array<T>& a, const char* const format) noexcept { constexpr int32_t print(const Array<T>& a, const char* const format) noexcept {
int32_t num_written = 0; int32_t num_written = 0;
num_written += printf("["); num_written += printf("[");
char formatter[BUFSIZ] = { 0 }; char formatter[256] = { 0 };
sprintf(formatter, "%s,", format); sprintf(formatter, "%s,", format);
for (size_t i = 0; i < a.length; ++i) for (size_t i = 0; i < a.length; ++i)
num_written += printf(formatter, a[i]); num_written += printf(formatter, a[i]);
@ -57,33 +70,36 @@ namespace asp {
return num_written; return num_written;
} }
inline constexpr int32_t print(const Array<uint16_t>& a) noexcept { constexpr int32_t print(const Array<uint16_t>& a) noexcept {
return print(a, "%b"); return print(a, "%b");
} }
inline constexpr int32_t print(const Array<int32_t>& a) noexcept { constexpr int32_t print(const Array<int32_t>& a) noexcept {
return print(a, "%i"); return print(a, "%i");
} }
inline constexpr int32_t print(const Array<uint64_t>& a) noexcept { constexpr int32_t print(const Array<uint32_t>& a) noexcept {
return print(a, "%u");
}
constexpr int32_t print(const Array<uint64_t>& a) noexcept {
return print(a, "%lu"); return print(a, "%lu");
} }
inline constexpr int32_t print(const Array<int16_t>& a) noexcept { constexpr int32_t print(const Array<int16_t>& a) noexcept {
//printf("%i\n", a[0]);
return print(a, "%i"); return print(a, "%i");
} }
inline int32_t print(const std::string& s) noexcept { constexpr int32_t print(const std::string_view& s) noexcept {
return printf("%s\n", s.c_str()); return printf("%s\n", s.data());
} }
inline constexpr int32_t print(const char* const s) noexcept { constexpr int32_t print(const char* const s) noexcept {
return printf("%s\n", s); return printf("%s\n", s);
} }
template<typename T> template<typename T>
inline constexpr T& max(const Array<T>& a) noexcept { constexpr T& max(const Array<T>& a) noexcept {
T& max_el = a[0]; T& max_el = a[0];
for (size_t i = 1; i < a.length; ++i) for (size_t i = 1; i < a.length; ++i)
if (a[i] > max_el) if (a[i] > max_el)
@ -92,7 +108,7 @@ namespace asp {
} }
template<typename T> template<typename T>
inline constexpr T& min(const Array<T>& a) noexcept { constexpr T& min(const Array<T>& a) noexcept {
T& max_el = a[0]; T& max_el = a[0];
for (size_t i = 1; i < a.length; ++i) for (size_t i = 1; i < a.length; ++i)
if (a[i] < max_el) if (a[i] < max_el)
@ -101,57 +117,67 @@ namespace asp {
} }
template<typename T, typename F> template<typename T, typename F>
inline constexpr Array<T>& map(Array<T>& a, const F& fnc) noexcept { constexpr Array<T>& map(Array<T>& a, const F& fnc) noexcept {
for (size_t i = 0; i < a.length; ++i) for (size_t i = 0; i < a.length; ++i)
a[i] = fnc(i, a[i]); a[i] = fnc(i, a[i]);
return a; return a;
} }
template<typename T, typename F> template<typename T, typename F>
inline constexpr void foreach(const Array<T>& a, const F& fnc) noexcept { constexpr void foreach(const Array<T>& a, const F& fnc) noexcept {
for (size_t i = 0; i < a.length; ++i) for (size_t i = 0; i < a.length; ++i)
fnc(i, a[i]); fnc(i, a[i]);
} }
inline Array<size_t> range(const size_t& n) noexcept { inline Array<size_t> range(const size_t& n) noexcept {
Array<size_t> a(n); Array<size_t> a(n);
return std::move(map(a, [](const size_t& i, const size_t&) -> const size_t& { return map(a, [](const size_t& i, const size_t&) -> const size_t& {
return i; return i;
})); });
} }
template<typename T> template<typename T>
inline constexpr void swap(T* const a, T* const b) noexcept { constexpr void swap(T* const a, T* const b) noexcept {
const T temp = *a; const T temp = *a;
*a = *b; *a = *b;
*b = temp; *b = temp;
} }
template<typename T> template<typename T>
inline constexpr void bubble_sort(const Array<T>& a) noexcept { constexpr void bubble_sort(const Array<T>& a) noexcept {
size_t j = 0; size_t j = 0;
for (size_t i = 0; i < a.length; ++i) for (size_t i = 0; i < a.length; ++i) {
bool swapped = false;
for (j = i + 1; j < a.length; ++j) for (j = i + 1; j < a.length; ++j)
if (a[i] > a[j]) if (a[i] > a[j]) {
swap(&a[i], &a[j]); swap(&a[i], &a[j]);
swapped = true;
}
if (!swapped)
break;
}
} }
template<typename T> template<typename T>
inline Array<size_t> bubble_sort_arg(const Array<T>& a) noexcept { Array<size_t> bubble_sort_arg(const Array<T>& a) noexcept {
Array<size_t> indices = range(a.length); Array<size_t> indices = range(a.length);
size_t j; size_t j;
for (size_t i = 0; i < a.length; ++i) for (size_t i = 0; i < a.length; ++i) {
bool swapped = false;
for (j = i + 1; j < a.length; ++j) for (j = i + 1; j < a.length; ++j)
if (a[i] > a[j]){ if (a[i] > a[j]) {
swap(&indices[i], &indices[j]); swap(&indices[i], &indices[j]);
swap(&a[i], &a[j]); swap(&a[i], &a[j]);
swapped = true;
} }
if (!swapped)
break;
}
return indices; return indices;
} }
template<typename T> template<typename T>
inline constexpr size_t qs_partition(const Array<T>& a, const size_t& l, const size_t& h) noexcept { constexpr size_t qs_partition(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
size_t i = l - 1; size_t i = l - 1;
for (size_t j = l; j <= h; ++j) for (size_t j = l; j <= h; ++j)
if (a[j] < a[h]) if (a[j] < a[h])
@ -161,7 +187,7 @@ namespace asp {
} }
template<typename T> template<typename T>
inline constexpr void quicksort(const Array<T>& a, const size_t& l, const size_t& h) noexcept { constexpr void quicksort(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
if (l >= h) if (l >= h)
return; return;
@ -172,12 +198,12 @@ namespace asp {
} }
template<typename T> template<typename T>
inline constexpr void quicksort(const Array<T>& a) noexcept { constexpr void quicksort(const Array<T>& a) noexcept {
quicksort(a, 0, a.length - 1); quicksort(a, 0, a.length - 1);
} }
template<typename T> template<typename T>
inline constexpr void quicksort_iter(const Array<T>& a, const size_t& l, const size_t& h) noexcept { constexpr void quicksort_iter(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
// Create an auxiliary stack // Create an auxiliary stack
const size_t total = h - l + 1; const size_t total = h - l + 1;
@ -220,12 +246,12 @@ namespace asp {
} }
template<typename T> template<typename T>
inline constexpr void quicksort_iter(const Array<T>& a) noexcept { constexpr void quicksort_iter(const Array<T>& a) noexcept {
quicksort_iter(a, 0, a.length - 1); quicksort_iter(a, 0, a.length - 1);
} }
template<typename T> template<typename T>
inline constexpr size_t qs_arg_partition(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept { constexpr size_t qs_arg_partition(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
size_t i = l - 1; size_t i = l - 1;
for (size_t j = l; j <= h; ++j) for (size_t j = l; j <= h; ++j)
if (a[j] < a[h]){ if (a[j] < a[h]){
@ -238,7 +264,7 @@ namespace asp {
} }
template<typename T> template<typename T>
inline constexpr void quicksort_arg(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept { constexpr void quicksort_arg(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
if (l >= h) if (l >= h)
return; return;
@ -249,29 +275,26 @@ namespace asp {
} }
template<typename T> template<typename T>
inline Array<size_t> quicksort_arg(const Array<T>& other, const size_t& l, const size_t& h) noexcept { Array<size_t> quicksort_arg(const Array<T>& other, const size_t& l, const size_t& h) noexcept {
Array<size_t> indices = range(other.length); Array<size_t> indices = range(other.length);
quicksort_arg(other, indices, l, h); quicksort_arg(other, indices, l, h);
return indices; return indices;
} }
template<typename T> template<typename T>
inline Array<size_t> quicksort_arg(const Array<T>& a) noexcept { Array<size_t> quicksort_arg(const Array<T>& a) noexcept {
return quicksort_arg(a, 0, a.length - 1); return quicksort_arg(a, 0, a.length - 1);
} }
template<typename T> template<typename T>
inline constexpr void quicksort_arg_iter(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept { constexpr void quicksort_arg_iter(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
// Create an auxiliary stack // Create an auxiliary stack
const size_t total = h - l + 1; const size_t total = h - l + 1;
// push initial values of l and h to stack // push initial values of l and h to stack
size_t* const stack = new size_t[total]{l,h}; size_t* const stack = new size_t[total]{l,h};
// initialize top of stack // initialize top of stack
size_t top = 1; size_t top = 1, low = l, high = h;
size_t low = l, high = h;
// Keep popping from stack while is not empty // Keep popping from stack while is not empty
while (top <= total) { while (top <= total) {
@ -304,7 +327,7 @@ namespace asp {
} }
template<typename T> template<typename T>
inline Array<size_t> quicksort_arg_iter(const Array<T>& a) noexcept { Array<size_t> quicksort_arg_iter(const Array<T>& a) noexcept {
Array<size_t> indices = range(a.length); Array<size_t> indices = range(a.length);
quicksort_arg_iter(a, indices, 0, a.length - 1); quicksort_arg_iter(a, indices, 0, a.length - 1);
return indices; return indices;
@ -315,25 +338,25 @@ namespace asp {
size_t indice; size_t indice;
T val; T val;
ArgVal(void) noexcept = default; constexpr ArgVal(void) noexcept = default;
ArgVal(const size_t& _i, const T& _v) noexcept : indice(_i), val(_v) {} constexpr ArgVal(const size_t& _i, const T& _v) noexcept : indice(_i), val(_v) {}
inline constexpr bool operator>(const ArgVal<T>& other) const noexcept { constexpr bool operator>(const ArgVal<T>& other) const noexcept {
return std::move(val > other.val); return std::move(val > other.val);
} }
inline constexpr bool operator<(const ArgVal<T>& other) const noexcept { constexpr bool operator<(const ArgVal<T>& other) const noexcept {
return std::move(val < other.val); return std::move(val < other.val);
} }
inline constexpr bool operator>=(const ArgVal<T>& other) const noexcept { constexpr bool operator>=(const ArgVal<T>& other) const noexcept {
return std::move(val >= other.val); return std::move(val >= other.val);
} }
inline constexpr bool operator<=(const ArgVal<T>& other) const noexcept { constexpr bool operator<=(const ArgVal<T>& other) const noexcept {
return std::move(val <= other.val); return std::move(val <= other.val);
} }
}; };
template<typename T> template<typename T>
inline constexpr void merge(const Array<T>& a, const size_t& l, const size_t& m, const size_t& r) noexcept { constexpr void merge(const Array<T>& a, const size_t& l, const size_t& m, const size_t& r) noexcept {
Array<T> left_arr(m - l + 1); Array<T> left_arr(m - l + 1);
memcpy(&left_arr.data[0], &a[l], left_arr.length * sizeof(T)); memcpy(&left_arr.data[0], &a[l], left_arr.length * sizeof(T));
@ -354,7 +377,7 @@ namespace asp {
} }
template<typename T> template<typename T>
inline constexpr void mergesort(const Array<T>& a, const size_t& l, const size_t& r) noexcept { constexpr void mergesort(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
if (l >= r) if (l >= r)
return; return;
@ -365,12 +388,12 @@ namespace asp {
} }
template<typename T> template<typename T>
inline constexpr void mergesort(const Array<T>& a) noexcept { constexpr void mergesort(const Array<T>& a) noexcept {
mergesort(a, 0, a.length - 1); mergesort(a, 0, a.length - 1);
} }
template<typename T> template<typename T>
inline Array<size_t> mergesort_arg(const Array<T>& a, const size_t& l, const size_t& r) noexcept { Array<size_t> mergesort_arg(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
Array<ArgVal<T>> temp_vals(a.length); Array<ArgVal<T>> temp_vals(a.length);
map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> const ArgVal<T> { map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> const ArgVal<T> {
@ -386,107 +409,162 @@ namespace asp {
} }
template<typename T> template<typename T>
inline Array<size_t> mergesort_arg(const Array<T>& a) noexcept { Array<size_t> mergesort_arg(const Array<T>& a) noexcept {
return mergesort_arg(a, 0, a.length - 1); return mergesort_arg(a, 0, a.length - 1);
} }
//void count_sort(const Array<int32_t>& a, const int32_t& exp, const int32_t& d) noexcept {
// Array<int32_t> output(a.length), count(d);
// memset(&count[0], 0, d * sizeof(int32_t));
// foreach(a, [count, exp, d](const int32_t&, const int32_t& val) -> void {
// count[(val / exp) % d]++;
// });
// for (int32_t i = 1; i <= d; ++i)
// count[i] += count[i - 1];
// for (int32_t i = a.length - 1; i >= 0; --i) {
// output[count[(a[i] / exp) % d] - 1] = a[i];
// count[(a[i] / exp) % d]--;
// }
// memcpy(&a[0], &output[0], a.length * sizeof(int32_t));
//}
template<typename T> template<typename T>
inline constexpr void counting_sort(const Array<T>& a) noexcept { constexpr void insertion_sort(const Array<T>& a) noexcept {
Array<T> output(a); size_t j = 0;
for(size_t i = 1; i < a.length; ++i) {
map(a, [output](const size_t& i, const T&) -> const T& { T key = a[i];
return output[i]; for(j = i; j > 0 && a[j - 1] > key; --j)
}); a[j] = a[j - 1];
a[j] = key;
}
} }
template<typename T> template<typename T>
inline Array<size_t> counting_sort_arg(const Array<T>& a) noexcept { Array<size_t> insertion_argsort(const Array<T>& a) noexcept {
Array<size_t> indices = range(a.length);
size_t j = 0;
for(size_t i = 1; i < a.length; ++i){
for(j = i; j > 0 && a[indices[j - 1]] > a[indices[i]]; --j)
indices[j] = indices[j - 1];
indices[j] = i;
}
return indices;
}
template<typename T>
constexpr void counting_sort(const Array<T>& a) noexcept {
const size_t N = a.length;
const T M = max(a);
const T exp = 10;
const T d = 128;
Array<T> countArray(M);
memset(&countArray[0], 0, M * sizeof(T));
foreach(a, [&countArray](const size_t, const T val) -> void {
++countArray[val];
});
for (T i = 1; i <= M; ++i)
countArray[i] += countArray[i - 1];
Array<T> output(N);
for (size_t i = N - 1; i > 0; --i){
output[countArray[a[i]] - 1] = a[i];
--countArray[a[i]];
}
memmove(&a[0], &output[0], a.length * sizeof(T));
}
template<typename T>
Array<size_t> counting_sort_arg(const Array<T>& a) noexcept {
Array<size_t> indices = range(a.length); Array<size_t> indices = range(a.length);
return indices; return indices;
} }
template<typename T> inline void countsort(int a[], int n, int pos){
inline constexpr void radix_sort_256(T* a, const size_t& n) noexcept { int* output = new int[n + 1];
//template<typename T> int max = (a[0] / pos) % 10;
//void radix_sort(const Array<int32_t>& a) noexcept { for (int i = 1; i < n; i++) {
if (n <= 1) if (((a[i] / pos) % 10) > max)
//if (a.length <= 1) max = a[i];
return;
T* output = new T[n]; // output array
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) {
// Zero the counts
for (size_t i = 0; i < 256; i++)
count[i] = 0;
// Store count of occurrences in count[]
for (size_t i = 0; i < n; i++)
count[(a[i] >> s) & 0xff]++;
// Change count[i] so that count[i] now contains
// actual position of this digit in output[]
for (size_t i = 1; i < 256; i++)
count[i] += count[i - 1];
// Build the output array
for (int32_t i = n - 1; i >= 0; i--) {
// precalculate the offset as it's a few instructions
const size_t idx = (a[i] >> s) & 0xff;
// Subtract from the count and store the value
output[--count[idx]] = a[i];
}
// Copy the output array to input[], so that input[]
// is sorted according to current digit
// We can just swap the pointers
swap(a, output);
} }
int* count = new int[max + 1];
// If we switched posize_ters an odd number of times, for (int i = 0; i < max; ++i)
// make sure we copy before returning count[i] = 0;
if (originalArr == output) { for (int i = 0; i < n; i++)
swap(a, output); count[(a[i] / pos) % 10]++;
for (size_t i = 0; i < n; i++) for (int i = 1; i < 10; i++)
a[i] = output[i]; count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
output[count[(a[i] / pos) % 10] - 1] = a[i];
count[(a[i] / pos) % 10]--;
} }
for (int i = 0; i < n; i++)
a[i] = output[i];
delete[] output, delete[] count; delete[] output;
delete[] count;
}
// template<typename T>
// constexpr void radix_sort_256(T* a, const size_t& n) noexcept {
// //template<typename T>
// //void radix_sort(const Array<int32_t>& a) noexcept {
// if (n <= 1)
// //if (a.length <= 1)
// return;
//
// T* output = new T[n]; // output array
// 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) {
// // Zero the counts
// for (size_t i = 0; i < 256; i++)
// count[i] = 0;
//
// // Store count of occurrences in count[]
// for (size_t i = 0; i < n; i++)
// count[(a[i] >> s) & 0xff]++;
//
// // Change count[i] so that count[i] now contains
// // actual position of this digit in output[]
// for (size_t i = 1; i < 256; i++)
// count[i] += count[i - 1];
//
// // Build the output array
// for (int32_t i = n - 1; i >= 0; i--) {
// // precalculate the offset as it's a few instructions
// const size_t idx = (a[i] >> s) & 0xff;
//
// // Subtract from the count and store the value
// output[--count[idx]] = a[i];
// }
//
// // Copy the output array to input[], so that input[]
// // is sorted according to current digit
//
// // We can just swap the pointers
// swap(a, output);
// }
//
// // If we switched posize_ters an odd number of times,
// // make sure we copy before returning
// if (originalArr == output) {
// swap(a, output);
// for (size_t i = 0; i < n; i++)
// a[i] = output[i];
// }
//
// delete[] output, delete[] count;
// }
constexpr void radix_sort_256(int32_t a[], int n){
int max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max)
max = a[i];
for (int pos = 1; max / pos > 0; pos *= 10)
countsort(a, n, pos);
} }
template<typename T> template<typename T>
inline constexpr void radix_sort(const Array<T>& a) noexcept { constexpr void radix_sort(const Array<T>& a) noexcept {
radix_sort_256(a.data.get(), a.length); radix_sort_256(a.data.get(), a.length);
} }
template<typename T> // template<typename T>
inline Array<size_t> radix_sort_arg(const Array<T>& a) noexcept { // constexpr Array<size_t> radix_sort_arg(const Array<T>& a) noexcept {
Array<T> indices = range(a.length); // Array<T> indices = range(a.length);
//
return indices; // return indices;
} // }
}; };

View File

@ -1,83 +1,133 @@
#include <numeric>
#include <cassert> #include <cassert>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include "toolbox.hpp" #include "toolbox.hpp"
namespace asp { namespace asp {
static constexpr size_t N_TIMES = 10; /**
static constexpr std::array<const char* const, N_TIMES> time_formats = { "ns", "us", "ms", "s", "m", "h", "j", "w", "M", "y" }; * @brief Swap two given memory values
static constexpr std::array<uint16_t, N_TIMES> 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<uint64_t>()); * @tparam T Type of memory placeholder
* @param a Firat memory pointer
* @param b Second memory pointer
*/
template<typename T>
constexpr void swap(T* const a, T* const b) noexcept {
const T temp = *a;
*a = *b;
*b = temp;
}
/**
* @brief Convert a given number to string
*
* @param num Number to convert
* @param offset of the string location to append
* @param str String to append the number to
* @return number of written bytes
*/
constexpr size_t ullstr(uint64_t num, const size_t offset, char* const str) noexcept {
size_t i = 0;
for (; num > 0; num /= 10)
str[offset + i++] = num % 10 + '0';
str[offset + i] = '\0';
for (size_t j = 0; j < i / 2; ++j)
swap(str + offset + j, str + offset + i - j - 1);
return i;
}
/**
* @brief Cast a variable to an unsigned 64 bit integer
*
* @tparam T Type of the variable to cast
* @param vae Variable to cast
* @return Casted variable
*/
template<typename T>
static constexpr uint64_t u64(const T var) noexcept {
return static_cast<uint64_t>(var);
}
static constexpr const size_t STR_BUFFER_SIZE = 64;
static constexpr const int8_t N_TIMES = 11;
static constexpr const std::array<const char* const, N_TIMES> time_formats = { "ns", "us", "ms", "s", "m", "h", "j", "w", "M", "y", "c" };
static constexpr const std::array<const uint64_t, N_TIMES> time_numbers = { 1, 1000, 1000000, u64(1e9), u64(6e10), u64(36e11), u64(864e11),
u64(6048e11), u64(26298e11), u64(315576e11), u64(315576e13) };
static constexpr size_t N_BYTES = 7; static constexpr size_t N_BYTES = 7;
static constexpr std::array<const char*, N_BYTES> bytes_formats = { "", "K", "M", "G", "P", "E", "Z" }; //, "Y" }; static constexpr const std::array<const char[3], N_BYTES> format_prefix = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
static constexpr uint64_t total_bytes = uint64_t(1)<<(10 * (N_BYTES - 1));
/** /**
* @brief Format the time in seconds in human readable format. * @brief Convert a given numbers of bytes to a human readable format
* *
* @param time Time in seconds * @param n Number of bytes
* @return std::string The formatted human readable string. * @return Human readable format of the numbers of bytes
*/ */
std::string format_time(const uint64_t time) noexcept { std::string format_byte_size(uint64_t n) noexcept {
return time < 2 ? std::to_string(time) + "s" : format_time_ns(time * (uint64_t)1e9); char s[STR_BUFFER_SIZE] = { 0 };
}
/** if(n == 0){
* @brief Format the time in nanoseconds in human readable format. sprintf(s, "0%s", format_prefix[0]);
* return s;
* @param time Time in nanoseconds
* @return std::string The formatted human readable string.
*/
std::string format_byte_size(uint64_t bytes) noexcept {
if (bytes == 0)
return "0B";
uint64_t prod = total_bytes;
std::string s = "";
uint64_t res;
for (size_t i = N_BYTES; i > 0; --i) {
if (bytes >= prod) {
res = bytes / prod;
bytes %= prod;
s += std::to_string(res) + bytes_formats[i - 1] + "B ";
}
prod /= uint64_t(1)<<10;
} }
if (s.back() == ' ') size_t j = 0;
s.pop_back(); for(int8_t i = static_cast<int8_t>(format_prefix.size() - 1) * 10; i >= 0; i -= 10){
const uint64_t nsi = n >> i;
if(nsi > 0){
const int8_t idx = i / 10;
j += ullstr(nsi, j, s);
for(int k = 0; format_prefix[idx][k] > 0; ++k)
s[j++] = format_prefix[idx][k];
s[j++] = ' ';
n &= u64(-1) >> (64 - i);
}
}
return s; /* Remove trailing character */
s[j - 1] = '\0';
return std::string(s);
} }
/** /**
* @brief Format the time in nanoseconds in human readable format. * @brief Convert a given number of nanoseconds to a human readable format
* *
* @param time Time in nanoseconds * @param time Number of nanoseconds
* @return std::string The formatted human readable string. * @return Human readable formatted string
*/ */
std::string format_time_ns(uint64_t time) noexcept { std::string format_time_ns(uint64_t time) noexcept {
if (time == 0) char s[STR_BUFFER_SIZE] = {0};
return "0ns"; size_t j = 0;
uint64_t prod = total_time;
std::string s = ""; if (time == 0){
uint64_t res; sprintf(s, "0%s", time_formats[0]);
for (size_t i = N_TIMES; i > 0; --i) { return s;
if (time >= prod) {
res = time / prod;
time %= prod;
s += std::to_string(res) + time_formats[i - 1] + " ";
}
prod /= time_numbers[i - 1];
} }
if (s.back() == ' ') uint64_t res;
s.pop_back(); for (int8_t i = time_numbers.size() - 1; i >= 0; --i) {
if (time >= time_numbers[i]) {
res = time / time_numbers[i];
time %= time_numbers[i];
j += ullstr(res, j, s);
for(int k = 0; time_formats[i][k] > 0; ++k)
s[j++] = time_formats[i][k];
s[j++] = ' ';
}
}
return s; /* Remove trailing character */
s[j - 1] = '\0';
std::string ss(s);
return ss;
} }
/** /**
@ -85,40 +135,65 @@ namespace asp {
* *
*/ */
void toolbox_unit_test(void) noexcept { void toolbox_unit_test(void) noexcept {
assert(std::string("0B") == format_byte_size(uint64_t(0))); assert(std::string("0B") == format_byte_size(u64(0)));
assert(std::string("1B") == format_byte_size(uint64_t(1))); assert(std::string("1B") == format_byte_size(u64(1)));
assert(std::string("1KB") == format_byte_size(uint64_t(1)<<10)); assert(std::string("1KB") == format_byte_size(u64(1) << 10));
assert(std::string("1MB") == format_byte_size(uint64_t(1)<<20)); assert(std::string("1MB") == format_byte_size(u64(1) << 20));
assert(std::string("1GB") == format_byte_size(uint64_t(1)<<30)); assert(std::string("1GB") == format_byte_size(u64(1) << 30));
assert(std::string("1PB") == format_byte_size(uint64_t(1)<<40)); assert(std::string("1TB") == format_byte_size(u64(1) << 40));
assert(std::string("1EB") == format_byte_size(uint64_t(1)<<50)); assert(std::string("1PB") == format_byte_size(u64(1) << 50));
assert(std::string("1ZB") == format_byte_size(uint64_t(1)<<60)); assert(std::string("1EB") == format_byte_size(u64(1) << 60));
//assert(std::string("1YB") == format_byte_size(uint64_t(1)<<70)); // uint64_t_MAX == 2**64 == 18446744073709551615 == -1
// UINT64_MAX == 18446744073709551615I64u == -1 assert(std::string("15EB 1023PB 1023TB 1023GB 1023MB 1023KB 1023B") == format_byte_size(u64(-1)));
assert(std::string("15ZB 1023EB 1023PB 1023GB 1023MB 1023KB 1023B") == format_byte_size(uint64_t(-1))); assert(std::string("15EB 1023PB 1023TB 1023GB 1023MB 1023KB 1023B") == format_byte_size(18446744073709551615ull));
assert(std::string("10EB 1000PB 1000TB 1000GB 1000MB 1000KB 1000B") == format_byte_size(12656215539330294760ull));
assert(std::string("0s") == format_time(uint64_t(0))); // https://en.wikipedia.org/wiki/Unit_of_time
assert(std::string("1s") == format_time(uint64_t(1))); assert(std::string("0ns") == format_time_ns(u64(0)));
assert(std::string("1m") == format_time(uint64_t(60))); assert(std::string("1ns") == format_time_ns(u64(1)));
assert(std::string("1h") == format_time(uint64_t(3600))); assert(std::string("10ns") == format_time_ns(u64(10)));
assert(std::string("1j") == format_time(uint64_t(86400))); assert(std::string("1us") == format_time_ns(u64(1e3)));
assert(std::string("1w") == format_time(uint64_t(604800))); assert(std::string("1ms") == format_time_ns(u64(1e6)));
assert(std::string("1M") == format_time(uint64_t(2419200))); assert(std::string("10ms") == format_time_ns(u64(1e7)));
assert(std::string("1y") == format_time(uint64_t(29030400))); assert(std::string("100ms") == format_time_ns(u64(1e8)));
assert(std::string("1s") == format_time_ns(u64(1e9)));
assert(std::string("0ns") == format_time_ns(uint64_t(0))); assert(std::string("10s") == format_time_ns(u64(1e10)));
assert(std::string("1ns") == format_time_ns(uint64_t(1))); assert(std::string("1m") == format_time_ns(u64(6e10)));
assert(std::string("1us") == format_time_ns(uint64_t(1e3))); assert(std::string("1m 26s 400ms") == format_time_ns(u64(864e8)));
assert(std::string("1ms") == format_time_ns(uint64_t(1e6))); assert(std::string("1m 40s") == format_time_ns(u64(1e11)));
assert(std::string("1s") == format_time_ns(uint64_t(1e9))); assert(std::string("16m 40s") == format_time_ns(u64(1e12)));
assert(std::string("1m") == format_time_ns(uint64_t(6e10))); assert(std::string("1h") == format_time_ns(u64(36e11)));
assert(std::string("1h") == format_time_ns(uint64_t(36e11))); assert(std::string("1j") == format_time_ns(u64(864e11)));
assert(std::string("1j") == format_time_ns(uint64_t(864e11))); assert(std::string("1w") == format_time_ns(u64(6048e11)));
assert(std::string("1w") == format_time_ns(uint64_t(6048e11))); assert(std::string("1w 4j 13h 46m 40s") == format_time_ns(u64(1e15)));
assert(std::string("1M") == format_time_ns(uint64_t(24192e11))); assert(std::string("2w") == format_time_ns(u64(12096e11)));
assert(std::string("1y") == format_time_ns(uint64_t(290304e11))); assert(std::string("3w 6j 5h 5m 35s 800ms") == format_time_ns(u64(23511358e8)));
// UINT64_MAX == 18446744073709551615I64u == -1 assert(std::string("3w 6j 7h 43m 4s 700ms") == format_time_ns(u64(23605847e8)));
assert(std::string("635y 5M 3j 23h 34m 33s 709ms 551us 615ns") == format_time_ns(uint64_t(-1))); assert(std::string("3w 6j 7h 43m 11s 600ms") == format_time_ns(u64(23605916e8)));
assert(std::string("3w 6j 13h 18m 33s 200ms") == format_time_ns(u64(23807132e8)));
assert(std::string("4w 1j 12h 44m 2s 900ms") == format_time_ns(u64(25514429e8)));
assert(std::string("1M") == format_time_ns(u64(26298e11)));
assert(std::string("1M 1w 2j 13h 30m") == format_time_ns(u64(3456e12)));
assert(std::string("4M 4j 6h") == format_time_ns(u64(108864e11)));
assert(std::string("11M 2w 5j 13h 22m 48s") == format_time_ns(u64(30617568e9)));
assert(std::string("11M 4w 2j 4h 30m") == format_time_ns(u64(31536e12)));
assert(std::string("1y") == format_time_ns(u64(315576e11)));
assert(std::string("11M 4w 2j 10h 18m 45s") == format_time_ns(u64(31556925e9)));
assert(std::string("11M 4w 2j 10h 19m 12s") == format_time_ns(u64(31556952e9)));
assert(std::string("1y 9m 9s") == format_time_ns(u64(31558149e9)));
assert(std::string("1y 18h") == format_time_ns(u64(316224e11)));
assert(std::string("4y") == format_time_ns(u64(1262304e11)));
assert(std::string("5y") == format_time_ns(u64(157788e12)));
assert(std::string("10y") == format_time_ns(u64(315576e12)));
assert(std::string("15y") == format_time_ns(u64(473364e12)));
assert(std::string("20y") == format_time_ns(u64(631152e12)));
assert(std::string("31y 8M 1w 19h 46m 40s") == format_time_ns(u64(1e18)));
assert(std::string("50y") == format_time_ns(u64(157788e13)));
assert(std::string("1c") == format_time_ns(u64(315576e13)));
// uint64_t_MAX == 2**64 == 18446744073709551615 == -1
assert(std::string("5c 84y 6M 2w 1j 8h 34m 33s 709ms 551us 615ns") == format_time_ns(u64(-1)));
assert(std::string("5c 84y 6M 2w 1j 8h 34m 33s 709ms 551us 615ns") == format_time_ns(18446744073709551615ull));
assert(std::string("1c 10y 10M 3w 6j 10h 10m 10s 100ms 100us 100ns") == format_time_ns(3500003410100100100ull));
} }
std::string thousand_sep(const uint64_t& k, const char& sep) noexcept { std::string thousand_sep(const uint64_t& k, const char& sep) noexcept {
@ -145,13 +220,4 @@ namespace asp {
std::string thousand_sep(const uint64_t& k) noexcept { std::string thousand_sep(const uint64_t& k) noexcept {
return thousand_sep(k, ','); return thousand_sep(k, ',');
} }
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
}
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <chrono> #include <chrono>
#include <string> #include <string>
#include <array>
#define W_NAME 49 #define W_NAME 49
#define W_TIME 17 #define W_TIME 17
@ -19,28 +20,88 @@ namespace asp {
void toolbox_unit_test(void) noexcept; void toolbox_unit_test(void) noexcept;
std::string thousand_sep(const uint64_t&, const char&) noexcept; std::string thousand_sep(const uint64_t&, const char&) noexcept;
std::string thousand_sep(const uint64_t&) noexcept; std::string thousand_sep(const uint64_t&) noexcept;
void print_separator(const char* const) noexcept;
template <typename F, typename... Args> /**
inline constexpr void measure_time_void(const char* step_name, const F& fnc, Args &&...args) noexcept { * @brief Print a formatted row of titles with of gaps seperated by a separator.
*
* @param gaps List of size gaps
* @param titles List of titles
* @param separator Separator character between each gap
*/
template<size_t N>
constexpr void formatted_row(const std::array<int32_t, N>& gaps, const std::array<const char* const, N>& titles,
const char* const separator = "", const char line_ending = '\n') noexcept {
for(size_t i = 0; i < N; ++i)
printf("%s %*s ", separator, -gaps[i], titles[i]);
printf("%s%c", separator, line_ending);
}
/**
* @brief Print a formatted line of repeated characters.
*
* @param gaps List of size gaps
* @param right Character on the left
* @param middle Character between each separator
* @param separator Separator character between each gap
* @param left Character on the right
*/
template<size_t N>
constexpr void formatted_line(const std::array<int32_t, N>& gaps, const char* const left, const char* const middle,
const char* const separator, const char* const right, const char line_ending = '\n') noexcept {
printf("%s", left);
for(size_t i = 0; i < N; ++i){
for(int32_t j = std::abs(gaps[i]) + 2; j > 0; --j)
printf("%s", separator);
if(i != N - 1)
printf("%s", middle);
}
printf("%s%c", right, line_ending);
}
/**
* @brief Print a formatted header with the given titles and sizes.
*
* @param gaps List of size gaps
* @param titles List of titles
*/
template<size_t N>
constexpr void header(const std::array<int32_t, N>& gaps, const std::array<const char* const, N>& titles) noexcept {
formatted_line(gaps, "", "", "", "");
formatted_row(gaps, titles);
formatted_line(gaps, "", "", "", "");
}
/**
* @brief Print a formatted footer with the given sizes.
*
* @param gaps List of size gaps
*/
template<size_t N>
constexpr void footer(const std::array<int32_t, N>& gaps) noexcept {
formatted_line(gaps, "", "", "", "");
}
template <typename F, size_t N, typename... Args>
constexpr void measure_time_void(const std::array<int32_t, N>& gaps, const char* step_name, const F& fnc, Args &&...args) noexcept {
#ifndef __DEBUG #ifndef __DEBUG
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress"); formatted_row(gaps, { "Testing summary", "In progress", "In progress" }, "", '\r');
#endif #endif
const auto start = time(); const auto start = time();
fnc(std::forward<Args>(args)...); fnc(std::forward<Args>(args)...);
const long long timespent = duration_ns(time() - start); const long long timespent = duration_ns(time() - start);
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str()); formatted_row(gaps, { step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str() });
} }
template <typename T, typename F, typename... Args> template <typename T, typename F, size_t N, typename... Args>
inline constexpr T measure_time(const char* step_name, const F& fnc, Args &&...args) noexcept { constexpr T measure_time(const std::array<int32_t, N>& gaps, const char* step_name, const F& fnc, Args &&...args) noexcept {
#ifndef __DEBUG #ifndef __DEBUG
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress"); formatted_row(gaps, { "Testing summary", "In progress", "In progress" }, "", '\r');
#endif #endif
const auto start = time(); const auto start = time();
const T res = fnc(std::forward<Args>(args)...); const T res = fnc(std::forward<Args>(args)...);
const long long timespent = duration_ns(time() - start); const long long timespent = duration_ns(time() - start);
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str()); formatted_row(gaps, { step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str() });
return res; return res;
} }
}; };