Add more comments and a new comparison function.

This commit is contained in:
Danila Fedorin 2016-12-28 19:29:51 -08:00
parent a4d41361dd
commit 936b2835b2
5 changed files with 69 additions and 4 deletions

View File

@ -11,8 +11,17 @@
* data as to avoid conflicting hash functions.
*/
struct ht_node_s {
/**
* A copy of the key used to store this node's data.
*/
void* key;
/**
* The data stored in this node.
*/
void* data;
/**
* The next linked list node with the same hash.
*/
struct ht_node_s* next;
};
@ -27,11 +36,35 @@ struct ht_node_s {
* hash table to know something went wrong.
*/
struct ht_s {
/**
* The buckets in this hash table.
*/
struct ht_node_s* data[LIBDS_HT_SIZE];
unsigned int (*hash_func)(void*);
int (*cmp_func)(void*, void*);
void* (*copy_func)(void*);
void (*free_func)(void*);
/**
* Hash function used to store data.
* @param input the data being hashed.
* @return the data's hash.
*/
unsigned int (*hash_func)(void* input);
/**
* Function to compare two keys.
* @param given_key the key being used to retrieve data
* @param current_key the key of a hash table node
* @return nonzero value if the keys match.
*/
int (*cmp_func)(void* given_key, void* current_key);
/**
* Function used to duplicate the given key and
* store it in a node.
* @param key the key to copy
* @return pointer to a new key.
*/
void* (*copy_func)(void* key);
/**
* Function used to free a previously copied key.
* @param key the key to free.
*/
void (*free_func)(void* key);
};
typedef struct ht_node_s ht_node;

View File

@ -30,5 +30,12 @@ typedef int (*foreach_func)(void*, va_list);
* @return always true, i.e 1
*/
int compare_always(void* a, void* b);
/**
* A compare_func implementation that compares two pointers using ==.
* @param a the first piece of data being compared
* @param b the second piece of data being compared
* @return true if a and b are the same value.
*/
int compare_pointer(void* a, void* b);
#endif

View File

@ -7,8 +7,17 @@
* A simple linked list node, doubly-linked.
*/
struct ll_node_s {
/**
* The next node in the linked list
*/
struct ll_node_s* next;
/**
* The previous node in the linked list.
*/
struct ll_node_s* prev;
/**
* The data of the linked list node.
*/
void* data;
};
@ -16,7 +25,13 @@ struct ll_node_s {
* A linked list struct, containing pointers to the head and tail node.
*/
struct ll_s {
/**
* The head of the linked list.
*/
struct ll_node_s* head;
/**
* The tail of the linked list.
*/
struct ll_node_s* tail;
};

View File

@ -11,8 +11,17 @@
* Whenever the size limit is reached, the vector allocates a new array that is two times bigger.
*/
struct vec_s {
/**
* The data array of the vector.
*/
void* data;
/**
* The maximum size of the vector.
*/
int capacity;
/**
* The current number of items in the vector.
*/
int size;
};

View File

@ -1,3 +1,4 @@
#include "libds.h"
int compare_always(void* a, void* b) { return 1; }
int compare_pointer(void* a, void* b) { return a == b; }