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_AT_MOST_ONE___VA_ARGS__(...) \
61  HAS_AT_MOST_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, t, garbage)
66 #define HAS_AT_MOST_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, a34, ...) 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_IS_EMPTY_, IS_EMPTY(CAR(__VA_ARGS__)))(__VA_ARGS__)
78 #define CDR_IS_EMPTY_true(...)
79 #define CDR_IS_EMPTY_false(...) \
80  CONCAT(CDR_HELPER_, HAS_AT_MOST_ONE___VA_ARGS__(__VA_ARGS__))(__VA_ARGS__)
81 #define CDR_HELPER_t(...)
82 #define CDR_HELPER_f(first, ...) __VA_ARGS__
83 #define CADR(...) CAR(CDR(__VA_ARGS__))
84 #define CADDR(...) CAR(CDR(CDR(__VA_ARGS__)))
85 #define CADDDR(...) CAR(CDR(CDR(CDR(__VA_ARGS__))))
86 #define CADDDDR(...) CAR(CDR(CDR(CDR(CDR(__VA_ARGS__)))))
87 #define CDDDDDR(...) CDR(CDR(CDR(CDR(CDR(__VA_ARGS__)))))
88 
89 // Some of our catalog data generators also includes this file, but they don't
90 // want anything other than the macro definitions.
91 #ifndef INCLUDE_MACROS_ONLY
92 #include <utility>
93 #include <cstdint>
94 #include <absl/strings/string_view.h>
95 
96 using std::uint64_t;
97 
98 namespace taco {
99 
100 template<class Container, class Arg0, class ...Args>
101 inline void
102 emplace_back_parameter_pack(Container &c, Arg0 &&arg0, Args&& ...args) {
103  c.emplace_back(std::forward<Arg0>(arg0));
104  emplace_back_parameter_pack(c, std::forward<Args>(args)...);
105 }
106 
107 template<class Container>
108 inline void
110 
111 static constexpr char LogTable256[256] = {
112 #define LogTableLT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
113  0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
117  LogTableLT(7), LogTableLT(7), LogTableLT(7)
118 };
119 #undef LogTableLT
120 
126 constexpr int
127 logn_floor(uint64_t x) {
128  return(x >> 32) ? (
129  (x >> 48) ? (
130  (x >> 56) ? (LogTable256[x >> 56] + 56)
131  : (LogTable256[x >> 48] + 48)
132  ) : (
133  (x >> 40) ? (LogTable256[x >> 40] + 40)
134  : (LogTable256[x >> 32] + 32)
135  )
136  ) : (
137  (x >> 16) ?(
138  (x >> 24) ? (LogTable256[x >> 24] + 24)
139  : (LogTable256[x >> 16] + 16)
140  ) : (
141  (x >> 8) ? (LogTable256[x >> 8] + 8)
142  : (LogTable256[x])
143  )
144  );
145 }
146 
150 constexpr int
151 logn_ceil(uint64_t x) {
152  return x ? (logn_floor(x - 1) + 1) : 0;
153 }
154 
161 bool FilePathIsTDBFilePath(absl::string_view filepath);
162 
163 absl::string_view StripSourcePath(absl::string_view path);
164 
172 uint64_t GetCurrentDataSize();
173 
174 } // namespace taco
175 #endif
176 
177 #endif // UTILS_MISC_H
#define LogTableLT(n)
Definition: datum.h:28
absl::string_view StripSourcePath(absl::string_view path)
Definition: misc.cpp:26
void emplace_back_parameter_pack(Container &c, Arg0 &&arg0, Args &&...args)
Definition: misc.h:102
constexpr int logn_floor(uint64_t x)
Returns $\lfloor log_2(x) \rfloor$ for x > 0, or 0 for x = 0.
Definition: misc.h:127
constexpr int logn_ceil(uint64_t x)
Returns $\lceil log_2(x) \rceil$ for x > 0, or 0 for x = 0.
Definition: misc.h:151
uint64_t GetCurrentDataSize()
Returns the approximate size of heap-allocated memory in bytes.
Definition: misc.cpp:53
bool FilePathIsTDBFilePath(absl::string_view filepath)
Returns true if filepath belongs to tdb source code path.
Definition: misc.cpp:18
static constexpr char LogTable256[256]
Definition: misc.h:111