forked from niklasso/minisatp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobal.h
More file actions
373 lines (287 loc) · 13.2 KB
/
Copy pathGlobal.h
File metadata and controls
373 lines (287 loc) · 13.2 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/****************************************************************************************[Global.h]
Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/
/**************************************************************************************************
Contains types, macros, and inline functions generally useful in a C++ program.
**************************************************************************************************/
#ifndef Global_h
#define Global_h
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cfloat>
#include <new>
//=================================================================================================
// Basic Types & Minor Things:
#ifdef _MSC_VER
typedef INT64 int64;
typedef UINT64 uint64;
typedef INT_PTR intp;
typedef UINT_PTR uintp;
#define I64_fmt "I64d"
#else
typedef long long int64;
typedef unsigned long long uint64;
typedef __PTRDIFF_TYPE__ intp;
typedef unsigned __PTRDIFF_TYPE__ uintp;
#define I64_fmt "lld"
#endif
typedef signed char schar;
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef const char cchar;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned uint32;
#ifdef __LP64__
#define LP64 // LP : int=32 long,ptr=64 (unix model -- the windows model is LLP : int,long=32 ptr=64)
#endif
#define macro static inline
template<class T> macro T min(T x, T y) { return (x < y) ? x : y; }
template<class T> macro T max(T x, T y) { return (x > y) ? x : y; }
template <bool> struct STATIC_ASSERTION_FAILURE;
template <> struct STATIC_ASSERTION_FAILURE<true>{};
#define TEMPLATE_FAIL STATIC_ASSERTION_FAILURE<false>()
#define PANIC(msg) assert((fprintf(stderr, "%s\n", msg), fflush(stderr), false))
#define Ping (fflush(stdout), fprintf(stderr, "%s %d\n", __FILE__, __LINE__), fflush(stderr))
#define INITIALIZER(tag) struct Initializer_ ## tag { Initializer_ ## tag(); } static Initializer_ ## tag ## _instance; Initializer_ ## tag:: Initializer_ ## tag(void)
#define FINALIZER( tag) struct Finalizer_ ## tag { ~Finalizer_ ## tag(); } static Finalizer_ ## tag ## _instance; Finalizer_ ## tag::~Finalizer_ ## tag(void)
#define elemsof(x) (sizeof(x)/sizeof(*(x)))
template <class T>
macro void swp(T& x, T& y) { // 'swap' is used by STL
T tmp = x; x = y; y = tmp; }
// Some GNUC extensions:
//
#ifdef __GNUC__
#define ___noreturn __attribute__ ((__noreturn__))
#define ___unused __attribute__ ((__unused__))
#define ___const __attribute__ ((__const__))
#define ___format(type,fmt,arg) __attribute__ ((__format__(type, fmt, arg) ))
#if (__GNUC__ > 2) || (__GNUC__ >= 2 && __GNUC_MINOR__ > 95)
#define ___malloc __attribute__ ((__malloc__))
#else
#define ___malloc
#endif
#else
#define ___noreturn
#define ___unused
#define ___const
#define ___format(type,fmt,arg)
#define ___malloc
#endif
// The right 'sprintf()' -- allocating the string itself!
char* nsprintf (cchar* format, ...) ___format(printf, 1, 2) ___malloc;
char* vnsprintf(const char* format, va_list args) ___malloc;
//=================================================================================================
// 'malloc()'-style memory allocation -- never returns NULL; aborts instead:
template<class T> macro T* xmalloc(size_t size) {
T* tmp = (T*)malloc(size * sizeof(T));
assert(size == 0 || tmp != NULL);
return tmp; }
template<class T> macro T* xrealloc(T* ptr, size_t size) {
T* tmp = (T*)realloc((void*)ptr, size * sizeof(T));
assert(size == 0 || tmp != NULL);
return tmp; }
template<class T> macro void xfree(T *ptr) {
if (ptr != NULL) free((void*)ptr); }
macro char* Xstrdup(cchar* src);
macro char* Xstrdup(cchar* src) {
int size = strlen(src)+1;
char* tmp = xmalloc<char>(size);
memcpy(tmp, src, size);
return tmp; }
#define xstrdup(s) Xstrdup(s)
macro char* xstrndup(cchar* src, int len) ___malloc;
macro char* xstrndup(cchar* src, int len) {
int size; for (size = 0; size < len && src[size] != '\0'; size++);
char* tmp = xmalloc<char>(size+1);
memcpy(tmp, src, size); tmp[size] = '\0';
return tmp; }
//=================================================================================================
// Random numbers:
// Returns a random float 0 <= x < 1. Seed must never be 0.
macro double drand(double& seed) {
seed *= 1389796;
int q = (int)(seed / 2147483647);
seed -= (double)q * 2147483647;
return seed / 2147483647; }
// Returns a random integer 0 <= x < size. Seed must never be 0.
macro int irand(double& seed, int size) {
return (int)(drand(seed) * size); }
//=================================================================================================
// Time:
#ifdef _MSC_VER
#include <ctime>
macro double cpuTime(void) {
return (double)clock() / CLOCKS_PER_SEC; }
#else
#include <sys/time.h>
#include <sys/resource.h>
macro double cpuTime(void) {
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
return (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000; }
#endif
//=================================================================================================
// 'Pair':
template <class Fst, class Snd>
struct Pair {
typedef Fst Fst_t;
typedef Snd Snd_t;
Fst fst;
Snd snd;
Pair(void) { }
Pair(const Fst& x, const Snd& y) : fst(x), snd(y) { }
template <class FstCompat, class SndCompat>
Pair(const Pair<FstCompat, SndCompat>& p) : fst(p.fst), snd(p.snd) { }
void split(Fst& out_fst, Snd& out_snd) { out_fst = fst; out_snd = snd; }
};
template <class Fst, class Snd>
inline bool operator == (const Pair<Fst, Snd>& x, const Pair<Fst, Snd>& y) {
return (x.fst == y.fst) && (x.snd == y.snd); }
template <class Fst, class Snd>
inline bool operator < (const Pair<Fst, Snd>& x, const Pair<Fst, Snd>& y) {
return x.fst < y.fst ||
(!(y.fst < x.fst) && x.snd < y.snd); }
template <class Fst, class Snd>
inline Pair<Fst, Snd> Pair_new(const Fst& x, const Snd& y) {
return Pair<Fst, Snd>(x, y); }
//=================================================================================================
// 'vec' -- automatically resizable arrays (via 'push()' method):
// NOTE! Don't use this vector on datatypes that cannot be re-located in memory (with realloc)
template<class T>
class vec {
T* data;
int sz;
int cap;
void init(int size, const T& pad);
void grow(int min_cap);
public:
// Types:
typedef int Key;
typedef T Datum;
// Constructors:
vec(void) : data(NULL) , sz(0) , cap(0) { }
vec(int size) : data(NULL) , sz(0) , cap(0) { growTo(size); }
vec(int size, const T& pad) : data(NULL) , sz(0) , cap(0) { growTo(size, pad); }
vec(T* array, int size) : data(array), sz(size), cap(size) { } // (takes ownership of array -- will be deallocated with 'xfree()')
~vec(void) { clear(true); }
// Ownership of underlying array:
T* release (void) { T* ret = data; data = NULL; sz = 0; cap = 0; return ret; }
operator T* (void) { return data; } // (unsafe but convenient)
operator const T* (void) const { return data; }
// Size operations:
int size (void) const { return sz; }
void shrink (int nelems) { assert(nelems <= sz); for (int i = 0; i < nelems; i++) sz--, data[sz].~T(); }
void shrink_ (int nelems) { assert(nelems <= sz); sz -= nelems; }
void pop (void) { sz--, data[sz].~T(); }
void growTo (int size);
void growTo (int size, const T& pad);
void clear (bool dealloc = false);
void clear_ (void) { sz = 0; }
void capacity (int size) { grow(size); }
// Stack interface:
void push (void) { if (sz == cap) grow(sz+1); new (&data[sz]) T() ; sz++; }
void push (const T& elem) { if (sz == cap) grow(sz+1); new (&data[sz]) T(elem); sz++; }
//void push (const T& elem) { if (sz == cap) grow(sz+1); data[sz++] = elem; }
void push_ (const T& elem) { data[sz++] = elem; }
const T& last (void) const { return data[sz-1]; }
T& last (void) { return data[sz-1]; }
// Vector interface:
#ifdef DEBUG
const T& operator [] (int index) const { assert((uint)index < (uint)sz); return data[index]; }
T& operator [] (int index) { assert((uint)index < (uint)sz); return data[index]; }
#else
const T& operator [] (int index) const { return data[index]; }
T& operator [] (int index) { return data[index]; }
#endif
// Don't allow copying (error prone):
vec<T>& operator = (vec<T>& other) { TEMPLATE_FAIL; return *this; }
vec (vec<T>& other) { TEMPLATE_FAIL; }
// Duplicatation (preferred instead):
void copyTo(vec<T>& copy) const { copy.clear(); copy.growTo(sz); for (int i = 0; i < sz; i++) new (©[i]) T(data[i]); }
void moveTo(vec<T>& dest) { dest.clear(true); dest.data = data; dest.sz = sz; dest.cap = cap; data = NULL; sz = 0; cap = 0; }
};
template<class T>
void vec<T>::grow(int min_cap) {
if (min_cap <= cap) return;
if (cap == 0) cap = (min_cap >= 2) ? min_cap : 2;
else do cap = (cap*3+1) >> 1; while (cap < min_cap);
data = xrealloc(data, cap); }
template<class T>
void vec<T>::growTo(int size, const T& pad) {
if (sz >= size) return;
grow(size);
for (int i = sz; i < size; i++) new (&data[i]) T(pad);
sz = size; }
template<class T>
void vec<T>::growTo(int size) {
if (sz >= size) return;
grow(size);
for (int i = sz; i < size; i++) new (&data[i]) T();
sz = size; }
template<class T>
void vec<T>::clear(bool dealloc) {
if (data != NULL){
for (int i = 0; i < sz; i++) data[i].~T();
sz = 0;
if (dealloc) xfree(data), data = NULL, cap = 0; } }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// (for convenience)
void splitString(cchar* text, cchar* seps, vec<char*>& out);
template <class T>
macro void xfreeAll(vec<T*>& ptrs) {
for (int i = 0; i < ptrs.size(); i++)
xfree(ptrs[i]); }
//=================================================================================================
// Lifted booleans:
#if 0
class lbool {
int value;
explicit lbool(int v) : value(v) { }
public:
lbool() : value(0) { }
lbool(bool x) : value((int)x*2-1) { }
int toInt(void) const { return value; }
bool operator == (const lbool& other) const { return value == other.value; }
bool operator != (const lbool& other) const { return value != other.value; }
lbool operator ~ (void) const { return lbool(-value); }
friend int toInt (lbool l);
friend lbool toLbool(int v);
friend char name (lbool l);
};
inline int toInt (lbool l) { return l.toInt(); }
inline lbool toLbool(int v) { return lbool(v); }
inline char name (lbool l) { static char name[4] = {'!','0','?','1'}; int x = l.value; x = 2 + ((x >> (sizeof(int)*8-2)) | (x & ~(1 << (sizeof(int)*8-1)))); return name[x]; }
const lbool l_True = toLbool( 1);
const lbool l_False = toLbool(-1);
const lbool l_Undef = toLbool( 0);
const lbool l_Error = toLbool(1 << (sizeof(int)*8-1));
#endif
//=================================================================================================
// Relation operators -- extend definitions from '==' and '<'
#ifndef __SGI_STL_INTERNAL_RELOPS // (be aware of SGI's STL implementation...)
#define __SGI_STL_INTERNAL_RELOPS
template <class T> macro bool operator != (const T& x, const T& y) { return !(x == y); }
template <class T> macro bool operator > (const T& x, const T& y) { return y < x; }
template <class T> macro bool operator <= (const T& x, const T& y) { return !(y < x); }
template <class T> macro bool operator >= (const T& x, const T& y) { return !(x < y); }
#endif
//=================================================================================================
#endif