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 sign(int m)
25{
26 return m < 0 ? -1:
27 m > 0 ? 1:
28 0;
29}
30
31static 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
47static inline int randrange(int min, int max)
48{
49 return rand() % (max + 1 - min) + min;
50}
51
52static 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
64static 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