5d085b033cd6dc2695e094e5909d3bd7ccd72695
[ghc.git] / rts / Hash.h
1 /*-----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1999
4 *
5 * Prototypes for Hash.c
6 *
7 * -------------------------------------------------------------------------- */
8
9 #ifndef HASH_H
10 #define HASH_H
11
12 #include "BeginPrivate.h"
13
14 typedef struct hashtable HashTable; /* abstract */
15
16 /* Hash table access where the keys are StgWords.
17 * Values are passed into the hash table and stored as `const void *` values,
18 * but when the value is looked up or removed, the value is returned without the
19 * `const` so that calling function can mutate what the pointer points to if it
20 * needs to.
21 */
22 HashTable * allocHashTable ( void );
23 void insertHashTable ( HashTable *table, StgWord key, const void *data );
24 void * lookupHashTable ( const HashTable *table, StgWord key );
25 void * removeHashTable ( HashTable *table, StgWord key, const void *data );
26
27 int keyCountHashTable (HashTable *table);
28
29 // Puts up to szKeys keys of the hash table into the given array. Returns the
30 // actual amount of keys that have been retrieved.
31 //
32 // If the table is modified concurrently, the function behavior is undefined.
33 //
34 int keysHashTable(HashTable *table, StgWord keys[], int szKeys);
35
36 typedef void (*MapHashFn)(void *data, StgWord key, const void *value);
37
38 void mapHashTable(HashTable *table, void *data, MapHashFn fn);
39
40 /* Hash table access where the keys are C strings (the strings are
41 * assumed to be allocated by the caller, and mustn't be deallocated
42 * until the corresponding hash table entry has been removed).
43 */
44 HashTable * allocStrHashTable ( void );
45
46 #define lookupStrHashTable(table, key) \
47 (lookupHashTable(table, (StgWord)key))
48
49 #define insertStrHashTable(table, key, data) \
50 (insertHashTable(table, (StgWord)key, data))
51
52 #define removeStrHashTable(table, key, data) \
53 (removeHashTable(table, (StgWord)key, data))
54
55 /* Hash tables for arbitrary keys */
56 typedef int HashFunction(const HashTable *table, StgWord key);
57 typedef int CompareFunction(StgWord key1, StgWord key2);
58 HashTable * allocHashTable_(HashFunction *hash, CompareFunction *compare);
59 int hashWord(const HashTable *table, StgWord key);
60 int hashStr(const HashTable *table, char *key);
61
62 /* Freeing hash tables
63 */
64 void freeHashTable ( HashTable *table, void (*freeDataFun)(void *) );
65
66 void exitHashTable ( void );
67
68 #include "EndPrivate.h"
69
70 #endif /* HASH_H */