libamxc
1.10.3
C Generic Data Containers
|
The Ambiorix Linked List is a doubly linked list. More...
Modules | |
Linked List Iterator | |
Data Structures | |
struct | _amxc_llist |
The linked list structure. More... | |
Macros | |
#define | amxc_llist_for_each(it, list) |
Loops over the list from head to tail. More... | |
#define | amxc_llist_iterate(it, list) |
Loops over the list from head to tail. More... | |
#define | amxc_llist_for_each_reverse(it, list) |
Loops over the list from tail to head. More... | |
#define | amxc_llist_iterate_reverse(it, list) |
Loops over the list from tail to head. More... | |
Typedefs | |
typedef struct _amxc_llist | amxc_llist_t |
The linked list structure. More... | |
typedef void(* | amxc_llist_it_delete_t) (amxc_llist_it_t *it) |
Definition of the linked list item delete function. More... | |
Functions | |
int | amxc_llist_new (amxc_llist_t **llist) |
Allocates a linked list. More... | |
void | amxc_llist_delete (amxc_llist_t **llist, amxc_llist_it_delete_t func) |
Frees the previously allocated linked list. More... | |
int | amxc_llist_init (amxc_llist_t *const llist) |
Initializes a linked list. More... | |
void | amxc_llist_clean (amxc_llist_t *const llist, amxc_llist_it_delete_t func) |
Removes all items from the linked list. More... | |
int | amxc_llist_move (amxc_llist_t *const dest, amxc_llist_t *const src) |
Moves all items from one linked list to another linked list. More... | |
bool | amxc_llist_is_empty (const amxc_llist_t *const llist) |
Checks that the linked list is empty. More... | |
size_t | amxc_llist_size (const amxc_llist_t *const llist) |
Calculates the size of the linked list. More... | |
int | amxc_llist_append (amxc_llist_t *const llist, amxc_llist_it_t *const it) |
Adds an item to the end of the linked list. More... | |
int | amxc_llist_prepend (amxc_llist_t *const llist, amxc_llist_it_t *const it) |
Adds an item to the beginning of the linked list. More... | |
amxc_llist_it_t * | amxc_llist_get_at (const amxc_llist_t *const llist, const unsigned int index) |
Gets an item at a certain position of the linked list. More... | |
int | amxc_llist_set_at (amxc_llist_t *llist, const unsigned int index, amxc_llist_it_t *const it) |
Inserts an item at a certain position. More... | |
int | amxc_llist_sort (amxc_llist_t *const llist, amxc_llist_it_cmp_t cmp) |
Sorts a linked list. More... | |
AMXC_INLINE amxc_llist_it_t * | amxc_llist_get_first (const amxc_llist_t *const llist) |
Gets the first item of the linked list. More... | |
AMXC_INLINE amxc_llist_it_t * | amxc_llist_get_last (const amxc_llist_t *const llist) |
Gets the last item of the linked list. More... | |
AMXC_INLINE amxc_llist_it_t * | amxc_llist_take_first (amxc_llist_t *const llist) |
Removes the first item from the linked list. More... | |
AMXC_INLINE amxc_llist_it_t * | amxc_llist_take_last (amxc_llist_t *const llist) |
Removes the last item from the linked list. More... | |
AMXC_INLINE amxc_llist_it_t * | amxc_llist_take_at (const amxc_llist_t *llist, const unsigned int index) |
Removes an item at a certain position of the linked list. More... | |
The Ambiorix Linked List is a doubly linked list.
A doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields: a reference to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list.
The linked list itself contains two fields: a reference to the first node and a reference to the last node
The nodes of an Ambiorix linked list are defined using the amxc_llist_it_t structure (the iterator). To create a linked list of a C structure, put an linked list iterator as member of that structure.
You can implement your own loop and use the linked list navigation functions:
Deleting an item can be done using:
#define amxc_llist_for_each | ( | it, | |
list | |||
) |
Loops over the list from head to tail.
Iterates over the list and updates the iterator each time.
Definition at line 253 of file amxc_llist.h.
#define amxc_llist_for_each_reverse | ( | it, | |
list | |||
) |
Loops over the list from tail to head.
Iterates over the list and updates the iterator each time.
Definition at line 287 of file amxc_llist.h.
#define amxc_llist_iterate | ( | it, | |
list | |||
) |
Loops over the list from head to tail.
Iterates over the list and updates the iterator each time.
Definition at line 270 of file amxc_llist.h.
#define amxc_llist_iterate_reverse | ( | it, | |
list | |||
) |
Loops over the list from tail to head.
Iterates over the list and updates the iterator each time.
Definition at line 304 of file amxc_llist.h.
typedef void(* amxc_llist_it_delete_t) (amxc_llist_it_t *it) |
Definition of the linked list item delete function.
A pointer to a delete function is used in the following functions amxc_llist_delete, amxc_llist_clean, amxc_llist_it_clean.
Definition at line 317 of file amxc_llist.h.
typedef struct _amxc_llist amxc_llist_t |
The linked list structure.
int amxc_llist_append | ( | amxc_llist_t *const | llist, |
amxc_llist_it_t *const | it | ||
) |
Adds an item to the end of the linked list.
If the item is already in a list, it is removed from that list.
llist | a pointer to the linked list structure |
it | a pointer to the linked list item iterator |
Definition at line 169 of file amxc_llist.c.
void amxc_llist_clean | ( | amxc_llist_t *const | llist, |
amxc_llist_it_delete_t | func | ||
) |
Removes all items from the linked list.
Removes all items from the linked list, if a delete function is provided, it is called for each item after it was removed from the list.
llist | a pointer to the linked list structure |
func | a pointer to a function that is called to free each item in the linked list |
Definition at line 124 of file amxc_llist.c.
void amxc_llist_delete | ( | amxc_llist_t ** | llist, |
amxc_llist_it_delete_t | func | ||
) |
Frees the previously allocated linked list.
Removes all items from the linked list, if a delete function is provided, it is called for each item after it was removed from the list.
Frees the allocated memory and sets the pointer to NULL.
llist | a pointer to the location where the pointer to the linked list is stored |
func | a pointer to a function that is called to free each item in the linked list |
Definition at line 101 of file amxc_llist.c.
amxc_llist_it_t* amxc_llist_get_at | ( | const amxc_llist_t *const | llist, |
const unsigned int | index | ||
) |
Gets an item at a certain position of the linked list.
This function is not removing the item from the linked list. To remove the item from the linked list use amxc_llist_take_at.
llist | a pointer to the linked list structure |
index | the position of the item to get |
Definition at line 222 of file amxc_llist.c.
AMXC_INLINE amxc_llist_it_t* amxc_llist_get_first | ( | const amxc_llist_t *const | llist | ) |
Gets the first item of the linked list.
This function does not remove the item from the linked list. To remove the item, use amxc_llist_take_first.
llist | a pointer to the linked list structure |
Definition at line 713 of file amxc_llist.h.
AMXC_INLINE amxc_llist_it_t* amxc_llist_get_last | ( | const amxc_llist_t *const | llist | ) |
Gets the last item of the linked list.
This function does not remove the item from the linked list. To remove the item, use amxc_llist_take_last.
llist | a pointer to the linked list structure |
Definition at line 732 of file amxc_llist.h.
int amxc_llist_init | ( | amxc_llist_t *const | llist | ) |
Initializes a linked list.
Initializes the linked list structure. All pointers are reset to NULL. This function is typically called for linked lists that are on the stack. Allocating and initializing a linked list on the heap can be done using amxc_llist_new
llist | a pointer to the linked list structure. |
Definition at line 111 of file amxc_llist.c.
bool amxc_llist_is_empty | ( | const amxc_llist_t *const | llist | ) |
Checks that the linked list is empty.
llist | a pointer to the linked list structure |
Definition at line 165 of file amxc_llist.c.
int amxc_llist_move | ( | amxc_llist_t *const | dest, |
amxc_llist_t *const | src | ||
) |
Moves all items from one linked list to another linked list.
After the move the source linked list will be empty.
If the destination linked list already contains items, the items of the source are appended to the destination linked list.
dest | a pointer to the destination linked list |
src | a pointer to the source linked list |
Definition at line 135 of file amxc_llist.c.
int amxc_llist_new | ( | amxc_llist_t ** | llist | ) |
Allocates a linked list.
Allocates and initializes memory to store a linked list. This function allocates memory from the heap, if a linked list is on the stack, it can be initialized using function amxc_llist_init
llist | a pointer to the location where the pointer to the new linked list can be stored |
Definition at line 88 of file amxc_llist.c.
int amxc_llist_prepend | ( | amxc_llist_t *const | llist, |
amxc_llist_it_t *const | it | ||
) |
Adds an item to the beginning of the linked list.
If the item is already in a list, it is removed from that list.
llist | a pointer to the linked list structure |
it | a pointer to the linked list item iterator |
Definition at line 196 of file amxc_llist.c.
int amxc_llist_set_at | ( | amxc_llist_t * | llist, |
const unsigned int | index, | ||
amxc_llist_it_t *const | it | ||
) |
Inserts an item at a certain position.
If the item is already in a list, it is removed from that list. If the index is out of range (higher then number of items in the linked list) the item is not added.
llist | a pointer to the linked list structure |
index | the position where the item must be inserted |
it | a pointer to the linked list item iterator |
Definition at line 237 of file amxc_llist.c.
size_t amxc_llist_size | ( | const amxc_llist_t *const | llist | ) |
Calculates the size of the linked list.
Loops over all items in the linked list and counts them. The size of the linked list is expressed in items.
llist | a pointer to the linked list structure |
Definition at line 151 of file amxc_llist.c.
int amxc_llist_sort | ( | amxc_llist_t *const | llist, |
amxc_llist_it_cmp_t | cmp | ||
) |
Sorts a linked list.
Using a callback compare function, the items in the linked list are sorted. The callback function must match amxc_llist_it_cmp_t signature.
llist | a pointer to the linked list |
cmp | the sort compare callback function |
Definition at line 262 of file amxc_llist.c.
AMXC_INLINE amxc_llist_it_t* amxc_llist_take_at | ( | const amxc_llist_t * | llist, |
const unsigned int | index | ||
) |
Removes an item at a certain position of the linked list.
This function removes the item from the linked list. To get a pointer to the item, without removing it, use amxc_llist_get_at.
llist | a pointer to the linked list structure |
index | the position of the item to get |
Definition at line 803 of file amxc_llist.h.
AMXC_INLINE amxc_llist_it_t* amxc_llist_take_first | ( | amxc_llist_t *const | llist | ) |
Removes the first item from the linked list.
This function removes the item from the linked list. To get the item without removing it from the list use amxc_llist_get_first.
llist | a pointer to the linked list structure |
Definition at line 752 of file amxc_llist.h.
AMXC_INLINE amxc_llist_it_t* amxc_llist_take_last | ( | amxc_llist_t *const | llist | ) |
Removes the last item from the linked list.
This function removes the item from the linked list. To get the item without removing it from the list use amxc_llist_get_last.
llist | a pointer to the linked list structure |
Definition at line 774 of file amxc_llist.h.