Compare commits
3 Commits
ac6755517b
...
9af11bd4ec
Author | SHA1 | Date | |
---|---|---|---|
|
9af11bd4ec | ||
|
e4d6ee79ff | ||
|
8058fac200 |
@ -1,6 +1,8 @@
|
|||||||
FROM alpine:3.19.1
|
FROM alpine:3.20.0
|
||||||
|
|
||||||
RUN apk add --no-cache make=4.4.1-r2 g++=13.2.1_git20231014-r0 \
|
RUN apk add --no-cache \
|
||||||
|
make=4.4.1-r2 \
|
||||||
|
g++=13.2.1_git20240309-r0 \
|
||||||
&& adduser --disabled-password saundersp
|
&& adduser --disabled-password saundersp
|
||||||
|
|
||||||
USER saundersp
|
USER saundersp
|
||||||
|
4
Makefile
4
Makefile
@ -2,8 +2,8 @@ CC := g++ -m64 -std=c++17
|
|||||||
OBJ_DIR := bin
|
OBJ_DIR := bin
|
||||||
SRC_DIR := .
|
SRC_DIR := .
|
||||||
#CFLAGS := -Og -g -rdynamic -pg -ggdb3 -D__DEBUG
|
#CFLAGS := -Og -g -rdynamic -pg -ggdb3 -D__DEBUG
|
||||||
CFLAGS := -O2
|
CFLAGS := -O4
|
||||||
CFLAGS := $(CFLAGS) -Wall -Wextra -Wno-error=unused-function -pedantic
|
CFLAGS := $(CFLAGS) -Wall -Werror -Wextra -Wno-error=unused-function -pedantic
|
||||||
EXEC := $(OBJ_DIR)/data
|
EXEC := $(OBJ_DIR)/data
|
||||||
SRC := $(wildcard $(SRC_DIR)/*.cpp)
|
SRC := $(wildcard $(SRC_DIR)/*.cpp)
|
||||||
HEADER := $(wildcard $(SRC_DIR)/*.hpp)
|
HEADER := $(wildcard $(SRC_DIR)/*.hpp)
|
||||||
|
16
data.cpp
16
data.cpp
@ -4,7 +4,7 @@
|
|||||||
#include "data.hpp"
|
#include "data.hpp"
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static bool is_arg_sorted(const asp::Array<T>& a, const asp::Array<size_t>& indices) noexcept {
|
constexpr static 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 (a[indices[i - 1]] > a[indices[i]])
|
||||||
return false;
|
return false;
|
||||||
@ -12,7 +12,7 @@ static bool is_arg_sorted(const asp::Array<T>& a, const asp::Array<size_t>& indi
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static bool is_sorted(const asp::Array<T>& a) noexcept {
|
constexpr static 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;
|
||||||
@ -20,9 +20,9 @@ static bool is_sorted(const asp::Array<T>& a) noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static void test_sort(const asp::Array<T>& original, void (* const fnc)(const asp::Array<T>&), const char* title) noexcept {
|
constexpr static void test_sort(const asp::Array<T>& original, void (* const fnc)(const asp::Array<T>&), const char* const title) noexcept {
|
||||||
#ifdef __DEBUG
|
#ifdef __DEBUG
|
||||||
printf("xxxxxxxxxxxxxxx INGORE 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(title, fnc, a);
|
||||||
@ -36,9 +36,9 @@ static void test_sort(const asp::Array<T>& original, void (* const fnc)(const as
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static void test_argsort(const asp::Array<T>& original, asp::Array<size_t>(* const fnc)(const asp::Array<T>&), const char* title) noexcept {
|
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 {
|
||||||
#ifdef __DEBUG
|
#ifdef __DEBUG
|
||||||
printf("xxxxxxxxxxxxxxx INGORE 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>>(title, fnc, a);
|
||||||
@ -56,8 +56,8 @@ static asp::Array<T> create_random_array(const size_t& n) noexcept {
|
|||||||
asp::Array<T> original(n);
|
asp::Array<T> original(n);
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
std::default_random_engine gen(rd());
|
std::default_random_engine gen(rd());
|
||||||
std::uniform_int_distribution<T> distrib(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
|
std::uniform_int_distribution<T> distribution(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
|
||||||
return std::move(asp::map(original, [& distrib, & gen](const size_t&, const T&) -> const T { return distrib(gen); }));
|
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) {
|
int32_t main(int32_t argc, char** argv) {
|
||||||
|
96
data.hpp
96
data.hpp
@ -11,7 +11,7 @@ namespace asp {
|
|||||||
std::shared_ptr<T[]> data;
|
std::shared_ptr<T[]> data;
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
|
|
||||||
Array() noexcept = delete;
|
Array(void) noexcept = delete;
|
||||||
Array(const size_t& size) noexcept: data(std::shared_ptr<T[]>(new T[size])), length(size) {
|
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);
|
||||||
@ -33,7 +33,7 @@ namespace asp {
|
|||||||
other.length = 0;
|
other.length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr T& operator[](const size_t& index) const {
|
inline 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,7 +45,7 @@ namespace asp {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
int32_t print(const Array<T>& a, const char* format) noexcept {
|
inline 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[BUFSIZ] = { 0 };
|
||||||
@ -57,33 +57,33 @@ namespace asp {
|
|||||||
return num_written;
|
return num_written;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t print(const Array<uint16_t>& a) noexcept {
|
inline constexpr int32_t print(const Array<uint16_t>& a) noexcept {
|
||||||
return print(a, "%b");
|
return print(a, "%b");
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t print(const Array<int32_t>& a) noexcept {
|
inline constexpr int32_t print(const Array<int32_t>& a) noexcept {
|
||||||
return print(a, "%i");
|
return print(a, "%i");
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t print(const Array<uint64_t>& a) noexcept {
|
inline constexpr int32_t print(const Array<uint64_t>& a) noexcept {
|
||||||
return print(a, "%lu");
|
return print(a, "%lu");
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t print(const Array<int16_t>& a) noexcept {
|
inline constexpr int32_t print(const Array<int16_t>& a) noexcept {
|
||||||
//printf("%i\n", a[0]);
|
//printf("%i\n", a[0]);
|
||||||
return print(a, "%i");
|
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());
|
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);
|
return printf("%s\n", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr T& max(const Array<T>& a) noexcept {
|
inline 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 +92,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr T& min(const Array<T>& a) noexcept {
|
inline 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,19 +101,19 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename F>
|
template<typename T, typename F>
|
||||||
Array<T>& map(Array<T>& a, const F& fnc) noexcept {
|
inline 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>
|
||||||
void foreach(const Array<T>& a, const F& fnc) noexcept {
|
inline 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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 std::move(map(a, [](const size_t& i, const size_t&) -> const size_t& {
|
||||||
return i;
|
return i;
|
||||||
@ -121,15 +121,15 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
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;
|
const T temp = *a;
|
||||||
*a = *b;
|
*a = *b;
|
||||||
*b = temp;
|
*b = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void bubble_sort(const Array<T>& a) noexcept {
|
inline constexpr void bubble_sort(const Array<T>& a) noexcept {
|
||||||
size_t j;
|
size_t j = 0;
|
||||||
for (size_t i = 0; i < a.length; ++i)
|
for (size_t i = 0; i < a.length; ++i)
|
||||||
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])
|
||||||
@ -137,7 +137,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> bubble_sort_arg(const Array<T>& a) noexcept {
|
inline 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)
|
||||||
@ -151,7 +151,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static size_t qs_partition(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
inline 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 +161,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static void quicksort(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
inline 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,17 +172,17 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void quicksort(const Array<T>& a) noexcept {
|
inline 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>
|
||||||
static void quicksort_iter(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
inline 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;
|
||||||
// push initial values of l and h to stack
|
// 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
|
// initialize top of stack
|
||||||
size_t top = 1;
|
size_t top = 1;
|
||||||
@ -220,12 +220,12 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void quicksort_iter(const Array<T>& a) noexcept {
|
inline 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>
|
||||||
static size_t qs_arg_partition(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
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 {
|
||||||
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 +238,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static void quicksort_arg(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
inline 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,24 +249,24 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> quicksort_arg(const Array<T>& other, const size_t& l, const size_t& h) noexcept {
|
inline 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>
|
||||||
Array<size_t> quicksort_arg(const Array<T>& a) noexcept {
|
inline 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>
|
||||||
void quicksort_arg_iter(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
inline 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* 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;
|
||||||
@ -304,7 +304,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> quicksort_arg_iter(const Array<T>& a) noexcept {
|
inline 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 +315,25 @@ namespace asp {
|
|||||||
size_t indice;
|
size_t indice;
|
||||||
T val;
|
T val;
|
||||||
|
|
||||||
ArgVal() noexcept = default;
|
ArgVal(void) noexcept = default;
|
||||||
ArgVal(const size_t& _i, const T& _v) noexcept : indice(_i), val(_v) {}
|
ArgVal(const size_t& _i, const T& _v) noexcept : indice(_i), val(_v) {}
|
||||||
|
|
||||||
constexpr bool operator>(const ArgVal<T>& other) const noexcept {
|
inline constexpr bool operator>(const ArgVal<T>& other) const noexcept {
|
||||||
return std::move(val > other.val);
|
return std::move(val > other.val);
|
||||||
}
|
}
|
||||||
constexpr bool operator<(const ArgVal<T>& other) const noexcept {
|
inline constexpr bool operator<(const ArgVal<T>& other) const noexcept {
|
||||||
return std::move(val < other.val);
|
return std::move(val < other.val);
|
||||||
}
|
}
|
||||||
constexpr bool operator>=(const ArgVal<T>& other) const noexcept {
|
inline constexpr bool operator>=(const ArgVal<T>& other) const noexcept {
|
||||||
return std::move(val >= other.val);
|
return std::move(val >= other.val);
|
||||||
}
|
}
|
||||||
constexpr bool operator<=(const ArgVal<T>& other) const noexcept {
|
inline 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>
|
||||||
static void merge(const Array<T>& a, const size_t& l, const size_t& m, const size_t& r) noexcept {
|
inline 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 +354,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void mergesort(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
|
inline 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 +365,12 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void mergesort(const Array<T>& a) noexcept {
|
inline 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>
|
||||||
Array<size_t> mergesort_arg(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
|
inline 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,11 +386,11 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> mergesort_arg(const Array<T>& a) noexcept {
|
inline 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
//static void count_sort(const Array<int32_t>& a, const int32_t& exp, const int32_t& d) noexcept {
|
//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);
|
// Array<int32_t> output(a.length), count(d);
|
||||||
// memset(&count[0], 0, d * sizeof(int32_t));
|
// memset(&count[0], 0, d * sizeof(int32_t));
|
||||||
|
|
||||||
@ -410,7 +410,7 @@ namespace asp {
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void counting_sort(const Array<T>& a) noexcept {
|
inline constexpr void counting_sort(const Array<T>& a) noexcept {
|
||||||
Array<T> output(a);
|
Array<T> output(a);
|
||||||
|
|
||||||
map(a, [output](const size_t& i, const T&) -> const T& {
|
map(a, [output](const size_t& i, const T&) -> const T& {
|
||||||
@ -419,14 +419,14 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> counting_sort_arg(const Array<T>& a) noexcept {
|
inline 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>
|
template<typename T>
|
||||||
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<typename T>
|
//template<typename T>
|
||||||
//void radix_sort(const Array<int32_t>& a) noexcept {
|
//void radix_sort(const Array<int32_t>& a) noexcept {
|
||||||
if (n <= 1)
|
if (n <= 1)
|
||||||
@ -434,7 +434,7 @@ namespace asp {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
T* output = new T[n]; // output array
|
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
|
T* originalArr = a; // So we know which was input
|
||||||
|
|
||||||
for (size_t shift = 0, s = 0; shift < 4; shift++, s += 8) {
|
for (size_t shift = 0, s = 0; shift < 4; shift++, s += 8) {
|
||||||
@ -479,12 +479,12 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void radix_sort(const Array<T>& a) noexcept {
|
inline 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>
|
||||||
Array<size_t> radix_sort_arg(const Array<T>& a) noexcept {
|
inline 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;
|
||||||
|
89
toolbox.cpp
89
toolbox.cpp
@ -5,14 +5,14 @@
|
|||||||
#include "toolbox.hpp"
|
#include "toolbox.hpp"
|
||||||
|
|
||||||
namespace asp {
|
namespace asp {
|
||||||
static const constexpr size_t N_TIMES = 10;
|
static constexpr size_t N_TIMES = 10;
|
||||||
static const constexpr std::array<const char*, N_TIMES> time_formats = { "ns", "us", "ms", "s", "m", "h", "j", "w", "M", "y" };
|
static constexpr std::array<const char* const, N_TIMES> time_formats = { "ns", "us", "ms", "s", "m", "h", "j", "w", "M", "y" };
|
||||||
static const constexpr std::array<uint16_t, N_TIMES> time_numbers = { 1, 1000, 1000, 1000, 60, 60, 24, 7, 4, 12 };
|
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>());
|
static const uint64_t total_time = std::accumulate(time_numbers.begin(), time_numbers.end(), uint64_t(1), std::multiplies<uint64_t>());
|
||||||
|
|
||||||
static const constexpr size_t N_BYTES = 7;
|
static constexpr size_t N_BYTES = 7;
|
||||||
static const constexpr std::array<const char*, N_BYTES> bytes_formats = { "", "K", "M", "G", "P", "E", "Z" }; //, "Y" };
|
static constexpr std::array<const char*, N_BYTES> bytes_formats = { "", "K", "M", "G", "P", "E", "Z" }; //, "Y" };
|
||||||
static const constexpr uint64_t total_bytes = static_cast<uint64_t>(1)<<(10 * (N_BYTES - 1));
|
static constexpr uint64_t total_bytes = uint64_t(1)<<(10 * (N_BYTES - 1));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Format the time in seconds in human readable format.
|
* @brief Format the time in seconds in human readable format.
|
||||||
@ -43,7 +43,7 @@ namespace asp {
|
|||||||
bytes %= prod;
|
bytes %= prod;
|
||||||
s += std::to_string(res) + bytes_formats[i - 1] + "B ";
|
s += std::to_string(res) + bytes_formats[i - 1] + "B ";
|
||||||
}
|
}
|
||||||
prod /= static_cast<uint64_t>(1)<<10;
|
prod /= uint64_t(1)<<10;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s.back() == ' ')
|
if (s.back() == ' ')
|
||||||
@ -84,49 +84,49 @@ namespace asp {
|
|||||||
* @brief Run some unitary tests on format functions
|
* @brief Run some unitary tests on format functions
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void toolbox_unit_test() noexcept {
|
void toolbox_unit_test(void) noexcept {
|
||||||
assert(std::string("0B") == format_byte_size(static_cast<uint64_t>(0)));
|
assert(std::string("0B") == format_byte_size(uint64_t(0)));
|
||||||
assert(std::string("1B") == format_byte_size(static_cast<uint64_t>(1)));
|
assert(std::string("1B") == format_byte_size(uint64_t(1)));
|
||||||
assert(std::string("1KB") == format_byte_size(static_cast<uint64_t>(1)<<10));
|
assert(std::string("1KB") == format_byte_size(uint64_t(1)<<10));
|
||||||
assert(std::string("1MB") == format_byte_size(static_cast<uint64_t>(1)<<20));
|
assert(std::string("1MB") == format_byte_size(uint64_t(1)<<20));
|
||||||
assert(std::string("1GB") == format_byte_size(static_cast<uint64_t>(1)<<30));
|
assert(std::string("1GB") == format_byte_size(uint64_t(1)<<30));
|
||||||
assert(std::string("1PB") == format_byte_size(static_cast<uint64_t>(1)<<40));
|
assert(std::string("1PB") == format_byte_size(uint64_t(1)<<40));
|
||||||
assert(std::string("1EB") == format_byte_size(static_cast<uint64_t>(1)<<50));
|
assert(std::string("1EB") == format_byte_size(uint64_t(1)<<50));
|
||||||
assert(std::string("1ZB") == format_byte_size(static_cast<uint64_t>(1)<<60));
|
assert(std::string("1ZB") == format_byte_size(uint64_t(1)<<60));
|
||||||
//assert(std::string("1YB") == format_byte_size(static_cast<uint64_t>(1)<<70));
|
//assert(std::string("1YB") == format_byte_size(uint64_t(1)<<70));
|
||||||
// UINT64_MAX == 18446744073709551615I64u == -1
|
// UINT64_MAX == 18446744073709551615I64u == -1
|
||||||
assert(std::string("15ZB 1023EB 1023PB 1023GB 1023MB 1023KB 1023B") == format_byte_size(static_cast<uint64_t>(-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<uint64_t>(0)));
|
assert(std::string("0s") == format_time(uint64_t(0)));
|
||||||
assert(std::string("1s") == format_time(static_cast<uint64_t>(1)));
|
assert(std::string("1s") == format_time(uint64_t(1)));
|
||||||
assert(std::string("1m") == format_time(static_cast<uint64_t>(60)));
|
assert(std::string("1m") == format_time(uint64_t(60)));
|
||||||
assert(std::string("1h") == format_time(static_cast<uint64_t>(3600)));
|
assert(std::string("1h") == format_time(uint64_t(3600)));
|
||||||
assert(std::string("1j") == format_time(static_cast<uint64_t>(86400)));
|
assert(std::string("1j") == format_time(uint64_t(86400)));
|
||||||
assert(std::string("1w") == format_time(static_cast<uint64_t>(604800)));
|
assert(std::string("1w") == format_time(uint64_t(604800)));
|
||||||
assert(std::string("1M") == format_time(static_cast<uint64_t>(2419200)));
|
assert(std::string("1M") == format_time(uint64_t(2419200)));
|
||||||
assert(std::string("1y") == format_time(static_cast<uint64_t>(29030400)));
|
assert(std::string("1y") == format_time(uint64_t(29030400)));
|
||||||
|
|
||||||
assert(std::string("0ns") == format_time_ns(static_cast<uint64_t>(0)));
|
assert(std::string("0ns") == format_time_ns(uint64_t(0)));
|
||||||
assert(std::string("1ns") == format_time_ns(static_cast<uint64_t>(1)));
|
assert(std::string("1ns") == format_time_ns(uint64_t(1)));
|
||||||
assert(std::string("1us") == format_time_ns(static_cast<uint64_t>(1e3)));
|
assert(std::string("1us") == format_time_ns(uint64_t(1e3)));
|
||||||
assert(std::string("1ms") == format_time_ns(static_cast<uint64_t>(1e6)));
|
assert(std::string("1ms") == format_time_ns(uint64_t(1e6)));
|
||||||
assert(std::string("1s") == format_time_ns(static_cast<uint64_t>(1e9)));
|
assert(std::string("1s") == format_time_ns(uint64_t(1e9)));
|
||||||
assert(std::string("1m") == format_time_ns(static_cast<uint64_t>(6e10)));
|
assert(std::string("1m") == format_time_ns(uint64_t(6e10)));
|
||||||
assert(std::string("1h") == format_time_ns(static_cast<uint64_t>(36e11)));
|
assert(std::string("1h") == format_time_ns(uint64_t(36e11)));
|
||||||
assert(std::string("1j") == format_time_ns(static_cast<uint64_t>(864e11)));
|
assert(std::string("1j") == format_time_ns(uint64_t(864e11)));
|
||||||
assert(std::string("1w") == format_time_ns(static_cast<uint64_t>(6048e11)));
|
assert(std::string("1w") == format_time_ns(uint64_t(6048e11)));
|
||||||
assert(std::string("1M") == format_time_ns(static_cast<uint64_t>(24192e11)));
|
assert(std::string("1M") == format_time_ns(uint64_t(24192e11)));
|
||||||
assert(std::string("1y") == format_time_ns(static_cast<uint64_t>(290304e11)));
|
assert(std::string("1y") == format_time_ns(uint64_t(290304e11)));
|
||||||
// UINT64_MAX == 18446744073709551615I64u == -1
|
// UINT64_MAX == 18446744073709551615I64u == -1
|
||||||
assert(std::string("635y 5M 3j 23h 34m 33s 709ms 551us 615ns") == format_time_ns(static_cast<uint64_t>(-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 thousand_sep(const uint64_t& k, const char& sep) noexcept {
|
||||||
std::string s = "", n = std::to_string(k);
|
std::string s = "", n = std::to_string(k);
|
||||||
|
|
||||||
int32_t c = 0;
|
uint8_t c = 0;
|
||||||
for (int32_t i = static_cast<int32_t>(n.size()) - 1; i >= 0; --i) {
|
for (int64_t i = int64_t(n.size()) - 1; i >= 0; --i) {
|
||||||
c++;
|
++c;
|
||||||
s.push_back(n[i]);
|
s.push_back(n[i]);
|
||||||
if (c == 3) {
|
if (c == 3) {
|
||||||
s.push_back(sep);
|
s.push_back(sep);
|
||||||
@ -146,11 +146,12 @@ namespace asp {
|
|||||||
return thousand_sep(k, ',');
|
return thousand_sep(k, ',');
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_separator(const char* title) noexcept {
|
void print_separator(const char* const title) noexcept {
|
||||||
#define S(N) std::string(N, '-').c_str()
|
#define S(N) std::string(N, '-').c_str()
|
||||||
const constexpr size_t SEPARATOR_SIZE = W_NAME + W_TIME + W_FTIME + 6 + 6;
|
constexpr size_t SEPARATOR_SIZE = W_NAME + W_TIME + W_FTIME + 6 + 6;
|
||||||
char separator[SEPARATOR_SIZE];
|
char separator[SEPARATOR_SIZE];
|
||||||
sprintf(separator, "|%s|%s|%s|\n", S(W_NAME + 2), S(W_TIME + 2), S(W_FTIME + 2));
|
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);
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,13 @@ namespace asp {
|
|||||||
std::string format_byte_size(uint64_t) noexcept;
|
std::string format_byte_size(uint64_t) noexcept;
|
||||||
std::string format_time(const uint64_t) noexcept;
|
std::string format_time(const uint64_t) noexcept;
|
||||||
std::string format_time_ns(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&, const char&) noexcept;
|
||||||
std::string thousand_sep(const uint64_t&) noexcept;
|
std::string thousand_sep(const uint64_t&) noexcept;
|
||||||
void print_separator(const char*) noexcept;
|
void print_separator(const char* const) noexcept;
|
||||||
|
|
||||||
template <typename F, typename... Args>
|
template <typename F, typename... Args>
|
||||||
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
|
#ifndef __DEBUG
|
||||||
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress");
|
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress");
|
||||||
#endif
|
#endif
|
||||||
@ -33,7 +33,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename F, typename... Args>
|
template <typename T, typename F, typename... Args>
|
||||||
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
|
#ifndef __DEBUG
|
||||||
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress");
|
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress");
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user