isbadawi / chash

Simple hash table in C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

chash

chash is a simple C implementation of a hash table. It permits only C strings as keys, and values are void pointers.

Using chash

Start by including chash.h:

#include<chash.h>

Each hash table is an instance of chash. Create one with chash_new:

chash* mytable = chash_new();

Insert elements with chash_put:

chash_put(mytable, "x", something);

Access them with chash_get:

void *something = chash_get(mytable, "x");

Check the numbers of elements with chash_size:

int size = chash_size(mytable);

Remove them with chash_del:

chash_del(mytable, "x");

Iterate over the entries using a chash_iterator:

chash_iterator iter;
chash_iterator_init(&iter, mytable);
char *key; void *value;
while (chash_iterator_next(&iter, &key, &value)) {
    /* Do something with key and value here.
       Note it is safe to call chash_del(mytable, key) here.  */
}

Free a table with chash_free:

chash_free(mytable);

A chash doesn't assume anything about the data stored within it. In particular, it won't free a pointer if it's removed (either through chash_del, chash_free or a duplicate key in chash_put). Instead, you can specify a function to call, which is set by default to a no-op. The function must have the same signature as free, e.g.

chash *mytable = chash_new();
mytable->free = free;

About

Simple hash table in C

License:MIT License


Languages

Language:C 100.0%