hashtable.c File Reference


Detailed Description

Hashtable implementation internals.

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

hashtablecopy_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.
hashtablemake_hashtable (unsigned int(*hash)(const char *), unsigned int nbuckets)
 Create a hash table.


Define Documentation

#define NEW (  )     (T*) malloc(sizeof(T))

Allocate memory for some type.

Parameters:
T The type to allocate memory for.
Returns:
A pointer to the newly allocated storage.

Definition at line 18 of file hashtable.c.

#define NEWARRAY ( T,
 )     (T*) calloc((N), sizeof(T))

Allocate memory for an array.

Parameters:
T The type of the elements of the array.
N The number of elements in the array.
Returns:
A pointer to the array.

Definition at line 25 of file hashtable.c.


Function Documentation

struct hashtable* copy_hashtable ( struct hashtable dest,
struct hashtable src 
)

Insert all elements from one hash table into another.

Parameters:
dest The hash table to insert elements into.
src The hash table whose elements are to be inserted.
Returns:
dest

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.

Parameters:
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.

Parameters:
table The hash table the entry is to be added to.
key The key for the entry.
value The value for the entry.
Returns:
Nonzero on success, 0 on failure.

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().

Parameters:
key The key to be hashed.
Returns:
The hash for the key.

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.

Parameters:
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.

Parameters:
table The hash table in which to look up the key.
key The key to look up.
Returns:
The value belonging to the key if found, NULL if not found.

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.

Parameters:
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.
Returns:
The new hash table on success, NULL on failure.

Definition at line 169 of file hashtable.c.

References hashtable::bucket, hashtable::hash, hashtable::nbuckets, NEW, and NEWARRAY.


Generated on Mon Jul 30 14:58:22 2007 for hashtable by  doxygen 1.5.1