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