|
libft
|
Utility functions for general-purpose operations. More...
Files | |
| file | ft_utils.c |
| General-purpose utility functions. | |
| file | ft_utils.h |
| Utility functions for general-purpose operations. | |
Functions | |
| int | ft_rand (int min, int max) |
| Returns a random integer in the range [min, max] using xorshift32. | |
| void | ft_swap (void *a, void *b, size_t size) |
| Swaps the contents of two memory blocks. | |
| static unsigned int | xorshift32 (unsigned int *state) |
| Performs a 32-bit xorshift operation to generate a pseudo-random number. | |
Utility functions for general-purpose operations.
This group contains utility functions like ft_swap and ft_rand, which perform general operations that are often needed in various situations. These functions include swapping values and generating random numbers.
| int ft_rand | ( | int | min, |
| int | max | ||
| ) |
#include <include/ft_utils.h>
Returns a random integer in the range [min, max] using xorshift32.
This function uses the xorshift32 algorithm to generate a random number between the specified minimum and maximum values, inclusive. The state is initialized once using the system time and is then used to produce random values.
| min | The minimum value of the range (inclusive). |
| max | The maximum value of the range (inclusive). |
min and max, inclusive.| void ft_swap | ( | void * | a, |
| void * | b, | ||
| size_t | size | ||
| ) |
#include <include/ft_utils.h>
Swaps the contents of two memory blocks.
This function swaps the contents of two memory locations a and b of the specified size. The swap is performed using a temporary buffer to prevent data loss. It is essential to ensure that both pointers are non-NULL, and that the size is greater than zero for the function to work.
The function internally uses ft_memcpy to copy the contents between the two locations. After the swap, the temporary buffer is freed to avoid memory leaks.
| a | A pointer to the first memory block. |
| b | A pointer to the second memory block. |
| size | The size of the memory blocks being swapped. |
a, b, or size is invalid (NULL or zero), the function does nothing.
|
static |
#include <srcs/utilities/ft_utils.c>
Performs a 32-bit xorshift operation to generate a pseudo-random number.
This function implements the xorshift32 algorithm, a simple random number generator that uses bitwise operations to produce pseudo-random numbers. It modifies the provided state and returns the resulting random number.
| state | A pointer to the state value that is modified by the algorithm. |