This file contains the full documentation of all functions, types, constants, and variables used in the hashtable implementation, including those that are intended only for internal use. For the public interface to hash tables, see hashtable.h.
Definition in file hashtable.c.
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"
Go to the source code of this file.
Defines | |
#define | NEW(T) (T*) malloc(sizeof(T)) |
Allocate memory for some type. | |
#define | NEWARRAY(T, N) (T*) calloc((N), sizeof(T)) |
Allocate memory for an array. | |
Functions | |
hashtable * | copy_hashtable (struct hashtable *dest, struct hashtable *src) |
Insert all elements from one hash table into another. | |
void | free_hashtable (struct hashtable *table) |
Free all the memory used by a hash table. | |
int | hashtable_add (struct hashtable *table, char *key, void *value) |
Add an entry to a hash table. | |
unsigned int | hashtable_default_hash (const char *key) |
The default hash function. | |
void | hashtable_iter (const struct hashtable *table, void(*func)(char *key, void *value)) |
Call a function for each entry in a hash table. | |
void * | hashtable_lookup (const struct hashtable *table, const char *key) |
Look up the value bound to a key. | |
hashtable * | make_hashtable (unsigned int(*hash)(const char *), unsigned int nbuckets) |
Create a hash table. |
#define NEW | ( | T | ) | (T*) malloc(sizeof(T)) |
Allocate memory for some type.
T | The type to allocate memory for. |
Definition at line 18 of file hashtable.c.
#define NEWARRAY | ( | T, | |||
N | ) | (T*) calloc((N), sizeof(T)) |
Allocate memory for an array.
T | The type of the elements of the array. | |
N | The number of elements in the array. |
Definition at line 25 of file hashtable.c.
Insert all elements from one hash table into another.
dest | The hash table to insert elements into. | |
src | The hash table whose elements are to be inserted. |
Definition at line 73 of file hashtable.c.
References hashtable::bucket, hashtable_add(), hashtable_entry::key, hashtable::nbuckets, hashtable_entry::next, and hashtable_entry::value.
void free_hashtable | ( | struct hashtable * | table | ) |
Free all the memory used by a hash table.
table | The hash table to be deallocated. |
Definition at line 106 of file hashtable.c.
References hashtable::bucket, hashtable::nbuckets, and hashtable_entry::next.
int hashtable_add | ( | struct hashtable * | table, | |
char * | key, | |||
void * | value | |||
) |
Add an entry to a hash table.
table | The hash table the entry is to be added to. | |
key | The key for the entry. | |
value | The value for the entry. |
Definition at line 45 of file hashtable.c.
References hashtable::bucket, hashtable_entry::key, NEW, hashtable_entry::next, and hashtable_entry::value.
unsigned int hashtable_default_hash | ( | const char * | key | ) |
The default hash function.
This function can be used as the hash
parameter to make_hash_table()
.
key | The key to be hashed. |
Definition at line 91 of file hashtable.c.
void hashtable_iter | ( | const struct hashtable * | table, | |
void(*)(char *key, void *value) | func | |||
) |
Call a function for each entry in a hash table.
table | The hash table whose entries to iterate over. | |
func | The function to call for each entry. The function receives two argument: 1. The key of the entry. 2. The value of the entry. |
Definition at line 131 of file hashtable.c.
References hashtable::bucket, hashtable_entry::key, hashtable::nbuckets, hashtable_entry::next, and hashtable_entry::value.
void* hashtable_lookup | ( | const struct hashtable * | table, | |
const char * | key | |||
) |
Look up the value bound to a key.
table | The hash table in which to look up the key. | |
key | The key to look up. |
Definition at line 149 of file hashtable.c.
References hashtable::bucket, hashtable_entry::key, hashtable_entry::next, and hashtable_entry::value.
struct hashtable* make_hashtable | ( | unsigned int(*)(const char *) | hash, | |
unsigned int | nbuckets | |||
) |
Create a hash table.
hash | The hash function to use with this hash table. The function has to map NUL-terminated strings to unsigned ints. | |
nbuckets | The number of buckets in the hash table. |
Definition at line 169 of file hashtable.c.
References hashtable::bucket, hashtable::hash, hashtable::nbuckets, NEW, and NEWARRAY.