Tatooine
utility.h
Go to the documentation of this file.
1#ifndef TATOOINE_UTILITY_H
2#define TATOOINE_UTILITY_H
3//==============================================================================
4//#include <tatooine/rank.h>
8
9#include <array>
10#include <vector>
11//==============================================================================
12namespace tatooine {
13//==============================================================================
14template <typename T>
16 using type = T;
17};
18//------------------------------------------------------------------------------
19template <typename T>
21//==============================================================================
23template <size_t Omit, size_t... Is, size_t... Js>
24constexpr auto sliced_indices(std::index_sequence<Is...>,
25 std::index_sequence<Js...>) {
26 std::array indices{Is...};
27 (++indices[Js + Omit], ...);
28 return indices;
29}
30// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32template <size_t N, size_t Omit>
33constexpr auto sliced_indices() {
34 return sliced_indices<Omit>(std::make_index_sequence<N - 1>{},
35 std::make_index_sequence<N - Omit - 1>{});
36}
37//==============================================================================
38template <typename F, typename... Ts>
39constexpr void for_each(F&& f, Ts&&... ts) {
40 (f(std::forward<Ts>(ts)), ...);
41}
42
43//==============================================================================
46template <size_t N>
47auto partition_resolution(const std::array<size_t, N>& resolution,
48 const std::array<size_t, N>& max_chunk_resolution) {
49 auto partitions = make_array<std::vector<std::pair<size_t, size_t>>, N>();
50 for (size_t j = 0; j < N; ++j) {
51 const auto num_partitions = static_cast<size_t>(ceil(
52 static_cast<double>(resolution[j]) / (max_chunk_resolution[j] - 1)));
53 partitions[j] = std::vector<std::pair<size_t, size_t>>(
54 num_partitions, {0, max_chunk_resolution[j]});
55 for (size_t i = 0; i < num_partitions; ++i) {
56 partitions[j][i].first = (max_chunk_resolution[j] - 1) * i;
57 }
58 partitions[j].back().second = resolution[j] - partitions[j].back().first;
59 }
60 return partitions;
61}
62
63//==============================================================================
64inline constexpr auto debug_mode() {
65#ifndef NDEBUG
66 return true;
67#else
68 return false;
69#endif
70}
71// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72inline constexpr auto release_mode() {
73#ifdef NDEBUG
74 return true;
75#else
76 return false;
77#endif
78}
79//------------------------------------------------------------------------------
80template <typename T>
81constexpr void tat_swap(T& t0, T& t1) {
82 T tmp = std::move(t0);
83 t0 = std::move(t1);
84 t1 = std::move(tmp);
85}
86//==============================================================================
87template <typename T, typename Ret, typename ... Args>
88using const_method_ptr = Ret (T::*)(Args...) const;
89template <typename T, typename Ret, typename ... Args>
90using non_const_method_ptr = Ret (T::*)(Args...);
91//==============================================================================
92template <typename F, typename... Args>
93auto repeat(size_t const n, F&& f, Args&&... args) {
94 for (size_t i = 0; i < n; ++i) {
95 f(std::forward<Args>(args)...);
96 }
97}
98//==============================================================================
99auto copy_or_keep_if_rvalue(auto&& x) -> decltype(auto) {
100 if constexpr (std::is_rvalue_reference_v<decltype(x)>) {
101 return std::forward<decltype(x)>(x);
102 } else {
103 return std::decay_t<decltype(x)>{x};
104 }
105}
106//==============================================================================
107} // namespace tatooine
108//==============================================================================
109#endif
Definition: algorithm.h:6
constexpr void tat_swap(T &t0, T &t1)
Definition: utility.h:81
auto repeat(size_t const n, F &&f, Args &&... args)
Definition: utility.h:93
auto partition_resolution(const std::array< size_t, N > &resolution, const std::array< size_t, N > &max_chunk_resolution)
Definition: utility.h:47
constexpr auto release_mode()
Definition: utility.h:72
typename internal_data_type< T >::type internal_data_type_t
Definition: utility.h:20
auto copy_or_keep_if_rvalue(auto &&x) -> decltype(auto)
Definition: utility.h:99
constexpr auto debug_mode()
Definition: utility.h:64
Ret(T::*)(Args...) const const_method_ptr
Definition: utility.h:88
constexpr void for_each(F &&f, Ts &&... ts)
Definition: utility.h:39
Ret(T::*)(Args...) non_const_method_ptr
Definition: utility.h:90
constexpr auto sliced_indices()
creates an index sequence and removes an element from it
Definition: utility.h:33
Definition: utility.h:15
T type
Definition: utility.h:16