NeoMutt  2019-12-07
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  const char *strkey;
36  unsigned int intkey;
37 };
38 
42 struct HashElem
43 {
44  int type;
45  union HashKey key;
46  void *data;
47  struct HashElem *next;
48 };
49 
56 typedef void (*hashelem_free_t)(int type, void *obj, intptr_t data);
57 
61 struct Hash
62 {
63  size_t nelem;
64  bool strdup_keys : 1;
65  bool allow_dups : 1;
66  struct HashElem **table;
67  size_t (*gen_hash)(union HashKey, size_t);
68  int (*cmp_key)(union HashKey, union HashKey);
69  intptr_t hdata;
71 };
72 
73 typedef uint8_t HashFlags;
74 #define MUTT_HASH_NO_FLAGS 0
75 #define MUTT_HASH_STRCASECMP (1 << 0)
76 #define MUTT_HASH_STRDUP_KEYS (1 << 1)
77 #define MUTT_HASH_ALLOW_DUPS (1 << 2)
78 
79 void mutt_hash_delete(struct Hash *table, const char *strkey, const void *data);
80 struct HashElem *mutt_hash_find_bucket(const struct Hash *table, const char *strkey);
81 void * mutt_hash_find(const struct Hash *table, const char *strkey);
82 struct HashElem *mutt_hash_find_elem(const struct Hash *table, const char *strkey);
83 void mutt_hash_free(struct Hash **ptr);
84 struct HashElem *mutt_hash_insert(struct Hash *table, const char *strkey, void *data);
85 void mutt_hash_int_delete(struct Hash *table, unsigned int intkey, const void *data);
86 void * mutt_hash_int_find(const struct Hash *table, unsigned int intkey);
87 struct HashElem *mutt_hash_int_insert(struct Hash *table, unsigned int intkey, void *data);
88 struct Hash * mutt_hash_int_new(size_t nelem, HashFlags flags);
89 struct Hash * mutt_hash_new(size_t nelem, HashFlags flags);
90 void mutt_hash_set_destructor(struct Hash *table, hashelem_free_t fn, intptr_t fn_data);
91 struct HashElem *mutt_hash_typed_insert(struct Hash *table, const char *strkey, int type, void *data);
92 
97 {
98  size_t index;
99  struct HashElem *last;
100 };
101 
102 struct HashElem *mutt_hash_walk(const struct Hash *table, struct HashWalkState *state);
103 
104 #endif /* MUTT_LIB_HASH_H */
struct Hash * mutt_hash_new(size_t nelem, HashFlags flags)
Create a new Hash table (with string keys)
Definition: hash.c:275
void mutt_hash_set_destructor(struct Hash *table, hashelem_free_t fn, intptr_t fn_data)
Set the destructor for a Hash Table.
Definition: hash.c:317
A Hash Table.
Definition: hash.h:61
hashelem_free_t free_hdata
Function to free a Hash element.
Definition: hash.h:70
void(* hashelem_free_t)(int type, void *obj, intptr_t data)
typedef hashelem_free_t - Prototype for Hash Destructor callback function
Definition: hash.h:56
Cursor to iterate through a Hash Table.
Definition: hash.h:96
void mutt_hash_int_delete(struct Hash *table, unsigned int intkey, const void *data)
Remove an element from a Hash table.
Definition: hash.c:458
struct HashElem * mutt_hash_find_bucket(const struct Hash *table, const char *strkey)
Find the HashElem in a Hash table element using a key.
Definition: hash.c:425
intptr_t hdata
Data to pass to the free_hdata() function.
Definition: hash.h:69
void mutt_hash_free(struct Hash **ptr)
Free a hash table.
Definition: hash.c:471
struct HashElem * mutt_hash_insert(struct Hash *table, const char *strkey, void *data)
Add a new element to the Hash table (with string keys)
Definition: hash.c:351
struct HashElem * mutt_hash_walk(const struct Hash *table, struct HashWalkState *state)
Iterate through all the HashElem&#39;s in a Hash table.
Definition: hash.c:503
struct HashElem * next
Definition: hash.h:47
struct HashElem * last
Definition: hash.h:99
struct HashElem * mutt_hash_int_insert(struct Hash *table, unsigned int intkey, void *data)
Add a new element to the Hash table (with integer keys)
Definition: hash.c:363
unsigned int intkey
Definition: hash.h:36
struct Hash * mutt_hash_int_new(size_t nelem, HashFlags flags)
Create a new Hash table (with integer keys)
Definition: hash.c:301
struct HashElem ** table
Array of Hash keys.
Definition: hash.h:66
size_t nelem
Number of elements in the Hash table.
Definition: hash.h:63
void * mutt_hash_find(const struct Hash *table, const char *strkey)
Find the HashElem data in a Hash table element using a key.
Definition: hash.c:378
void mutt_hash_delete(struct Hash *table, const char *strkey, const void *data)
Remove an element from a Hash table.
Definition: hash.c:443
bool allow_dups
if set, duplicate keys are allowed
Definition: hash.h:65
size_t index
Definition: hash.h:98
void * data
Definition: hash.h:46
void * mutt_hash_int_find(const struct Hash *table, unsigned int intkey)
Find the HashElem data in a Hash table element using a key.
Definition: hash.c:408
struct HashElem * mutt_hash_typed_insert(struct Hash *table, const char *strkey, int type, void *data)
Insert a string with type info into a Hash Table.
Definition: hash.c:333
const char * strkey
Definition: hash.h:35
int type
Definition: hash.h:44
bool strdup_keys
if set, the key->strkey is strdup&#39;ed
Definition: hash.h:64
uint8_t HashFlags
Flags for mutt_hash_new(), e.g. MUTT_HASH_STRCASECMP.
Definition: hash.h:73
struct HashElem * mutt_hash_find_elem(const struct Hash *table, const char *strkey)
Find the HashElem in a Hash table element using a key.
Definition: hash.c:393
The item stored in a Hash Table.
Definition: hash.h:42
The data item stored in a HashElem.
Definition: hash.h:34