penguins
1.0.0
|
Go to the source code of this file.
Various miscellaneous utilities.
Definition in file utils.h.
Data Structures | |
struct | Coords |
A pair of 2D coordinates, used for addressing the Game::board_grid. More... | |
struct | Rng |
A wrapper around random number generators. More... | |
Macros | |
#define | UNUSED(var) ((void)(var)) |
Helper for silencing warnings about unused variables. More... | |
#define | ALWAYS_INLINE |
Strongly suggests to the compiler that a function should be inlined. More... | |
#define | free_and_clear(ptr) (free((ptr)), (ptr) = NULL) |
Calls free on a pointer and then sets it to NULL . More... | |
#define | sizeof_array(ptr) (sizeof((ptr)) / sizeof(*(ptr))) |
Computes the number of elements of an array. More... | |
#define | my_max(x, y) (((x) > (y)) ? (x) : (y)) |
Compares two numbers and returns the larger one. More... | |
#define | my_min(x, y) (((x) < (y)) ? (x) : (y)) |
Compares two numbers and returns the smaller one. More... | |
#define | FNV32_INITIAL_STATE ((uint32_t)2166136261) |
A constant for fnv32_hash. More... | |
#define | FNV32_PRIME ((uint32_t)16777619) |
A constant for fnv32_hash. More... | |
Macros for working with bitfields | |
#define | set_bit(num, bit) ((num) | (bit)) |
#define | clear_bit(num, bit) ((num) & ~(bit)) |
#define | toggle_bit(num, bit) ((num) ^ (bit)) |
#define | change_bit(num, bit, val) (((num) & ~(bit)) | ((val) ? (bit) : 0)) |
#define | test_bit(num, bit) (((num) & (bit)) != 0) |
Enumerations | |
enum | Direction { DIRECTION_RIGHT , DIRECTION_DOWN , DIRECTION_LEFT , DIRECTION_UP , DIRECTION_MAX } |
enum | Neighbor { NEIGHBOR_RIGHT , NEIGHBOR_BOTTOM_RIGHT , NEIGHBOR_BOTTOM , NEIGHBOR_BOTTOM_LEFT , NEIGHBOR_LEFT , NEIGHBOR_TOP_LEFT , NEIGHBOR_TOP , NEIGHBOR_TOP_RIGHT , NEIGHBOR_MAX } |
Functions | |
bool | coords_same (Coords a, Coords b) |
Checks if two Coords pairs are equal. More... | |
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, otherwise returns NULL . More... | |
bool | parse_number (const char *str, long *result) |
Converts a string into a number, returns false if the string was invalid. More... | |
void * | memdup (const void *src, size_t size) |
A shorthand for malloc + memcpy (analogous to strdup ). More... | |
Rng | init_stdlib_rng (void) |
Returns an RNG implementation based on the rand function from <stdlib.h> (and seeds it with srand ). More... | |
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. More... | |
Variables | |
const Coords | DIRECTION_TO_COORDS [DIRECTION_MAX] |
A table that maps Direction variants to relative Coords. More... | |
const Coords | NEIGHBOR_TO_COORDS [NEIGHBOR_MAX] |
A table that maps Neighbor variants to relative Coords. More... | |
const Neighbor | DIRECTION_TO_NEIGHBOR [DIRECTION_MAX] |
A table that maps Direction variants to corresponding Neighbor variants. More... | |
#define UNUSED | ( | var | ) | ((void)(var)) |
Helper for silencing warnings about unused variables.
To use it just pass the name of the variable you want to hide the usage warning for, like this:
One notable use is getting rid of warnings related to variables used in assert
conditions. When the assertions are disabled, the condition passed to the assert
macro effectively becomes a useless bit of text and the compiler can't find any usages of variables within it since the condition itself doesn't even get parsed.
#define ALWAYS_INLINE |
Strongly suggests to the compiler that a function should be inlined.
Can be used to ask the compiler to inline a function even when inlining is disabled (in the debug configuration, for example). Note that this is still treated by the compilers as a hint, though an authoritative one.
#define free_and_clear | ( | ptr | ) | (free((ptr)), (ptr) = NULL) |
#define sizeof_array | ( | ptr | ) | (sizeof((ptr)) / sizeof(*(ptr))) |
#define my_max | ( | x, | |
y | |||
) | (((x) > (y)) ? (x) : (y)) |
#define my_min | ( | x, | |
y | |||
) | (((x) < (y)) ? (x) : (y)) |
#define change_bit | ( | num, | |
bit, | |||
val | |||
) | (((num) & ~(bit)) | ((val) ? (bit) : 0)) |
#define FNV32_INITIAL_STATE ((uint32_t)2166136261) |
A constant for fnv32_hash.
#define FNV32_PRIME ((uint32_t)16777619) |
A constant for fnv32_hash.
enum Direction |
enum Neighbor |
Checks if two Coords pairs are equal.
Definition at line 46 of file utils.h.
Referenced by PlayerPlacementController::on_mouse_move(), PlayerMovementController::on_mouse_move(), PlayerPlacementController::on_mouse_up(), PlayerMovementController::on_mouse_up(), CanvasPanel::paint_board(), CanvasPanel::paint_move_arrow(), and PlayerMovementController::paint_overlay().
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, otherwise returns NULL
.
Definition at line 46 of file utils.c.
Referenced by parse_arguments().
bool parse_number | ( | const char * | str, |
long * | result | ||
) |
Converts a string into a number, returns false
if the string was invalid.
A wrapper around the strtol
function, which, unlike atoi
, can reliably tell us whether an error has occurred, whereas atoi
simply returns zero, so there is no way to distinguish between an error or the string legitimately containing a zero.
[in] | str | |
[out] | result |
Definition at line 64 of file utils.c.
Referenced by parse_arguments().
void* memdup | ( | const void * | src, |
size_t | size | ||
) |
A shorthand for malloc
+ memcpy
(analogous to strdup
).
Definition at line 79 of file utils.c.
Referenced by Game::game_clone().
Rng init_stdlib_rng | ( | void | ) |
Returns an RNG implementation based on the rand
function from <stdlib.h>
(and seeds it with srand
).
Definition at line 123 of file utils.c.
Referenced by run_autonomous_mode(), and run_interactive_mode().
|
inline |
Computes the 32-bit FNV-1a hash of a given byte sequence.
This is a fast and simple hash function that shouldn't be used for anything serious. The invocations may be chained together to compute the hash of, say, a struct or an array by passing the returned value to the state
argument of the next call, however, the first call must use FNV32_INITIAL_STATE
as the value of state
.
Definition at line 151 of file utils.h.
Referenced by CanvasPanel::paint_tiles().
|
extern |
A table that maps Direction variants to relative Coords.
Definition at line 19 of file utils.c.
Referenced by BotState::bot_generate_all_moves_list(), BotState::bot_rate_move(), BotState::bot_rate_moves_list(), Game::calculate_penguin_possible_moves(), Game::count_obstructed_directions(), and PlayerMovementController::update_tile_attributes().
|
extern |
A table that maps Neighbor variants to relative Coords.
Definition at line 27 of file utils.c.
Referenced by BotState::bot_quick_junction_check(), and CanvasPanel::on_paint().
|
extern |
A table that maps Direction variants to corresponding Neighbor variants.
Definition at line 35 of file utils.c.
Referenced by BotState::bot_quick_junction_check().