Compare commits

...

18 Commits

Author SHA1 Message Date
03f2607bda Dockerfile : updated base image to alpine:3.22.0 2025-06-19 21:03:03 +02:00
508cf9fa2c Dockerfile : updated base image to alpine:3.21.3 2025-02-21 12:49:35 +01:00
645f231d64 Updated Dockerfile base images to 3.21.2 2025-01-19 22:26:16 +01:00
d81b6c07ac Updated Dockerfile image and packages version to 3.21.0 2024-12-12 17:43:25 +01:00
e2b0eb4a50 Updated Dockerfile image and packages version 2024-11-07 21:31:46 +01:00
0cd833a07d docker-compose.yaml : disabled pulling for custom images 2024-07-22 22:11:05 +02:00
73d0996c30 Dockerfile : multi-phase to reduce image from 224 MB to 10 MB 2024-07-02 20:45:44 +02:00
9af11bd4ec Updated Dockerfile 2024-06-09 22:42:20 +02:00
e4d6ee79ff Makefile : raised optimization level to O4 2024-06-09 22:42:12 +02:00
8058fac200 Added inline and constexpr when possible 2024-06-09 22:41:37 +02:00
ac6755517b Simplified docker building 2024-05-01 13:36:08 +02:00
e5f2eac0f0 Dockerfile : updated alpine && put nproc in quote to avoid potential globbing 2024-03-09 20:44:03 +01:00
6011177dd4 Makefile : now checks if CC compiler is installed and executable 2024-03-09 20:36:07 +01:00
0da832aa8b Makefile now checks for dependencies 2023-12-16 08:50:26 +01:00
674c5da3cc Changed every int to int32_t 2023-12-16 08:47:35 +01:00
a19787f273 Removed unused header files 2023-12-16 08:05:21 +01:00
1193c504cd Added .dockerignore 2023-12-16 08:05:00 +01:00
ea871e388a Updated Dockerfile's alpine version 2023-12-16 08:04:52 +01:00
7 changed files with 199 additions and 153 deletions

View File

@ -1,22 +1,31 @@
FROM alpine:3.17.1
FROM alpine:3.22.0 AS builder
RUN apk add make g++
RUN apk add --no-cache \
libstdc++=14.2.0-r6 \
&& adduser --disabled-password saundersp
WORKDIR /home/saundersp/sorting_algorithms
RUN adduser \
--disabled-password \
--gecos '' \
--home "$(pwd)" \
--no-create-home \
saundersp
COPY . .
RUN chown -R saundersp /home/saundersp/sorting_algorithms
RUN apk add --no-cache \
make=4.4.1-r3 \
g++=14.2.0-r6
USER saundersp
RUN make -j $NPROC
WORKDIR /home/saundersp/sorting_algorithms
ENTRYPOINT ["bin/data"]
COPY *.cpp *.hpp Makefile ./
RUN make -j "$(nproc)"
FROM alpine:3.22.0
RUN apk add --no-cache \
libstdc++=14.2.0-r6 \
&& adduser --disabled-password saundersp
USER saundersp
WORKDIR /home/saundersp/sorting_algorithms
COPY --from=builder /home/saundersp/sorting_algorithms/bin/data .
ENTRYPOINT ["./data"]

View File

@ -1,11 +1,9 @@
CC := g++ -m64 -std=c++17
OBJ_DIR := bin
$(shell mkdir -p $(OBJ_DIR))
SRC_DIR := .
#CFLAGS := -Og -g -Wall -Wextra -Wno-error=unused-function -pedantic -rdynamic
#CFLAGS := $(CFLAGS) -pg -ggdb3
#CFLAGS := $(CFLAGS) -D__DEBUG
CFLAGS := -O4 -Wall -Wextra -Wno-error=unused-function
#CFLAGS := -Og -g -rdynamic -pg -ggdb3 -D__DEBUG
CFLAGS := -O4
CFLAGS := $(CFLAGS) -Wall -Werror -Wextra -Wno-error=unused-function -pedantic
EXEC := $(OBJ_DIR)/data
SRC := $(wildcard $(SRC_DIR)/*.cpp)
HEADER := $(wildcard $(SRC_DIR)/*.hpp)
@ -16,35 +14,65 @@ ifeq ($(OS), Windows_NT)
endif
OBJ := $(SRC:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.$(OBJ_EXT))
.PHONY: all start clean mrproper
.PHONY: all start profile debug check r2 clean mrproper
all: $(EXEC)
$(OBJ_DIR)/%.$(OBJ_EXT): $(SRC_DIR)/%.cpp $(SRC_DIR)/%.hpp
$(OBJ_DIR):
@mkdir -v $@
$(OBJ_DIR)/%.$(OBJ_EXT): $(SRC_DIR)/%.cpp $(SRC_DIR)/%.hpp | $(OBJ_DIR) check-cc-works
@echo Compiling $<
@$(CC) $(CFLAGS) -c $< -o $@
$(EXEC): $(OBJ)
$(EXEC): $(OBJ) | check-cc-works
@echo Linking objects files to $@
@$(CC) $(CFLAGS) $^ -o $@
start: $(EXEC)
@./$(EXEC)
profile: start
@gprof $(EXEC) gmon.out | gprof2dot | dot -Tpng -o output.png
profile: start | check-gprof-works check-gprof2dot-works
@gprof $(EXEC) gmon.out | gprof2dot | dot -T png -o output.png
debug: $(EXEC)
@gdb -q -tui $(EXEC) -x copies
debug: $(EXEC) | check-gdb-works
@gdb -q -tui $(EXEC)
check: $(EXEC)
check: $(EXEC) | check-valgrind-works
@valgrind -q -s --leak-check=full --show-leak-kinds=all $(EXEC)
r2: $(EXEC)
r2: $(EXEC) | check-r2-works
@r2 $(EXEC)
clean:
@rm $(EXEC) gmon.out output.png
@rm -fv $(EXEC) gmon.out output.png
mrproper:
@rm -r $(OBJ_DIR)
mrproper: clean
@rm -rv $(OBJ_DIR)
.PHONY: check-env
check-env: check-gprof2dot-works check-gprof-works check-gdb-works check-valgrind-works check-r2-works
.PHONY: check-cc-works
check-cc-works:
@${CC} --help >/dev/null 2>&1 || (echo 'Please install a valid CC compiler.' && exit 1)
.PHONY: check-gprof2dot-works
check-gprof2dot-works:
@gprof2dot --help >/dev/null 2>&1 || (echo 'Please install gprof2dot.' && exit 1)
.PHONY: check-gprof-works
check-gprof-works:
@gprof --version >/dev/null 2>&1 || (echo 'Please install gprof.' && exit 1)
.PHONY: check-gdb-works
check-gdb-works:
@gdb --version >/dev/null 2>&1 || (echo 'Please install gdb.' && exit 1)
.PHONY: check-valgrind-works
check-valgrind-works:
@valgrind --version >/dev/null 2>&1 || (echo 'Please install valgrind.' && exit 1)
.PHONY: check-r2-works
check-r2-works:
@r2 -v >/dev/null 2>&1 || (echo 'Please install r2.' && exit 1)

View File

@ -1,11 +1,10 @@
#include <iostream>
#include <assert.h>
#include <random>
#include "toolbox.hpp"
#include "data.hpp"
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)
if (a[indices[i - 1]] > a[indices[i]])
return false;
@ -13,7 +12,7 @@ static bool is_arg_sorted(const asp::Array<T>& a, const asp::Array<size_t>& indi
}
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)
if (a[i - 1] > a[i])
return false;
@ -21,9 +20,9 @@ static bool is_sorted(const asp::Array<T>& a) noexcept {
}
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
printf("xxxxxxxxxxxxxxx INGORE COPY ");
printf("xxxxxxxxxxxxxxx IGNORE COPY ");
#endif
const asp::Array<T> a(original);
asp::measure_time_void(title, fnc, a);
@ -37,9 +36,9 @@ static void test_sort(const asp::Array<T>& original, void (* const fnc)(const as
}
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
printf("xxxxxxxxxxxxxxx INGORE COPY ");
printf("xxxxxxxxxxxxxxx IGNORE COPY ");
#endif
const asp::Array<T> a(original);
const asp::Array<size_t> indices = asp::measure_time<asp::Array<size_t>>(title, fnc, a);
@ -57,11 +56,11 @@ static asp::Array<T> create_random_array(const size_t& n) noexcept {
asp::Array<T> original(n);
std::random_device rd;
std::default_random_engine gen(rd());
std::uniform_int_distribution<T> distrib(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); }));
std::uniform_int_distribution<T> distribution(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
return std::move(asp::map(original, [&distribution, &gen](const size_t&, const T&) -> const T { return distribution(gen); }));
}
int main(int argc, char** argv) {
int32_t main(int32_t argc, char** argv) {
asp::toolbox_unit_test();
using array_type = uint16_t;

121
data.hpp
View File

@ -1,8 +1,9 @@
#pragma once
#include <iostream>
#include <functional>
#include <memory>
#include <cstring>
#ifdef __DEBUG
#include <stdexcept>
#endif
namespace asp {
template<typename T>
@ -10,7 +11,7 @@ namespace asp {
std::shared_ptr<T[]> data;
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) {
// #ifdef __DEBUG
// printf("Creating array of size %lu\n", size);
@ -32,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);
@ -44,8 +45,8 @@ namespace asp {
};
template<typename T>
int print(const Array<T>& a, const char* format) noexcept {
int num_written = 0;
inline constexpr int32_t print(const Array<T>& a, const char* const format) noexcept {
int32_t num_written = 0;
num_written += printf("[");
char formatter[BUFSIZ] = { 0 };
sprintf(formatter, "%s,", format);
@ -56,29 +57,33 @@ namespace asp {
return num_written;
}
int print(const Array<int>& a) noexcept {
inline constexpr int32_t print(const Array<uint16_t>& a) noexcept {
return print(a, "%b");
}
inline constexpr int32_t print(const Array<int32_t>& a) noexcept {
return print(a, "%i");
}
int print(const Array<uint64_t>& a) noexcept {
inline constexpr int32_t print(const Array<uint64_t>& a) noexcept {
return print(a, "%lu");
}
int print(const Array<int16_t>& a) noexcept {
inline constexpr int32_t print(const Array<int16_t>& a) noexcept {
//printf("%i\n", a[0]);
return print(a, "%i");
}
int print(const std::string& s) noexcept {
inline int32_t print(const std::string& s) noexcept {
return printf("%s\n", s.c_str());
}
int print(const char* s) noexcept {
inline constexpr int32_t print(const char* const s) noexcept {
return printf("%s\n", s);
}
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];
for (size_t i = 1; i < a.length; ++i)
if (a[i] > max_el)
@ -87,7 +92,7 @@ namespace asp {
}
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];
for (size_t i = 1; i < a.length; ++i)
if (a[i] < max_el)
@ -96,19 +101,19 @@ namespace asp {
}
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)
a[i] = fnc(i, a[i]);
return a;
}
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)
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);
return std::move(map(a, [](const size_t& i, const size_t&) -> const size_t& {
return i;
@ -116,15 +121,15 @@ namespace asp {
}
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;
*a = *b;
*b = temp;
}
template<typename T>
void bubble_sort(const Array<T>& a) noexcept {
size_t j;
inline constexpr void bubble_sort(const Array<T>& 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])
@ -132,7 +137,7 @@ namespace asp {
}
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);
size_t j;
for (size_t i = 0; i < a.length; ++i)
@ -146,7 +151,7 @@ namespace asp {
}
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;
for (size_t j = l; j <= h; ++j)
if (a[j] < a[h])
@ -156,7 +161,7 @@ namespace asp {
}
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)
return;
@ -167,17 +172,17 @@ namespace asp {
}
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);
}
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
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;
@ -215,12 +220,12 @@ namespace asp {
}
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);
}
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;
for (size_t j = l; j <= h; ++j)
if (a[j] < a[h]){
@ -233,7 +238,7 @@ namespace asp {
}
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)
return;
@ -244,24 +249,24 @@ namespace asp {
}
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);
quicksort_arg(other, indices, l, h);
return indices;
}
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);
}
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
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;
@ -299,7 +304,7 @@ namespace asp {
}
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);
quicksort_arg_iter(a, indices, 0, a.length - 1);
return indices;
@ -310,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<T>& other) const noexcept {
inline constexpr bool operator>(const ArgVal<T>& other) const noexcept {
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);
}
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);
}
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);
}
};
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);
memcpy(&left_arr.data[0], &a[l], left_arr.length * sizeof(T));
@ -349,7 +354,7 @@ namespace asp {
}
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)
return;
@ -360,12 +365,12 @@ namespace asp {
}
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);
}
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);
map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> const ArgVal<T> {
@ -381,31 +386,31 @@ namespace asp {
}
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);
}
//static void count_sort(const Array<int>& a, const int& exp, const int& d) noexcept {
// Array<int> output(a.length), count(d);
// memset(&count[0], 0, d * sizeof(int));
//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 int&, const int& val) -> void {
// foreach(a, [count, exp, d](const int32_t&, const int32_t& val) -> void {
// count[(val / exp) % d]++;
// });
// for (int i = 1; i <= d; ++i)
// for (int32_t i = 1; i <= d; ++i)
// count[i] += count[i - 1];
// for (int i = a.length - 1; i >= 0; --i) {
// 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(int));
// memcpy(&a[0], &output[0], a.length * sizeof(int32_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);
map(a, [output](const size_t& i, const T&) -> const T& {
@ -414,22 +419,22 @@ namespace asp {
}
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);
return indices;
}
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>
//void radix_sort(const Array<int>& a) noexcept {
//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* 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) {
@ -447,7 +452,7 @@ namespace asp {
count[i] += count[i - 1];
// Build the output array
for (int i = n - 1; i >= 0; i--) {
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;
@ -474,12 +479,12 @@ namespace asp {
}
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);
}
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);
return indices;

5
docker-compose.yaml Normal file
View File

@ -0,0 +1,5 @@
services:
sorting_algorithms:
image: saundersp/sorting_algorithms
pull_policy: never
build: .

View File

@ -1,19 +1,18 @@
#include <numeric>
#include <cassert>
#include <algorithm>
#include <memory>
#include <array>
#include "toolbox.hpp"
namespace asp {
static const 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 const 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 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" };
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 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 const constexpr uint64_t total_bytes = static_cast<uint64_t>(1)<<(10 * (N_BYTES - 1));
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 uint64_t total_bytes = uint64_t(1)<<(10 * (N_BYTES - 1));
/**
* @brief Format the time in seconds in human readable format.
@ -44,7 +43,7 @@ namespace asp {
bytes %= prod;
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() == ' ')
@ -85,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<uint64_t>(0)));
assert(std::string("1B") == format_byte_size(static_cast<uint64_t>(1)));
assert(std::string("1KB") == format_byte_size(static_cast<uint64_t>(1)<<10));
assert(std::string("1MB") == format_byte_size(static_cast<uint64_t>(1)<<20));
assert(std::string("1GB") == format_byte_size(static_cast<uint64_t>(1)<<30));
assert(std::string("1PB") == format_byte_size(static_cast<uint64_t>(1)<<40));
assert(std::string("1EB") == format_byte_size(static_cast<uint64_t>(1)<<50));
assert(std::string("1ZB") == format_byte_size(static_cast<uint64_t>(1)<<60));
//assert(std::string("1YB") == format_byte_size(static_cast<uint64_t>(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<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("1s") == format_time(static_cast<uint64_t>(1)));
assert(std::string("1m") == format_time(static_cast<uint64_t>(60)));
assert(std::string("1h") == format_time(static_cast<uint64_t>(3600)));
assert(std::string("1j") == format_time(static_cast<uint64_t>(86400)));
assert(std::string("1w") == format_time(static_cast<uint64_t>(604800)));
assert(std::string("1M") == format_time(static_cast<uint64_t>(2419200)));
assert(std::string("1y") == format_time(static_cast<uint64_t>(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<uint64_t>(0)));
assert(std::string("1ns") == format_time_ns(static_cast<uint64_t>(1)));
assert(std::string("1us") == format_time_ns(static_cast<uint64_t>(1e3)));
assert(std::string("1ms") == format_time_ns(static_cast<uint64_t>(1e6)));
assert(std::string("1s") == format_time_ns(static_cast<uint64_t>(1e9)));
assert(std::string("1m") == format_time_ns(static_cast<uint64_t>(6e10)));
assert(std::string("1h") == format_time_ns(static_cast<uint64_t>(36e11)));
assert(std::string("1j") == format_time_ns(static_cast<uint64_t>(864e11)));
assert(std::string("1w") == format_time_ns(static_cast<uint64_t>(6048e11)));
assert(std::string("1M") == format_time_ns(static_cast<uint64_t>(24192e11)));
assert(std::string("1y") == format_time_ns(static_cast<uint64_t>(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<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 s = "", n = std::to_string(k);
int c = 0;
for (int i = static_cast<int>(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);
@ -147,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
}
}

View File

@ -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 <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
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 <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
printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress");
#endif