My library
Library for common tasks
Functions
arrays.h File Reference

Common tasks with arrays: sorting, searching, printing etc. More...

#include "types.h"
Include dependency graph for arrays.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void chooseBubbleSortArr (const spec_t spec, void *arr, unsigned int size,...)
 Bubble sort for arrays. More...
 
void chooseQuickSortArr (const spec_t spec, void *arr, int size,...)
 Quick sort for arrays. More...
 
int chooseLinearSearchArr (const spec_t spec, const void *arr, int size,...)
 Linear search for arrays. More...
 
void printMatrix (const spec_t spec, const void *matrix, const unsigned int nRows, const unsigned int nColumns)
 Print a matrix of specified size with specified formatting. More...
 
void charBubbleSortArr (char *arr, unsigned int size)
 Bubblesort for arrays of chars. More...
 
void intBubbleSortArr (int *arr, unsigned int size)
 Bubblesort for arrays of ints. More...
 
void floatBubbleSortArr (float *arr, unsigned int size)
 Bubblesort for arrays of floats. More...
 
void doubleBubbleSortArr (double *arr, unsigned int size)
 Bubblesort for arrays of doubles. More...
 
void ptrBubbleSortArr (void **arr, unsigned int size, int(*cmpFunc)(const void *a, const void *b))
 Bubblesort for arrays of pointers. More...
 
void charQuickSortArr (char *arr, int size)
 Quicksort for arrays of chars. More...
 
void intQuickSortArr (int *arr, int size)
 Quicksort for arrays of ints. More...
 
void floatQuickSortArr (float *arr, int size)
 Quicksort for arrays of floats. More...
 
void doubleQuickSortArr (double *arr, int size)
 Quicksort for arrays of doubles. More...
 
void ptrQuickSortArr (void *arr, int size, int(*cmpFunc)(const void *a, const void *b))
 Quicksort for arrays of pointers. More...
 
int charLinearSearchArr (const char *arr, int size, char key)
 Linear search for arrays of chars. More...
 
int intLinearSearchArr (const char *arr, int size, int key)
 Linear search for arrays of integers. More...
 
int floatLinearSearchArr (const char *arr, int size, float key)
 Linear search for arrays of floats. More...
 
int doubleLinearSearchArr (const char *arr, int size, double key)
 Linear search for arrays of doubles. More...
 
int ptrLinearSearchArr (const void *arr, int size, void *key, int(*cmpFunc)(const void *a, const void *b))
 Linear search for arrays of pointers. More...
 

Detailed Description

Common tasks with arrays: sorting, searching, printing etc.

Author
Pietro Firpo (pietr.nosp@m.o.fi.nosp@m.rpo@p.nosp@m.m.me)

Function Documentation

◆ charBubbleSortArr()

void charBubbleSortArr ( char *  arr,
unsigned int  size 
)

Bubblesort for arrays of chars.

Equivalent to chooseBubbleSortArr("%c", arr, size). Refer to chooseBubbleSortArr()

◆ charLinearSearchArr()

int charLinearSearchArr ( const char *  arr,
int  size,
char  key 
)

Linear search for arrays of chars.

Equivalent to chooseLinearSearchArr("%c", arr, size, key). Refer to chooseQuickSortArr()

◆ charQuickSortArr()

void charQuickSortArr ( char *  arr,
int  size 
)

Quicksort for arrays of chars.

Equivalent to chooseQuickSortArr("%c", arr, size). Refer to chooseQuickSortArr()

◆ chooseBubbleSortArr()

void chooseBubbleSortArr ( const spec_t  spec,
void *  arr,
unsigned int  size,
  ... 
)

Bubble sort for arrays.

Parameters
specType specifier of the array to be sorted. Refer to spec_t for supported types
arrPointer to the first element of the array to be sorted
sizeNumber of elements of the array to be sorted
...The comparison function needed to compare items inside given lists. This parameter is necessary only for pointer ArrayList type and is ignored otherwise. Must be a function that takes two pointers as argument and returns a positive int if the item pointed by the first argument is greater than the item pointed by the second argument, a negative int if the item pointed by the first argument is is smaller than the item pointed by second, a zero int if the item pointed by first and second arguments are equal

◆ chooseLinearSearchArr()

int chooseLinearSearchArr ( const spec_t  spec,
const void *  arr,
int  size,
  ... 
)

Linear search for arrays.

Parameters
specType specifier of the array to be sorted. Refer to spec_t for supported types
arrPointer to the first element of the array to be inspected
sizeNumber of elements of the array to be inspected
...The key to be searched. If searaching in a pointer array, after the item you want so search, you must provide the comparison function needed to compare the item you want to search and the items in the array. Must be a function that takes two pointers as argument and returns a zero int only if the item pointed by first and second arguments are equal
Note
Even though passing more than one key does not throw a compiler nor runtime error, only searching one key is supported. Other items are ignored. If you don't specify any item to be searched, still no errors occur but the return value of the function can be unpredictable. If searching in a pointer array and the comparing function is not passed a compiler or runtime error is not given either, but the return value of the function can be unpredictable
Returns
The index of the first occurence of the key in the array or the return code of the function
Return values
KEY_NOT_FOUNDThe key was not found

◆ chooseQuickSortArr()

void chooseQuickSortArr ( const spec_t  spec,
void *  arr,
int  size,
  ... 
)

Quick sort for arrays.

Parameters
specType specifier of the array to be sorted. Refer to spec_t for supported types
arrPointer to the first element of the array to be sorted
sizeNumber of elements of the array to be sorted
...The comparison function needed to compare items inside given lists. This parameter is necessary only for pointer ArrayList type and is ignored otherwise. Must be a function that takes two pointers as argument and returns a positive int if the item pointed by the first argument is greater than the item pointed by the second argument, a negative int if the item pointed by the first argument is is smaller than the item pointed by second, a zero int if the item pointed by first and second arguments are equal

◆ doubleBubbleSortArr()

void doubleBubbleSortArr ( double *  arr,
unsigned int  size 
)

Bubblesort for arrays of doubles.

Equivalent to chooseBubbleSortArr("%lf", arr, size). Refer to chooseBubbleSortArr()

◆ doubleLinearSearchArr()

int doubleLinearSearchArr ( const char *  arr,
int  size,
double  key 
)

Linear search for arrays of doubles.

Equivalent to chooseLinearSearchArr("%lf", arr, size, key). Refer to chooseLinearSearchArr()

◆ doubleQuickSortArr()

void doubleQuickSortArr ( double *  arr,
int  size 
)

Quicksort for arrays of doubles.

Equivalent to chooseQuickSortArr("%lf", arr, size). Refer to chooseQuickSortArr()

◆ floatBubbleSortArr()

void floatBubbleSortArr ( float *  arr,
unsigned int  size 
)

Bubblesort for arrays of floats.

Equivalent to chooseBubbleSortArr("%f", arr, size). Refer to chooseBubbleSortArr()

◆ floatLinearSearchArr()

int floatLinearSearchArr ( const char *  arr,
int  size,
float  key 
)

Linear search for arrays of floats.

Equivalent to chooseLinearSearchArr("%f", arr, size, key). Refer to chooseLinearSearchArr()

◆ floatQuickSortArr()

void floatQuickSortArr ( float *  arr,
int  size 
)

Quicksort for arrays of floats.

Equivalent to chooseQuickSortArr("%f", arr, size). Refer to chooseQuickSortArr()

◆ intBubbleSortArr()

void intBubbleSortArr ( int *  arr,
unsigned int  size 
)

Bubblesort for arrays of ints.

Equivalent to chooseBubbleSortArr("%i", arr, size). Refer to chooseBubbleSortArr()

◆ intLinearSearchArr()

int intLinearSearchArr ( const char *  arr,
int  size,
int  key 
)

Linear search for arrays of integers.

Equivalent to chooseLinearSearchArr("%i", arr, size, key). Refer to chooseLinearSearchArr()

◆ intQuickSortArr()

void intQuickSortArr ( int *  arr,
int  size 
)

Quicksort for arrays of ints.

Equivalent to chooseQuickSortArr("%i", arr, size). Refer to chooseQuickSortArr()

◆ printMatrix()

void printMatrix ( const spec_t  spec,
const void *  matrix,
const unsigned int  nRows,
const unsigned int  nColumns 
)

Print a matrix of specified size with specified formatting.

Parameters
specType and format specifier used to print a cell. The printf() identifier and formatting convention is supported. See spec_t for details. Additional supported specifiers: "%hi" (numerical output for char)
Note
The format specifier must end with the letter of the type specifier. For example, "%5.3lf" is supported, "%5.3lf\n" or "%5.3lfTest" is not supported and nothing is printed
Parameters
matrixPointer to the first element of the matrix
nRowsNumber of rows of the matrix
nColumnsNumber of rows of the matrix

◆ ptrBubbleSortArr()

void ptrBubbleSortArr ( void **  arr,
unsigned int  size,
int(*)(const void *a, const void *b)  cmpFunc 
)

Bubblesort for arrays of pointers.

Parameters
arrThe array to be sorted
sizeThe number of items contained in arr
cmpFuncThe comparison function needed to compare items inside given lists. Must be a function that takes two pointers as argument and returns a positive int if the item pointed by the first argument is greater than the item pointed by the second argument, a negative int if the item pointed by the first argument is is smaller than the item pointed by second, a zero int if the item pointed by first and second arguments are equal

◆ ptrLinearSearchArr()

int ptrLinearSearchArr ( const void *  arr,
int  size,
void *  key,
int(*)(const void *a, const void *b)  cmpFunc 
)

Linear search for arrays of pointers.

Parameters
arrPointer to the first element of the array to be inspected
sizeNumber of elements of the array to be inspected
keyThe key to be searched
cmpFuncThe comparison function to be used. Must be a function that returns a positive int if first argument is greater than the second, a negative byte if first argument is smaller than the second, a zero byte if first and second arguments are equal
Returns
The index of the first occurence of the key in the array or the return code of the function
Return values
KEY_NOT_FOUNDThe key was not found

◆ ptrQuickSortArr()

void ptrQuickSortArr ( void *  arr,
int  size,
int(*)(const void *a, const void *b)  cmpFunc 
)

Quicksort for arrays of pointers.

Parameters
arrThe array to be sorted
sizeThe number of items contained in arr
cmpFuncThe comparison function to be used. Must be a function that returns a positive int if first argument is greater than the second, a negative byte if first argument is smaller than the second, a zero byte if first and second arguments are equal