2 * This is an example of using htable.
11 * This is a table item, which is keyed on a 16-byte value.
20 * Hash function for the Counter keys. Note that this is an
21 * illsutration example only and there is no analysis of it being
24 static int Counter_hashcode( struct _htable *table, unsigned char *key ) {
27 unsigned char *end = key + 16;
28 unsigned char *p = key;
30 value += *(p++) + (i--);
36 * A hash table of Counter records using Counter_hashcode
38 static htable counters = HTABLEINIT( struct Counter, key, Counter_hashcode );
40 #define MIN( a, b ) ({ int ax = a, bx = b; ( ax < bx )? ax : bx; })
43 * setup a temporary struct Counter, and return a usefully cast
46 static struct Counter *Counter_create(char *key) {
47 struct Counter *counter = (struct Counter *)
48 calloc( 1, sizeof( struct Counter ) );
49 memcpy( counter->key, key, MIN( strlen( key ), sizeof( counter->key ) ) );
53 static void Counter_dumpitem(int i,unsigned char *itemp) {
54 struct Counter *item = (struct Counter *) itemp;
55 fprintf(stdout, "[%03d] %16s %d %p\n",
56 i, item->key, item->value, item->unused );
59 int main(int argc,char **argv) {
60 static char *keys[] = {
61 "something", "intro", "foo bar", "hexadecimal", "burp", 0 };
64 for ( i = 0; keys[ i ]; i++ ) {
65 struct Counter *item = Counter_create( keys[ i ] );
68 htadd( &counters, (unsigned char *)item );
70 fprintf( stdout, "table size = %d\n", counters.size );
71 htdump( &counters, Counter_dumpitem );