# linkhash.h File Reference

## Data Structures

struct | lh_entry
struct | lh_table

## Defines

#define | LH_PRIME 0x9e370001UL
#define | LH_LOAD_FACTOR 0.66
#define | LH_EMPTY (void*)-1
#define | LH_FREED (void*)-2
#define | lh_foreach(table, entry) for(entry = table->head; entry; entry = entry->next)
#define | lh_foreach_safe(table, entry, tmp) for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)

## Typedefs

typedef void( | lh_entry_free_fn )(struct lh_entry *e)
typedef unsigned long( | lh_hash_fn )(const void *k)
typedef int( | lh_equal_fn )(const void *k1, const void *k2)

## Functions

unsigned long | lh_ptr_hash (const void *k)
int | lh_ptr_equal (const void *k1, const void *k2)
unsigned long | lh_char_hash (const void *k)
int | lh_char_equal (const void *k1, const void *k2)
struct lh_table * | lh_table_new (int size, const char *name, lh_entry_free_fn *free_fn, lh_hash_fn *hash_fn, lh_equal_fn *equal_fn)
struct lh_table * | lh_kchar_table_new (int size, const char *name, lh_entry_free_fn *free_fn)
struct lh_table * | lh_kptr_table_new (int size, const char *name, lh_entry_free_fn *free_fn)
void | lh_table_free (struct lh_table *t)
int | lh_table_insert (struct lh_table *t, void *k, const void *v)
struct lh_entry * | lh_table_lookup_entry (struct lh_table *t, const void *k)
const void * | lh_table_lookup (struct lh_table *t, const void *k)
json_bool | lh_table_lookup_ex (struct lh_table *t, const void *k, void **v)
int | lh_table_delete_entry (struct lh_table *t, struct lh_entry *e)
int | lh_table_delete (struct lh_table *t, const void *k)
void | lh_abort (const char *msg,...)
void | lh_table_resize (struct lh_table *t, int new_size)

## Define Documentation

#define LH_EMPTY (void*)-1

sentinel pointer value for empty slots

#define lh_foreach | ( | table,
|  | entry |  | ) | for(entry = table->head; entry; entry = entry->next)

Convenience list iterator.

#define lh_foreach_safe | ( | table,
|  | entry,
|  | tmp |  | ) | for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)

lh_foreach_safe allows calling of deletion routine while iterating.

#define LH_FREED (void*)-2

sentinel pointer value for freed slots

#define LH_LOAD_FACTOR 0.66

The fraction of filled hash buckets until an insert will cause the table to be resized. This can range from just above 0 up to 1.0.

#define LH_PRIME 0x9e370001UL

golden prime used in hash functions

## Typedef Documentation

typedef void( lh_entry_free_fn)(struct lh_entry *e)

callback function prototypes

typedef int( lh_equal_fn)(const void *k1, const void *k2)

callback function prototypes

typedef unsigned long( lh_hash_fn)(const void *k)

callback function prototypes

## Function Documentation

void lh_abort | ( | const char * | msg,
|  |  | ... |
| ) |  |  |

int lh_char_equal | ( | const void * | k1,
|  | const void * | k2 |
| ) |  |  |

unsigned long lh_char_hash | ( | const void * | k | ) |

struct lh_table* lh_kchar_table_new | ( | int | size,
|  | const char * | name,
|  | lh_entry_free_fn * | free_fn |
| ) |  |  | `[read]`

Convenience function to create a new linkhash table with char keys.

Parameters:

  | size | initial table size.
  | name | table name.
  | free_fn | callback function used to free memory for entries.

Returns:
  a pointer onto the linkhash table.

struct lh_table* lh_kptr_table_new | ( | int | size,
|  | const char * | name,
|  | lh_entry_free_fn * | free_fn |
| ) |  |  | `[read]`

Convenience function to create a new linkhash table with ptr keys.

Parameters:

  | size | initial table size.
  | name | table name.
  | free_fn | callback function used to free memory for entries.

Returns:
  a pointer onto the linkhash table.

int lh_ptr_equal | ( | const void * | k1,
|  | const void * | k2 |
| ) |  |  |

unsigned long lh_ptr_hash | ( | const void * | k | ) |

Pre-defined hash and equality functions

int lh_table_delete | ( | struct lh_table * | t,
|  | const void * | k |
| ) |  |  |

Delete a record from the table. If a callback free function is provided then it is called for the for the item being deleted.

Parameters:

  | t | the table to delete from.
  | k | a pointer to the key to delete.

Returns:
  0 if the item was deleted.
  -1 if it was not found.

int lh_table_delete_entry | ( | struct lh_table * | t,
|  | struct lh_entry * | e |
| ) |  |  |

Delete a record from the table. If a callback free function is provided then it is called for the for the item being deleted.

Parameters:

  | t | the table to delete from.
  | e | a pointer to the entry to delete.

Returns:
  0 if the item was deleted.
  -1 if it was not found.

void lh_table_free | ( | struct lh_table * | t | ) |

Free a linkhash table. If a callback free function is provided then it is called for all entries in the table.

Parameters:

  | t | table to free.

int lh_table_insert | ( | struct lh_table * | t,
|  | void * | k,
|  | const void * | v |
| ) |  |  |

Insert a record into the table.

Parameters:

  | t | the table to insert into.
  | k | a pointer to the key to insert.
  | v | a pointer to the value to insert.

const void* lh_table_lookup | ( | struct lh_table * | t,
|  | const void * | k |
| ) |  |  |

Lookup a record into the table

Parameters:

  | t | the table to lookup
  | k | a pointer to the key to lookup

Returns:
  a pointer to the found value or NULL if it does not exist.

Deprecated:
  Use lh_table_lookup_ex instead.

struct lh_entry* lh_table_lookup_entry | ( | struct lh_table * | t,
|  | const void * | k |
| ) |  |  | `[read]`

Lookup a record into the table.

Parameters:

  | t | the table to lookup
  | k | a pointer to the key to lookup

Returns:
  a pointer to the record structure of the value or NULL if it does not exist.

json_bool lh_table_lookup_ex | ( | struct lh_table * | t,
|  | const void * | k,
|  | void ** | v |
| ) |  |  |

Lookup a record in the table

Parameters:

  | t | the table to lookup
  | k | a pointer to the key to lookup
  | v | a pointer to a where to store the found value (set to NULL if it doesn't exist).

Returns:
  whether or not the key was found

struct lh_table* lh_table_new | ( | int | size,
|  | const char * | name,
|  | lh_entry_free_fn * | free_fn,
|  | lh_hash_fn * | hash_fn,
|  | lh_equal_fn * | equal_fn |
| ) |  |  | `[read]`

Create a new linkhash table.

Parameters:

  | size | initial table size. The table is automatically resized although this incurs a performance penalty.
  | name | the table name.
  | free_fn | callback function used to free memory for entries when lh_table_free or lh_table_delete is called. If NULL is provided, then memory for keys and values must be freed by the caller.
  | hash_fn | function used to hash keys. 2 standard ones are defined: lh_ptr_hash and lh_char_hash for hashing pointer values and C strings respectively.
  | equal_fn | comparison function to compare keys. 2 standard ones defined: lh_ptr_hash and lh_char_hash for comparing pointer values and C strings respectively.

Returns:
  a pointer onto the linkhash table.

void lh_table_resize | ( | struct lh_table * | t,
|  | int | new_size |
| ) |  |  |