Added inline and constexpr when possible
This commit is contained in:
100
data.hpp
100
data.hpp
@@ -11,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);
|
||||
@@ -33,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);
|
||||
@@ -45,7 +45,7 @@ namespace asp {
|
||||
};
|
||||
|
||||
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;
|
||||
num_written += printf("[");
|
||||
char formatter[BUFSIZ] = { 0 };
|
||||
@@ -57,33 +57,33 @@ namespace asp {
|
||||
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");
|
||||
}
|
||||
|
||||
int32_t print(const Array<int32_t>& a) noexcept {
|
||||
inline constexpr int32_t print(const Array<int32_t>& a) noexcept {
|
||||
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");
|
||||
}
|
||||
|
||||
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]);
|
||||
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());
|
||||
}
|
||||
|
||||
int32_t 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)
|
||||
@@ -92,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)
|
||||
@@ -101,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;
|
||||
@@ -121,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])
|
||||
@@ -137,21 +137,21 @@ 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)
|
||||
for (j = i + 1; j < a.length; ++j)
|
||||
if (a[i] > a[j]){
|
||||
swap(&indices[i], &indices[j]);
|
||||
swap(&a[i], &a[j]);
|
||||
swap(&indices[i], &indices[j]);
|
||||
swap(&a[i], &a[j]);
|
||||
|
||||
}
|
||||
return indices;
|
||||
}
|
||||
|
||||
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])
|
||||
@@ -161,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;
|
||||
|
||||
@@ -172,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;
|
||||
@@ -220,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]){
|
||||
@@ -238,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;
|
||||
|
||||
@@ -249,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;
|
||||
@@ -304,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;
|
||||
@@ -315,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));
|
||||
@@ -354,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;
|
||||
|
||||
@@ -365,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> {
|
||||
@@ -386,11 +386,11 @@ 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<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);
|
||||
// memset(&count[0], 0, d * sizeof(int32_t));
|
||||
|
||||
@@ -410,7 +410,7 @@ namespace asp {
|
||||
//}
|
||||
|
||||
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& {
|
||||
@@ -419,14 +419,14 @@ 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<int32_t>& a) noexcept {
|
||||
if (n <= 1)
|
||||
@@ -434,7 +434,7 @@ namespace asp {
|
||||
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) {
|
||||
@@ -479,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;
|
||||
|
Reference in New Issue
Block a user