My library
Library for common tasks
Macros
macros.h File Reference

Macros for emulated overloading. More...

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

Go to the source code of this file.

Macros

#define cmpVal(a, b)
 Compare two values. More...
 
#define bubbleSortArr(arr, size)
 BubbleSort for arrays. More...
 
#define quickSortArr(arr, size, ...)
 Quicksort for arrays. More...
 
#define newALFromArray(arr, size)
 Create an ArrayList from a static array. More...
 
#define newLLFromArray(arr, size)
 Create a LinkedList from a static array. More...
 
#define newStackFromArray(arr, size)
 Create a Stack from a static array. More...
 
#define newQueueFromArray(arr, size)
 Create a Queue from a static array. More...
 
#define newStackFromArray(arr, size)
 Create a Stack from a static array. More...
 
#define print(spec, collection)
 Print contents from an ArrayList, LinkedList, Stack or Queue. More...
 
#define areEqual(collection1, collection2)
 Compare two ArrayList, LinkedList, Stack or Queue. More...
 
#define append(list, item)
 Insert an item at the end of an ArrayList or LinkedList. More...
 
#define insert(list, index, item)
 Insert an element at a specified position of an ArrayList or LinkedList. More...
 
#define set(list, index, newItem)
 Set value of an element of an ArrayList or LinkedList. More...
 
#define merge(list1, list2)
 Merge two ArrayList or LinkedList. More...
 
#define slice(list, begin, end)
 Slice an ArrayList or LinkedList. More...
 
#define removeItem(list, index)
 Remove an item from an ArrayList or LinkedList. More...
 
#define getItem(list, index, dest)
 Get an item from an ArrayList or LinkedList. More...
 
#define delete(collection)
 Delete an ArrayList, LinkedList, Stack or Queue. More...
 
#define isIn(collection, item)
 Detect if an item is inside an ArrayList, LinkedList, Stack or Queue. More...
 
#define getLength(collection)
 Get the number of elements in an ArrayList, LinkedList, Stack, Queue or string. More...
 
#define linearSearch(list, key)
 Linear search for an ArrayList or LinkedList. More...
 
#define deleteHead(collection)
 Delete current Stack or Queue head. More...
 
#define isEmpty(collection)
 Check if an ArrayList, LinkedList, Stack or Queue is empty. More...
 
#define peek(collection, dest)
 Get the item at the head of a Stack or Queue without popping/dequeueing it. More...
 

Detailed Description

Macros for emulated overloading.

Author
Pietro Firpo (pietr.nosp@m.o.fi.nosp@m.rpo@p.nosp@m.m.me)
Note
Many of these macros work on C11 or newer compilers only. If they are not supported by your compiler you have to use the function the macro expands to in your case. For example, if you want to bubblesort an array of floats and the macro bubbleSort() is not supported by your compiler, you have to call floatBubbleSortArr() or chooseBubbleSortArr(). Moreover, these macros don't work with pointer arrays, ArrayList, LinkedList, Stack or Queue type
In some development environments, for example Vscode, calls to these macros can be reported as errors even if they are correct. If you use Vscode you have to set "C_Cpp.default.cStandard": "c17" in your settings.json file in order to avoid these error reportings

Macro Definition Documentation

◆ append

#define append (   list,
  item 
)
Value:
_Generic(list, ArrayList \
: appendToLL)(list, item)
void appendToAL(ArrayList list,...)
Insert an item at the end of an ArrayList.
void appendToLL(LinkedList list,...)
Insert an item at the end of a LinkedList.
ArrayList type
Definition: types.h:31
LinkedList type
Definition: types.h:69

Insert an item at the end of an ArrayList or LinkedList.

Parameters
listThe list you want to append an item to
itemThe item you want to append to list

◆ areEqual

#define areEqual (   collection1,
  collection2 
)
Value:
_Generic(collection1, ArrayList \
: areQueuesEqual)(collection1, collection2)
byte areALEqual(const ArrayList list1, const ArrayList list2,...)
Compare two ArrayList.
byte areLLEqual(const LinkedList list1, const LinkedList list2)
Compare two LinkedList.
byte areQueuesEqual(const Queue queue1, const Queue queue2)
Compare two Queue.
byte areStacksEqual(const Stack stack1, const Stack stack2)
Compare two Stack.
Queue type
Definition: types.h:111
Stack type
Definition: types.h:95

Compare two ArrayList, LinkedList, Stack or Queue.

Parameters
collection1The first ArrayList, LinkedList, Stack or Queue you want to compare
collection2The second ArrayList, LinkedList, Stack or Queue you want to compare
Note
Passing two different types (for example, an ArrayList and a Stack) does not throw errors but does not work and the result can be unpredictable

◆ bubbleSortArr

#define bubbleSortArr (   arr,
  size 
)
Value:
_Generic(arr, char * \
: charBubbleSortArr, int * \
: intBubbleSortArr, float * \
: floatBubbleSortArr, double * \
: doubleBubbleSortArr, void ** \
: ptrBubbleSortArr)(arr, size)
void intBubbleSortArr(int *arr, unsigned int size)
Bubblesort for arrays of ints.
void floatBubbleSortArr(float *arr, unsigned int size)
Bubblesort for arrays of floats.
void ptrBubbleSortArr(void **arr, unsigned int size, int(*cmpFunc)(const void *a, const void *b))
Bubblesort for arrays of pointers.
void doubleBubbleSortArr(double *arr, unsigned int size)
Bubblesort for arrays of doubles.
void charBubbleSortArr(char *arr, unsigned int size)
Bubblesort for arrays of chars.

BubbleSort for arrays.

Returns
The return code of the function called
Parameters
arrPointer to the array to be sorted
sizeNumber of elements in the array to be sorted

◆ cmpVal

#define cmpVal (   a,
 
)
Value:
_Generic((a, b), char * \
: charCmp, int * \
: intCmp, float * \
: floatCmp, double * \
: doubleCmp, void ** \
: ptrCmp)(a, b)
int doubleCmp(const void *a, const void *b)
Compare two doubles.
int floatCmp(const void *a, const void *b)
Compare two floats.
int charCmp(const void *a, const void *b)
Compare two chars.
int intCmp(const void *a, const void *b)
Compare two ints.
int ptrCmp(const void *a, const void *b)
Compare two pointers.

Compare two values.

Parameters
aPointer to the first value to be compared
bPointer to the second value to be compared
Returns
The return code of the function called
Return values
GREATERFirst element is grater than the second
EQUALFirst element is equal to the second
SMALLERFirst element is smaller than the second

◆ delete

#define delete (   collection)
Value:
_Generic(collection, ArrayList \
: deleteQueue)(collection)
void deleteAL(ArrayList list,...)
Delete an ArrayList.
void deleteLL(LinkedList list)
Delete a LinkedList.
void deleteQueue(Queue queue)
Delete a Queue.
void deleteStack(Stack stack)
Delete a Stack.

Delete an ArrayList, LinkedList, Stack or Queue.

Parameters
collectionThe ArrayList, LinkedList, Stack or Queue you want to delete

◆ deleteHead

#define deleteHead (   collection)
Value:
_Generic(list, Stack \
: deleteHeadFromQueue)(collection)
void deleteHeadFromQueue(Queue queue)
Delete current Queue head.
void deleteHeadFromStack(Stack stack)
Delete current Stack head.

Delete current Stack or Queue head.

Parameters
collectionThe Stack or Queue you want to delete the head from

◆ getItem

#define getItem (   list,
  index,
  dest 
)
Value:
_Generic(list, ArrayList \
: getFromLL)(list, index, dest)
void getFromAL(const ArrayList list, unsigned int index, void *dest)
Get an item from an ArrayList.
void getFromLL(LinkedList list, unsigned int index, void *dest)
Get an item from a LinkedList.

Get an item from an ArrayList or LinkedList.

Parameters
listThe list you want to get an item from
indexThe index of the item you want to get
destThe address of the variable you want to store the item in

◆ getLength

#define getLength (   collection)
Value:
_Generic(collection, ArrayList \
: getQueueLength, string \
: strlen)(collection)
unsigned int getALLength(const ArrayList list)
Get the size of an ArrayList.
unsigned int getLLLength(const LinkedList list)
Get the size of a LinkedList.
unsigned int getQueueLength(const Queue queue)
Get the size of a Queue.
unsigned int getStackLength(const Stack stack)
Get the size of a Stack.

Get the number of elements in an ArrayList, LinkedList, Stack, Queue or string.

Parameters
collectionThe ArrayList, LinkedList, Stack, Queue or string you want to evaluate
Returns
The number of elements in collection

◆ insert

#define insert (   list,
  index,
  item 
)
Value:
_Generic(list, ArrayList \
: insertToLL)(list, index, item)
void insertToAL(ArrayList list, unsigned int index,...)
Insert an item at a specified position of an ArrayList.
void insertToLL(LinkedList list, unsigned int index,...)
Insert an element at a specified position of a LinkedList.

Insert an element at a specified position of an ArrayList or LinkedList.

Parameters
listThe list you want to insert an element into
indexThe position you want to insert an item at
itemThe item you want to insert into list

◆ isEmpty

#define isEmpty (   collection)
Value:
_Generic(collection, ArrayList \
: isQueueEmpty)(collection, item)
byte isALEmpty(ArrayList list)
Check if ArrayList is empty.
byte isLLEmpty(LinkedList list)
Check if LinkedList is empty.
byte isQueueEmpty(Stack stack)
Check if Queue is empty.
byte isStackEmpty(Stack stack)
Check if Stack is empty.

Check if an ArrayList, LinkedList, Stack or Queue is empty.

Parameters
collectionThe ArrayList, LinkedList, Stack or Queue to be checked
Return values
TRUEcollection is empty
FALSEcollection is not empty

◆ isIn

#define isIn (   collection,
  item 
)
Value:
_Generic(collection, ArrayList \
: isInQueue)(collection, item)
byte isInAL(ArrayList list,...)
Detect if an item is inside an ArrayList.
byte isInLL(LinkedList list,...)
Detect if an element is inside a LinkedList.
byte isInQueue(Queue queue,...)
Detect if an item is inside a Queue.
byte isInStack(Stack stack,...)
Detect if an item is inside a Stack.

Detect if an item is inside an ArrayList, LinkedList, Stack or Queue.

Parameters
collectionThe ArrayList, LinkedList, Stack or Queue you want search in
itemThe item you want to search
Note
Passing float or double ArrayList, LinkedList, Stack or Queue is not supported
Return values
TRUEGiven item is contained in collection
FALSEGiven item is not contained in collection

◆ linearSearch

#define linearSearch (   list,
  key 
)
Value:
_Generic(list, ArrayList \
: linearSearchLL)(list, key)
int linearSearchAL(ArrayList list,...)
Linear search for ArrayList.
int linearSearchLL(LinkedList list,...)
Linear search for LinkedList.

Linear search for an ArrayList or LinkedList.

Parameters
listThe ArrayList or LinkedList to be inspected
keyThe key to be searched
Note
This function does not support float and double LinkedList or ArrayList types
Returns
The index of the first occurence of the key in the list or the return code of the function called
Return values
KEY_NOT_FOUNDThe key was not found

◆ merge

#define merge (   list1,
  list2 
)
Value:
_Generic(list1, ArrayList \
: mergeLL)(list1, list2)
void mergeAL(ArrayList list1, const ArrayList list2)
Merge two ArrayList.
void mergeLL(LinkedList list1, const LinkedList list2)
Merge two LinkedList.

Merge two ArrayList or LinkedList.

Parameters
list1The first list to be merged, where the merged list is saved
list2The second list to be merged
Note
Passing an ArrayList and a LinkedList does not throw errors but does not work and list1 is messed up

◆ newALFromArray

#define newALFromArray (   arr,
  size 
)
Value:
_Generic(arr, char * \
: newALFromCharArray, int * \
: newALFromIntArray, float * \
: newALFromFloatArray, double * \
: newALFromDoubleArray, void ** \
: newALFromPtrArray)(arr, size)
ArrayList newALFromFloatArray(const float list[], unsigned int size)
Create ArrayList from a list of floats.
ArrayList newALFromCharArray(const char list[], unsigned int size)
Create ArrayList from a list of chars.
ArrayList newALFromIntArray(const int list[], unsigned int size)
Create ArrayList from a list of ints.
ArrayList newALFromDoubleArray(const double list[], unsigned int size)
Create ArrayList from an list of doubles.
ArrayList newALFromPtrArray(const void *list, unsigned int size)
Create ArrayList from an list of pointers.

Create an ArrayList from a static array.

Parameters
arrThe array you want to create an ArrayList from
sizeThe size of arr
Returns
An ArrayList containing all the elements of arr

◆ newLLFromArray

#define newLLFromArray (   arr,
  size 
)
Value:
_Generic(arr, char * \
: newLLFromCharArray, int * \
: newLLFromIntArray, float * \
: newLLFromFloatArray, double * \
: newLLFromDoubleArray, void ** \
: newLLFromPtrArray)(arr, size)
LinkedList newLLFromIntArray(const int arr[], unsigned int size)
Create a LinkedList from a array of ints.
LinkedList newLLFromDoubleArray(const double arr[], unsigned int size)
Create a LinkedList from an array of doubles.
LinkedList newLLFromFloatArray(const float arr[], unsigned int size)
Create a LinkedList from a array of floats.
LinkedList newLLFromCharArray(const char arr[], unsigned int size)
Create a LinkedList from a array of chars.
LinkedList newLLFromPtrArray(const void *arr, unsigned int size)
Create a LinkedList from an array of pointers.

Create a LinkedList from a static array.

Parameters
arrThe array you want to create a LinkedList from
sizeThe size of arr
Returns
A LinkedList containing all the elements of arr in the same order

◆ newQueueFromArray

#define newQueueFromArray (   arr,
  size 
)
Value:
_Generic(arr, char * \
: newQueueFromIntArray, float * \
: newQueueFromFloatArray, double * \
: newQueueFromPtrArray)(arr, size)
Queue newQueueFromIntArray(const int arr[], unsigned int size)
Create a Queue from an array of integers.
Queue newQueueFromPtrArray(const void *arr, unsigned int size)
Create a Queue from an array of pointers.
Queue newQueueFromFloatArray(const float arr[], unsigned int size)
Create a Queue from an array of floats.
Queue newQueueFromDoubleArray(const double arr[], unsigned int size)
Create a Queue from an array of doubles.
Queue newQueueFromCharArray(const char arr[], unsigned int size)
Create a Queue from an array of chars.

Create a Queue from a static array.

Parameters
arrThe array you want to create a Queue from
sizeThe size of arr
Returns
A Queue containing all the elements of arr with the first element of arr as head

◆ newStackFromArray [1/2]

#define newStackFromArray (   arr,
  size 
)
Value:
_Generic(arr, char * \
: newStackFromIntArray, float * \
: newStackFromFloatArray, double * \
: newStackFromPtrArray)(arr, size)
Stack newStackFromFloatArray(const float arr[], unsigned int size)
Create a Stack from an array of floats.
Stack newStackFromCharArray(const char arr[], unsigned int size)
Create a Stack from an array of chars.
Stack newStackFromPtrArray(const void *arr, unsigned int size)
Create a Stack from an array of pointers.
Stack newStackFromIntArray(const int arr[], unsigned int size)
Create a Stack from an array of integers.
Stack newStackFromDoubleArray(const double arr[], unsigned int size)
Create a Stack from an array of doubles.

Create a Stack from a static array.

Parameters
arrThe array you want to create a Stack from
sizeThe size of arr
Returns
A Stack containing all the elements of arr with the last element of arr as head
Parameters
arrThe array you want to create a Stack from
sizeThe size of arr
Returns
A Stack containing all the elements of arr with the first element of arr as head

◆ newStackFromArray [2/2]

#define newStackFromArray (   arr,
  size 
)
Value:
_Generic(arr, char * \
: newStackFromIntArray, float * \
: newStackFromFloatArray, double * \
: newStackFromPtrArray)(arr, size)

Create a Stack from a static array.

Parameters
arrThe array you want to create a Stack from
sizeThe size of arr
Returns
A Stack containing all the elements of arr with the last element of arr as head
Parameters
arrThe array you want to create a Stack from
sizeThe size of arr
Returns
A Stack containing all the elements of arr with the first element of arr as head

◆ peek

#define peek (   collection,
  dest 
)
Value:
_Generic(list, Stack \
: peekQueue)(collection)
void peekQueue(const Queue queue, void *dest)
Get the item in the head of a Queue without dequeueing it.
void peekStack(Stack stack, void *dest)
Get the item at the head of a Stack without popping it.

Get the item at the head of a Stack or Queue without popping/dequeueing it.

Parameters
collectionThe Stack or Queue you want to get the item from
destThe address of the variable you want to store the item in

◆ print

#define print (   spec,
  collection 
)
Value:
_Generic(collection, ArrayList \
: printQueue)(spec, collection)
void printAL(const spec_t spec, const ArrayList list)
Print contents from an ArrayList.
void printLL(const spec_t spec, const LinkedList list)
Print contents from a LinkedList.
void printQueue(const spec_t spec, const Queue queue)
Print contents from a Queue.
void printStack(const spec_t spec, const Stack stack)
Print contents from a Stack.

Print contents from an ArrayList, LinkedList, Stack or Queue.

Parameters
specThe type and format specifier you want to use to print the single element. Use the printf() conventions
collectionThe ArrayList, LinkedList, Stack or Queue you want to print

◆ quickSortArr

#define quickSortArr (   arr,
  size,
  ... 
)
Value:
_Generic(arr, char * \
: charQuickSortArr, int * \
: intQuickSortArr, float * \
: floatQuickSortArr, double * \
: doubleQuickSortArr, void ** \
: ptrQuickSortArr)(arr, size, ...)
void floatQuickSortArr(float *arr, int size)
Quicksort for arrays of floats.
void charQuickSortArr(char *arr, int size)
Quicksort for arrays of chars.
void intQuickSortArr(int *arr, int size)
Quicksort for arrays of ints.
void ptrQuickSortArr(void *arr, int size, int(*cmpFunc)(const void *a, const void *b))
Quicksort for arrays of pointers.
void doubleQuickSortArr(double *arr, int size)
Quicksort for arrays of doubles.

Quicksort for arrays.

Returns
The return code of the function called
Parameters
arrPointer to the array to be sorted
sizeNumber of elements in the array to be sorted

◆ removeItem

#define removeItem (   list,
  index 
)
Value:
_Generic(list, ArrayList \
: removeFromLL)(list, index)
void removeFromAL(ArrayList list, unsigned int index)
Remove an item from an ArrayList.
void removeFromLL(LinkedList list, unsigned int index)
Remove an item from a LinkedList.

Remove an item from an ArrayList or LinkedList.

Parameters
listThe list you want to delete an item from
indexThe index of the item you want to delete

◆ set

#define set (   list,
  index,
  newItem 
)
Value:
_Generic(list, ArrayList \
: setLLItem)(list, index, newItem)
void setALItem(ArrayList list, unsigned int index,...)
Set value of an item of an ArrayList.
void setLLItem(LinkedList list, unsigned int index,...)
Set value of an element of a LinkedList.

Set value of an element of an ArrayList or LinkedList.

Parameters
listThe list you want to edit
indexThe index of the item you want to change
newItemThe item you want to set the index-th element of list to

◆ slice

#define slice (   list,
  begin,
  end 
)
Value:
_Generic(list, ArrayList \
: sliceLL)(list, begin, end)
void sliceAL(ArrayList list, unsigned int begin, unsigned int end)
Slice an ArrayList.
void sliceLL(LinkedList list, unsigned int begin, unsigned int end)
Slice a LinkedList.

Slice an ArrayList or LinkedList.

Parameters
listThe list you want to slice, where the sliced list is saved
beginThe index of the beginning of the slice
endThe index of the end of the slice