ds_*ll_insert -> ds_*ll_append to better reflect what it does and there is now ds_*ll_add to add data anywhere in the ll.
171 lines
3.8 KiB
C
171 lines
3.8 KiB
C
#ifndef DS_H
|
|
#define DS_H
|
|
|
|
typedef struct _ds_sll {
|
|
struct _ds_sll *next;
|
|
void *data;
|
|
} ds_sll_t;
|
|
|
|
typedef struct _ds_dll {
|
|
struct _ds_dll *next;
|
|
struct _ds_dll *prev;
|
|
void *data;
|
|
} ds_dll_t;
|
|
|
|
typedef struct {
|
|
ds_sll_t **data;
|
|
unsigned data_len;
|
|
} ds_hmp_t;
|
|
|
|
typedef struct {
|
|
char *key;
|
|
void *val;
|
|
} _ds_hmp_kv_t;
|
|
|
|
#define ds_ll_foreach(t, ll) for (t *cur = ll; cur; cur = cur->next)
|
|
|
|
/**
|
|
* @brief initialize a new singly linked list
|
|
*
|
|
* @return the new linked list
|
|
*/
|
|
ds_sll_t *ds_sll_init(void);
|
|
|
|
/**
|
|
* @brief initialize a new doubly linked list
|
|
*
|
|
* @return the new linked list
|
|
*/
|
|
ds_dll_t *ds_dll_init(void);
|
|
|
|
/**
|
|
* @brief create a new allocated node for a singly linked list
|
|
*
|
|
* @param data the data you want to store
|
|
* @return the node
|
|
*/
|
|
ds_sll_t *ds_sll_new_node(void *data);
|
|
|
|
/**
|
|
* @brief create a new allocated node for a doubly linked list
|
|
*
|
|
* @param data the data you want to store
|
|
* @return the node
|
|
*/
|
|
ds_dll_t *ds_dll_new_node(void *data);
|
|
|
|
/**
|
|
* @brief data to add at x of a singly linked list. This will use
|
|
* ds_sll_new_node to create the node for you.
|
|
*
|
|
* @param ll singly linked list
|
|
* @param data data you want to add
|
|
* @param x the position to add it at
|
|
* @return 0 on success
|
|
*/
|
|
int ds_sll_add(ds_sll_t **ll, void *data, unsigned x);
|
|
|
|
/**
|
|
* @brief data to add at x of a doubly linked list. This will use
|
|
* ds_dll_new_node to create the node for you.
|
|
*
|
|
* @param ll doubly linked list
|
|
* @param data data you want to add
|
|
* @param x the position to add it at
|
|
* @return 0 on success
|
|
*/
|
|
int ds_dll_add(ds_dll_t **ll, void *data, unsigned x);
|
|
|
|
/**
|
|
* @brief data to append to the end of a singly linked list. This will use
|
|
* ds_sll_new_node to create the node for you.
|
|
*
|
|
* @param ll singly linked list
|
|
* @param data data you want to append
|
|
* @return 0 on success
|
|
*/
|
|
int ds_sll_append(ds_sll_t *ll, void *data);
|
|
|
|
/**
|
|
* @brief data to append to the end of a doubly linked list. This will use
|
|
* ds_dll_new_node to create the node for you.
|
|
*
|
|
* @param ll doubly linked list
|
|
* @param data data you want to append
|
|
* @return 0 on success
|
|
*/
|
|
int ds_dll_append(ds_dll_t *ll, void *data);
|
|
|
|
/**
|
|
* @brief remove an index from a singly linked list
|
|
*
|
|
* @param ll singly linked list
|
|
* @param idx the node
|
|
* @return the data in the node
|
|
*/
|
|
void *ds_sll_remove(ds_sll_t **ll, unsigned idx);
|
|
|
|
/**
|
|
* @brief remove an index from a doubly linked list
|
|
*
|
|
* @param ll doubly linked list
|
|
* @param idx the node
|
|
* @return the data in the node
|
|
*/
|
|
void *ds_dll_remove(ds_dll_t **ll, unsigned idx);
|
|
|
|
/**
|
|
* @brief initialize a new hashmap
|
|
*
|
|
* @param data_len the amount of slots in the array
|
|
* @return the hashmap
|
|
*/
|
|
ds_hmp_t *ds_hmp_init(int data_len);
|
|
|
|
/**
|
|
* @brief free all data allocated by ds_hmp_init and ds_hmp_insert
|
|
*
|
|
* @param hmp pointer to the hashmap
|
|
* @return 0 on success
|
|
*/
|
|
int ds_hmp_free(ds_hmp_t **hmp, void kv_callback(_ds_hmp_kv_t *kv));
|
|
|
|
/**
|
|
* @brief generate a numerical hash from a given string. You shouldn't need to
|
|
* use this, but it's available just incase.
|
|
*
|
|
* @param str
|
|
* @return the hash
|
|
*/
|
|
int _ds_hmp_gen_hash(char *str);
|
|
|
|
/**
|
|
* @brief insert data at key (which will be hashed using _ds_hmp_gen_hash). You
|
|
* are responsible for freeing the key later.
|
|
*
|
|
* @param hmp the hashmap to insert into
|
|
* @param key the key
|
|
* @param data the data
|
|
* @return 0 on success
|
|
*/
|
|
int ds_hmp_insert(ds_hmp_t *hmp, char *key, void *data);
|
|
|
|
/**
|
|
* @brief get something from a hashmap using it's key
|
|
*
|
|
* @param hmp the hashmap
|
|
* @param key the key where it's stored
|
|
* @return the data stored at the key
|
|
*/
|
|
void *ds_hmp_get(ds_hmp_t *hmp, char *key);
|
|
|
|
/**
|
|
* @brief remove something from a hashmap using it's key
|
|
*
|
|
* @param hmp the hashmap
|
|
* @param key the key where it's stored
|
|
* @return the data stored at the key
|
|
*/
|
|
void *ds_hmp_remove(ds_hmp_t *hmp, char *key);
|
|
|
|
#endif
|