| 1 | |
| 2 | /* Author : Stephen Smalley, <[email protected]> */ |
| 3 | |
| 4 | /* FLASK */ |
| 5 | |
| 6 | /* |
| 7 | * A hash table (hashtab) maintains associations between |
| 8 | * key values and datum values. The type of the key values |
| 9 | * and the type of the datum values is arbitrary. The |
| 10 | * functions for hash computation and key comparison are |
| 11 | * provided by the creator of the table. |
| 12 | */ |
| 13 | |
| 14 | #ifndef _NEWROLE_HASHTAB_H_ |
| 15 | #define _NEWROLE_HASHTAB_H_ |
| 16 | |
| 17 | #include <stdint.h> |
| 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | typedef char *hashtab_key_t; /* generic key type */ |
| 22 | typedef const char *const_hashtab_key_t; /* constant generic key type */ |
| 23 | typedef void *hashtab_datum_t; /* generic datum type */ |
| 24 | |
| 25 | typedef struct hashtab_node *hashtab_ptr_t; |
| 26 | |
| 27 | typedef struct hashtab_node { |
| 28 | hashtab_key_t key; |
| 29 | hashtab_datum_t datum; |
| 30 | hashtab_ptr_t next; |
| 31 | } hashtab_node_t; |
| 32 | |
| 33 | typedef struct hashtab_val { |
| 34 | hashtab_ptr_t *htable; /* hash table */ |
| 35 | unsigned int size; /* number of slots in hash table */ |
| 36 | uint32_t nel; /* number of elements in hash table */ |
| 37 | unsigned int (*hash_value) (struct hashtab_val * h, const_hashtab_key_t key); /* hash function */ |
| 38 | int (*keycmp) (struct hashtab_val * h, const_hashtab_key_t key1, const_hashtab_key_t key2); /* key comparison function */ |
| 39 | } hashtab_val_t; |
| 40 | |
| 41 | typedef hashtab_val_t *hashtab_t; |
| 42 | |
| 43 | /* Define status codes for hash table functions */ |
| 44 | #define HASHTAB_SUCCESS 0 |
| 45 | #define HASHTAB_OVERFLOW -ENOMEM |
| 46 | #define HASHTAB_PRESENT -EEXIST |
| 47 | #define HASHTAB_MISSING -ENOENT |
| 48 | |
| 49 | /* |
| 50 | Creates a new hash table with the specified characteristics. |
| 51 | |
| 52 | Returns NULL if insufficent space is available or |
| 53 | the new hash table otherwise. |
| 54 | */ |
| 55 | extern hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, |
| 56 | const_hashtab_key_t |
| 57 | key), |
| 58 | int (*keycmp) (hashtab_t h, |
| 59 | const_hashtab_key_t key1, |
| 60 | const_hashtab_key_t key2), |
| 61 | unsigned int size); |
| 62 | /* |
| 63 | Inserts the specified (key, datum) pair into the specified hash table. |
| 64 | |
| 65 | Returns HASHTAB_OVERFLOW if insufficient space is available or |
| 66 | HASHTAB_PRESENT if there is already an entry with the same key or |
| 67 | HASHTAB_SUCCESS otherwise. |
| 68 | */ |
| 69 | extern int hashtab_insert(hashtab_t h, hashtab_key_t k, hashtab_datum_t d); |
| 70 | |
| 71 | /* |
| 72 | Removes the entry with the specified key from the hash table. |
| 73 | Applies the specified destroy function to (key,datum,args) for |
| 74 | the entry. |
| 75 | |
| 76 | Returns HASHTAB_MISSING if no entry has the specified key or |
| 77 | HASHTAB_SUCCESS otherwise. |
| 78 | */ |
| 79 | extern int hashtab_remove(hashtab_t h, hashtab_key_t k, |
| 80 | void (*destroy) (hashtab_key_t k, |
| 81 | hashtab_datum_t d, |
| 82 | void *args), void *args); |
| 83 | |
| 84 | /* |
| 85 | Insert or replace the specified (key, datum) pair in the specified |
| 86 | hash table. If an entry for the specified key already exists, |
| 87 | then the specified destroy function is applied to (key,datum,args) |
| 88 | for the entry prior to replacing the entry's contents. |
| 89 | |
| 90 | Returns HASHTAB_OVERFLOW if insufficient space is available or |
| 91 | HASHTAB_SUCCESS otherwise. |
| 92 | */ |
| 93 | extern int hashtab_replace(hashtab_t h, hashtab_key_t k, hashtab_datum_t d, |
| 94 | void (*destroy) (hashtab_key_t k, |
| 95 | hashtab_datum_t d, |
| 96 | void *args), void *args); |
| 97 | |
| 98 | /* |
| 99 | Searches for the entry with the specified key in the hash table. |
| 100 | |
| 101 | Returns NULL if no entry has the specified key or |
| 102 | the datum of the entry otherwise. |
| 103 | */ |
| 104 | extern hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t k); |
| 105 | |
| 106 | /* |
| 107 | Destroys the specified hash table. |
| 108 | */ |
| 109 | extern void hashtab_destroy(hashtab_t h); |
| 110 | |
| 111 | /* |
| 112 | Applies the specified apply function to (key,datum,args) |
| 113 | for each entry in the specified hash table. |
| 114 | |
| 115 | The order in which the function is applied to the entries |
| 116 | is dependent upon the internal structure of the hash table. |
| 117 | |
| 118 | If apply returns a non-zero status, then hashtab_map will cease |
| 119 | iterating through the hash table and will propagate the error |
| 120 | return to its caller. |
| 121 | */ |
| 122 | extern int hashtab_map(hashtab_t h, |
| 123 | int (*apply) (hashtab_key_t k, |
| 124 | hashtab_datum_t d, |
| 125 | void *args), void *args); |
| 126 | |
| 127 | /* |
| 128 | Same as hashtab_map, except that if apply returns a non-zero status, |
| 129 | then the (key,datum) pair will be removed from the hashtab and the |
| 130 | destroy function will be applied to (key,datum,args). |
| 131 | */ |
| 132 | extern void hashtab_map_remove_on_error(hashtab_t h, |
| 133 | int (*apply) (hashtab_key_t k, |
| 134 | hashtab_datum_t d, |
| 135 | void *args), |
| 136 | void (*destroy) (hashtab_key_t k, |
| 137 | hashtab_datum_t d, |
| 138 | void *args), |
| 139 | void *args); |
| 140 | |
| 141 | extern void hashtab_hash_eval(hashtab_t h, char *tag); |
| 142 | |
| 143 | #endif |