docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
helpers.h
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cstdint>
5#include <memory>
6#include <sstream>
7#include <string>
8#include <unordered_set>
9#include <vector>
10
11#ifndef NDEBUG
12#define DEBUG_PRINTLN(msg) \
13 do { \
14 std::cerr << "[DEBUG] " << msg << std::endl; \
15 } while (0)
16#define DEBUG_PRINT(msg) \
17 do { \
18 std::cerr << "[DEBUG] " << msg; \
19 } while (0)
20#else
21#define DEBUG_PRINTLN(msg) \
22 do { \
23 } while (0)
24#define DEBUG_PRINT(msg) \
25 do { \
26 } while (0)
27#endif
28
29#define WARN_PRINTLN(msg) \
30 do { \
31 std::cerr << "[WARN] " << msg << std::endl; \
32 } while (0)
33
34namespace sdfg {
35namespace helpers {
36
37// Transformation functions
38template<typename T>
39inline T& indirect(const std::unique_ptr<T>& ptr) {
40 return *ptr;
41};
42
43template<typename T>
44inline const T& add_const(T& s) {
45 return s;
46};
47
48// string operations
49
50inline bool is_number(const std::string& s) {
51 std::string::const_iterator it = s.begin();
52 while (it != s.end() && std::isdigit(*it)) {
53 ++it;
54 }
55 return !s.empty() && it == s.end();
56};
57
58inline uint64_t parse_number_unsigned(const std::string& s) {
59 if (s == "false") {
60 return 0;
61 } else if (s == "true") {
62 return 1;
63 }
64
65 return std::stoull(s);
66};
67
68inline int64_t parse_number_signed(const std::string& s) {
69 if (s == "false") {
70 return 0;
71 } else if (s == "true") {
72 return 1;
73 }
74
75 return std::stoll(s);
76};
77
78inline bool endswith(std::string const& value, std::string const& ending) {
79 if (ending.size() > value.size()) {
80 return false;
81 }
82 return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
83};
84
85template<class S, class T>
86inline std::string join(std::vector<T>& elems, S& delim) {
87 if (elems.size() == 0) {
88 return "";
89 }
90 std::stringstream ss;
91 typename std::vector<T>::iterator e = elems.begin();
92 ss << *e++;
93 for (; e != elems.end(); ++e) {
94 ss << delim << *e;
95 }
96 return ss.str();
97};
98
99inline void split(std::vector<std::string>& result, std::string s, std::string del = " ") {
100 int start, end = -1 * del.size();
101 do {
102 start = end + del.size();
103 end = s.find(del, start);
104 result.push_back(s.substr(start, end - start));
105 } while (end != -1);
106}
107
108template<typename T>
109inline bool sets_intersect(const std::unordered_set<T>& set1, const std::unordered_set<T>& set2) {
110 // Determine the smaller and larger set
111 const std::unordered_set<T>& smaller = (set1.size() < set2.size()) ? set1 : set2;
112 const std::unordered_set<T>& larger = (set1.size() < set2.size()) ? set2 : set1;
113
114 // Iterate through the smaller set and check for existence in the larger set
115 for (const T& element : smaller) {
116 if (larger.find(element) != larger.end()) {
117 return true; // Intersection found
118 }
119 }
120
121 return false; // No intersection found
122}
123
124} // namespace helpers
125} // namespace sdfg
Definition helpers.h:35
bool is_number(const std::string &s)
Definition helpers.h:50
int64_t parse_number_signed(const std::string &s)
Definition helpers.h:68
const T & add_const(T &s)
Definition helpers.h:44
bool sets_intersect(const std::unordered_set< T > &set1, const std::unordered_set< T > &set2)
Definition helpers.h:109
std::string join(std::vector< T > &elems, S &delim)
Definition helpers.h:86
uint64_t parse_number_unsigned(const std::string &s)
Definition helpers.h:58
bool endswith(std::string const &value, std::string const &ending)
Definition helpers.h:78
void split(std::vector< std::string > &result, std::string s, std::string del=" ")
Definition helpers.h:99
T & indirect(const std::unique_ptr< T > &ptr)
Definition helpers.h:39
Definition analysis.h:7