man rhash (Fonctions bibliothèques) - Roy hash table
NAME
rhash - Roy hash table
SYNOPSIS
#include <roy.h>
DESCRIPTION
A memory and speed efficent hash table implementation designed for small as well as a large number of entries. The entries are defined by the programmer, as well as allocated by the programmer prior to insertion into the hash table.
Hash tables are an excellent choice for storing/retrieving by key as the time to perform insert and lookups is nearly linear regardless of the number of entries. The RHash can perform a lookup as fast as the average lookup of a linked list with 3 entries, or a binary tree with 7 entries, and the RHash structure uses only 20 bytes when empty (on 32 bit platforms).
- The User Defined Entry
-
In order to use this hash table, you must create your own structure
to be used as the entries in the hash table. This structure *MUST*
start with the first entry as 'RHASH_HEADER;'.
struct FooEntry { RHASH_HEADER; char *foodata; /* ... */ };
You can create the toplevel RHash structure by using rhash_new(3) (free with rhash_free(3)), or you can allocate the hash structure yourself, and call RHASH_INIT(hash) on it. This initializes hash to be totally empty. - RHASH_INIT (hash)
-
Initialize a newly created hash table. The 'hash' argument
needs to be an RHash struct.
{ RHash hash;
RHASH_INIT (&hash); /* ... rhash functions ... */ }
- RHASH_DESTRUCT (hash)
-
Free up the table pointing to the entries and reset the hash table completely
empty.
{ RHash hash;
RHASH_INIT (&hash); /* ... */ RHASH_DESTRUCT (&hash); }
- RHASH_FOREACH (hash, entry)
-
This macro walks through a hash, assigning 'entry' to a hash entry
through each iteration. Caution while the programmer can use CWbreak,
CWcontinue, or CWgoto safely, the order of the entries is not defined.
No rhash function/macro save rhash_remove_entry(3) can be called safely inside
the RHASH_FOREACH macro. Do not use, rhash_remove(3) in place of
rhash_remove_entry(3).
RHASH_FOREACH (hash, entry) { if (entry->foodata == NULL) break; printf ("Data: %s.\n", entry->foodata); } RFOREACH_CLOSE;
/* Freeing entries out of a hashtable. */ RHASH_FOREACH (hash, entry) { rhash_remove_entry (hash, entry); rmem_free (entry); } RFOREACH_CLOSE;
/* Now free up the hashtable. */ rhash_free (hash);
SEE ALSO
rhash_entry_getkey(3), rhash_entry_setkey(3), rhash_free(3), rhash_insert(3), rhash_len(3), rhash_lookup(3), rhash_lookup_next(3), rhash_new(3), rhash_remove(3), rhash_remove_entry(3), roy(3)