Introduction
Header only, macro generated, generic and type-safe Collections in C.
The C Macro Collections Library is a compilation of macros that can be used to generate common data structures for any desired type. These data structures are type safe and are powered with many features (see Features section).
The documentation is currently being written in hopes that in the future it can be used to help you get to know better this amazing C library.
Why this library
C is a low level language and lacks all the data structures that we commonly use. Many high level languages already come with these collections so that we don't have to implement everything ourselves. A lot of third party libraries that implement these missing data structures for the C language usually make use of a void pointers and a lot of macros. This is why the C Macro Collections Library was created. All you need to do is to write down one macro and which data type you wish to work with. The library currently provides many data structures, such as:
- Linear Collections : List, LinkedList, Deque, Stack, Queue, SortedList
- Sets : HashSet, TreeSet, MultiSet
- Maps : HashMap, TreeMap, MultiMap, BidiMap
- Heaps : Heap, IntervalHeap
All with the following features:
- Type-safe - No
void *pointers are used. A collection of typeintwill only accept integers; - Customization - Custom struct name and function namespace;
- Bidirectional Iterators - Full support for iterators;
- Nesting - Collections can be nested (List of Lists, HashSet of Stacks, etc);
- One macro to rule them all - Only one macro to generate everything and that's it.
and many other powerful features (see Features).
How to install
To start using the library you need first to download the source code. You can either fork the repository or download a zip file containing everything. Either way, after you unzip everything you will end up with the root folder (probably something called C-Macro-Collections) and inside it is the src folder.
With the src folder you can start including the library headers to your project with -I path/to/library/src.
The library has no external dependencies other than the C standard library.
// Include header files like this
#include <cmc/list.h>
#include <dev/deque.h>
#include <utl/assert.h>
// Not like this
#include <list.h>
#include <deque.h> // dev or cmc?
#include <assert.h> // this will import from the standard library
Library Structure
The src folder is subdivided in 5 other folders and one file:
- cmc - The main C Macro Collections Library
- dev - The main C Macro Collections Library for development
- sac - Statically Allocated Collections
- utl - Utility like ForEach macros, logging, etc
- macro_collections.h - Master header containing all collections and utilities
cmc
This is where the C Macro Collections are hosted.
dev
In this folder is an exact copy of the cmc Collections with the added logging utility (./utl/log.h). These are useful to debug your code because everything that is happening inside the data structure can be seen.
sac
This is where the Statically Allocated Collections are hosted. These collections are just like the cmc Collections but they have a constant size, a C array, instead of a dynamically allocated array (yes, even for Linked List).
utl
Utility. Here you will find things like assert macros, foreach macros, logging utility, unit test and timer.
macro_collections.h
This is the master header. Include this big boy and all functionalities of the library will be in your hands.
Understanding the Library
Every macro within the library is prefixed by CMC or cmc. If you have intellisense you can easily check all the features of the library once you include the necessary headers.
When you generate code for a certain collection you will need to pass four very important parameters: SNAME, PFX, K and V.
SNAME
SNAME is short for struct name. This parameter will define your collection so that its type name becomes:
struct SNAME;
No typedefs are used within the code that is generated in order to not pollute the global namespace. If you wish to typedef feel free to give your own naming conventions.
Every other struct that is generated as part of the implementation will be prefixed by SNAME. So iterators and nodes will become:
struct SNAME##_iter;
struct SNAME##_node;
Note that the ## is a Token Pasting Operator.
PFX
PFX is a short for prefix. Every function generated will be within this namespace. When using Intellisense, write whichever prefix you gave to your functions and everything will be in there.
Functions that are part of the implementation and shouldn't be called directly are prefixed by PFX and then by _impl_. Every iterator function will be prefixed by PFX and then by _iter_. For example:
// A function that you shouldn't use directly
size_t PFX##_impl_quick_sort(struct SNAME *list);
// A function that is associated with the iterator
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target);
K
K, short for Key, is the data type that is only used by associative collections to map a key to a value.
V
V, short for Value, is the primary data type for most collections.
Summarizing
With SNAME and PFX a common function definition looks like:
size_t PFX##_count(struct SNAME *list);
So if input SNAME as i32list and PFX as i32l you will have the following function definition:
size_t i32l_count(struct i32list *list);
How to Use the Library
The C Macro Collections Library comes with very powerful data structures and they are only one macro away from you.
Features
The C Macro Collections Library comes with powerful collections. In this section you will better understand many features that are common to all data structures.
Two-Way Iterators
Every collection comes with an interface of iterators where you can easily access the elements of a collection. These can move at any direction, can start at any 'end' of a collection and can move any amount of steps per iteration. Every collection generated comes with an iterator.
By design choice these iterators do not support modifications to the collection. If a collection is modified, all iterators that have been initialized will most likely be invalidated and may cause undefined behavior if used afterwards.
Custom Allocation
All collections have an allocation node. This node can be modified so that every allocation and de-allocation can be done with custom functions. The allocation node has pointers to functions that correspond to:
callocfreemallocrealloc
Callbacks
Every collection can have an optional callback node. In this node there are five functions:
create | Is called when an element was successfully added to the collection |
read | Is called when the collection was successfully queried about an element |
update | Is called when an element in the collection was successfully updated |
delete | Is called when an element was successfully removed from the collection |
resize | Is called when the collection was full and successfully resized |
Check the documentation for each collection to see which functions call which callbacks.
Functions Table
Functions table are like a methods table for you custom data type. It is a struct of function pointers that you will need to define in order to some collections to work. Some functions are optional and others are needed in order to a collection to operate.
CMP
A comparator function is used in sorted collections or when an equality is being checked like when trying to find a certain element in a list. It is responsible for taking two arguments of the same data type and comparing them. The return value is an int with the following definitions:
- Return
1if the first argument is greater than the second; - Return
0if the first argument equals the second; - Return
-1if the first argument is less than the second.
CPY
A copy function is used when a collection is being copied. It can be used to make a deep copy of of your custom data type. It must take a single parameter and return a new copy of that same data type. If this function is absent (NULL) the data type will be copied by assignment (for pointers this is a shallow copy).
STR
A string function is responsible for taking a FILE pointer and a custom data type and outputting the string representation of that data returning a bool indication success or failure. It is useful for debugging.
FREE
The free function is called when a collection is cleared (all elements removed) or freed (all elements removed and freed from memory) and it is responsible for completely freeing all resources that are usually acquired by your data type.
HASH
This function receives a custom data type as parameter and returns a size_t hash of that data. Used in hashtables.
PRI
A priority function works much like the comparator function except that it compares the priority between two elements. It is used in collections whose structure is based on the priority of elements and not in their general comparison.
- Return
1if the first argument has a greater priority than the second; - Return
0if the first argument has the same priority as second; - Return
-1if the first argument has a lower priority than the second.
The following table shows which functions are required, optional or never used for each Collection:
| Collection | CMP | CPY | STR | FREE | HASH | PRI |
|---|---|---|---|---|---|---|
| BidiMap | ||||||
| Deque | ||||||
| HashMap | ||||||
| HashSet | ||||||
| Heap | ||||||
| IntervalHeap | ||||||
| List | ||||||
| LinkedList | ||||||
| MultiMap | ||||||
| MultiSet | ||||||
| Queue | ||||||
| SortedList | ||||||
| Stack | ||||||
| TreeMap | ||||||
| TreeSet |
| Color | Label |
|---|---|
| Required for basic functionality. | |
| Required for specific functions. | |
| Required for non-core specific functions. | |
| Optional. | |
| Not Used. |
Collections Overview
| Collection | Abstract Data Type | Data Structure | Details |
|---|---|---|---|
| BidiMap bidimap.h | Bidirectional Map | Two Hashtables | A bijection between two sets of unique keys and unique values K <-> V using two hashtables |
| Deque deque.h | Double-Ended Queue | Dynamic Circular Array | A circular array that allows push and pop on both ends (only) at constant time |
| HashMap hashmap.h | Map | Hashtable | A unique set of keys associated with a value K -> V with constant time look up using a hashtable with open addressing and robin hood hashing |
| HashSet hashset.h | Set | Hashtable | A unique set of values with constant time look up using a hashtable with open addressing and robin hood hashing |
| Heap heap.h | Priority Queue | Dynamic Array | A binary heap as a dynamic array as an implicit data structure |
| IntervalHeap intervalheap.h | Double-Ended Priority Queue | Custom Dynamic Array | A dynamic array of nodes, each hosting one value from the MinHeap and one from the MaxHeap |
| LinkedList linkedlist.h | List | Doubly-Linked List | A default doubly-linked list |
| List list.h | List | Dynamic Array | A dynamic array with push and pop anywhere on the array |
| MultiMap multimap.h | Multimap | Custom Hashtable | A mapping of multiple keys with one node per key using a hashtable with separate chaining |
| Multiset multiset.h | Multiset | Hashtable | A mapping of a value and its multiplicity using a hashtable with open addressing and robin hood hashing |
| Queue queue.h | FIFO | Dynamic Circular Array | A queue using a circular array with enqueue at the back index and dequeue at the front index |
| SortedList sortedlist.h | Sorted List | Sorted Dynamic Array | A lazily sorted dynamic array that is sorted only when necessary |
| Stack stack.h | FILO | Dynamic Array | A stack with push and pop at the end of a dynamic array |
| TreeMap treemap.h | Sorted Map | AVL Tree | A unique set of keys associated with a value K -> V using an AVL tree with log(n) look up and sorted iteration |
| TreeSet treeset.h | Sorted Set | AVL Tree | A unique set of keys using an AVL tree with log(n) look up and sorted iteration |
Minimal Example
(TODO this example is currently outdated)
Lets create a list of type int, add some elements to it and get the sum of all the elements printed on the screen.
Include headers
#include <cmc/list.h>
First you need to include only the necessary headers. It is not recommended that you include everything with macro_collections.h, only in cases where you might be using almost all features from the library.
Generating a list
// CMC_GENERATE_LIST(PFX, SNAME, V)
CMC_GENERATE_LIST(il, int_list, int)
The List is also known as vector, ArrayList and dynamic array. In fact the latter is the best description for this collection. It is a dynamic array that can grow and shrink as needed. To generate it we can simply call the macro CMC_GENERATE_LIST after our includes. It has three parameters:
| Parameter name | Purpose |
|---|---|
| PFX | Functions prefix (namespace) |
| SNAME | Struct name |
| V | The list value type (can be any valid data type) |
Initializing and adding elements
// Allocate an int_list with an initial capacity of 100
int_list *list = il_new(100);
// Adding elements to the back of the list
for (int i = 0; i <= 1000; i++)
{
if (!il_push_back(list, i))
{
// Something went wrong (check out documentation)
}
}
To initialize any collection we call a function _new. Since the PFX parameter is set to il all functions related to our list of integer will be prefixed by il, so _new becomes il_new.
The list name is int_list as defined by SNAME. This type is a typedef like:
typedef struct SNAME##_s SNAME;
Which expands to:
typedef struct int_list_s int_list
To add elements to the back of a list we can use il_push_back. This function will first check if the list has enough space for a new element. If not, reallocate the internal buffer with doubled size and then add the new element.
Iteration
It is recommended that the iterator is allocated on the stack
// Resulting sum will be stored here
int sum = 0;
// Declare our iterator (allocating it on the stack)
int_list_iter it;
// Loop for each element
for (il_iter_init(&it, list); !il_iter_end(&it); il_iter_next(&it))
{
// Accessing the value
int elem = il_iter_value(&it);
sum += elem;
}
printf("%d\n", sum);
There are 6 (2 optional) steps to iterate over the elements of any collection:
| Step | Description | Function |
|---|---|---|
| 1 | Allocate the iterator (optional) | il_iter_new |
| 2 | Initialize the iterator given a target list | il_iter_init |
| 3 | Access the iterator's elements (value, index, etc) | il_iter_value |
| 4 | Go to the next element | il_iter_next |
| 5 | Check if we haven't reached the end of the list | il_iter_end |
| 6 | Free the iterator if it was allocated on the heap | il_iter_free |
Note that all iterator functions are prefixed by the user defined prefix and then by iter (il + iter = il_iter). Also, the steps 1 and 6 are optional so il_iter_new and il_iter_free are not used in the example.
By the end of the example we have the variable sum with the sum of all integers inside our list. We can then print it and this example is almost done.
Freeing resources
// void il_free(int_list *_list_, void(*deallocator)(int))
il_free(list, NULL);
The list is currently allocated in memory. To deallocate it we call il_free. This function takes a pointer to the allocated list and a pointer to a deallocator function that will be responsible for deallocating each element in the list. This last parameter is optional and we won't be using it since our data type int is not allocated in the heap. This optional parameter can be set to NULL to be ignored by the il_free function.
Compile and run
source.c
#include <cmc/list.h>
CMC_GENERATE_LIST(il, int_list, int)
int main(void)
{
int_list *list = il_new(100);
for (int i = 0; i <= 1000; i++)
{
if (!il_push_back(list, i))
{
// Something went wrong (check out documentation)
}
}
int sum = 0;
int_list_iter it;
for (il_iter_init(&it, list); !il_iter_end(&it); il_iter_next(&it))
{
int elem = il_iter_value(&it);
sum += elem;
}
printf("%d\n", sum);
il_free(list, NULL);
}
If the source file is source.c, all you have to do is to include the src folder to the include path and compile. The following example uses gcc:
gcc source.c -I path/to/library/src
Conclusion
The C Macro Collections Library is prepared to accept any data type and is highly customizable. It is also very easy to integrate and no previous compilation is required. The library also comes with many utilities in the utl folder that are designed to work well with all collections.