src/clustal/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.
void * CkMalloc (size_t bytes, 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 *src)
 safe version of strdup
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.
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
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()/.
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().
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.
bool FileIsWritable (char *pcFileName)
 Test if file is writable. File may or may not exist.

Function Documentation

int CheckIfFileExists ( char *  pcFilename  ) 

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

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
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()
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
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
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.
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
int IntCmp ( const void *  a,
const void *  b 
)

int comparison function for qsort

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

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 
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()
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()
Generated on Fri Aug 31 05:32:52 2012 for Clustal Omega by  doxygen 1.6.3