hashtable.h 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/****************************************************************************
 * Hash table header file	                                                *
 ****************************************************************************/

/**
 * @file hashtable.h
 * @author Celine Mercier
 * @date July 26th 2016
 * @brief Header file for hash table functions.
 */


#ifndef HASHTABLE_H_
#define HASHTABLE_H_


#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>


#define SEED (0x9747b28c)   		/**< The seed used by the hash function.
                                	 */


/**
 * @brief Structure for an entry.
 */
typedef struct entry_s {
	char*           key;		/**< Key used to refer to the entry.
	 	 	 	 	 	 	 	 */
	void*           value;		/**< Pointer on the value to be stored.
	 	 	 	 	 	 	 	 */
	struct entry_s* next;		/**< Pointer on the next entry in the bin.
	 	 	 	 	 	 	 	 */
} entry_t, *entry_p;


/**
 * @brief Structure for a hash table.
 */
typedef struct hashtable {
	size_t size;			/**< Number of bins in the table.
	 	 	 	 	 	 	 */
	entry_p* table;			/**< Table of bins.
	 	 	 	 	 	 	 */
} hashtable_t, *hashtable_p;


/**
 * @brief Creates a new hashtable.
 *
 * @param size The number of bins in the hash table.
 *
 * @returns A pointer to the newly created hash table.
 * @retval NULL if an error occurred.
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
hashtable_p ht_create(size_t size);


/**
 * @brief Inserts a new entry in the hash table.
 * 		  If the key is already in the table, the value is replaced by the new one.
 *
 * @param hashtable A pointer on the hash table structure.
 * @param key The key.
 * @param value A pointer on the value associated with the key.
 *
 * @retval 0 if the entry was correctly set.
 * @retval -1 if an error occurred.
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int ht_set(hashtable_p hashtable, const char* key, void* value);


/**
 * @brief Retrieves a value from a hash table.
 *
 * @param hashtable A pointer on the hash table structure.
 * @param key The key.
 *
 * @returns A pointer on the value associated with the key.
 * @retval NULL if the key was not found.
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
void* ht_get(hashtable_p hashtable, const char* key);


/**
 * @brief Deletes an entry.
 *
 * @param hashtable A pointer on the hash table structure.
 * @param key The key.
 *
 * @retval 0 if the entry was correctly deleted.
 * @retval -1 if an error occurred.
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int ht_delete_entry(hashtable_p hashtable, const char* key);


/**
 * @brief Frees a hash table.
 *
 * @param hashtable A pointer on the hash table structure.
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
void ht_free(hashtable_p hashtable);


#endif /* HASHTABLE_H_ */