NeoMutt  2025-01-09-117-gace867
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1
23#ifndef MUTT_MUTT_HASH_H
24#define MUTT_MUTT_HASH_H
25
26#include <stdbool.h>
27#include <stdint.h>
28#include <stdlib.h>
29#include "array.h"
30
35{
36 const char *strkey;
37 unsigned int intkey;
38};
39
44{
45 int type;
46 union HashKey key;
47 void *data;
48 struct HashElem *next;
49};
50ARRAY_HEAD(HashElemArray, struct HashElem *);
51
63typedef void (*hash_hdata_free_t)(int type, void *obj, intptr_t data);
64
76typedef size_t (*hash_gen_hash_t)(union HashKey key, size_t num_elems);
77
91typedef int (*hash_cmp_key_t)(union HashKey a, union HashKey b);
92
99{
100 size_t num_elems;
101 bool strdup_keys : 1;
102 bool allow_dups : 1;
103 struct HashElem **table;
106 intptr_t hdata;
108};
109
110typedef uint8_t HashFlags;
111#define MUTT_HASH_NO_FLAGS 0
112#define MUTT_HASH_STRCASECMP (1 << 0)
113#define MUTT_HASH_STRDUP_KEYS (1 << 1)
114#define MUTT_HASH_ALLOW_DUPS (1 << 2)
115
116void mutt_hash_delete (struct HashTable *table, const char *strkey, const void *data);
117struct HashElem * mutt_hash_find_bucket (const struct HashTable *table, const char *strkey);
118void * mutt_hash_find (const struct HashTable *table, const char *strkey);
119struct HashElem * mutt_hash_find_elem (const struct HashTable *table, const char *strkey);
120void mutt_hash_free (struct HashTable **ptr);
121struct HashElem * mutt_hash_insert (struct HashTable *table, const char *strkey, void *data);
122void mutt_hash_int_delete (struct HashTable *table, unsigned int intkey, const void *data);
123void * mutt_hash_int_find (const struct HashTable *table, unsigned int intkey);
124struct HashElem * mutt_hash_int_insert (struct HashTable *table, unsigned int intkey, void *data);
125struct HashTable *mutt_hash_int_new (size_t num_elems, HashFlags flags);
126struct HashTable *mutt_hash_new (size_t num_elems, HashFlags flags);
127void mutt_hash_set_destructor(struct HashTable *table, hash_hdata_free_t fn, intptr_t fn_data);
128struct HashElem * mutt_hash_typed_insert (struct HashTable *table, const char *strkey, int type, void *data);
129
134{
135 size_t index;
136 struct HashElem *last;
137};
138
139struct HashElem *mutt_hash_walk(const struct HashTable *table, struct HashWalkState *state);
140
141#endif /* MUTT_MUTT_HASH_H */
Linear Array data structure.
#define ARRAY_HEAD(name, type)
Define a named struct for arrays of elements of a certain type.
Definition: array.h:47
uint8_t HashFlags
Flags for mutt_hash_new(), e.g. MUTT_HASH_STRCASECMP.
Definition: hash.h:110
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:335
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:427
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:362
int(* hash_cmp_key_t)(union HashKey a, union HashKey b)
Definition: hash.h:91
void mutt_hash_int_delete(struct HashTable *table, unsigned int intkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:444
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:409
struct HashTable * mutt_hash_int_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with integer keys)
Definition: hash.c:285
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:317
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:489
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:259
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:377
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:301
size_t(* hash_gen_hash_t)(union HashKey key, size_t num_elems)
Definition: hash.h:76
void(* hash_hdata_free_t)(int type, void *obj, intptr_t data)
Definition: hash.h:63
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:392
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:347
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
The item stored in a Hash Table.
Definition: hash.h:44
struct HashElem * next
Linked List.
Definition: hash.h:48
union HashKey key
Key representing the data.
Definition: hash.h:46
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
void * data
User-supplied data.
Definition: hash.h:47
A Hash Table.
Definition: hash.h:99
hash_gen_hash_t gen_hash
Function to generate hash id from the key.
Definition: hash.h:104
struct HashElem ** table
Array of Hash keys.
Definition: hash.h:103
hash_cmp_key_t cmp_key
Function to compare two Hash keys.
Definition: hash.h:105
bool allow_dups
if set, duplicate keys are allowed
Definition: hash.h:102
size_t num_elems
Number of buckets in the Hash Table.
Definition: hash.h:100
bool strdup_keys
if set, the key->strkey is strdup()'d
Definition: hash.h:101
intptr_t hdata
Data to pass to the hdata_free() function.
Definition: hash.h:106
hash_hdata_free_t hdata_free
Function to free a Hash element.
Definition: hash.h:107
Cursor to iterate through a Hash Table.
Definition: hash.h:134
size_t index
Current position in table.
Definition: hash.h:135
struct HashElem * last
Current element in linked list.
Definition: hash.h:136
The data item stored in a HashElem.
Definition: hash.h:35
const char * strkey
String key.
Definition: hash.h:36
unsigned int intkey
Integer key.
Definition: hash.h:37