Gymbo

utils.h
Go to the documentation of this file.
1
7 #pragma once
8 #include <bitset>
9 #include <cmath>
10 #include <cstdint>
11 #include <cstring>
12 #include <string>
13 #include <vector>
14
15 namespace gymbo {
16
25 inline bool is_integer(float x) { return std::floor(x) == x; }
26
36 inline uint32_t FloatToWord(float val) {
37 uint32_t word;
38 std::memcpy(&word, &val, sizeof(val));
39 return word;
40 }
41
51 inline float wordToFloat(uint32_t word) {
52 float val;
53 std::memcpy(&val, &word, sizeof(word));
54 return val;
55 }
56
66 inline int wordToInt(uint32_t word) { return static_cast<int>(word); }
67
74 inline int wordToSignedInt(uint32_t word) {
75 if (word & 0x80000000) {
76 return -static_cast<int>(wordToSignedInt(~word + 1));
77 } else {
78 return static_cast<int>(word);
79 }
80 }
81
88 inline bool wordToBool(uint32_t word) { return word != 0; }
89
96 inline uint32_t boolToWord(bool value) { return value ? 1 : 0; }
97
104 inline uint32_t twosComplement(uint32_t i) { return 1 + ~i; }
105
112 inline bool isNegative(uint32_t word) { return (word & 0x80000000) != 0; }
113
120 inline std::string valName(int i) { return "val_" + std::to_string(i); }
121
130 template <typename T>
131 class LLNode {
132 public:
133 T data;
134 LLNode *next;
135 LLNode *prev;
136
142 LLNode(T data) : data(data), next(NULL), prev(NULL) {}
143 };
144
155 template <typename T>
156 class Linkedlist {
157 public:
158 LLNode<T>
159 *ghost;
160 LLNode<T> *head;
161 LLNode<T> *tail;
162
166 Linkedlist() : ghost(NULL), head(NULL), tail(NULL) {}
167
173 uint32_t len() {
174 LLNode<T> *tmp = head;
175 uint32_t cnt = 0;
176 while (tmp != NULL) {
177 cnt++;
178 tmp = tmp->next;
179 }
180 return cnt;
181 }
182
188 void push(T data) {
189 // Create the new Node.
190 LLNode<T> *newNode = new LLNode<T>(data);
191
192 // Assign to head
193 if (head == NULL) {
194 head = newNode;
195 tail = head;
196 return;
197 }
198
199 tail->next = newNode;
200 newNode->prev = tail;
201 tail = newNode;
202 }
203
209 T *back() {
210 if (tail == NULL) {
211 fprintf(stderr, "Warning... tail of linked-list is null\n");
212 }
213 return &(tail->data);
214 }
215
219 void pop() {
220 if (tail == NULL) {
221 fprintf(stderr, "Warning... tail is NULL when trying to pop\n");
222 return;
223 }
224
225 LLNode<T> *current_tail = tail;
226 tail = tail->prev;
227 if (tail != NULL) {
228 tail->next = NULL;
229 }
230
231 if (current_tail == head) {
232 head = NULL;
233 }
234
235 if (ghost == NULL) {
236 ghost = current_tail;
237 } else {
238 ghost->next = current_tail;
239 current_tail->prev = ghost;
240 ghost = tail;
241 }
242 }
243 };
244
256 inline std::vector<std::vector<int>> cartesianProduct(
257 const std::vector<std::vector<int>> &vectors) {
258 std::vector<std::vector<int>> result;
259
260 if (vectors.size() == 0) {
261 return result;
262 }
263
264 for (int v : vectors[0]) {
265 std::vector<int> tmp = {v};
266 result.emplace_back(tmp);
267 }
268
269 for (int i = 1; i < vectors.size(); ++i) {
270 std::vector<std::vector<int>> tempResult;
271 for (const auto &element1 : result) {
272 for (const auto &element2 : vectors[i]) {
273 std::vector<int> tempElement = element1;
274 tempElement.push_back(element2);
275 tempResult.push_back(tempElement);
276 }
277 }
278 result = tempResult;
279 }
280
281 return result;
282 }
283 }; // namespace gymbo
gymbo::LLNode
Node for a Doubly Linked List.
Definition: utils.h:131
gymbo::LLNode::next
LLNode * next
Pointer to the next node in the linked list.
Definition: utils.h:134
gymbo::LLNode::prev
LLNode * prev
Pointer to the previous node in the linked list.
Definition: utils.h:135
gymbo::LLNode::data
T data
The data stored in the node.
Definition: utils.h:133
gymbo::LLNode::LLNode
LLNode(T data)
Default constructor for LLNode.
Definition: utils.h:142
gymbo::Linkedlist
Doubly Linked List Implementation.
Definition: utils.h:156
gymbo::Linkedlist::push
void push(T data)
Pushes a new element onto the back of the linked list.
Definition: utils.h:188
gymbo::Linkedlist::Linkedlist
Linkedlist()
Default constructor for Linkedlist.
Definition: utils.h:166
gymbo::Linkedlist::ghost
LLNode< T > * ghost
Ghost node for maintaining previous tails during pops.
Definition: utils.h:159
gymbo::Linkedlist::back
T * back()
Returns the element at the back of the linked list.
Definition: utils.h:209
gymbo::Linkedlist::head
LLNode< T > * head
Pointer to the head of the linked list.
Definition: utils.h:160
gymbo::Linkedlist::tail
LLNode< T > * tail
Pointer to the tail of the linked list.
Definition: utils.h:161
gymbo::Linkedlist::pop
void pop()
Pops the element at the back of the linked list.
Definition: utils.h:219
gymbo::Linkedlist::len
uint32_t len()
Get the length of the linked list.
Definition: utils.h:173
gymbo
Definition: compiler.h:11
gymbo::wordToFloat
float wordToFloat(uint32_t word)
Converts a 32-bit word representation to a float value.
Definition: utils.h:51
gymbo::valName
std::string valName(int i)
Generates a name for a variable based on its index.
Definition: utils.h:120
gymbo::wordToInt
int wordToInt(uint32_t word)
Converts a 32-bit word representation to an integer value.
Definition: utils.h:66
gymbo::wordToSignedInt
int wordToSignedInt(uint32_t word)
Converts a uint32_t word to a signed int.
Definition: utils.h:74
gymbo::twosComplement
uint32_t twosComplement(uint32_t i)
Returns the twos complement of a uint32_t word.
Definition: utils.h:104
gymbo::is_integer
bool is_integer(float x)
Checks if a float is an integer.
Definition: utils.h:25
gymbo::isNegative
bool isNegative(uint32_t word)
Checks if a uint32_t word is negative.
Definition: utils.h:112
gymbo::FloatToWord
uint32_t FloatToWord(float val)
Converts a float value to a 32-bit word representation.
Definition: utils.h:36
gymbo::cartesianProduct
std::vector< std::vector< int > > cartesianProduct(const std::vector< std::vector< int >> &vectors)
Compute the Cartesian product of a vector of vectors of integers.
Definition: utils.h:256
gymbo::wordToBool
bool wordToBool(uint32_t word)
Converts a uint32_t word to a bool.
Definition: utils.h:88
gymbo::boolToWord
uint32_t boolToWord(bool value)
Converts a bool to a uint32_t word.
Definition: utils.h:96