#include <limits.h>
#include <string.h>
#include <strings.h>
#include <stdarg.h>
#include <stdbool.h>
Go to the source code of this file.
|
#define | CKMALLOC(b) CkMalloc((b), __FUNCTION__, __LINE__) |
|
#define | CKCALLOC(c, s) CkCalloc((c), (s), __FUNCTION__, __LINE__) |
|
#define | CKREALLOC(p, b) CkRealloc((p), (b), __FUNCTION__, __LINE__) |
|
#define | CKFREE(b) ((b)=CkFree((b), __FUNCTION__, __LINE__)) |
|
#define | MAX(a, b) ((a)>(b)?(a):(b)) |
|
#define | MIN(a, b) ((a)<(b)?(a):(b)) |
|
#define | STR_EQ(a, b) (strcmp((a),(b)) == 0) |
|
#define | STR_NC_EQ(a, b) (strcasecmp((a),(b)) == 0) |
|
#define | TRUE true |
|
#define | FALSE false |
|
|
void * | CkMalloc (size_t size, const char *function, const int line) |
| Allocates memory (malloc)
|
|
void * | CkCalloc (size_t count, size_t size, const char *function, const int line) |
| Allocates memory (calloc). Memory will be set to zero.
|
|
void * | CkRealloc (void *ptr, size_t bytes, const char *function, const int line) |
| Reallocates memory.
|
|
void * | CkFree (void *ptr, const char *function, const int line) |
| Frees memory.
|
|
char * | CkStrdup (const char *s) |
| safe version of strdup
|
|
void | PermutationArray (int **array, const int len) |
| Creates an int array of size len with len-1 random but unique integers with values 0–len-1. This is "a modified version of
Fisher-Yates known as Durstenfeld-Fisher-Yates or
Knuth-Fisher-Yates". See http://stackoverflow.com/questions/196017/unique-random-numbers-in-o1.
|
|
void | RandomUniqueIntArray (int *array, const int array_len, const int max_value) |
| Creates an array filled with random, but unique ints of given range. Implementation of the Floyd algorithm. See http://stackoverflow.com/questions/1608181/unique-random-numbers-in-an-integer-array-in-the-c-programming-language Assuming M is the length of the desired array and N is the numeric range: "This approach has the complexity of about O(M) (depends on
the search structure used), so it is better suitable when M << N.
This approach keeps track of already generated random numbers, so
it requires extra memory. However, the beauty of it is that it does
not make any of those horrendous "trial and error" iterations,
trying to find an unused random number. This algorithm is
guaranteed to generate one unique random number after each single
call to the random number generator.".
|
|
int | IntCmp (const void *a, const void *b) |
| int comparison function for qsort
|
|
bool | FileIsWritable (char *pcFileName) |
| Test if file is writable. File may or may not exist.
|
|
void | QSortAndTrackIndex (int *piSortedIndices, int *piArrayToSort, const int uArrayLen, const char cOrder, const bool bOverwriteArrayToSort) |
| Sort a given int array in ascending or descending order, while keeping track of the element order.
|
|
◆ CKCALLOC
#define CKCALLOC |
( |
|
c, |
|
|
|
s |
|
) |
| CkCalloc((c), (s), __FUNCTION__, __LINE__) |
◆ CKFREE
#define CKFREE |
( |
|
b | ) |
((b)=CkFree((b), __FUNCTION__, __LINE__)) |
◆ CKMALLOC
#define CKMALLOC |
( |
|
b | ) |
CkMalloc((b), __FUNCTION__, __LINE__) |
◆ CKREALLOC
#define CKREALLOC |
( |
|
p, |
|
|
|
b |
|
) |
| CkRealloc((p), (b), __FUNCTION__, __LINE__) |
◆ FALSE
◆ MAX
#define MAX |
( |
|
a, |
|
|
|
b |
|
) |
| ((a)>(b)?(a):(b)) |
◆ MIN
#define MIN |
( |
|
a, |
|
|
|
b |
|
) |
| ((a)<(b)?(a):(b)) |
◆ STR_EQ
#define STR_EQ |
( |
|
a, |
|
|
|
b |
|
) |
| (strcmp((a),(b)) == 0) |
◆ STR_NC_EQ
#define STR_NC_EQ |
( |
|
a, |
|
|
|
b |
|
) |
| (strcasecmp((a),(b)) == 0) |
◆ TRUE
◆ CkCalloc()
void * CkCalloc |
( |
size_t |
count, |
|
|
size_t |
size, |
|
|
const char * |
function, |
|
|
const int |
line |
|
) |
| |
Allocates memory (calloc). Memory will be set to zero.
- Parameters
-
[in] | count | Allocate space for count objects |
[in] | size | Objects are of this size |
[in] | function | calling function name |
[in] | line | calling function line |
- Returns
- void pointer to the newly allocated and zeroed memory (calloc).
- Note
- use provided macro CKCALLOC() which automatically adds function name and line number
◆ CkFree()
void * CkFree |
( |
void * |
ptr, |
|
|
const char * |
function, |
|
|
const int |
line |
|
) |
| |
Frees memory.
- Parameters
-
[in] | ptr | Pointer to memory to be freed |
[in] | function | calling function name |
[in] | line | calling function line |
- Returns
- void pointer to the now zeroed memory
- Note
- use provided macro CKFREE()
◆ CkMalloc()
void * CkMalloc |
( |
size_t |
bytes, |
|
|
const char * |
function, |
|
|
const int |
line |
|
) |
| |
Allocates memory (malloc)
- Parameters
-
[in] | bytes | bytes to allocated |
[in] | function | calling function name |
[in] | line | calling function line |
- Returns
- void pointer to the newly allocated memory
- Note
- use provided macro CKMALLOC() which automatically adds function name and line number
◆ CkRealloc()
void * CkRealloc |
( |
void * |
ptr, |
|
|
size_t |
bytes, |
|
|
const char * |
function, |
|
|
const int |
line |
|
) |
| |
Reallocates memory.
- Parameters
-
[in] | ptr | Pointer to memory to be reallocated |
[in] | bytes | bytes to allocated |
[in] | function | calling function name |
[in] | line | calling function line |
- Returns
- void pointer to the newly allocated memory
- Note
- use provided macro CKREALLOC() which automatically adds function name and line number
◆ CkStrdup()
char * CkStrdup |
( |
const char * |
src | ) |
|
safe version of strdup
- Parameters
-
[in] | src | String to copy from |
- Returns
- copy of string
- Note
- src is not allowed to be NULL.
◆ FileIsWritable()
bool FileIsWritable |
( |
char * |
pcFileName | ) |
|
Test if file is writable. File may or may not exist.
- Parameters
-
[in] | pcFileName | Filename to check |
- Returns
- True if file is writable at the time of calling
◆ IntCmp()
int IntCmp |
( |
const void * |
a, |
|
|
const void * |
b |
|
) |
| |
int comparison function for qsort
◆ PermutationArray()
void PermutationArray |
( |
int ** |
perm, |
|
|
const int |
len |
|
) |
| |
Creates an int array of size len with len-1 random but unique integers with values 0–len-1. This is "a modified version of
Fisher-Yates known as Durstenfeld-Fisher-Yates or
Knuth-Fisher-Yates". See http://stackoverflow.com/questions/196017/unique-random-numbers-in-o1.
- Parameters
-
[in] | perm | The permutation array. Has to be preallocated |
[out] | len | Length of the permutation array |
◆ QSortAndTrackIndex()
void QSortAndTrackIndex |
( |
int * |
piSortedIndices, |
|
|
int * |
piArrayToSort, |
|
|
const int |
iArrayLen, |
|
|
const char |
cOrder, |
|
|
const bool |
bOverwriteArrayToSort |
|
) |
| |
Sort a given int array in ascending or descending order, while keeping track of the element order.
- Parameters
-
[out] | piSortedIndices | Will contain the indices of the sorted elements. Has to be preallocated. |
[out] | piArrayToSort | Array with values to sort. Will only be overwritten if bOverwriteArrayToSort it true. |
[in] | iArrayLen | Number of elements in piArrayToSort. |
[in] | cOrder | Sort order. 'a' for ascending, 'd' for descending. |
[in] | bOverwriteArrayToSort | If false do not overwrite the array to sort. |
< aux
◆ RandomUniqueIntArray()
void RandomUniqueIntArray |
( |
int * |
array, |
|
|
const int |
array_len, |
|
|
const int |
max_value |
|
) |
| |
Creates an array filled with random, but unique ints of given range. Implementation of the Floyd algorithm. See http://stackoverflow.com/questions/1608181/unique-random-numbers-in-an-integer-array-in-the-c-programming-language Assuming M is the length of the desired array and N is the numeric range: "This approach has the complexity of about O(M) (depends on
the search structure used), so it is better suitable when M << N.
This approach keeps track of already generated random numbers, so
it requires extra memory. However, the beauty of it is that it does
not make any of those horrendous "trial and error" iterations,
trying to find an unused random number. This algorithm is
guaranteed to generate one unique random number after each single
call to the random number generator.".
- Warning
- This won't work if max_value<=array_len. Only use for cases where array_len<<max_value
- Parameters
-
[in] | array | Preallocated int array whose values will be set |
[in] | array_len | Size of array |
[in] | max_value | |