CLibs
|
A dynamic array. (also called List, Vector or ArrayList) More...
#include "../headers/array_printf.h"
#include "../headers/attributes.h"
#include <stdbool.h>
#include <stdint.h>
Go to the source code of this file.
Macros | |
#define | list_init_type(TYPE) |
#define | list_fetch(LIST, IDX, TYPE) |
#define | list_access(LIST, IDX, TYPE) |
#define | list_printf_sde(LIST, ITEM_TYPE, FORMAT, START_STR, DELIM, END_STR) |
#define | list_printf_d(LIST, ITEM_TYPE, FORMAT, DELIM) |
#define | list_printf(LIST, ITEM_TYPE, FORMAT) |
#define | list_sprintf_d(STRING, LIST, TYPE, FMTSTR, DELIM) |
#define | list_sprintf(STRING, LIST, TYPE, FMTSTR) |
Typedefs | |
typedef struct dynamic_array | List |
Functions | |
Constructor struct dynamic_array * | list_init_size (size_t el_size) |
Constructor struct dynamic_array * | list_init_cap_size (size_t el_size, size_t init_cap) |
int | list_copy (const struct dynamic_array *old, struct dynamic_array **new_ls_container) |
Constructor struct dynamic_array * | list_copy_of (const struct dynamic_array *) |
const void * | list_see (const struct dynamic_array *ls, size_t idx) |
const void * | list_peek (const struct dynamic_array *ls) |
void * | list_at (struct dynamic_array *, size_t idx) |
void * | list_at_last (struct dynamic_array *) |
int | list_set_at (struct dynamic_array *, size_t index, const void *data) |
int | list_extend (struct dynamic_array *ls, const void *array, size_t array_len) |
int | list_extend_list (struct dynamic_array *ls, const struct dynamic_array *app) |
int | list_append (struct dynamic_array *, const void *datap) |
int | list_insert (struct dynamic_array *, size_t index, const void *data) |
int | list_pop (struct dynamic_array *, void *container) |
int | list_remove_fast (struct dynamic_array *, size_t index, void *container) |
int | list_remove (struct dynamic_array *, size_t index, void *container) |
const void * | list_bsearch_p (const struct dynamic_array *, const void *needle, int(*cmp)(const void *, const void *)) |
int64_t | list_bsearch_i (const struct dynamic_array *, const void *needle, int(*cmp)(const void *, const void *)) |
const void * | list_lsearch_p (const struct dynamic_array *, const void *needle) |
int64_t | list_lsearch_i (const struct dynamic_array *, const void *needle) |
void | list_sort (struct dynamic_array *ls, int(*cmp)(const void *, const void *)) |
int | list_reverse (struct dynamic_array *) |
Constructor struct dynamic_array * | list_reversed (const struct dynamic_array *) |
int | list_cmp_size (const void *l1, const void *l2) |
int | list_cmp_elsize (const void *l1, const void *l2) |
void | list_destroy (struct dynamic_array *) |
int | list_clear (struct dynamic_array *ls) |
bool | list_is_empty (const struct dynamic_array *) |
Returns true if list is empty. | |
size_t | list_size (const struct dynamic_array *) |
Returns number of elements in the list. | |
size_t | list_el_size (const struct dynamic_array *) |
Returns sizeof elements (e.g. if list element type is char, returns 1) | |
const void * | list_items (const struct dynamic_array *) |
UseResult void * | list_items_copy (const struct dynamic_array *) |
A dynamic array. (also called List, Vector or ArrayList)
The List can store any elements of arbitrary size.
A new List can (and should) be created with list_init_* functions
list_init_type() function takes as its type argument the C keyword e.g. char
list_init_size() function takes as its el_size argument the number of bytes (acquired by sizeof) for the desired type to be stored
list_init_* functions allocate and initialize a List with these default values:
Finalization (destruction) of a List is done with list_destroy().
#define list_access | ( | LIST, | |
IDX, | |||
TYPE ) |
Accesses the list as if it was an array – like ARRAY[IDX] => assignment is possible. Type must be the same as the list element type.
OOB index dereferences a NULL pointer (so don't do that).
#define list_fetch | ( | LIST, | |
IDX, | |||
TYPE ) |
#define list_init_type | ( | TYPE | ) |
Initializes the List to be of type TYPE
#define list_printf | ( | LIST, | |
ITEM_TYPE, | |||
FORMAT ) |
#define list_printf_d | ( | LIST, | |
ITEM_TYPE, | |||
FORMAT, | |||
DELIM ) |
#define list_printf_sde | ( | LIST, | |
ITEM_TYPE, | |||
FORMAT, | |||
START_STR, | |||
DELIM, | |||
END_STR ) |
#define list_sprintf | ( | STRING, | |
LIST, | |||
TYPE, | |||
FMTSTR ) |
#define list_sprintf_d | ( | STRING, | |
LIST, | |||
TYPE, | |||
FMTSTR, | |||
DELIM ) |
typedef struct dynamic_array List |
int list_append | ( | struct dynamic_array * | , |
const void * | datap ) |
Appends an element to the end of the List.
The function assumes data is of the same type as the List.
Example:
void * list_at | ( | struct dynamic_array * | , |
size_t | idx ) |
Gets a mutable look of the element at the specified index
void * list_at_last | ( | struct dynamic_array * | ) |
Gets a mutable look of the last element
int64_t list_bsearch_i | ( | const struct dynamic_array * | , |
const void * | needle, | ||
int(* | cmp )(const void *, const void *) ) |
Binary search, returns index (-1 if not found)
const void * list_bsearch_p | ( | const struct dynamic_array * | , |
const void * | needle, | ||
int(* | cmp )(const void *, const void *) ) |
Binary search, returns pointer (NULL if not found)
int list_clear | ( | struct dynamic_array * | ls | ) |
Resets the list to the defaults
int list_cmp_elsize | ( | const void * | l1, |
const void * | l2 ) |
Compares the lists based on the size of elements, similar to memcmp/strcmp
int list_cmp_size | ( | const void * | l1, |
const void * | l2 ) |
Compares the lists based on the number of elements, similar to memcmp/strcmp
int list_copy | ( | const struct dynamic_array * | old, |
struct dynamic_array ** | new_ls_container ) |
Constructor struct dynamic_array * list_copy_of | ( | const struct dynamic_array * | ) |
Creates a copy of the original List
void list_destroy | ( | struct dynamic_array * | ) |
Frees all memory owned by the List, including itself
size_t list_el_size | ( | const struct dynamic_array * | ) |
Returns sizeof elements (e.g. if list element type is char, returns 1)
int list_extend | ( | struct dynamic_array * | ls, |
const void * | array, | ||
size_t | array_len ) |
Copies all elements from array of length array_len to the end of the List.
ls | a List to be extended |
array | array of array_len elements of size equal to ls::el_size |
array_len | the number of elements in the array |
int list_extend_list | ( | struct dynamic_array * | ls, |
const struct dynamic_array * | app ) |
Constructor struct dynamic_array * list_init_cap_size | ( | size_t | el_size, |
size_t | init_cap ) |
Creates a new list
el_size | sizeof a single element |
init_cap | initial capacity in terms of elements (rather than bytes) |
Constructor struct dynamic_array * list_init_size | ( | size_t | el_size | ) |
Initializes the lists elements to be of size el_size
el_size | sizeof() a single element |
int list_insert | ( | struct dynamic_array * | , |
size_t | index, | ||
const void * | data ) |
Inserts data to list at specified index, all following data is moved one place forward
index | index of new element |
data | pointer to data of size ls::el_size |
bool list_is_empty | ( | const struct dynamic_array * | ) |
Returns true if list is empty.
const void * list_items | ( | const struct dynamic_array * | ) |
Gets a const view of the items.
UseResult void * list_items_copy | ( | const struct dynamic_array * | ) |
Gets the lists items as an array. This pointer should be freed
int64_t list_lsearch_i | ( | const struct dynamic_array * | , |
const void * | needle ) |
Linear search, returns index (-1 if not found)
const void * list_lsearch_p | ( | const struct dynamic_array * | , |
const void * | needle ) |
Linear search, returns pointer (NULL if not found)
const void * list_peek | ( | const struct dynamic_array * | ls | ) |
Gets a non-mutable look of the last element
int list_pop | ( | struct dynamic_array * | , |
void * | container ) |
int list_remove | ( | struct dynamic_array * | , |
size_t | index, | ||
void * | container ) |
Removes the element at index by moving the following elements back by one space.
If container is NULL, this method discards the last element.
index | |
container | pointer to valid space in memory of at least ls::el_size bytes |
int list_remove_fast | ( | struct dynamic_array * | , |
size_t | index, | ||
void * | container ) |
Removes the element at index by swapping it with the last element and popping.
If container is NULL, this method discards the last element.
index | |
container | pointer to valid space in memory of at least ls::el_size bytes |
int list_reverse | ( | struct dynamic_array * | ) |
Reverses the List – in place
Constructor struct dynamic_array * list_reversed | ( | const struct dynamic_array * | ) |
const void * list_see | ( | const struct dynamic_array * | ls, |
size_t | idx ) |
Gets a non-mutable look of the element at the specified index
int list_set_at | ( | struct dynamic_array * | , |
size_t | index, | ||
const void * | data ) |
Changes element at index to data
size_t list_size | ( | const struct dynamic_array * | ) |
Returns number of elements in the list.