taco-db  0.1.0
misc.h
Go to the documentation of this file.
1 #ifndef UTILS_MISC_H
2 #define UTILS_MISC_H
3 
4 
5 #define STRINGIFY_HELPER(_1) #_1
6 #define STRINGIFY(_1) STRINGIFY_HELPER(_1)
7 
8 #define CONCAT_HELPER(_1, _2) _1 ## _2
9 #define CONCAT(_1, _2) CONCAT_HELPER(_1, _2)
10 
11 #define CONCAT3_HELPER(_1, _2, _3) _1 ## _2 ## _3
12 #define CONCAT3(_1, _2, _3) CONCAT3_HELPER(_1, _2, _3)
13 
14 // the ``garbage'' argument is required up until C++20 and for all C versions
15 #define SELECT_FIRST(...) SELECT_FIRST_HELPER(__VA_ARGS__, garbage)
16 #define SELECT_FIRST_HELPER(first, ...) first
17 #define SELECT_SECOND(...) SELECT_SECOND_HELPER(__VA_ARGS__, , garbage)
18 #define SELECT_SECOND_HELPER(first, second, ...) second
19 
20 #define NOT(boolean_var) CONCAT(NOT_HELPER_, boolean_var)
21 #define NOT_HELPER_true false
22 #define NOT_HELPER_false true
23 
24 // IF_NONEMPTY(arg_to_test, if_branch[, else_branch])
25 #define IF_NONEMPTY(arg, ...) \
26  CONCAT(SELECT_, CONCAT(IF_NONEMPTY_HELPER_, IS_EMPTY(arg))) (__VA_ARGS__)
27 #define IF_NONEMPTY_HELPER_true SECOND
28 #define IF_NONEMPTY_HELPER_false FIRST
29 
30 // IF_EMPTY(arg_to_test, if_branch[, else_branch])
31 #define IF_EMPTY(arg, ...) \
32  CONCAT(SELECT_, CONCAT(IF_NONEMPTY_HELPER_, NOT(IS_EMPTY(arg)))) (__VA_ARGS__)
33 
34 // add an optional comma if condition holds
35 #define COMMA_true ,
36 #define COMMA_false
37 #define IF_NONEMPTY_COMMA(arg, if_branch) \
38  IF_NONEMPTY(arg, if_branch) CONCAT(COMMA_, NOT(IS_EMPTY(arg)))
39 #define IF_EMPTY_COMMA(arg, if_branch) \
40  IF_EMPTY(arg, if_branch) CONCAT(COMMA_, IS_EMPTY(arg))
41 
42 #define IF_BOOLEAN_LITERAL(arg, ...) \
43  IF_EMPTY(CONCAT(IF_BOOLEAN_LITERAL_HELPER_, arg), __VA_ARGS__)
44 #define IF_BOOLEAN_LITERAL_HELPER_true
45 #define IF_BOOLEAN_LITERAL_HELPER_false
46 
47 #define EXPAND_TO_COMMA(...) ,
48 #define HAS_COMMA_1_HELPER(_1, _2, _3, ...) _3
49 #define HAS_COMMA_1(...) HAS_COMMA_1_HELPER(__VA_ARGS__, t, f)
50 #define IS_EMPTY(arg) \
51  CONCAT3(IS_EMPTY_HELPER_, \
52  HAS_COMMA_1(EXPAND_TO_COMMA arg ()), \
53  HAS_COMMA_1(EXPAND_TO_COMMA arg))
54 #define IS_EMPTY_HELPER_tf true
55 #define IS_EMPTY_HELPER_ff false
56 #define IS_EMPTY_HELPER_tt false
57 
58 #define IS_NONEMPTY(arg) NOT(IS_EMPTY(arg))
59 
60 #define HAS_ONLY_ONE___VA_ARGS__(...) \
61  HAS_ONLY_ONE___VA_ARGS___HELPER(__VA_ARGS__,\
62  f, f, f, f, f, f, f, f, f, f, \
63  f, f, f, f, f, f, f, f, f, f, \
64  f, f, f, f, f, f, f, f, f, f, \
65  f, t, garbage)
66 #define HAS_ONLY_ONE___VA_ARGS___HELPER(\
67  a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, \
68  a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, \
69  a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, \
70  a31, a32, a33, ...) a33
71 
72 // Contrary to lisp: we can call CAR and CDR on empty __VA_ARGS__
73 // CAR() and CDR() produce empty
74 // Limit of 33 arguments
75 #define CAR SELECT_FIRST
76 #define CDR(...) \
77  CONCAT(CDR_HELPER_, HAS_ONLY_ONE___VA_ARGS__(__VA_ARGS__))(__VA_ARGS__)
78 #define CDR_HELPER_t(...)
79 #define CDR_HELPER_f(first, ...) __VA_ARGS__
80 #define CADR(...) CAR(CDR(__VA_ARGS__))
81 #define CADDR(...) CAR(CDR(CDR(__VA_ARGS__)))
82 #define CADDDR(...) CAR(CDR(CDR(CDR(__VA_ARGS__))))
83 #define CADDDDR(...) CAR(CDR(CDR(CDR(CDR(__VA_ARGS__)))))
84 #define CDDDDDR(...) CDR(CDR(CDR(CDR(CDR(__VA_ARGS__)))))
85 
86 // Some of our catalog data generators also includes this file, but they don't
87 // want anything other than the macro definitions.
88 #ifndef INCLUDE_MACROS_ONLY
89 #include <utility>
90 #include <cstdint>
91 #include <absl/strings/string_view.h>
92 
93 using std::uint64_t;
94 
95 namespace taco {
96 
97 template<class Container, class Arg0, class ...Args>
98 inline void
99 emplace_back_parameter_pack(Container &c, Arg0 &&arg0, Args&& ...args) {
100  c.emplace_back(std::forward<Arg0>(arg0));
101  emplace_back_parameter_pack(c, std::forward<Args>(args)...);
102 }
103 
104 template<class Container>
105 inline void
107 
108 static constexpr char LogTable256[256] = {
109 #define LogTableLT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
110  0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
114  LogTableLT(7), LogTableLT(7), LogTableLT(7)
115 };
116 #undef LogTableLT
117 
123 constexpr int
124 logn_floor(uint64_t x) {
125  return(x >> 32) ? (
126  (x >> 48) ? (
127  (x >> 56) ? (LogTable256[x >> 56] + 56)
128  : (LogTable256[x >> 48] + 48)
129  ) : (
130  (x >> 40) ? (LogTable256[x >> 40] + 40)
131  : (LogTable256[x >> 32] + 32)
132  )
133  ) : (
134  (x >> 16) ?(
135  (x >> 24) ? (LogTable256[x >> 24] + 24)
136  : (LogTable256[x >> 16] + 16)
137  ) : (
138  (x >> 8) ? (LogTable256[x >> 8] + 8)
139  : (LogTable256[x])
140  )
141  );
142 }
143 
147 constexpr int
148 logn_ceil(uint64_t x) {
149  return x ? (logn_floor(x - 1) + 1) : 0;
150 }
151 
158 bool FilePathIsTDBFilePath(absl::string_view filepath);
159 
160 absl::string_view StripSourcePath(absl::string_view path);
161 
169 uint64_t GetCurrentDataSize();
170 
171 } // namespace taco
172 #endif
173 
174 #endif // UTILS_MISC_H
taco::LogTable256
static constexpr char LogTable256[256]
Definition: misc.h:108
LogTableLT
#define LogTableLT(n)
taco::logn_ceil
constexpr int logn_ceil(uint64_t x)
Returns $\lceil log_2(x) \rceil$ for x > 0, or 0 for x = 0.
Definition: misc.h:148
taco
Definition: datum.h:28
taco::logn_floor
constexpr int logn_floor(uint64_t x)
Returns $\lfloor log_2(x) \rfloor$ for x > 0, or 0 for x = 0.
Definition: misc.h:124
taco::StripSourcePath
absl::string_view StripSourcePath(absl::string_view path)
Definition: misc.cpp:26
taco::GetCurrentDataSize
uint64_t GetCurrentDataSize()
Returns the approximate size of heap-allocated memory in bytes.
Definition: misc.cpp:53
taco::emplace_back_parameter_pack
void emplace_back_parameter_pack(Container &c, Arg0 &&arg0, Args &&...args)
Definition: misc.h:99
taco::FilePathIsTDBFilePath
bool FilePathIsTDBFilePath(absl::string_view filepath)
Returns true if filepath belongs to tdb source code path.
Definition: misc.cpp:18