Skip to content

Instantly share code, notes, and snippets.

@pavi2410
Forked from fmeyer/macros.h
Last active March 2, 2022 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavi2410/2b0318a8d13efeccdca71e9fd0bc4bf7 to your computer and use it in GitHub Desktop.
Save pavi2410/2b0318a8d13efeccdca71e9fd0bc4bf7 to your computer and use it in GitHub Desktop.
#define MAT(name, T, n, d) \
vector<vector<T>> name(n, vector<T>(n, d))
#define MAT2(name, T, n, m, d) \
vector<vector<T>> name(n, vector<T>(m, d))
#define DBG(e) \
cout << #e << " = " << (e)
/* STMT, useful for creating multiple statements macros */
#define STMT( stuff ) do { stuff } while (0)
/* Loops */
#define FOREVER for(;;)
#define FOR(i,a,b) for(i=(a);(i<=(b));((i)++))
#define FOREACH(i, A) for(int _keep=1, \
_count=0,\
_size=sizeof (A) / sizeof *(A); \
_keep && _count != _size; \
_keep = !_keep, _count++) \
for(i = (A)+_count; _keep; _keep = !_keep)
/* Maths */
#define PI 3.14159265
#define RAD2DEG(x) ((x)/PI*180)
#define DEG2RAD(x) ((x)*PI/180)
#define CLIP(x, min, max) (((x) < (min)) ? (min) : \
(((x) > (max)) ? (max) : (x)))
#define UCLIP(x, max) (((x) > (max)) ? (max) : (x))
#define LCLIP(x, min) (((x) < (min)) ? (min) : (x))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define SWAP(a, b) STMT( a ^= b; b ^= a; a ^= b; )
#define SORT(a, b) STMT( if ((a) > (b)) SWAP((a), (b)); )
#define COMPARE(x, y) (((x) > (y)) - ((x) < (y)))
#define SIGN(x) COMPARE(x, 0)
#define IS_ODD( num ) ((num) & 1)
#define IS_EVEN( num ) (!IS_ODD( (num) ))
#define IS_BETWEEN(n,L,H) ((n) >= (L) && (n) <= (H))
/* STRINGS */
#define STRING char*
#define STR2(s) #s
#define STR(s) STR2(s)
#define CAT(str1,str2) (str1 "" str2)
/* TOKENS */
#define PASTE2(a,b) a##b
#define PASTE(a,b) PASTE2(a,b)
#define PRINT_TOKEN(token) printf(#token " is %d", token)
/* DEBUGGING */
#define LOG(x, fmt, ...) if(x){printf("%s:%d: " fmt "\n",\
__FILE__, __LINE__,__VA_ARGS__);}
#define TRY(x,s) if(!(x)){printf("%s:%d:%s",__FILE__, __LINE__,s);}
/*you have to #define DEBUG to enable ASSERT*/
#ifndef DEBUG
#define ASSERT(n)
#else
#define ASSERT(n) if(!(n)) { \
printf("%s - Failed ",#n); \
printf("On %s ",__DATE__); \
printf("At %s ",__TIME__); \
printf("In File %s ",__FILE__); \
printf("At Line %d\n",__LINE__); \
return(-1);}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment