Timetabler
utils.h
Go to the documentation of this file.
1 
3 #ifndef UTILS_H
4 #define UTILS_H
5 
6 #include <iostream>
7 #include <sstream>
8 #include <string>
9 #include <vector>
10 #include "data.h"
11 #include "global.h"
12 #include "mtl/Vec.h"
13 
14 using namespace NSPACE;
15 
16 namespace Utils {
17 
27 template <typename T>
28 vec<T> convertVectorToVec(std::vector<T> inputs) {
29  vec<T> result(inputs.size());
30  for (unsigned i = 0; i < inputs.size(); i++) {
31  result[i] = inputs[i];
32  }
33  return result;
34 }
35 
45 template <typename T>
46 std::vector<T> convertVecToVector(vec<T> inputs) {
47  std::vector<T> result(inputs.size());
48  for (unsigned i = 0; i < inputs.size(); i++) {
49  result[i] = inputs[i];
50  }
51  return result;
52 }
53 
64 template <typename T>
65 std::vector<T> convertVecDataToVector(T *data, unsigned size) {
66  std::vector<T> result(size);
67  for (unsigned i = 0; i < size; i++) {
68  result[i] = data[i];
69  }
70  return result;
71 }
72 
84 template <typename T>
85 std::vector<T> flattenVector(std::vector<std::vector<T>> inputs) {
86  std::vector<T> result;
87  result.clear();
88  for (unsigned i = 0; i < inputs.size(); i++) {
89  result.insert(result.end(), inputs[i].begin(), inputs[i].end());
90  }
91  return result;
92 }
93 
105 template <typename T>
106 std::vector<T> flattenVector(std::vector<std::vector<std::vector<T>>> inputs) {
107  std::vector<T> result;
108  result.clear();
109  for (unsigned i = 0; i < inputs.size(); i++) {
110  std::vector<T> nextInsert = flattenVector<T>(inputs[i]);
111  result.insert(result.end(), nextInsert.begin(), nextInsert.end());
112  }
113  return result;
114 }
115 
116 std::string getFieldTypeName(FieldType fieldType);
117 std::string getPredefinedConstraintName(const PredefinedClauses clauseType);
118 
119 std::string getFieldName(FieldType fieldType, int index, Data &data);
120 
124 enum Severity { EMPTY = 0, ERROR = 1, WARNING = 2, INFO = 3 };
125 
129 enum DisplayColour { NORMAL = 0, RED = 31, YELLOW = 33 };
130 
131 // Reference: https://stackoverflow.com/a/2179782/, Evan Terran
132 // (https://stackoverflow.com/users/13430/evan-teran), CC-BY-SA 3.0
133 
137 class Log {
138  public:
139  Log(Severity severity = Severity::EMPTY, bool isDebug = false,
140  int lineWidth = 0, int indentWidth = 0);
141  ~Log();
142  template <class T>
143  Log &operator<<(const T &input) {
144  ss << input;
145  return *this;
146  }
147  static void setVerbosity(int verb);
148 
149  private:
150  static int verbosity;
151  std::ostringstream ss;
152  Severity severity;
153  bool isDebug;
154  int lineWidth;
155  int indentWidth;
156  int metaWidth;
157  int getSeverityCode();
158  std::string getSeverityIdentifier();
159  std::string applyIndent(std::string, int);
160  void displayOutput(std::ostream &out = std::cout);
161  std::string formatString(std::string);
162 };
163 
164 } // namespace Utils
165 
166 // Define shorthands for logging
167 #define LOG(x) Utils::Log(Utils::x)
168 #define LOG_DEBUG(x) Utils::Log(Utils::x, true)
169 #define LOG_FIXED(x) Utils::Log(Utils::x, false, 80)
170 #define LOG_FIXED_DEBUG(x) Utils::Log(Utils::x, true, 80)
171 
172 #define DISPLAY() LOG(EMPTY)
173 #define DISPLAY_DEBUG() LOG_DEBUG(EMPTY)
174 #define DISPLAY_FIXED() LOG_FIXED(EMPTY)
175 #define DISPLAY_FIXED_DEBUG() LOG_FIXED_DEBUG(EMPTY)
176 
177 #define DEBUG() LOG_DEBUG(INFO)
178 
179 #endif
DisplayColour
Define values for colours in ASCII, to be used when logging.
Definition: utils.h:129
std::string getPredefinedConstraintName(const PredefinedClauses clauseType)
Gets the predefined constraint name as a string.
Definition: utils.cpp:40
std::vector< T > convertVecDataToVector(T *data, unsigned size)
Converts given data and size, a NSPACE::vec to a std::vector.
Definition: utils.h:65
PredefinedClauses
Enum that represents all the predefined constraints.
Definition: global.h:14
std::string getFieldName(FieldType fieldType, int index, Data &data)
Gets the field name in the Data of a given FieldType at a given index.
Definition: utils.cpp:79
FieldType
Enum that represents all the field types.
Definition: global.h:9
Perform logging.
Definition: utils.h:137
std::vector< T > convertVecToVector(vec< T > inputs)
Converts a NSPACE::vec to a std::vector.
Definition: utils.h:46
vec< T > convertVectorToVec(std::vector< T > inputs)
Converts a std::vector to a NSPACE::vec.
Definition: utils.h:28
Severity
Specify severity levels for logging.
Definition: utils.h:124
Class for data.
Definition: data.h:31
Definition: utils.h:16
std::vector< T > flattenVector(std::vector< std::vector< T >> inputs)
Converts a two dimensional vector to a one dimensional vector.
Definition: utils.h:85
std::string getFieldTypeName(FieldType fieldType)
Gets the field type name as a string.
Definition: utils.cpp:19