Compare commits

..

No commits in common. "0da832aa8ba448ff0bd98c8511d72b9743ec1fb7" and "a90aca8c5b8cefb28233a3c7643aa26d4073b64f" have entirely different histories.

6 changed files with 39 additions and 72 deletions

View File

@ -1,6 +0,0 @@
.ccls-cache
.git
bin
Dockerfile
LICENCE
README.md

View File

@ -1,4 +1,4 @@
FROM alpine:3.19.0 FROM alpine:3.17.1
RUN apk add make g++ RUN apk add make g++
@ -17,6 +17,6 @@ RUN chown -R saundersp /home/saundersp/sorting_algorithms
USER saundersp USER saundersp
RUN make -j $(nproc) RUN make -j $NPROC
ENTRYPOINT ["bin/data"] ENTRYPOINT ["bin/data"]

View File

@ -1,9 +1,11 @@
CC := g++ -m64 -std=c++17 CC := g++ -m64 -std=c++17
OBJ_DIR := bin OBJ_DIR := bin
$(shell mkdir -p $(OBJ_DIR))
SRC_DIR := . SRC_DIR := .
#CFLAGS := -Og -g -rdynamic -pg -ggdb3 -D__DEBUG #CFLAGS := -Og -g -Wall -Wextra -Wno-error=unused-function -pedantic -rdynamic
CFLAGS := -O2 #CFLAGS := $(CFLAGS) -pg -ggdb3
CFLAGS := $(CFLAGS) -Wall -Wextra -Wno-error=unused-function -pedantic #CFLAGS := $(CFLAGS) -D__DEBUG
CFLAGS := -O4 -Wall -Wextra -Wno-error=unused-function
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)
@ -14,14 +16,11 @@ ifeq ($(OS), Windows_NT)
endif endif
OBJ := $(SRC:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.$(OBJ_EXT)) OBJ := $(SRC:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.$(OBJ_EXT))
.PHONY: all start profile debug check r2 clean mrproper .PHONY: all start clean mrproper
all: $(EXEC) all: $(EXEC)
$(OBJ_DIR): $(OBJ_DIR)/%.$(OBJ_EXT): $(SRC_DIR)/%.cpp $(SRC_DIR)/%.hpp
@mkdir -v $@
$(OBJ_DIR)/%.$(OBJ_EXT): $(SRC_DIR)/%.cpp $(SRC_DIR)/%.hpp | $(OBJ_DIR)
@echo Compiling $< @echo Compiling $<
@$(CC) $(CFLAGS) -c $< -o $@ @$(CC) $(CFLAGS) -c $< -o $@
@ -32,43 +31,20 @@ $(EXEC): $(OBJ)
start: $(EXEC) start: $(EXEC)
@./$(EXEC) @./$(EXEC)
profile: start | check-gprof-works check-gprof2dot-works profile: start
@gprof $(EXEC) gmon.out | gprof2dot | dot -Tpng -o output.png @gprof $(EXEC) gmon.out | gprof2dot | dot -Tpng -o output.png
debug: $(EXEC) | check-gdb-works debug: $(EXEC)
@gdb -q -tui $(EXEC) -x copies @gdb -q -tui $(EXEC) -x copies
check: $(EXEC) | check-valgrind-works check: $(EXEC)
@valgrind -q -s --leak-check=full --show-leak-kinds=all $(EXEC) @valgrind -q -s --leak-check=full --show-leak-kinds=all $(EXEC)
r2: $(EXEC) | check-r2-works r2: $(EXEC)
@r2 $(EXEC) @r2 $(EXEC)
clean: clean:
@rm -fv $(EXEC) gmon.out output.png @rm $(EXEC) gmon.out output.png
mrproper: clean mrproper:
@rm -rv $(OBJ_DIR) @rm -r $(OBJ_DIR)
.PHONY: check-env
check-env: check-gprof2dot-works check-gprof-works check-gdb-works check-valgrind-works check-r2-works
.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,3 +1,4 @@
#include <iostream>
#include <assert.h> #include <assert.h>
#include <random> #include <random>
#include "toolbox.hpp" #include "toolbox.hpp"
@ -60,7 +61,7 @@ static asp::Array<T> create_random_array(const size_t& n) noexcept {
return std::move(asp::map(original, [& distrib, & gen](const size_t&, const T&) -> const T { return distrib(gen); })); return std::move(asp::map(original, [& distrib, & gen](const size_t&, const T&) -> const T { return distrib(gen); }));
} }
int32_t main(int32_t argc, char** argv) { int main(int argc, char** argv) {
asp::toolbox_unit_test(); asp::toolbox_unit_test();
using array_type = uint16_t; using array_type = uint16_t;

View File

@ -1,9 +1,8 @@
#pragma once #pragma once
#include <iostream>
#include <functional>
#include <memory> #include <memory>
#include <cstring> #include <cstring>
#ifdef __DEBUG
#include <stdexcept>
#endif
namespace asp { namespace asp {
template<typename T> template<typename T>
@ -45,8 +44,8 @@ namespace asp {
}; };
template<typename T> template<typename T>
int32_t print(const Array<T>& a, const char* format) noexcept { int print(const Array<T>& a, const char* format) noexcept {
int32_t num_written = 0; int num_written = 0;
num_written += printf("["); num_written += printf("[");
char formatter[BUFSIZ] = { 0 }; char formatter[BUFSIZ] = { 0 };
sprintf(formatter, "%s,", format); sprintf(formatter, "%s,", format);
@ -57,28 +56,24 @@ namespace asp {
return num_written; return num_written;
} }
int32_t print(const Array<uint16_t>& a) noexcept { int print(const Array<int>& a) noexcept {
return print(a, "%b");
}
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 { int print(const Array<uint64_t>& a) noexcept {
return print(a, "%lu"); return print(a, "%lu");
} }
int32_t print(const Array<int16_t>& a) noexcept { int 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 { int 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 { int print(const char* s) noexcept {
return printf("%s\n", s); return printf("%s\n", s);
} }
@ -390,23 +385,23 @@ namespace asp {
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 { //static void count_sort(const Array<int>& a, const int& exp, const int& d) noexcept {
// Array<int32_t> output(a.length), count(d); // Array<int> output(a.length), count(d);
// memset(&count[0], 0, d * sizeof(int32_t)); // memset(&count[0], 0, d * sizeof(int));
// foreach(a, [count, exp, d](const int32_t&, const int32_t& val) -> void { // foreach(a, [count, exp, d](const int&, const int& val) -> void {
// count[(val / exp) % d]++; // count[(val / exp) % d]++;
// }); // });
// for (int32_t i = 1; i <= d; ++i) // for (int i = 1; i <= d; ++i)
// count[i] += count[i - 1]; // count[i] += count[i - 1];
// for (int32_t i = a.length - 1; i >= 0; --i) { // for (int i = a.length - 1; i >= 0; --i) {
// output[count[(a[i] / exp) % d] - 1] = a[i]; // output[count[(a[i] / exp) % d] - 1] = a[i];
// count[(a[i] / exp) % d]--; // count[(a[i] / exp) % d]--;
// } // }
// memcpy(&a[0], &output[0], a.length * sizeof(int32_t)); // memcpy(&a[0], &output[0], a.length * sizeof(int));
//} //}
template<typename T> template<typename T>
@ -428,7 +423,7 @@ namespace asp {
template<typename T> template<typename T>
inline void radix_sort_256(T* a, const size_t& n) noexcept { inline 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<int>& a) noexcept {
if (n <= 1) if (n <= 1)
//if (a.length <= 1) //if (a.length <= 1)
return; return;
@ -452,7 +447,7 @@ namespace asp {
count[i] += count[i - 1]; count[i] += count[i - 1];
// Build the output array // Build the output array
for (int32_t i = n - 1; i >= 0; i--) { for (int i = n - 1; i >= 0; i--) {
// precalculate the offset as it's a few instructions // precalculate the offset as it's a few instructions
const size_t idx = (a[i] >> s) & 0xff; const size_t idx = (a[i] >> s) & 0xff;

View File

@ -1,6 +1,7 @@
#include <numeric> #include <numeric>
#include <cassert> #include <cassert>
#include <algorithm> #include <algorithm>
#include <memory>
#include <array> #include <array>
#include "toolbox.hpp" #include "toolbox.hpp"
@ -124,8 +125,8 @@ namespace asp {
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; int c = 0;
for (int32_t i = static_cast<int32_t>(n.size()) - 1; i >= 0; --i) { for (int i = static_cast<int>(n.size()) - 1; i >= 0; --i) {
c++; c++;
s.push_back(n[i]); s.push_back(n[i]);
if (c == 3) { if (c == 3) {