diff --git a/data.cpp b/data.cpp index 48dbf97..d7aa069 100644 --- a/data.cpp +++ b/data.cpp @@ -52,8 +52,8 @@ static void test_argsort(const std::array& gaps, const asp::Array } template -static asp::Array create_random_array(const size_t& n) noexcept { - asp::Array original(n); +static inline asp::Array create_random_array(const int64_t n) noexcept { + asp::Array original((size_t(n))); std::random_device rd; std::default_random_engine gen(rd()); std::uniform_int_distribution distribution(std::numeric_limits::min(), std::numeric_limits::max()); @@ -64,22 +64,20 @@ int32_t main(int32_t argc, char** argv) { asp::toolbox_unit_test(); using array_type = uint16_t; - size_t N = (static_cast(1)<<15) - 1; - // size_t N = std::numeric_limits::max(); + int64_t N = (static_cast(1)<<15) - 1; + // int64_t N = std::numeric_limits::max(); if (argc > 2) { fprintf(stderr, "Too many arguments\nUsage: ./data \n"); return EXIT_FAILURE; } else if (argc == 2) - N = std::strtoul(argv[1], argv + strlen(argv[1]) + 1, 10); + N = std::strtol(argv[1], argv + strlen(argv[1]) + 1, 10); 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 gaps = { 48, -17, 25 }; + sprintf(title, "Sorting %s elements of %s", asp::thousand_sep(N).c_str(), asp::format_byte_size(2 * uint64_t(N) * sizeof(array_type)).c_str()); + constexpr std::array gaps = { 48, -17, 25 }; asp::header(gaps, { title, "Time spent (ns)", "Formatted time spent" }); const asp::Array original = asp::measure_time>(gaps, "Creating random array", create_random_array, N); - // const asp::Array original = { 2, 5, 3, 0, 2, 3, 0, 3 }; - // asp::print(original); test_sort(gaps, original, asp::bubble_sort, "Bubble sort"); test_argsort(gaps, original, asp::bubble_sort_arg, "Bubble sort (arg)"); diff --git a/toolbox.cpp b/toolbox.cpp index ce5f59e..d5048dc 100644 --- a/toolbox.cpp +++ b/toolbox.cpp @@ -1,24 +1,11 @@ #include -#include #include +#include +#include + #include "toolbox.hpp" namespace asp { - /** - * @brief Swap two given memory values - * - * @tparam T Type of memory placeholder - * @param a Firat memory pointer - * @param b Second memory pointer - */ - template - 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 * @@ -31,12 +18,12 @@ namespace asp { size_t i = 0; for (; num > 0; num /= 10) - str[offset + i++] = num % 10 + '0'; + str[offset + i++] = char(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); + std::swap(str[offset + j], str[offset + i - j - 1]); return i; } @@ -49,7 +36,7 @@ namespace asp { */ template static constexpr uint64_t u64(const T var) noexcept { - return static_cast(var); + return uint64_t(var); } static constexpr const size_t STR_BUFFER_SIZE = 64; @@ -77,10 +64,10 @@ namespace asp { } size_t j = 0; - for(int8_t i = static_cast(format_prefix.size() - 1) * 10; i >= 0; i -= 10){ + for(uint8_t i = uint8_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; + const uint8_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]; @@ -88,6 +75,12 @@ namespace asp { n &= u64(-1) >> (64 - i); } } + if(n > 0){ + j += ullstr(n, j, s); + for(int k = 0; format_prefix[0][k] > 0; ++k) + s[j++] = format_prefix[0][k]; + s[j++] = ' '; + } /* Remove trailing character */ s[j - 1] = '\0'; @@ -111,7 +104,7 @@ namespace asp { } uint64_t res; - for (int8_t i = time_numbers.size() - 1; i >= 0; --i) { + for (uint8_t i = time_numbers.size() - 1; i > 0; --i) { if (time >= time_numbers[i]) { res = time / time_numbers[i]; time %= time_numbers[i]; @@ -121,6 +114,14 @@ namespace asp { s[j++] = ' '; } } + if (time >= time_numbers[0]) { + res = time / time_numbers[0]; + time %= time_numbers[0]; + j += ullstr(res, j, s); + for(int k = 0; time_formats[0][k] > 0; ++k) + s[j++] = time_formats[0][k]; + s[j++] = ' '; + } /* Remove trailing character */ s[j - 1] = '\0'; diff --git a/toolbox.hpp b/toolbox.hpp index 515abf5..7fe398a 100644 --- a/toolbox.hpp +++ b/toolbox.hpp @@ -90,7 +90,7 @@ namespace asp { const auto start = time(); fnc(std::forward(args)...); const long long timespent = duration_ns(time() - start); - formatted_row(gaps, { 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(uint64_t(timespent)).c_str() }); } template @@ -101,7 +101,7 @@ namespace asp { const auto start = time(); const T res = fnc(std::forward(args)...); const long long timespent = duration_ns(time() - start); - formatted_row(gaps, { 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(uint64_t(timespent)).c_str() }); return res; } };