penguins  1.0.0
utils.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include <stdbool.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
15 typedef struct Coords {
16  int x;
17  int y;
18 } Coords;
19 
20 typedef enum Direction {
26 } Direction;
27 
28 typedef enum Neighbor {
38 } Neighbor;
39 
43 
46 inline bool coords_same(Coords a, Coords b) {
47  return a.x == b.x && a.y == b.y;
48 }
49 
68 // Generally several approaches are possible for implementing a similar macro:
69 // <https://stackoverflow.com/a/3599170/12005228>
70 // <https://stackoverflow.com/a/3599203/12005228>
71 // <https://stackoverflow.com/a/12891181/12005228>
72 #define UNUSED(var) ((void)(var))
73 
83 #if defined(__GNUC__)
84 #define ALWAYS_INLINE __attribute__((always_inline))
85 #elif defined(_MSC_VER)
86 #define ALWAYS_INLINE __forceinline
87 #else
88 #define ALWAYS_INLINE
89 #endif
90 
92 #define free_and_clear(ptr) (free((ptr)), (ptr) = NULL)
93 
95 #define sizeof_array(ptr) (sizeof((ptr)) / sizeof(*(ptr)))
96 
97 // Taken from <https://stackoverflow.com/a/21338744/12005228>
99 #define my_max(x, y) (((x) > (y)) ? (x) : (y))
101 #define my_min(x, y) (((x) < (y)) ? (x) : (y))
102 
105 #define set_bit(num, bit) ((num) | (bit))
106 #define clear_bit(num, bit) ((num) & ~(bit))
107 #define toggle_bit(num, bit) ((num) ^ (bit))
108 #define change_bit(num, bit, val) (((num) & ~(bit)) | ((val) ? (bit) : 0))
109 #define test_bit(num, bit) (((num) & (bit)) != 0)
111 
112 const char* strip_prefix(const char* str, const char* prefix);
113 
114 bool parse_number(const char* str, long* result);
115 
116 void* memdup(const void* src, size_t size);
117 
129 typedef struct Rng {
132  int (*random_range)(struct Rng* self, int min, int max);
133 } Rng;
134 
135 Rng init_stdlib_rng(void);
136 
138 #define FNV32_INITIAL_STATE ((uint32_t)2166136261)
140 #define FNV32_PRIME ((uint32_t)16777619)
141 
151 inline uint32_t fnv32_hash(uint32_t state, const void* buf, size_t len) {
152  const uint8_t* ptr = (const uint8_t*)buf;
153  const uint8_t* end = ptr + len;
154  for (; ptr != end; ptr++) state = (state ^ *ptr) * FNV32_PRIME;
155  return state;
156 }
157 
158 #ifdef __cplusplus
159 }
160 #endif
A pair of 2D coordinates, used for addressing the Game::board_grid.
Definition: utils.h:15
int x
Definition: utils.h:16
bool coords_same(Coords a, Coords b)
Checks if two Coords pairs are equal.
Definition: utils.h:46
int y
Definition: utils.h:17
A wrapper around random number generators.
Definition: utils.h:129
int(* random_range)(struct Rng *self, int min, int max)
Generates and returns a value in the range [min; max) (i.e. from min inclusive to max exclusive).
Definition: utils.h:132
Rng init_stdlib_rng(void)
Returns an RNG implementation based on the rand function from <stdlib.h> (and seeds it with srand).
Definition: utils.c:123
void * memdup(const void *src, size_t size)
A shorthand for malloc + memcpy (analogous to strdup).
Definition: utils.c:79
Direction
Definition: utils.h:20
@ DIRECTION_MAX
Definition: utils.h:25
@ DIRECTION_DOWN
Definition: utils.h:22
@ DIRECTION_LEFT
Definition: utils.h:23
@ DIRECTION_UP
Definition: utils.h:24
@ DIRECTION_RIGHT
Definition: utils.h:21
const Coords NEIGHBOR_TO_COORDS[NEIGHBOR_MAX]
A table that maps Neighbor variants to relative Coords.
Definition: utils.c:27
const Coords DIRECTION_TO_COORDS[DIRECTION_MAX]
A table that maps Direction variants to relative Coords.
Definition: utils.c:19
bool parse_number(const char *str, long *result)
Converts a string into a number, returns false if the string was invalid.
Definition: utils.c:64
const Neighbor DIRECTION_TO_NEIGHBOR[DIRECTION_MAX]
A table that maps Direction variants to corresponding Neighbor variants.
Definition: utils.c:35
#define FNV32_PRIME
A constant for fnv32_hash.
Definition: utils.h:140
Neighbor
Definition: utils.h:28
@ NEIGHBOR_TOP_RIGHT
Definition: utils.h:36
@ NEIGHBOR_BOTTOM_RIGHT
Definition: utils.h:30
@ NEIGHBOR_BOTTOM
Definition: utils.h:31
@ NEIGHBOR_TOP_LEFT
Definition: utils.h:34
@ NEIGHBOR_RIGHT
Definition: utils.h:29
@ NEIGHBOR_MAX
Definition: utils.h:37
@ NEIGHBOR_TOP
Definition: utils.h:35
@ NEIGHBOR_BOTTOM_LEFT
Definition: utils.h:32
@ NEIGHBOR_LEFT
Definition: utils.h:33
uint32_t fnv32_hash(uint32_t state, const void *buf, size_t len)
Computes the 32-bit FNV-1a hash of a given byte sequence.
Definition: utils.h:151
const char * strip_prefix(const char *str, const char *prefix)
Returns a substring with the prefix removed if the given string starts with the prefix,...
Definition: utils.c:46