This is C library with some common tasks and data structures. I know the name is not the best but I have no imagination for names.
Check here for the documentation and here to download latest build (for x86-64 only, but you can compile from source in order to support other architectures).
Table of contents
Introduction and examples
This library contains some useful data structures which are not supported by default in C and some frequently used functions and algorithms.
As this library is written in C, almost every function needs you to specify as a function argument which type of data you are using it on through type and formatting specifiers. The convention used is the same used in standard C for printf and scanf.
In order to make writing code a bit more lighter the library includes also some macros that automatically detect the type of arguments passed so you don't have to explicitly use format and type specifiers. However, these macros are supported on C11 and newer compilers only and using them in some development environments can cause warnings or error reportings even though they are used correctly. See macros.h in the docs for more. In order to be as inclusive as possibile, since this macros are not supported by every C compilers, in the following examples they are not used.
The approach used by this library to handle errors with pointers is that every error is fatal: for example, when a pointer passed to a function is null and it should not be null, or some needed memory could not be allocated, the program prints where the error occurred and exits.
ArrayLists
ArrayLists are dynamically growing and shrinking lists of data, which can be of char, int, float, double or pointer type. You can create an ArrayList from a C array or you can create a new empty ArrayList. You can append items at its end, insert items, change its items, get its items, sort it (only ascending order is currently supported), print it, merge it with another ArrayList and much more. See arrayList.h in the docs for all the details.
The difference with LinkedLists is in the implementation and hence in the time needed for accessing its item. For example, an ArrayList has constant time for accessing items, while a LinkedList takes linear time. If you are intrested in these topics I suggest you to search more information on the Internet, as LinkedList and ArrayList are very standard data structures and on the web you can find a lot of information.
Here are some examples of ArrayList usage:
int main() {
int extracted;
int myArray[] = {23, 4, 65, -5, 12};
return 0;
}
void reverseAL(ArrayList list)
Reverse an ArrayList.
void quickSortAL(ArrayList list,...)
Quicksort for ArrayList.
void getFromAL(const ArrayList list, unsigned int index, void *dest)
Get an item from an ArrayList.
void mergeAL(ArrayList list1, const ArrayList list2)
Merge two ArrayList.
void appendToAL(ArrayList list,...)
Insert an item at the end of an ArrayList.
ArrayList newAL(const spec_t spec)
Allocate a new ArrayList of specified type.
void deleteAL(ArrayList list,...)
Delete an ArrayList.
ArrayList chooseNewALFromArray(const spec_t spec, const void *list, unsigned int size)
Create an ArrayList from a static array.
byte areALEqual(const ArrayList list1, const ArrayList list2,...)
Compare two ArrayList.
unsigned int getALLength(const ArrayList list)
Get the size of an ArrayList.
void insertToAL(ArrayList list, unsigned int index,...)
Insert an item at a specified position of an ArrayList.
int linearSearchAL(ArrayList list,...)
Linear search for ArrayList.
void setALItem(ArrayList list, unsigned int index,...)
Set value of an item of an ArrayList.
void removeFromAL(ArrayList list, unsigned int index)
Remove an item from an ArrayList.
void printAL(const spec_t spec, const ArrayList list)
Print contents from an ArrayList.
#define areEqual(collection1, collection2)
Compare two ArrayList, LinkedList, Stack or Queue.
Definition: macros.h:139
Includes all other headers. Useful for rapid import.
ArrayList type
Definition: types.h:31
LinkedLists
LinkedLists are a quite standard implementation of linked lists, dynamically growing and shrinking lists of data, which can be of char, int, float, double or pointer type. You can create a LinkedList from a C array or you can create a new empty LinkedList. You can append items at its end, insert items, change its items, get its items, print it, merge it with another LinkedList and much more. See linkedList.h in the docs for all the details.
The difference with ArrayLists is in the implementation and hence in the time needed for accessing its item. For example, a LinkedList has constant time for accessing items, while a LinkedList takes linear time. If you are intrested in these topics I suggest you to search more information on the Internet, as ArrayLists and LinkedLists are very standard data structures and on the web you can find a lot of information. As for now, LinkedLists and ArrayLists have more or less the same functionalities except sorting and reversing, which are currently supported only on ArrayLists.
Here are some examples of LinkedList usage:
int main() {
int extracted;
int myArray[] = {23, 4, 65, -5, 12};
return 0;
}
LinkedList newLL(const spec_t spec)
Allocate a new LinkedList of specified type.
int linearSearchLL(LinkedList list,...)
Linear search for LinkedList.
LinkedList chooseNewLLFromArray(const spec_t spec, const void *arr, unsigned int size)
Create a LinkedList from an array.
void insertToLL(LinkedList list, unsigned int index,...)
Insert an element at a specified position of a LinkedList.
void deleteLL(LinkedList list)
Delete a LinkedList.
void mergeLL(LinkedList list1, const LinkedList list2)
Merge two LinkedList.
void setLLItem(LinkedList list, unsigned int index,...)
Set value of an element of a LinkedList.
void getFromLL(LinkedList list, unsigned int index, void *dest)
Get an item from a LinkedList.
void appendToLL(LinkedList list,...)
Insert an item at the end of a LinkedList.
void removeFromLL(LinkedList list, unsigned int index)
Remove an item from a LinkedList.
byte areLLEqual(const LinkedList list1, const LinkedList list2)
Compare two LinkedList.
unsigned int getLLLength(const LinkedList list)
Get the size of a LinkedList.
void printLL(const spec_t spec, const LinkedList list)
Print contents from a LinkedList.
LinkedList type
Definition: types.h:69
Stacks
Stacks are a quite standard implementation of LIFO stacks and can contain char, int, float, double or pointer data. You can create a Stack from a C array or you can create a new empty Stack. You can print its content, push items to its top, pop items from its top, peek from its top and much more. See stack.h in the docs for all the details.
Here are some examples of Stack usage:
int main() {
int extracted;
int myArray[] = {23, 4, 65, -5, 12};
return 0;
}
Stack newStack(const spec_t spec)
Allocate a new Stack of specified type.
byte areStacksEqual(const Stack stack1, const Stack stack2)
Compare two Stack.
void pop(Stack stack, void *dest)
Pop an item from a Stack.
void deleteHeadFromStack(Stack stack)
Delete current Stack head.
void deleteStack(Stack stack)
Delete a Stack.
unsigned int getStackLength(const Stack stack)
Get the size of a Stack.
void peekStack(Stack stack, void *dest)
Get the item at the head of a Stack without popping it.
Stack chooseNewStackFromArray(const spec_t spec, const void *arr, unsigned int size)
Create a Stack from an array.
void push(Stack stack,...)
Push an item into a Stack.
void printStack(const spec_t spec, const Stack stack)
Print contents from a Stack.
Stack type
Definition: types.h:95
Queues
Queues are a quite standard implementation of FIFO queues and can contain char, int, float, double or pointer data. You can create a Queue from a C array or you can create a new empty Queue. You can print its content, enqueue items to its end, dequeue items from its top, peek from its top and much more. See queue.h in the docs for all the details.
Here are some examples of Queue usage:
int main() {
int extracted;
int myArray[] = {23, 4, 65, -5, 12};
return 0;
}
void deleteQueue(Queue queue)
Delete a Queue.
void peekQueue(const Queue queue, void *dest)
Get the item in the head of a Queue without dequeueing it.
void deleteHeadFromQueue(Queue queue)
Delete current Queue head.
Queue newQueue(const spec_t spec)
Allocate a new Queue of specified type.
byte areQueuesEqual(const Queue queue1, const Queue queue2)
Compare two Queue.
void dequeue(Queue queue, void *dest)
Dequeue an item from a Queue.
Queue chooseNewQueueFromArray(const spec_t spec, const void *arr, unsigned int size)
Create a Queue from an array.
void printQueue(const spec_t spec, const Queue queue)
Print contents from a Queue.
unsigned int getQueueLength(const Queue queue)
Get the size of a Queue.
void enqueue(Queue queue,...)
Enqueue an item into a Queue.
Queue type
Definition: types.h:111
Array algorithms
This library contains some basic functions that implement some commonly used algorithms for arrays and matrix, such as linear searching or sorting. These functions are massively used inside the library itself, but they can be useful out of that context too.
Since these functions work with standard C static arrays, they always have its size and its type, specified using the printf convention, as parameters.
See arrays.h in the docs for all the details.
Here are some examples of their usage:
int main() {
int myArray[] = {23, 45, 11, -23, -43, 43};
int myMatrix[][6] = {{23, 45, 11, -23, -43, 43},
{23, 45, 11, -23, -43, 43}};
}
void chooseBubbleSortArr(const spec_t spec, void *arr, unsigned int size,...)
Bubble sort for arrays.
int chooseLinearSearchArr(const spec_t spec, const void *arr, int size,...)
Linear search for arrays.
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.
Strings
This library contains some basic functions for working with strings, such as getting a string of arbitrary size and saving it in memory, checking if it ends with a given substring, changing its last characters and getting a copy of it.
See strings.h in the docs for all the details.
Here are some examples of their usage:
int main() {
int endsWithST =
endsWith(myString,
"st");
string otherString =
copyOf(newString);
}
string copyOf(const string src)
Get a copy of the given string.
string changeLastCharacter(const string str, char newCharacter)
Get a tring with different last character.
byte endsWith(const string str, const string suffix)
Check if a string ends with the specified substring.
string getString()
Reads from terminal a string of arbitrary length.
Miscellaneous
This library contains also standard comparing functions for char, int, float, double and pointer type and also two functions that try to allocate or reallocate memory. These functions are massively used inside the library itself, but they can be useful out of that context too.
See utility.h in the docs for all the details.
Here are some examples of their usage:
int main() {
int a = 0, b = 1;
}
int chooseCmp(const spec_t spec, const void *a, const void *b)
Compare two values.
void * saferRealloc(void *pointer, unsigned int bytes)
Reallocate a space in memory.
void * saferMalloc(unsigned int bytes)
Return a pointer to a space in memory of specified size.
How to import
On Linux
Download the build for Linux, unzip it and place it somewhere. Consider the following code:
int main() {
byte myMatrix[][2] = {{42, 24}, {-24, 42}};
return 0;
}
Assuming it is saved in a file named myFile.c and you want to compile it using gcc, the correct command for compilation is:
gcc path/to/myFile.c -o path/to/myFileExecutable -I path/to/folder/with/myLibrary \
path/to/folder/with/myLibrary/build/myLibrary_Linux.lib
Where:
path/to/myFile.c is the relative or absolute path to myFile.c
path/to/myFileExecutable is the relative or absolute path for the compiler output
path/to/folder/with/myLibrary is the relative or absolute path of extracted myLibrary folder
path/to/folder/with/myLibrary/build/myLibrary_Linux.lib is the path to the binary file of the library
On Visual Studio for Windows
Download the build for Windows, unzip it and place it somewhere. Steps to import:
- Open the solution where you want to use myLibrary
- Ensure the source file where you want to import myLibrary has
.c extension. If its extension is .cpp, change it to .c
- Go to "Project" > "myProject Properties"
- In "Configuration" choose "All Configurations"
- In "Platform" choose "x64"
- Go to "Configuration Properties" > "C/C++" > "General". In "Additional Include Directories" add the path of the myLibrary folder you extracted before
- Go to "Configuration Properties" > "Linker" > "General". In "Additional Library Directories" add the path of the "build" folder inside the myLibrary folder you extracted before
- Go to "Configuration Properties" > "Linker" > "Input". In "Additional Dependencies" add
myLibrary_Windows.lib;legacy_stdio_definitions.lib;legacy_stdio_wide_specifiers.lib;
- Click on "Ok" at the bottom of the window
- Near to "Local Windows Debugger" choose "x64". Now you are ready to
#include "myLibrary.h" and compile and run your code
How to compile from source
Compilation from source is currently supported only on Linux. The only dependencies are gcc and make. Run:
git clone https://github.com/CatoMaior/myLibrary.git
cd myLibrary
make lib
The compiled binaries are myLibrary_Linux.lib and myLibrary_Windows.lib in the build folder. If you want a pdf version of the docs too run:
The pdf is now in the docs folder