NeoMutt  2021-02-05-89-gabe350
Teaching an old dog new tricks
DOXYGEN
hash.h
Go to the documentation of this file.
1 
24 #ifndef MUTT_LIB_HASH_H
25 #define MUTT_LIB_HASH_H
26 
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 
34 union HashKey
35 {
36  const char *strkey;
37  unsigned int intkey;
38 };
39 
43 struct HashElem
44 {
45  int type;
46  union HashKey key;
47  void *data;
48  struct HashElem *next;
49 };
50 
57 typedef void (*hash_hdata_free_t)(int type, void *obj, intptr_t data);
58 
67 typedef size_t (*hash_gen_hash_t)(union HashKey key, size_t num_elems);
68 
79 typedef int (*hash_cmp_key_t)(union HashKey a, union HashKey b);
80 
84 struct HashTable
85 {
86  size_t num_elems;
87  bool strdup_keys : 1;
88  bool allow_dups : 1;
89  struct HashElem **table;
92  intptr_t hdata;
94 };
95 
96 typedef uint8_t HashFlags;
97 #define MUTT_HASH_NO_FLAGS 0
98 #define MUTT_HASH_STRCASECMP (1 << 0)
99 #define MUTT_HASH_STRDUP_KEYS (1 << 1)
100 #define MUTT_HASH_ALLOW_DUPS (1 << 2)
101 
102 void mutt_hash_delete (struct HashTable *table, const char *strkey, const void *data);
103 struct HashElem * mutt_hash_find_bucket (const struct HashTable *table, const char *strkey);
104 void * mutt_hash_find (const struct HashTable *table, const char *strkey);
105 struct HashElem * mutt_hash_find_elem (const struct HashTable *table, const char *strkey);
106 void mutt_hash_free (struct HashTable **ptr);
107 struct HashElem * mutt_hash_insert (struct HashTable *table, const char *strkey, void *data);
108 void mutt_hash_int_delete (struct HashTable *table, unsigned int intkey, const void *data);
109 void * mutt_hash_int_find (const struct HashTable *table, unsigned int intkey);
110 struct HashElem * mutt_hash_int_insert (struct HashTable *table, unsigned int intkey, void *data);
111 struct HashTable *mutt_hash_int_new (size_t num_elems, HashFlags flags);
112 struct HashTable *mutt_hash_new (size_t num_elems, HashFlags flags);
113 void mutt_hash_set_destructor(struct HashTable *table, hash_hdata_free_t fn, intptr_t fn_data);
114 struct HashElem * mutt_hash_typed_insert (struct HashTable *table, const char *strkey, int type, void *data);
115 
120 {
121  size_t index;
122  struct HashElem *last;
123 };
124 
125 struct HashElem *mutt_hash_walk(const struct HashTable *table, struct HashWalkState *state);
126 
127 #endif /* MUTT_LIB_HASH_H */
mutt_hash_find_bucket
struct HashElem * mutt_hash_find_bucket(const struct HashTable *table, const char *strkey)
Find the HashElem in a Hash Table element using a key.
Definition: hash.c:401
hash_hdata_free_t
void(* hash_hdata_free_t)(int type, void *obj, intptr_t data)
Prototype for Hash Destructor callback function.
Definition: hash.h:57
HashKey::intkey
unsigned int intkey
Integer key.
Definition: hash.h:37
HashTable::num_elems
size_t num_elems
Number of elements in the Hash Table.
Definition: hash.h:86
HashElem::key
union HashKey key
Key representing the data.
Definition: hash.h:46
HashFlags
uint8_t HashFlags
Flags for mutt_hash_new(), e.g. MUTT_HASH_STRCASECMP.
Definition: hash.h:96
HashTable::gen_hash
hash_gen_hash_t gen_hash
Function to generate hash id from the key.
Definition: hash.h:90
HashTable::strdup_keys
bool strdup_keys
if set, the key->strkey is strdup()'d
Definition: hash.h:87
HashElem::data
void * data
User-supplied data.
Definition: hash.h:47
HashElem::next
struct HashElem * next
Linked List.
Definition: hash.h:48
mutt_hash_find_elem
struct HashElem * mutt_hash_find_elem(const struct HashTable *table, const char *strkey)
Find the HashElem in a Hash Table element using a key.
Definition: hash.c:369
HashTable::table
struct HashElem ** table
Array of Hash keys.
Definition: hash.h:89
mutt_hash_delete
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:419
HashTable::allow_dups
bool allow_dups
if set, duplicate keys are allowed
Definition: hash.h:88
HashTable::hdata
intptr_t hdata
Data to pass to the hdata_free() function.
Definition: hash.h:92
mutt_hash_free
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:447
HashKey::strkey
const char * strkey
String key.
Definition: hash.h:36
HashKey
The data item stored in a HashElem.
Definition: hash.h:34
HashTable::hdata_free
hash_hdata_free_t hdata_free
Function to free a Hash element.
Definition: hash.h:93
mutt_hash_new
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:251
mutt_hash_int_new
struct HashTable * mutt_hash_int_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with integer keys)
Definition: hash.c:277
mutt_hash_set_destructor
void mutt_hash_set_destructor(struct HashTable *table, hash_hdata_free_t fn, intptr_t fn_data)
Set the destructor for a Hash Table.
Definition: hash.c:293
hash_gen_hash_t
size_t(* hash_gen_hash_t)(union HashKey key, size_t num_elems)
Prototype for a Key hashing function.
Definition: hash.h:67
HashElem::type
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
mutt_hash_typed_insert
struct HashElem * mutt_hash_typed_insert(struct HashTable *table, const char *strkey, int type, void *data)
Insert a string with type info into a Hash Table.
Definition: hash.c:309
HashWalkState
Cursor to iterate through a Hash Table.
Definition: hash.h:119
HashWalkState::index
size_t index
Current position in table.
Definition: hash.h:121
HashTable::cmp_key
hash_cmp_key_t cmp_key
Function to compare two Hash keys.
Definition: hash.h:91
HashElem
The item stored in a Hash Table.
Definition: hash.h:43
hash_cmp_key_t
int(* hash_cmp_key_t)(union HashKey a, union HashKey b)
Prototype for a function to compare two Hash keys.
Definition: hash.h:79
mutt_hash_int_find
void * mutt_hash_int_find(const struct HashTable *table, unsigned int intkey)
Find the HashElem data in a Hash Table element using a key.
Definition: hash.c:384
mutt_hash_find
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition: hash.c:354
HashTable
A Hash Table.
Definition: hash.h:84
mutt_hash_insert
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:327
mutt_hash_int_delete
void mutt_hash_int_delete(struct HashTable *table, unsigned int intkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:434
mutt_hash_int_insert
struct HashElem * mutt_hash_int_insert(struct HashTable *table, unsigned int intkey, void *data)
Add a new element to the Hash Table (with integer keys)
Definition: hash.c:339
mutt_hash_walk
struct HashElem * mutt_hash_walk(const struct HashTable *table, struct HashWalkState *state)
Iterate through all the HashElem's in a Hash Table.
Definition: hash.c:479
HashWalkState::last
struct HashElem * last
Current element in linked list.
Definition: hash.h:122