hashtable.h File Reference

The public interface to hash tables. More...

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

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",
         (char*) hashtable_lookup(table, "hello"));

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

  return 0;
}

Definition in file hashtable.h.


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 108 of file hashtable.c.

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.

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 93 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 133 of file hashtable.c.

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 151 of file hashtable.c.

struct hashtable* make_hashtable ( unsigned int(*)(const char *)  hash,
unsigned int  nbuckets 
) [read]

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 171 of file hashtable.c.

 All Data Structures Files Functions Variables Defines
Generated on Sun Jul 11 13:37:58 2010 for hashtable by  doxygen 1.6.3