-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.h
185 lines (156 loc) · 4.27 KB
/
utility.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef UTILITY_H
#define UTILITY_H
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <stack>
#ifdef _DEBUG
#define DEBUG(t, message) (Utility::Debug(t, message))
#ifndef DWARNING
#define DWARNING(t, message) (Utility::Warning(t, message))
#define DERROR(t, message) (Utility::Error(t, message))
#endif
#else
#define DEBUG(t, message) ((void)0)
#ifndef DWARNING
#define DWARNING(t, message) ((void)0)
#define DERROR(t, message) ((void)0)
#endif
#endif
typedef unsigned char uchar;
// returns true if a is within e of b
#define Within(a, b, e) ((((a) <= ((b)+(e))) && ((a) >= ((b)-(e)))) ? 1 : 0)
// enumerate indecies
#define FOR_IDX(_i_, vec) for(unsigned int _i_ = 0; _i_ < vec.size(); ++_i_)
#define CV_PRECISE CV_32FC1
//#define CV_PRECISE CV_64FC1
namespace Utility {
template <typename T>
void print_array(T* arr, size_t s) {
using namespace std;
cout << "{ ";
for (size_t i = 0; i < s; i++) {
cout << arr[i] << " ";
if(i < s-1) {
cout << ",";
}
}
cout << "}";
cout << endl;
}
inline void print(const char* str) {
printf("%s\n", str);
}
/// function forward declarations
inline int Debug(const char* t, const char* message);
inline int Warning(const char* t, const char* message);
inline int Error(const char* t, const char* message);
inline int Debug(const char* t, const char* message) {
if (!t || !message)
return -1;
printf(">> Debug: %s:: %s\n", t, message);
return 0; // return debug_level;
}
inline int Warning(const char* t, const char* message) {
if (!t || !message)
return -1;
printf(">> Warning: %s:: %s\n", t, message);
return 1; // return debug_level;
}
inline int Error(const char* t, const char* message) {
if (!t || !message)
return -1;
printf(">> Error: %s:: %s\n", t, message);
return 2; // return debug_level;
}
} // utility
//
// Open MP Helpers
#if defined(_OPENMP)
int get_num_threads() {
int nthreads = 0;
int tid = 0;
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
if (tid == 0) {
nthreads = omp_get_num_threads();
// printf("number of threads: %d\n", nthreads);
}
}
return nthreads;
}
#else
inline int get_num_threads() {
return 1;
}
#endif
//
namespace StackEx {
/* returns a reversed s1 stack */
template <typename T>
std::stack<T> reverseStack(const std::stack<T>& s) {
std::stack<T> s1 = s;
std::stack<T> s2;
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
return s2;
}
/* Joines the stacks s1 and s2: new stack == {s1, s2} where the right side is the top of a generic stack */
template <typename T>
std::stack<T> joinStacks(const std::stack<T>& s1, const std::stack<T>& s2) {
std::stack<T> joined = s1;
std::stack<T> s3 = reverseStack(s2);
while (!s3.empty()) {
joined.push(s3.top());
s3.pop();
}
return joined;
}
template <typename T>
void emptyOut(std::stack<T>& s) {
while (!s.empty())
s.pop();
}
}
#ifndef BEGIN_TESTS
#define BEGIN_TESTS(title) \
{ \
printf("\n"); \
unsigned len = strlen(title); \
unsigned bar_size = len + 10; \
char* bar = 0; \
bar = (char*) malloc(bar_size + 1); \
memset(bar, '>', 3); \
memset(bar+3, '-', bar_size-6); \
memset(&(bar[bar_size-3]), '<', 3); \
bar[bar_size] = 0; \
printf("%s\n", bar); \
free(bar); \
bar = NULL; \
bar = (char*) malloc(len + 1); \
memset(bar, ' ', len); \
bar[len] = 0; \
printf(">>> %s <<<\n", bar); \
printf(">>> %s <<<\n", title); \
printf(">>> %s <<<\n", bar); \
free(bar); \
bar = (char*) malloc(bar_size + 1); \
memset(bar, '>', 3); \
memset(bar+3, '-', bar_size-6); \
memset(&(bar[bar_size-3]), '<', 3); \
bar[bar_size] = 0; \
printf("%s\n", bar); \
free(bar); \
}
#else
#warning "BEGIN_TESTS already defined.. Tests Broken"
#endif
#define DEFINE_SIBLING_MAP(CLASS, T) \
template <> \
std::map<CLASS##<##T##>*, CLASS##<##T##>*> CLASS##<##T##>::mLayersMap = std::map<CLASS##<##T##>*, CLASS##<##T##>*>();
#endif // UTILITY_H