Version: 1.0
utilities.h
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #ifndef GRAPHGEN_UTILITIES_H_
8 #define GRAPHGEN_UTILITIES_H_
9 
10 #include <algorithm>
11 #include <iomanip>
12 #include <string>
13 #include <sstream>
14 #include <filesystem>
15 
16 #include "config_data.h"
17 #include "performance_evaluator.h"
18 
19 extern ConfigData conf;
20 
21 using uint = uint32_t;
22 
23 std::string binary(size_t u, size_t nbits);
24 
28 #define DEFINE_ENUM_CLASS_OR_OPERATOR(class_name) \
29 constexpr enum class_name operator|(const enum class_name self_value, const enum class_name in_value) { \
30  return static_cast<enum class_name>(static_cast<uint32_t>(self_value) | static_cast<uint32_t>(in_value)); \
31 } \
32 
36 #define DEFINE_ENUM_CLASS_AND_OPERATOR(class_name) \
37 constexpr bool operator&(const enum class_name self_value, const enum class_name in_value) { \
38  return static_cast<bool>(static_cast<uint32_t>(self_value) & static_cast<uint32_t>(in_value)); \
39 } \
40 
41 #define DEFINE_ENUM_CLASS_FLAGS(class_name, ...) \
42 enum class class_name : uint32_t { \
43  __VA_ARGS__ \
44 }; \
45 DEFINE_ENUM_CLASS_OR_OPERATOR(class_name) \
46 DEFINE_ENUM_CLASS_AND_OPERATOR(class_name) \
47 
48 
49 struct nodeid {
50  size_t _id = 0;
51  size_t next() { return ++_id; }
52  size_t get() { return _id; }
53 
54  void Clear() {
55  _id = 0;
56  }
57 
58  void SetId(size_t new_id) {
59  _id = new_id;
60  }
61 };
62 
63 static inline void RemoveCharacter(std::string& s, const char c) {
64  s.erase(std::remove(s.begin(), s.end(), c), s.end());
65 }
66 
67 // Convert value into string filling the head of the string with zeros up to n characters
68 template <typename T>
69 std::string zerostr(const T& val, size_t n) {
70  std::stringstream ss;
71  ss << std::setw(n) << std::setfill('0') << val;
72  return ss.str();
73 }
74 
75 // This function tokenize an input string
76 template <typename T>
77 void StringSplit(const std::string& str, T& cont, char delim = '+')
78 {
79  std::stringstream ss(str);
80  std::string token;
81  while (std::getline(ss, token, delim)) {
82  cont.push_back(token);
83  }
84 }
85 
86 
87 
88 // Function to automatically print a message before and after each operation
89 // No braces around instruction, so you can log also variable definitions without scoping them
90 #define LOG(message, instructions) std::cout << (message) << "... "; instructions std::cout << "done.\n"
91 
92 static PerformanceEvaluator tlog_pe;
93 #define TLOG(message, instructions) std::cout << (message) << "... "; tlog_pe.start(); instructions std::cout << "done. " << tlog_pe.stop() << " ms.\n";
94 
95 #endif // !GRAPHGEN_UTILITIES_H_
96 
uint32_t uint
This class stores the configuration data loaded from file. All data are placed in the config global v...
Definition: config_data.h:20
size_t next()
Definition: utilities.h:51
void SetId(size_t new_id)
Definition: utilities.h:58
size_t get()
Definition: utilities.h:52
void Clear()
Definition: utilities.h:54
size_t _id
Definition: utilities.h:50
std::string zerostr(const T &val, size_t n)
Definition: utilities.h:69
void StringSplit(const std::string &str, T &cont, char delim='+')
Definition: utilities.h:77
ConfigData conf
Definition: utilities.cpp:9
std::string binary(size_t u, size_t nbits)
Definition: utilities.cpp:11