hashtable.h File Reference


Detailed Description

The public interface to hash tables.

This file contains the public interface to hash tables. For documentation of everything, including things meant only for internal use, see hashtable.c.

The following is a usage example of the interface defined in this file:

#include <stdio.h>
#include <hashtable.h>

int main(int argc, char **argv) {
  /* Create a hash table. */
  struct hashtable *table = make_hashtable(hashtable_default_hash, 16);

  /* Add an entry to the hash table. */
  hashtable_add(table, "hello", "world");

  /* Retrieve and print entry. */
  printf("%s -> %s\n", "hello", hashtable_lookup(table, "hello"));

  /* Free hash table. */
  free_hashtable(table);

  return 0;
}

Definition in file hashtable.h.

Go to the source code of this file.

Data Structures

struct  hashtable
 Struct representing a hash table. More...
struct  hashtable_entry
 Structure representing an entry in a hash table. More...

Functions

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.


Function Documentation

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