Update documentation
This commit is contained in:
1 parent
14845d9b23
commit
952292462a
57 files changed
+3027
-204
No files matched your search
@@ -1,13 +0,0 @@
|
||||
# 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.
|
||||
@@ -1,8 +0,0 @@
|
||||
# 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:
|
||||
|
||||
* `calloc`
|
||||
* `free`
|
||||
* `malloc`
|
||||
* `realloc`
|
||||
@@ -1,3 +0,0 @@
|
||||
# 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.
|
||||
@@ -1,3 +1,5 @@
|
||||
# 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.
|
||||
|
||||
TODO
|
||||
@@ -15,4 +15,4 @@ All with the following features:
|
||||
* __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](../Features/features.html)).
|
||||
and many other powerful features (see [Features](../Overview/features_overview.html)).
|
||||
File renamed without changes.
@@ -0,0 +1,10 @@
|
||||
# Features Overview
|
||||
|
||||
The C Macro Collections library comes with some core features available to all collections.
|
||||
|
||||
| Feature | Description |
|
||||
| ------- | ----------- |
|
||||
| [Callbacks](../cor/callbacks/index.html) | Callbacks are functions that are called when an operation is successful. These operations are divided in 5 categories (create, read, update, delete, resize). |
|
||||
| [Custom Allocation](../cor/custom_allocation/index.html) | Allows you to use your own custom dynamic memory allocation functions inside the collections. |
|
||||
| [Functions Table](../cor/functions_table/index.html) | A standard way to access required behaviors from the custom data types. Things like hash, comparison, freeing from memory if needed, etc. |
|
||||
| [Iterators](../cor/iterators/index.html) | Iterators are a simplified access to the values of a collection. |
|
||||
@@ -0,0 +1,3 @@
|
||||
# Overview
|
||||
|
||||
In this section you will be able to quickly glance at what are the core functionalities that the C Macro Collections library can bring you.
|
||||
@@ -6,18 +6,25 @@
|
||||
- [Library structure](./Introduction/library_structure.md)
|
||||
- [Understanding the library](./Introduction/understanding_the_library.md)
|
||||
- [How to use the library](./Introduction/how_to_use_the_library.md)
|
||||
- [Features](./Features/features.md)
|
||||
- [Two way iterators](./Features/two_way_iterators.md)
|
||||
- [Custom allocation](./Features/custom_allocation.md)
|
||||
- [Callbacks](./Features/callbacks.md)
|
||||
- [Functions Table](./Features/functions_table.md)
|
||||
- [Collections Overview](./Overview/overview.md)
|
||||
- [Overview](./Overview/index.md)
|
||||
- [Collections Overview](./Overview/collections_overview.md)
|
||||
- [Features Overview](./Overview/features_overview.md)
|
||||
- [cor](./cor/index.md)
|
||||
- [Callbacks](./cor/callbacks/index.md)
|
||||
- [Custom allocation](./cor/custom_allocation/index.md)
|
||||
- [Functions Table](./cor/functions_table/index.md)
|
||||
- [Iterators](./cor/iterators/index.md)
|
||||
- [cmc](./cmc/index.md)
|
||||
- [deque.h](./cmc/deque.h/index.md)
|
||||
- [dev](./dev/index.md)
|
||||
- [sac](./sac/index.md)
|
||||
- [utl](./utl/index.md)
|
||||
- [assert.h](./utl/assert.h/index.md)
|
||||
- [Overview](./utl/assert.h/overview.md)
|
||||
- [Valued Assertions](./utl/assert.h/valued_assertions.md)
|
||||
- [Generic Assertions](./utl/assert.h/generic_assertions.md)
|
||||
- [foreach.h](./utl/foreach.h/index.md)
|
||||
- [futils.h](./utl/futils.h/index.md)
|
||||
- [log.h](./utl/log.h/index.md)
|
||||
- [Log Functions](./utl/log.h/log_functions.md)
|
||||
- [Configuration](./utl/log.h/configuration.md)
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
# deque.h
|
||||
|
||||
A Deque, also known as a double-ended queue, is a linear data structure that is able to add and remove elements from both ends. It can also be thought of a double-ended stack since you can push and pop elements from two ends. The Deque can also be used as a Queue. The only elements accessible from a Deque ar the two elements at its ends (front element and back element).
|
||||
|
||||
## Deque Implementation
|
||||
|
||||
This implementation uses a circular buffer (ring buffer or cyclic buffer) in order to operate on O(1) for push and pop on either ends. The only case where it takes longer than O(1) is when the buffer is reallocated. If it was implemented as a regular array, adding or removing elements from the front would take O(N) due to the need to shift all elements in the Deque.
|
||||
|
||||
Two indices are kept track of. The `front` index and the `rear` index. These represent the both ends of the Deque. If an index reaches one end of the real buffer, they wrap around to the other end and an element is added there. This abstracts the real buffer as a circular buffer with the cost os constantly checking for boundaries and using the modulo operator.
|
||||
|
||||
## Deque Generation Macros
|
||||
|
||||
* `CMC_GENERATE_DEQUE(PFX, SNAME, V)` - Generate full definition.
|
||||
* `CMC_GENERATE_DEQUE_HEADER(PFX, SNAME, V)` - Generate only the header portion
|
||||
* `CMC_GENERATE_DEQUE_SOURCE(PFX, SNAME, V)` - Generate only the source portion
|
||||
|
||||
| Parameter | Description |
|
||||
| --------- | ----------------------------- |
|
||||
| `PFX` | Functions namespace or prefix |
|
||||
| `SNAME` | Structure name |
|
||||
| `V` | Value type |
|
||||
|
||||
## Deque Structures
|
||||
|
||||
| Declared structs | Description |
|
||||
| ------------------------- | ----------------------------------- |
|
||||
| `struct SNAME` | A double-ended queue data structure |
|
||||
| `struct SNAME##_fval` | A function table for the `V` type |
|
||||
| `struct SNAME##_iter` | A double-ended queue iterator |
|
||||
|
||||
## struct SNAME
|
||||
|
||||
| `struct SNAME` | Description |
|
||||
| --------------------------------- | ------------------------------------------------------------ |
|
||||
| `V *buffer` | Dynamic circular array of elements. |
|
||||
| `size_t capacity` | Current circular array capacity. |
|
||||
| `size_t count` | Current amount of elements. |
|
||||
| `size_t front` | Index representing the front of the Deque. |
|
||||
| `size_t rear` | Index representing the back of the Deque. |
|
||||
| `int flag` | Flag indicating errors or success. |
|
||||
| `struct SNAME##_fval *f_val` | Functions table for the Value type. |
|
||||
| `struct cmc_alloc_node *alloc` | Custom allocation functions. |
|
||||
| `struct cmc_callbacks *callbacks` | Callback functions. |
|
||||
| `it_start` <sup>\[1\]</sup> | Returns an iterator to the start of the Deque. |
|
||||
| `it_end` <sup>\[2\]</sup> | Returns an iterator to the end of the Deque. |
|
||||
|
||||
1. Full definition: `struct SNAME##_iter (*it_start)(struct SNAME *);`.
|
||||
2. Full definition: `struct SNAME##_iter (*it_end)(struct SNAME *);`.
|
||||
|
||||
## struct SNAME##_fval
|
||||
|
||||
The Functions Table for `V`. Check out the Functions Table documentation [here](../../cor/functions_table/index.html).
|
||||
|
||||
## struct SNAME##_iter
|
||||
|
||||
| `struct SNAME##_iter` | Description |
|
||||
| ---------------------- | --------------------------------------------------- |
|
||||
| `struct SNAME *target` | Deque being iterated over. |
|
||||
| `size_t cursor` | Cursor's current position (index). |
|
||||
| `size_t index` | Relative index to all elements in the iteration. |
|
||||
| `bool start` | If the iterator reached the start of the iteration. |
|
||||
| `bool end` | If the iterator reached the end of iteration. |
|
||||
|
||||
## Callbacks
|
||||
|
||||
This list associates which functions calls which callbacks.
|
||||
|
||||
* `create`
|
||||
* `PFX##_push_front()`
|
||||
* `PFX##_push_back()`
|
||||
* `read`
|
||||
* `PFX##_front()`
|
||||
* `PFX##_back()`
|
||||
* `PFX##_contains()`
|
||||
* `update`
|
||||
* `delete`
|
||||
* `PFX##_pop_front()`
|
||||
* `PFX##_pop_back()`
|
||||
* `resize`
|
||||
* `PFX##_resize()`
|
||||
|
||||
## Functions Table
|
||||
|
||||
| CMP | CPY | STR | FREE | HASH | PRI |
|
||||
| :-: | :-: | :-: | :--: | :--: | :-: |
|
||||
|  |  |  |  |  |  |
|
||||
@@ -0,0 +1,27 @@
|
||||
# CMC
|
||||
|
||||
The main C Macro Collections.
|
||||
|
||||
The C Macro Collections library was created out of necessity. To create powerful data structures of any type without the constant use of macros and `void *` pointers, but at the same time being [type-safe](https://en.wikipedia.org/wiki/Type_safety). These collections are generated by macros containing a template. These templates allow for some much needed customizations including the data type you wish to handle.
|
||||
|
||||
In the `cmc` folder is the default implementation of all collections. There is also a `dev` folder that pretty much has the same macros that expand to the same code, except that they have an extensive use of logging that might be useful for debugging or getting to know what is happening in your program. There is also the `sac` collections that are statically allocated (have a fixed sized buffer).
|
||||
|
||||
## Quick Example
|
||||
|
||||
Let's say you need a HashMap mapping keys of type `char *` to `int`. All you need to do is:
|
||||
|
||||
```c
|
||||
#include "cmc/hashmap"
|
||||
|
||||
// CMC_GENERATE_HASHMAP(PFX, SNAME, K, V)
|
||||
CMC_GENERATE_HASHMAP(map, hashmap, char *, int)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Here you have full functionalities of a HashMap
|
||||
// No more macros needed!
|
||||
// Every function can be found in the 'map_' prefix and are all type-safe
|
||||
}
|
||||
```
|
||||
|
||||
You can customize the functions prefix (`PFX`) and the `struct` name (`SNAME`). The macro `CMC_GENERATE_HASHMAP` will expand to all the code that you will need to operate on a HashMap with `char *` keys and `int` values. The only thing left to do is to define a hash function and a comparator functions for the keys of this map. In case of `char *` you can find these functions in `utl/futils.h` or make your own. To know which collections require which functions check out [Functions Table](../cor/functions_table/index.html) or the documentation for each collection.
|
||||
@@ -0,0 +1,60 @@
|
||||
# Callbacks
|
||||
|
||||
Every collection can have an optional callback node. In this node there are five functions:
|
||||
|
||||
| Callback | Description |
|
||||
| -------- | ----------------------------------------------------------------------- |
|
||||
| `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.
|
||||
|
||||
## cmc_callbacks
|
||||
|
||||
Every collection has a pointer to this `struct`. If it is `NULL` then the collection has no callbacks. Also, individual callbacks can be optional if set to `NULL`.
|
||||
|
||||
```c
|
||||
struct cmc_callbacks
|
||||
{
|
||||
void (*create)(void);
|
||||
void (*read)(void);
|
||||
void (*update)(void);
|
||||
void (*delete)(void);
|
||||
void (*resize)(void);
|
||||
};
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```c
|
||||
#include "cmc/heap.h"
|
||||
#include "utl/futils.h" // cmc_i32_cmp
|
||||
|
||||
// Generate a heap of integers
|
||||
CMC_GENERATE_HEAP(i32h, i32heap, int)
|
||||
|
||||
void heap_on_create(void)
|
||||
{
|
||||
printf("An element was added to the heap\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// create() is set but the other callbacks are not
|
||||
struct cmc_callbacks my_callbacks = { .create = heap_on_create, NULL };
|
||||
|
||||
// Create a max heap with the callbacks
|
||||
struct i32heap *heap = i32h_new_custom(
|
||||
100, cmc_max_heap, &(struct i32heap_fval){ .cmp = cmc_i32_cmp }, NULL,
|
||||
&my_callbacks);
|
||||
|
||||
i32h_insert(heap, 10);
|
||||
i32h_insert(heap, 11);
|
||||
i32h_insert(heap, 12);
|
||||
|
||||
i32h_free(heap);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,91 @@
|
||||
# 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. Every custom allocation function must follow the same prototype of the `stdlib.h` functions.
|
||||
|
||||
## cmc_alloc_node
|
||||
|
||||
```c
|
||||
struct cmc_alloc_node
|
||||
{
|
||||
void *(*malloc)(size_t);
|
||||
void *(*calloc)(size_t, size_t);
|
||||
void *(*realloc)(void *, size_t);
|
||||
void (*free)(void *);
|
||||
};
|
||||
```
|
||||
|
||||
## cmc_alloc_node_default
|
||||
|
||||
The default allocation node. If a collections is not initialized with a custom allocation node, this will be the default one using the functions from the C standard library.
|
||||
|
||||
```c
|
||||
static struct cmc_alloc_node cmc_alloc_node_default = { malloc, calloc,
|
||||
realloc, free };
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```c
|
||||
#include "cmc/heap.h"
|
||||
#include "utl/futils.h" // cmc_i32_cmp
|
||||
|
||||
// Total bytes allocated
|
||||
size_t total = 0;
|
||||
|
||||
void *my_malloc(size_t size)
|
||||
{
|
||||
void *result = malloc(size);
|
||||
|
||||
if (!result)
|
||||
return result;
|
||||
|
||||
total += size;
|
||||
return result;
|
||||
}
|
||||
|
||||
void *my_calloc(size_t count, size_t size)
|
||||
{
|
||||
void *result = calloc(count, size);
|
||||
|
||||
if (!result)
|
||||
return result;
|
||||
|
||||
total += count * size;
|
||||
return result;
|
||||
}
|
||||
|
||||
void *my_realloc(void *block, size_t size)
|
||||
{
|
||||
void *result = realloc(block, size);
|
||||
|
||||
if (!result)
|
||||
return result;
|
||||
|
||||
total += size;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Generate a heap of integers
|
||||
CMC_GENERATE_HEAP(i32h, i32heap, int)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// My custom allocation node
|
||||
struct cmc_alloc_node node = { .malloc = my_malloc,
|
||||
.calloc = my_calloc,
|
||||
.realloc = my_realloc,
|
||||
.free = free };
|
||||
|
||||
// Create a max heap with the custom allocation node
|
||||
struct i32heap *heap = i32h_new_custom(
|
||||
100, cmc_max_heap, &(struct i32heap_fval){ .cmp = cmc_i32_cmp }, &node,
|
||||
NULL);
|
||||
|
||||
for (int i = 0; i < 100000; i++)
|
||||
i32h_insert(heap, i);
|
||||
|
||||
i32h_free(heap);
|
||||
|
||||
printf("Total bytes allocated : %" PRIuMAX "\n", total);
|
||||
}
|
||||
```
|
||||
+67
-5
@@ -1,39 +1,101 @@
|
||||
# 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.
|
||||
Functions Table is a `struct` with function pointers that are used to extract behavior from a custom data type. This exists because an `int` or a `char *` or `struct my_data_type` don't have default hash functions or ways to compare between them. This could be achieved with [operator overloading](https://en.wikipedia.org/wiki/Operator_overloading) but C doesn't have that. Hence, Functions Table was created to provide a single access point to extract these behaviors for __any__ data type.
|
||||
|
||||
Some functions are optional and others are needed in order for a collection to operate. Say you want a HashMap. There are two required functions: `hash` and `cmp` (comparator). Without them it is impossible to know how to hash your custom that type (it could be anything). This means you can also customize the collection by using different hash functions.
|
||||
|
||||
Many required functions are simply mundane. Like a comparator function between integers or between floats. The header [`utl/futils.h`](../../utl/futils.h/index.html) defines many of these functions for basic C data types.
|
||||
|
||||
Some collections generate two functions table. One for `K` and one for `V` (maps). Others simply generate one for `V`. A Functions Table `struct` is always going to be called:
|
||||
|
||||
* `struct SNAME##_fkey` for the `K` type
|
||||
* `struct SNAME##_fval` for the `V` type
|
||||
|
||||
Their definition is defined as follows:
|
||||
|
||||
```c
|
||||
\
|
||||
struct SNAME##_fkey \
|
||||
{ \
|
||||
/* Comparator function */ \
|
||||
int (*cmp)(K, K); \
|
||||
\
|
||||
/* Copy function */ \
|
||||
K (*cpy)(K); \
|
||||
\
|
||||
/* To string function */ \
|
||||
bool (*str)(FILE *, K); \
|
||||
\
|
||||
/* Free from memory function */ \
|
||||
void (*free)(K); \
|
||||
\
|
||||
/* Hash function */ \
|
||||
size_t (*hash)(K); \
|
||||
\
|
||||
/* Priority function */ \
|
||||
int (*pri)(K, K); \
|
||||
}; \
|
||||
\
|
||||
```
|
||||
|
||||
### CMP
|
||||
|
||||
* `K` - `int (*cmp)(K, K)`
|
||||
* `V` - `int (*cmp)(V, V)`
|
||||
|
||||
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 `1` if the first argument is greater than the second;
|
||||
* Return `0` if the first argument equals the second;
|
||||
* Return `-1` if the first argument is less than the second.
|
||||
|
||||
Note that certain collections require a total ordering of its elements (sorted collections) while others only require to know if one data equals another. Check out the documentation for each collection to know more.
|
||||
|
||||
### CPY
|
||||
|
||||
* `K` - `K (*cpy)(K)`
|
||||
* `V` - `V (*cpy)(V)`
|
||||
|
||||
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).
|
||||
|
||||
Note that it doesn't makes sense to define a `cpy` function for data types that are not pointers. This function is useful for deep copies of a pointer in case you don't want two collections holding the same reference to the data.
|
||||
|
||||
### 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.
|
||||
* `K` - `bool (*str)(FILE *, K)`
|
||||
* `V` - `bool (*str)(FILE *, V)`
|
||||
|
||||
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 and doesn't necessarily needs to print all the data that the specific type holds.
|
||||
|
||||
### 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.
|
||||
* `K` - `void (*free)(K)`
|
||||
* `V` - `void (*free)(V)`
|
||||
|
||||
The free function is called when a collection is cleared (all elements removed) or freed (all elements removed and the collection is freed from memory) and it is responsible for completely freeing all resources that are usually acquired by your data type.
|
||||
|
||||
This function only makes sense to be used when the data type has dynamically allocated memory. It is the only completely optional function.
|
||||
|
||||
### HASH
|
||||
|
||||
This function receives a custom data type as parameter and returns a `size_t` hash of that data. Used in hashtables.
|
||||
* `K` - `size_t (*hash)(K)`
|
||||
* `V` - `size_t (*hash)(V)`
|
||||
|
||||
This function receives a custom data type as parameter and returns a `size_t` [hash](https://en.wikipedia.org/wiki/Hash_function) that represents that data. Used by collections with an underlying hash tables.
|
||||
|
||||
### 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.
|
||||
* `K` - `int (*cmp)(K, K)`
|
||||
* `V` - `int (*cmp)(V, V)`
|
||||
|
||||
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 (heaps).
|
||||
|
||||
* Return `1` if the first argument has a greater priority than the second;
|
||||
* Return `0` if the first argument has the same priority as second;
|
||||
* Return `-1` if the first argument has a lower priority than the second.
|
||||
|
||||
Why use both `cmp` and `pri`? Heaps have their internal structure based in the priority of elements. This priority is not necessarily how each element is compared to each other. Maybe their equality is defined differently for an equality of priorities. Maybe the rules for their priorities is different for when comparing an element against another.
|
||||
|
||||
The following table shows which functions are required, optional or never used for each Collection:
|
||||
|
||||
| Collection | CMP | CPY | STR | FREE | HASH | PRI |
|
||||
@@ -0,0 +1,4 @@
|
||||
# COR
|
||||
|
||||
Core functionalities of the C Macro Collections Library.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Two-Way Iterators
|
||||
# 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.
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# DEV
|
||||
|
||||
The development collections. These collections are an exact copy of the `cmc` collections but they come with many logging throughout the generated code. Useful for debugging or during development.
|
||||
@@ -0,0 +1,23 @@
|
||||
# SAC
|
||||
|
||||
The Statically Allocated Collections. These collections have a fixed size. Its size is one of the parameters during macro expansion, producing a C array.
|
||||
|
||||
## CMC vs. SAC
|
||||
|
||||
The `cmc` collections hold their data either with nodes or a buffer that can be reallocated. The `sac` collections only have an array with fixed size and don't perform any allocation on the heap.
|
||||
|
||||
```c
|
||||
// A CMC List
|
||||
struct List
|
||||
{
|
||||
T *buffer;
|
||||
/* Other fields */
|
||||
};
|
||||
|
||||
// A SAC List
|
||||
struct List
|
||||
{
|
||||
T buffer[SIZE];
|
||||
/* Other fields */
|
||||
};
|
||||
```
|
||||
@@ -1,4 +1,4 @@
|
||||
# foreach.md
|
||||
# foreach.h
|
||||
|
||||
For-Each macros. Since doing a for loop with iterators can be a lot to write, these macros solve that problem. There are two of them:
|
||||
|
||||
@@ -26,3 +26,35 @@ For-Each macros. Since doing a for loop with iterators can be a lot to write, th
|
||||
* `SNAME` - Struct name
|
||||
* `ITERNAME` - Iterator variable name
|
||||
* `TARGET` - Target collection variable name
|
||||
|
||||
## Example
|
||||
|
||||
```c
|
||||
#include "cmc/list.h"
|
||||
#include "utl/foreach.h"
|
||||
|
||||
CMC_GENERATE_LIST(i32l, i32list, int32_t)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct i32list *list = i32l_new(100, &(struct i32list_fval) { NULL });
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
i32l_push_back(list, i);
|
||||
|
||||
CMC_FOREACH(i32l, i32list, it, list)
|
||||
{
|
||||
int value = i32l_iter_value(&it);
|
||||
size_t index = i32l_iter_index(&it);
|
||||
|
||||
if (index == 0)
|
||||
printf("[ %d, ", value);
|
||||
else if (index == i32l_count(list) - 1)
|
||||
printf("%d ]\n", value);
|
||||
else
|
||||
printf("%d, ", value);
|
||||
}
|
||||
|
||||
i32l_free(list);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,87 @@
|
||||
# futils.h
|
||||
|
||||
Contains common functions that can be used by Functions Table, instead of writing another one every time.
|
||||
|
||||
A quick list of functions present in this file:
|
||||
|
||||
| cmp |
|
||||
| ----------- |
|
||||
| `static inline int cmc_i64_cmp(int64_t x1, int64_t x2);` |
|
||||
| `static inline int cmc_i32_cmp(int32_t x1, int32_t x2);` |
|
||||
| `static inline int cmc_i16_cmp(int16_t x1, int16_t x2);` |
|
||||
| `static inline int cmc_i8_cmp(int8_t x1, int8_t x2);` |
|
||||
| `static inline int cmc_u64_cmp(uint64_t x1, uint64_t x2);` |
|
||||
| `static inline int cmc_u32_cmp(uint32_t x1, uint32_t x2);` |
|
||||
| `static inline int cmc_u16_cmp(uint16_t x1, uint16_t x2);` |
|
||||
| `static inline int cmc_u8_cmp(uint8_t x1, uint8_t x2);` |
|
||||
| `static inline int cmc_size_cmp(size_t x1, size_t x2);` |
|
||||
| `static inline int cmc_imax_cmp(intmax_t x1, intmax_t x2);` |
|
||||
| `static inline int cmc_umax_cmp(uintmax_t x1, uintmax_t x2);` |
|
||||
| `static inline int cmc_float_cmp(float x1, float x2);` |
|
||||
| `static inline int cmc_double_cmp(double x1, double x2);` |
|
||||
| `static inline int cmc_str_cmp(char *ch1, char *ch2);` |
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
| cpy |
|
||||
| --- |
|
||||
| `static inline char *cmc_str_cpy(char *str);` |
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
| str |
|
||||
| --- |
|
||||
| `static inline bool cmc_i8_str(FILE *file, int8_t element);` |
|
||||
| `static inline bool cmc_i16_str(FILE *file, int16_t element);` |
|
||||
| `static inline bool cmc_i32_str(FILE *file, int32_t element);` |
|
||||
| `static inline bool cmc_i64_str(FILE *file, int64_t element);` |
|
||||
| `static inline bool cmc_u8_str(FILE *file, uint8_t element);` |
|
||||
| `static inline bool cmc_u16_str(FILE *file, uint16_t element);` |
|
||||
| `static inline bool cmc_u32_str(FILE *file, uint32_t element);` |
|
||||
| `static inline bool cmc_u64_str(FILE *file, uint64_t element);` |
|
||||
| `static inline bool cmc_size_str(FILE *file, size_t element);` |
|
||||
| `static inline bool cmc_imax_str(FILE *file, intmax_t element);` |
|
||||
| `static inline bool cmc_umax_str(FILE *file, uintmax_t element);` |
|
||||
| `static inline bool cmc_float_str(FILE *file, float element);` |
|
||||
| `static inline bool cmc_double_str(FILE *file, double element);` |
|
||||
| `static inline bool cmc_str_str(FILE *file, char *element);` |
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
| free |
|
||||
| --- |
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
| hash |
|
||||
| ---- |
|
||||
| `static inline size_t cmc_i64_hash(int64_t e);` |
|
||||
| `static inline size_t cmc_i32_hash(int32_t e);` |
|
||||
| `static inline size_t cmc_i16_hash(int16_t e);` |
|
||||
| `static inline size_t cmc_i8_hash(int8_t e);` |
|
||||
| `static inline size_t cmc_u64_hash(uint64_t e);` |
|
||||
| `static inline size_t cmc_u32_hash(uint32_t e);` |
|
||||
| `static inline size_t cmc_u16_hash(uint16_t e);` |
|
||||
| `static inline size_t cmc_u8_hash(uint8_t e);` |
|
||||
| `static inline size_t cmc_size_hash(size_t e);` |
|
||||
| `static inline size_t cmc_imax_hash(intmax_t e);` |
|
||||
| `static inline size_t cmc_umax_hash(uintmax_t e);` |
|
||||
| `static inline size_t cmc_float_hash(float e);` |
|
||||
| `static inline size_t cmc_double_hash(double e);` |
|
||||
| `static inline size_t cmc_str_hash_djb2(char *str);` |
|
||||
| `static inline size_t cmc_str_hash_sdbm(char *str);` |
|
||||
| `static inline size_t cmc_str_hash_java(char *str);` |
|
||||
| `static inline size_t cmc_str_hash_murmur3(uint64_t e);` |
|
||||
| `static inline size_t cmc_str_hash_murmur3_variant(uint64_t e);` |
|
||||
| `static inline size_t cmc_i64_hash_mix(int64_t element);` |
|
||||
| `static inline size_t cmc_u64_hash_mix(uint64_t element);` |
|
||||
|
||||
<br>
|
||||
<br>
|
||||
|
||||
| pri |
|
||||
| --- |
|
||||
@@ -4,8 +4,9 @@ Inside the `./utl/` folder there are many utility functions and macros that can
|
||||
|
||||
## Contents
|
||||
|
||||
* [assert.h](assert.h/index.html)
|
||||
* [foreach.h](foreach.h/index.html)
|
||||
* [log.h](log.h/index.html)
|
||||
* [test.h](test.h/index.html)
|
||||
* [timer.h](timer.h/index.html)
|
||||
* [assert.h](assert.h/index.html) - Non-abortive assert macros
|
||||
* [foreach.h](foreach.h/index.html) - For Each macros
|
||||
* [futils.h](futils.h/index.html) - Common functions used by Functions Table
|
||||
* [log.h](log.h/index.html) - Logging utility with levels of severity
|
||||
* [test.h](test.h/index.html) - Simple Unit Test building with macros
|
||||
* [timer.h](timer.h/index.html) - Timing code execution utility
|
||||
Reference in new issue
Block a user