penguins  1.0.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
utils.h File Reference

Go to the source code of this file.

Detailed Description

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...
 

Macro Definition Documentation

◆ UNUSED

#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:

void function(int some_param) {
UNUSED(some_param);
int some_var = 123;
UNUSED(some_var);
}
#define UNUSED(var)
Helper for silencing warnings about unused variables.
Definition: utils.h:72

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.

Definition at line 72 of file utils.h.

◆ ALWAYS_INLINE

#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.

See also
https://learn.microsoft.com/en-us/cpp/cpp/inline-functions-cpp?view=msvc-170
https://clang.llvm.org/docs/AttributeReference.html#always-inline-force-inline
https://gcc.gnu.org/onlinedocs/gcc/Inline.html

Definition at line 88 of file utils.h.

◆ free_and_clear

#define free_and_clear (   ptr)    (free((ptr)), (ptr) = NULL)

Calls free on a pointer and then sets it to NULL.

Definition at line 92 of file utils.h.

◆ sizeof_array

#define sizeof_array (   ptr)    (sizeof((ptr)) / sizeof(*(ptr)))

Computes the number of elements of an array.

Definition at line 95 of file utils.h.

◆ my_max

#define my_max (   x,
 
)    (((x) > (y)) ? (x) : (y))

Compares two numbers and returns the larger one.

Definition at line 99 of file utils.h.

◆ my_min

#define my_min (   x,
 
)    (((x) < (y)) ? (x) : (y))

Compares two numbers and returns the smaller one.

Definition at line 101 of file utils.h.

◆ set_bit

#define set_bit (   num,
  bit 
)    ((num) | (bit))

Definition at line 105 of file utils.h.

◆ clear_bit

#define clear_bit (   num,
  bit 
)    ((num) & ~(bit))

Definition at line 106 of file utils.h.

◆ toggle_bit

#define toggle_bit (   num,
  bit 
)    ((num) ^ (bit))

Definition at line 107 of file utils.h.

◆ change_bit

#define change_bit (   num,
  bit,
  val 
)    (((num) & ~(bit)) | ((val) ? (bit) : 0))

Definition at line 108 of file utils.h.

◆ test_bit

#define test_bit (   num,
  bit 
)    (((num) & (bit)) != 0)

Definition at line 109 of file utils.h.

◆ FNV32_INITIAL_STATE

#define FNV32_INITIAL_STATE   ((uint32_t)2166136261)

A constant for fnv32_hash.

Definition at line 138 of file utils.h.

◆ FNV32_PRIME

#define FNV32_PRIME   ((uint32_t)16777619)

A constant for fnv32_hash.

Definition at line 140 of file utils.h.

Enumeration Type Documentation

◆ Direction

enum Direction
Enumerator
DIRECTION_RIGHT 
DIRECTION_DOWN 
DIRECTION_LEFT 
DIRECTION_UP 
DIRECTION_MAX 

Definition at line 20 of file utils.h.

◆ Neighbor

enum Neighbor
Enumerator
NEIGHBOR_RIGHT 
NEIGHBOR_BOTTOM_RIGHT 
NEIGHBOR_BOTTOM 
NEIGHBOR_BOTTOM_LEFT 
NEIGHBOR_LEFT 
NEIGHBOR_TOP_LEFT 
NEIGHBOR_TOP 
NEIGHBOR_TOP_RIGHT 
NEIGHBOR_MAX 

Definition at line 28 of file utils.h.

Function Documentation

◆ coords_same()

◆ strip_prefix()

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().

◆ parse_number()

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.

Parameters
[in]str
[out]result
See also
https://en.cppreference.com/w/c/string/byte/strtol

Definition at line 64 of file utils.c.

Referenced by parse_arguments().

◆ memdup()

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().

◆ init_stdlib_rng()

Rng init_stdlib_rng ( void  )

Returns an RNG implementation based on the rand function from <stdlib.h> (and seeds it with srand).

Warning
It is probably not thread-safe though... Then again, it's not like we are using threads in the C program.

Definition at line 123 of file utils.c.

Referenced by run_autonomous_mode(), and run_interactive_mode().

◆ fnv32_hash()

uint32_t fnv32_hash ( uint32_t  state,
const void *  buf,
size_t  len 
)
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.

See also
https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function

Definition at line 151 of file utils.h.

Referenced by CanvasPanel::paint_tiles().

Variable Documentation

◆ DIRECTION_TO_COORDS

◆ NEIGHBOR_TO_COORDS

const Coords NEIGHBOR_TO_COORDS[NEIGHBOR_MAX]
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().

◆ DIRECTION_TO_NEIGHBOR

const Neighbor DIRECTION_TO_NEIGHBOR[DIRECTION_MAX]
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().