1 /*
2  * Copyright 1993, 1995 Christopher Seiwald.
3  *
4  * This file is part of Jam - see jam.c for Copyright information.
5  */
6 
7 /*
8  * hash.h - simple in-memory hashing routines
9  */
10 
11 #ifndef BOOST_JAM_HASH_H
12 #define BOOST_JAM_HASH_H
13 
14 #include "config.h"
15 #include "object.h"
16 
17 /*
18  * An opaque struct representing an item in the hash table. The first element of
19  * every struct stored in the table must be an OBJECT * which is treated as the
20  * key.
21  */
22 typedef struct hashdata HASHDATA;
23 
24 /*
25  * hashinit() - initialize a hash table, returning a handle.
26  *
27  * Parameters:
28  *   datalen - item size
29  *   name    - used for debugging
30  */
31 struct hash * hashinit( int datalen, char const * name );
32 
33 /*
34  * hash_free() - free a hash table, given its handle
35  */
36 void hash_free( struct hash * );
37 void hashdone( struct hash * );
38 
39 /*
40  * hashenumerate() - call f(i, data) on each item, i in the hash table. The
41  * enumeration order is unspecified.
42  */
43 void hashenumerate( struct hash *, void (* f)( void *, void * ), void * data );
44 
45 /*
46  * hash_insert() - insert a new item in a hash table, or return an existing one.
47  *
48  * Preconditions:
49  *   - hp must be a hash table created by hashinit()
50  *   - key must be an object created by object_new()
51  *
52  * Postconditions:
53  *   - if the key does not already exist in the hash table, *found == 0 and the
54  *     result will be a pointer to an uninitialized item. The key of the new
55  *     item must be set to a value equal to key before any further operations on
56  *     the hash table except hashdone().
57  *   - if the key is present then *found == 1 and the result is a pointer to the
58  *     existing record.
59  */
60 HASHDATA * hash_insert( struct hash *, OBJECT * key, int * found );
61 
62 /*
63  * hash_find() - find a record in the table or NULL if none exists
64  */
65 HASHDATA * hash_find( struct hash *, OBJECT * key );
66 
67 struct hashstats {
68     int count;
69     int num_items;
70     int tab_size;
71     int item_size;
72     int sets;
73     int num_hashes;
74 };
75 
76 void hashstats_init( struct hashstats * stats );
77 void hashstats_add( struct hashstats * stats, struct hash * );
78 void hashstats_print( struct hashstats * stats, char const * name );
79 
80 #endif
81