Clustal Omega  1.2.4
Data Structures | Functions
util.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include "log.h"
#include "util.h"
Include dependency graph for util.c:

Data Structures

struct  sortwithindex_t
 

Functions

int CheckIfFileExists (char *pcFilename)
 Copy of squid's FileExists(). Copied here to make squid independent. More...
 
void * CkMalloc (size_t bytes, const char *function, const int line)
 Allocates memory (malloc) More...
 
void * CkCalloc (size_t count, size_t size, const char *function, const int line)
 Allocates memory (calloc). Memory will be set to zero. More...
 
void * CkRealloc (void *ptr, size_t bytes, const char *function, const int line)
 Reallocates memory. More...
 
void * CkFree (void *ptr, const char *function, const int line)
 Frees memory. More...
 
char * CkStrdup (const char *src)
 safe version of strdup More...
 
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. More...
 
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.". More...
 
int IntCmp (const void *a, const void *b)
 int comparison function for qsort More...
 
int SortAndTrackIndexCmpAsc (const void *a, const void *b)
 Compare two sortwithindex_t pointers and return the difference between the int value of the 1st sortwithindex_t and the 2nd. Used for ascending sort order in QSortWithIndexes()/. More...
 
int SortAndTrackIndexCmpDesc (const void *a, const void *b)
 Compare two sortwithindex_t pointers and return the difference between the int value of the 2nd sortwithindex_t and the 1st. Used for descending sort order in QSortWithIndexes() More...
 
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. More...
 
bool FileIsWritable (char *pcFileName)
 Test if file is writable. File may or may not exist. More...
 

Function Documentation

§ CheckIfFileExists()

int CheckIfFileExists ( char *  pcFilename)

Copy of squid's FileExists(). Copied here to make squid independent.

§ 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]countAllocate space for count objects
[in]sizeObjects are of this size
[in]functioncalling function name
[in]linecalling 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]ptrPointer to memory to be freed
[in]functioncalling function name
[in]linecalling 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]bytesbytes to allocated
[in]functioncalling function name
[in]linecalling 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]ptrPointer to memory to be reallocated
[in]bytesbytes to allocated
[in]functioncalling function name
[in]linecalling 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]srcString 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]pcFileNameFilename 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]permThe permutation array. Has to be preallocated
[out]lenLength 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]piSortedIndicesWill contain the indices of the sorted elements. Has to be preallocated.
[out]piArrayToSortArray with values to sort. Will only be overwritten if bOverwriteArrayToSort it true.
[in]iArrayLenNumber of elements in piArrayToSort.
[in]cOrderSort order. 'a' for ascending, 'd' for descending.
[in]bOverwriteArrayToSortIf 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]arrayPreallocated int array whose values will be set
[in]array_lenSize of array
[in]max_value

§ SortAndTrackIndexCmpAsc()

int SortAndTrackIndexCmpAsc ( const void *  a,
const void *  b 
)

Compare two sortwithindex_t pointers and return the difference between the int value of the 1st sortwithindex_t and the 2nd. Used for ascending sort order in QSortWithIndexes()/.

See also
SortAndTrackIndexCmpDesc() and QSortAndTrackIndex()

§ SortAndTrackIndexCmpDesc()

int SortAndTrackIndexCmpDesc ( const void *  a,
const void *  b 
)

Compare two sortwithindex_t pointers and return the difference between the int value of the 2nd sortwithindex_t and the 1st. Used for descending sort order in QSortWithIndexes()

See also
SortAndTrackIndexCmpDesc() and QSortAndTrackIndex()