~emersion/mrsh

ref: 4da12aae651dac2f0c6818f54d5d8b5f0a9e8bf6 mrsh/include/mrsh/hashtable.h -rw-r--r-- 1009 bytes
4da12aaeDrew DeVault Implement 'shift' builtin 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef _MRSH_HASHTABLE_H
#define _MRSH_HASHTABLE_H

#define MRSH_HASHTABLE_BUCKETS 256

struct mrsh_hashtable_entry {
	struct mrsh_hashtable_entry *next;
	unsigned int hash;
	char *key;
	void *value;
};

struct mrsh_hashtable {
	struct mrsh_hashtable_entry *buckets[MRSH_HASHTABLE_BUCKETS];
};

typedef void (*mrsh_hashtable_iterator_func_t)(const char *key, void *value,
	void *user_data);

void mrsh_hashtable_finish(struct mrsh_hashtable *table);
void *mrsh_hashtable_get(struct mrsh_hashtable *table, const char *key);
void *mrsh_hashtable_set(struct mrsh_hashtable *table, const char *key,
	void *value);
void *mrsh_hashtable_del(struct mrsh_hashtable *table, const char *key);
/**
 * Calls `iterator` for each (key, value) pair in the hash table. It is safe to
 * call `mrsh_hashtable_del` on the current element, however it is not safe to
 * do so an any other element.
 */
void mrsh_hashtable_for_each(struct mrsh_hashtable *table,
	mrsh_hashtable_iterator_func_t iterator, void *user_data);

#endif