helper.h
| 1 | #ifndef HELPER_H |
| 2 | #define HELPER_H |
| 3 | |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdbool.h> |
| 7 | |
| 8 | struct point |
| 9 | { |
| 10 | int y; |
| 11 | int x; |
| 12 | }; |
| 13 | |
| 14 | static inline bool pointeq(struct point p1, struct point p2) |
| 15 | { |
| 16 | return p1.y == p2.y && p1.x == p2.x; |
| 17 | } |
| 18 | |
| 19 | static inline unsigned int iabs(int i) |
| 20 | { |
| 21 | return i < 0 ? -i : i; |
| 22 | } |
| 23 | |
| 24 | static inline int sign(int m) |
| 25 | { |
| 26 | return m < 0 ? -1: |
| 27 | m > 0 ? 1: |
| 28 | 0; |
| 29 | } |
| 30 | |
| 31 | static inline int ipow(int base, int exp) |
| 32 | { |
| 33 | int result = 1; |
| 34 | for (;;) |
| 35 | { |
| 36 | if (exp & 1) |
| 37 | result *= base; |
| 38 | exp >>= 1; |
| 39 | if (!exp) |
| 40 | break; |
| 41 | base *= base; |
| 42 | } |
| 43 | |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | static inline int randrange(int min, int max) |
| 48 | { |
| 49 | return rand() % (max + 1 - min) + min; |
| 50 | } |
| 51 | |
| 52 | static inline void *xmalloc(size_t n) |
| 53 | { |
| 54 | void *ret = malloc(n); |
| 55 | if(!ret) |
| 56 | { |
| 57 | fprintf(stderr, "malloc failed\n"); |
| 58 | abort(); |
| 59 | } |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | static inline void *xrealloc(void *ptr, size_t n) |
| 65 | { |
| 66 | void *ret = realloc(ptr, n); |
| 67 | if(!ret) |
| 68 | { |
| 69 | fprintf(stderr, "realloc failed\n"); |
| 70 | abort(); |
| 71 | } |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | #endif //HELPER_H |
| 77 | |
| 78 |