myMPD
Internal API documentation
Loading...
Searching...
No Matches
list.h File Reference

Linked list implementation. More...

#include "dist/sds/sds.h"
#include <stdbool.h>
Include dependency graph for list.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  t_list_node
 
struct  t_list
 

Typedefs

typedef void(* user_data_callback) (struct t_list_node *current)
 
typedef sds(* list_node_to_line_callback) (sds buffer, struct t_list_node *current, bool newline)
 
typedef bool(* list_sort_callback) (struct t_list_node *current, struct t_list_node *next, enum list_sort_direction direction)
 

Enumerations

enum  list_sort_direction { LIST_SORT_ASC = 0 , LIST_SORT_DESC = 1 }
 

Functions

struct t_listlist_new (void)
 
struct t_listlist_dup (struct t_list *l)
 
bool list_append (struct t_list *dst, struct t_list *src)
 
void list_init (struct t_list *l)
 
void list_clear (struct t_list *l)
 
void list_free (struct t_list *l)
 
void list_free_void (void *l)
 
void list_clear_user_data (struct t_list *l, user_data_callback free_cb)
 
void list_free_user_data (struct t_list *l, user_data_callback free_cb)
 
void list_free_cb_ignore_user_data (struct t_list_node *current)
 
void list_free_cb_sds_user_data (struct t_list_node *current)
 
void list_free_cb_ptr_user_data (struct t_list_node *current)
 
void * list_node_free_user_data (struct t_list_node *n, user_data_callback free_cb)
 
void * list_node_free (struct t_list_node *n)
 
bool list_push (struct t_list *l, const char *key, int64_t value_i, const char *value_p, void *user_data)
 
bool list_push_len (struct t_list *l, const char *key, size_t key_len, int64_t value_i, const char *value_p, size_t value_len, void *user_data)
 
bool list_insert (struct t_list *l, const char *key, int64_t value_i, const char *value_p, void *user_data)
 
bool list_replace (struct t_list *l, unsigned idx, const char *key, int64_t value_i, const char *value_p, void *user_data)
 
bool list_replace_len (struct t_list *l, unsigned idx, const char *key, size_t key_len, int64_t value_i, const char *value_p, size_t value_len, void *user_data)
 
bool list_replace_user_data (struct t_list *l, unsigned idx, const char *key, int64_t value_i, const char *value_p, void *user_data, user_data_callback free_cb)
 
bool list_replace_len_user_data (struct t_list *l, unsigned idx, const char *key, size_t key_len, int64_t value_i, const char *value_p, size_t value_len, void *user_data, user_data_callback free_cb)
 
void list_crop (struct t_list *l, unsigned length, user_data_callback free_cb)
 
bool list_shuffle (struct t_list *l)
 
bool list_swap_item (struct t_list_node *n1, struct t_list_node *n2)
 
bool list_move_item_pos (struct t_list *l, unsigned from, unsigned to)
 
unsigned list_get_node_idx (const struct t_list *l, const char *key)
 
struct t_list_nodelist_get_node (const struct t_list *l, const char *key)
 
struct t_list_nodelist_node_at (const struct t_list *l, unsigned idx)
 
struct t_list_nodelist_node_prev_at (const struct t_list *l, unsigned idx, struct t_list_node **previous)
 
struct t_list_nodelist_shift_first (struct t_list *l)
 
struct t_list_nodelist_node_extract (struct t_list *l, unsigned idx)
 
bool list_remove_node (struct t_list *l, unsigned idx)
 
bool list_remove_node_user_data (struct t_list *l, unsigned idx, user_data_callback free_cb)
 
bool list_remove_node_by_key (struct t_list *l, const char *key)
 
bool list_remove_node_by_key_user_data (struct t_list *l, const char *key, user_data_callback free_cb)
 
bool list_write_to_disk (sds filepath, struct t_list *l, list_node_to_line_callback node_to_line_cb)
 
bool list_sort_by_callback (struct t_list *l, enum list_sort_direction direction, list_sort_callback sort_cb)
 
bool list_sort_by_value_i (struct t_list *l, enum list_sort_direction direction)
 
bool list_sort_by_value_p (struct t_list *l, enum list_sort_direction direction)
 
bool list_sort_by_key (struct t_list *l, enum list_sort_direction direction)
 

Detailed Description

Linked list implementation.

Typedef Documentation

◆ list_node_to_line_callback

typedef sds(* list_node_to_line_callback) (sds buffer, struct t_list_node *current, bool newline)

Definition of list to line callback

◆ list_sort_callback

typedef bool(* list_sort_callback) (struct t_list_node *current, struct t_list_node *next, enum list_sort_direction direction)

Definition of sort callback

◆ user_data_callback

typedef void(* user_data_callback) (struct t_list_node *current)

Definition of user data callback

Enumeration Type Documentation

◆ list_sort_direction

Sort direction

Function Documentation

◆ list_append()

bool list_append ( struct t_list dst,
struct t_list src 
)

Appends a list to another list Leaves user_data pointer in place.

Parameters
dstlist to that was append
srclist that was append
Returns
duplicated list or NULL on error

◆ list_clear()

void list_clear ( struct t_list l)

Clears the list and frees all nodes, ignores user_data

Parameters
lpointer to list

◆ list_clear_user_data()

void list_clear_user_data ( struct t_list l,
user_data_callback  free_cb 
)

Clears the list and frees all nodes and there values, calls a function to free user_data, set free_cb to NULL, to free a generic pointer.

Parameters
lpointer to list
free_cb

◆ list_crop()

void list_crop ( struct t_list l,
unsigned  length,
user_data_callback  free_cb 
)

Crops the list to the defined length

Parameters
lpointer to list to crop
lengthmax length
free_cbcallback function to free user_data pointer

◆ list_dup()

struct t_list * list_dup ( struct t_list l)

Duplicates a list. Leaves user_data pointer in place.

Parameters
llist to duplicate
Returns
duplicated list or NULL on error

◆ list_free()

void list_free ( struct t_list l)

Clears the list, frees all nodes and the list itself, ignores user_data

Parameters
lpointer to list

◆ list_free_cb_ignore_user_data()

void list_free_cb_ignore_user_data ( struct t_list_node current)

Callback function to not free user_data.

Parameters
currentlist node

◆ list_free_cb_ptr_user_data()

void list_free_cb_ptr_user_data ( struct t_list_node current)

Callback function to free user_data of generic pointer.

Parameters
currentlist node

◆ list_free_cb_sds_user_data()

void list_free_cb_sds_user_data ( struct t_list_node current)

Callback function to free user_data of type sds.

Parameters
currentlist node

◆ list_free_user_data()

void list_free_user_data ( struct t_list l,
user_data_callback  free_cb 
)

Clears the list and frees all nodes and there values, calls a function to free user_data, set free_cb to NULL, to free a generic pointer.

Parameters
lpointer to list
free_cbCallback to free the user data

◆ list_free_void()

void list_free_void ( void *  l)

Clears the list, frees all nodes and the list itself, ignores user_data

Parameters
lpointer to list

◆ list_get_node()

struct t_list_node * list_get_node ( const struct t_list l,
const char *  key 
)

Gets a list node by key.

Parameters
llist
keykey to get
Returns
pointer to list node

◆ list_get_node_idx()

unsigned list_get_node_idx ( const struct t_list l,
const char *  key 
)

Gets a list node index by key.

Parameters
llist
keykey to get
Returns
int index of the key, UINT_MAX if not found

◆ list_init()

void list_init ( struct t_list l)

Inits a already allocated list

Parameters
lpointer to list

◆ list_insert()

bool list_insert ( struct t_list l,
const char *  key,
int64_t  value_i,
const char *  value_p,
void *  user_data 
)

Creates and inserts a node at the beginning of the list.

Parameters
llist
keykey value
value_iint64_t value
value_psds value
user_datapointer to user_data
Returns
true on success, else false

◆ list_move_item_pos()

bool list_move_item_pos ( struct t_list l,
unsigned  from,
unsigned  to 
)

Moves a node in the list to another position.

Parameters
llist
fromfrom pos
toto pos
Returns
true on success, else false

◆ list_new()

struct t_list * list_new ( void  )

Mallocs a new list and inits it.

Returns
allocated empty list

◆ list_node_at()

struct t_list_node * list_node_at ( const struct t_list l,
unsigned  idx 
)

Gets the list node at idx

Parameters
llist
idxnode index to get
Returns
pointer to list node

◆ list_node_extract()

struct t_list_node * list_node_extract ( struct t_list l,
unsigned  idx 
)

Removes the node at idx from the list and returns it.

Parameters
llist
idxnode index to remove
Returns
pointer to list node

◆ list_node_free()

void * list_node_free ( struct t_list_node n)

Frees a list node, ignoring its user_data pointer.

Parameters
nnode to free
Returns
NULL

◆ list_node_free_user_data()

void * list_node_free_user_data ( struct t_list_node n,
user_data_callback  free_cb 
)

Frees a list node and its user_data pointer

Parameters
nnode to free
free_cbcallback function to free user_data pointer
Returns
NULL

◆ list_node_prev_at()

struct t_list_node * list_node_prev_at ( const struct t_list l,
unsigned  idx,
struct t_list_node **  previous 
)

Gets the list node at idx and its previous node.

Parameters
llist
idxnode index to get
previouspointer to previous node
Returns
pointer to list node

◆ list_push()

bool list_push ( struct t_list l,
const char *  key,
int64_t  value_i,
const char *  value_p,
void *  user_data 
)

Creates and appends a node to the end of the list.

Parameters
llist
keykey value
value_iint64_t value
value_psds value
user_datapointer to user_data
Returns
true on success, else false

◆ list_push_len()

bool list_push_len ( struct t_list l,
const char *  key,
size_t  key_len,
int64_t  value_i,
const char *  value_p,
size_t  value_len,
void *  user_data 
)

Creates and appends a node to the end of the list. key and value_p len must be specified.

Parameters
llist
keykey value
key_lenkey length
value_iint64_t value
value_psds value
value_lenvalue_p length
user_datapointer to user_data
Returns
true on success, else false

◆ list_remove_node()

bool list_remove_node ( struct t_list l,
unsigned  idx 
)

Removes the node at idx from the list and frees it. Ignores the user_data pointer.

Parameters
llist
idxnode index to free
Returns
true on success, else false

◆ list_remove_node_by_key()

bool list_remove_node_by_key ( struct t_list l,
const char *  key 
)

Removes the node with key Ignores user_data pointer

Parameters
llist
keykey
Returns
bool true on success, else false

◆ list_remove_node_by_key_user_data()

bool list_remove_node_by_key_user_data ( struct t_list l,
const char *  key,
user_data_callback  free_cb 
)

Removes the node with key

Parameters
llist
keykey
free_cbCallback to free the user data
Returns
bool true on success, else false

◆ list_remove_node_user_data()

bool list_remove_node_user_data ( struct t_list l,
unsigned  idx,
user_data_callback  free_cb 
)

Removes the node at idx from the list and frees it and frees the user_data pointer

Parameters
llist
idxnode index to free
free_cbcallback function to free user_data pointer
Returns
true on success, else false

◆ list_replace()

bool list_replace ( struct t_list l,
unsigned  idx,
const char *  key,
int64_t  value_i,
const char *  value_p,
void *  user_data 
)

Replaces a list nodes values at pos. Ignores the old user_data pointer.

Parameters
llist
idxindex of node to change
keynew key value
value_inew int64_t value
value_pnew sds value
user_datanew user_data pointer
Returns
true on success, else false

◆ list_replace_len()

bool list_replace_len ( struct t_list l,
unsigned  idx,
const char *  key,
size_t  key_len,
int64_t  value_i,
const char *  value_p,
size_t  value_len,
void *  user_data 
)

Replaces a list nodes values at pos. Ignores the old user_data pointer.

Parameters
llist
idxindex of node to change
keynew key value
key_lennew key value length
value_inew int64_t value
value_pnew sds value
value_lennew sds value len
user_datanew user_data pointer
Returns
true on success, else false

◆ list_replace_len_user_data()

bool list_replace_len_user_data ( struct t_list l,
unsigned  idx,
const char *  key,
size_t  key_len,
int64_t  value_i,
const char *  value_p,
size_t  value_len,
void *  user_data,
user_data_callback  free_cb 
)

Replaces a list nodes values at pos. Frees the old user_data pointer.

Parameters
llist
idxindex of node to change
keynew key value
key_lennew key value length
value_inew int64_t value
value_pnew sds value
value_lennew sds value len
user_datanew user_data pointer
free_cbcallback function to free old user_data pointer
Returns
true on success, else false

◆ list_replace_user_data()

bool list_replace_user_data ( struct t_list l,
unsigned  idx,
const char *  key,
int64_t  value_i,
const char *  value_p,
void *  user_data,
user_data_callback  free_cb 
)

Replaces a list nodes values at pos. Frees the old user_data pointer.

Parameters
llist
idxindex of node to change
keynew key value
value_inew int64_t value
value_pnew sds value
user_datanew user_data pointer
free_cbcallback function to free old user_data pointer
Returns
true on success, else false

◆ list_shift_first()

struct t_list_node * list_shift_first ( struct t_list l)

Removes the first node from the list and returns it. This is only a shortcut for list_node_extract.

Parameters
llist
Returns
pointer to list node

◆ list_shuffle()

bool list_shuffle ( struct t_list l)

Shuffles the list.

Parameters
llist
Returns
true on success, else false

◆ list_sort_by_callback()

bool list_sort_by_callback ( struct t_list l,
enum list_sort_direction  direction,
list_sort_callback  sort_cb 
)

The list sorting function. Note that the sort is not very efficient, use it only for short lists.

Parameters
lpointer to list to sort
directionsort direction
sort_cbcompare function
Returns
true on success, else false

◆ list_sort_by_key()

bool list_sort_by_key ( struct t_list l,
enum list_sort_direction  direction 
)

Sorts the list by key

Parameters
lpointer to list to sort
directionsort direction
Returns
true on success, else false

◆ list_sort_by_value_i()

bool list_sort_by_value_i ( struct t_list l,
enum list_sort_direction  direction 
)

Sorts the list by value_i

Parameters
lpointer to list to sort
directionsort direction
Returns
true on success, else false

◆ list_sort_by_value_p()

bool list_sort_by_value_p ( struct t_list l,
enum list_sort_direction  direction 
)

Sorts the list by value_p

Parameters
lpointer to list to sort
directionsort direction
Returns
true on success, else false

◆ list_swap_item()

bool list_swap_item ( struct t_list_node n1,
struct t_list_node n2 
)

Swaps two list nodes values.

Parameters
n1first node
n2second node
Returns
true on success, else false

◆ list_write_to_disk()

bool list_write_to_disk ( sds  filepath,
struct t_list l,
list_node_to_line_callback  node_to_line_cb 
)

Saves the list to disk

Parameters
filepathfilepath to write the list
llist to save
node_to_line_cbcallback function to write a list node
Returns
true on success, else false