(my)TinyLibC 0.0.1
little tiny pretty lib c
Loading...
Searching...
No Matches
tlcllists.h File Reference
#include <stdbool.h>
#include "tlcutils.h"

Go to the source code of this file.

Data Structures

struct  node_t
 
struct  list_t
 
struct  node_result_t
 

Macros

#define _COPY_CHECK(x)   (x != 0 && (x)->copy != 0)
 
#define _DESTROY_CHECK(x)   (x != 0 && (x)->destroy != 0)
 
#define _VOID(c, e)   ((c) ? e : return_void())
 
#define L_LEN(x)   ((x != 0) ? (x)->len : 0)
 return the length of the list
 
#define L_NEXT(x)   ((x != 0) ? (x)->next : 0)
 return the next node
 
#define L_FIRST(list)   ((list != 0) ? (list)->start : 0)
 return the first node
 
#define L_LAST(list)   ((list != 0) ? (list)->end : 0)
 return the last node
 
#define L_COPY(x)   ((_COPY_CHECK(x)) ? (x)->copy((x)->data) : 0)
 return the copy of the node data
 
#define L_DATA(x)   ((x != 0) ? (x)->data : 0)
 return the data of the node
 
#define L_DESTROY(x)   (_VOID(_DESTROY_CHECK(x), (x)->destroy(L_DATA(x))))
 destroy the node
 
#define L_DECL_FIRST(x, list)   node_t *x = L_FIRST(list)
 create a variable with the first node of the list
 
#define L_DECL_LAST(x, list)   node_t *x = L_LAST(list)
 create a variable with the last node of the list
 
#define L_EACH(x, list)   L_DECL_FIRST(x, list); x != 0; x = L_NEXT(x)
 the for each that i like
 

Functions

list_t * list_append (list_t *list, void *data, void(*destroy)(void *data), void *(*copy)(void *data))
 append new data to list
 
list_t * list_create (void)
 create an empty list
 
node_t * node_create (void *data, void(*destroy)(void *data), void *(*copy)(void *data))
 create a node
 
void list_delete (list_t *list)
 delete the list
 
list_t * list_duplicate (list_t *list)
 duplicate list
 
node_result_t list_find_ptrdata (list_t *list, void *data)
 find node that have a node->data == data
 
int list_find_ptrnode (list_t *list, node_t *node_ptr)
 find node index that have node == node_ptr
 
node_result_t list_find_f (list_t *list, bool(is_this_result)(void *node_data, void *param), void *param)
 find node where is_this_result(node->data, param) = 1
 
node_t * list_index (list_t *list, int index)
 return node at index index
 
list_t * list_insert_end (list_t *list, node_t *node)
 insert node at end of list
 
list_t * list_insert_start (list_t *list, node_t *node)
 insert node at start of list
 
list_t * list_insert (list_t *list, node_t *node, int index)
 insert node at index index
 
int list_remove_start (list_t *list)
 remove the first node of list
 
int list_remove_end (list_t *list)
 remove the last node of list
 
int list_remove_ptrnode (list_t *list, node_t *node)
 remove node where its ptr is node
 
int list_remove_ptrdata (list_t *list, void *ptrdata)
 remove node where node data is ptdrdata
 

Macro Definition Documentation

◆ _COPY_CHECK

#define _COPY_CHECK (   x)    (x != 0 && (x)->copy != 0)

Definition at line 35 of file tlcllists.h.

◆ _DESTROY_CHECK

#define _DESTROY_CHECK (   x)    (x != 0 && (x)->destroy != 0)

Definition at line 36 of file tlcllists.h.

◆ _VOID

#define _VOID (   c,
 
)    ((c) ? e : return_void())

Definition at line 37 of file tlcllists.h.

◆ L_COPY

#define L_COPY (   x)    ((_COPY_CHECK(x)) ? (x)->copy((x)->data) : 0)

return the copy of the node data

Parameters
xthe node
Returns
the copy of the node data

Definition at line 73 of file tlcllists.h.

◆ L_DATA

#define L_DATA (   x)    ((x != 0) ? (x)->data : 0)

return the data of the node

Parameters
xthe node
Returns
the data of the node

Definition at line 80 of file tlcllists.h.

◆ L_DECL_FIRST

#define L_DECL_FIRST (   x,
  list 
)    node_t *x = L_FIRST(list)

create a variable with the first node of the list

Parameters
xthe variable name to create
listthe list

Definition at line 95 of file tlcllists.h.

◆ L_DECL_LAST

#define L_DECL_LAST (   x,
  list 
)    node_t *x = L_LAST(list)

create a variable with the last node of the list

Parameters
xthe variable name to create
listthe list

Definition at line 102 of file tlcllists.h.

◆ L_DESTROY

#define L_DESTROY (   x)    (_VOID(_DESTROY_CHECK(x), (x)->destroy(L_DATA(x))))

destroy the node

Parameters
xthe node
Warning
don't use it if you don't know exactly what you are doing

Definition at line 87 of file tlcllists.h.

◆ L_EACH

#define L_EACH (   x,
  list 
)    L_DECL_FIRST(x, list); x != 0; x = L_NEXT(x)

the for each that i like

Parameters
xthe variable name to create
listthe list
Warning
don't delete a node in the for (if you do so, it can crash)

Definition at line 110 of file tlcllists.h.

◆ L_FIRST

#define L_FIRST (   list)    ((list != 0) ? (list)->start : 0)

return the first node

Parameters
listthe list
Returns
the first node

Definition at line 59 of file tlcllists.h.

◆ L_LAST

#define L_LAST (   list)    ((list != 0) ? (list)->end : 0)

return the last node

Parameters
listthe list
Returns
the last node

Definition at line 66 of file tlcllists.h.

◆ L_LEN

#define L_LEN (   x)    ((x != 0) ? (x)->len : 0)

return the length of the list

Parameters
xthe list
Returns
the length

Definition at line 45 of file tlcllists.h.

◆ L_NEXT

#define L_NEXT (   x)    ((x != 0) ? (x)->next : 0)

return the next node

Parameters
xcurrent node
Returns
the next node

Definition at line 52 of file tlcllists.h.

Function Documentation

◆ list_append()

list_t * list_append ( list_t *  list,
void *  data,
void(*)(void *data)  destroy,
void *(*)(void *data)  copy 
)

append new data to list

If destroy is NULL, the data will not be freed when the node is deleted If copy is NULL, the node will not be copied if you duplicate the list

Parameters
listthe list to update
datathe data to append
destroythe function called when need to destroy data (can be NULL)
copythe function called when need to copy data (can be NULL)
Returns
NULL if list = 0;; the list

◆ list_create()

list_t * list_create ( void  )

create an empty list

Returns
NULL if malloc failed;; the new created list

◆ list_delete()

void list_delete ( list_t *  list)

delete the list

Parameters
listlist to delete

◆ list_duplicate()

list_t * list_duplicate ( list_t *  list)

duplicate list

Parameters
listthe list to duplicate
Returns
NULL if list = 0;; the new duplicated list

◆ list_find_f()

node_result_t list_find_f ( list_t *  list,
bool(is_this_result)(void *node_data, void *param)  ,
void *  param 
)

find node where is_this_result(node->data, param) = 1

Parameters
listthe list in which to search
is_this_resultfunction that return 1 when the item is found
paramadditional parameter to is_this_result
Returns
(node_index = -1 & node_ptr = 0) if not found;; the good info

◆ list_find_ptrdata()

node_result_t list_find_ptrdata ( list_t *  list,
void *  data 
)

find node that have a node->data == data

Parameters
listthe list in which to search
datathe ptr to search in list
Returns
(node_index = -1 & node_ptr = 0) if not found;; the good info

◆ list_find_ptrnode()

int list_find_ptrnode ( list_t *  list,
node_t *  node_ptr 
)

find node index that have node == node_ptr

Parameters
listthe list in which to search
node_ptrthe ptr to search in list
Returns
-1 if not found;; the index

◆ list_index()

node_t * list_index ( list_t *  list,
int  index 
)

return node at index index

If the index <= 0, it return the first node If the index is out of range, it return the last node

Parameters
listthe list in which to search
indexthe index of the node
Returns
NULL if list = 0;; the node

◆ list_insert()

list_t * list_insert ( list_t *  list,
node_t *  node,
int  index 
)

insert node at index index

If node = 0, the node will ne be added, and list will be returned

Parameters
listlist to update
nodenode to add
indexindex where node will be in list
Returns
NULL if list = 0;; list

◆ list_insert_end()

list_t * list_insert_end ( list_t *  list,
node_t *  node 
)

insert node at end of list

Parameters
listlist to update
nodenode to add
Returns
NULL if list = 0;; list

◆ list_insert_start()

list_t * list_insert_start ( list_t *  list,
node_t *  node 
)

insert node at start of list

Parameters
listlist to update
nodenode to add
Returns
NULL if list = 0;; list

◆ list_remove_end()

int list_remove_end ( list_t *  list)

remove the last node of list

Parameters
listlist to update
Returns
0 if (list = 0)|(list->len = 0);; 1

◆ list_remove_ptrdata()

int list_remove_ptrdata ( list_t *  list,
void *  ptrdata 
)

remove node where node data is ptdrdata

Parameters
listlist to update
ptrdataptr of data where data is in the node to remove
Returns
0 if node not found;; 1

◆ list_remove_ptrnode()

int list_remove_ptrnode ( list_t *  list,
node_t *  node 
)

remove node where its ptr is node

Parameters
listlist to update
nodeptr to the node to remove
Returns
0 if node not found;; 1

◆ list_remove_start()

int list_remove_start ( list_t *  list)

remove the first node of list

Parameters
listlist to update
Returns
0 if (list = 0)|(list->len = 0);; 1

◆ node_create()

node_t * node_create ( void *  data,
void(*)(void *data)  destroy,
void *(*)(void *data)  copy 
)

create a node

If destroy is NULL, the data will not be freed when the node is deleted If copy is NULL, the node will not be copied if you duplicate the list

Parameters
datadata to set
destroyfunction to destroy data when needed (can be NULL)
copyfunction to copy data when needed (can be NULL)
Returns
NULL if malloc failed;; the new node created