2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
20 #define SOMEPRIME 149711
22 int hash_string (const unsigned char *s, int n)
32 h = (h * SOMEPRIME) % n;
33 h = (h >= 0) ? h : h + n;
39 HASH *hash_create (int nelem)
41 HASH *table = safe_malloc (sizeof (HASH));
47 table->table = safe_calloc (nelem, sizeof (struct hash_elem *));
51 HASH *hash_resize (HASH * ptr, int nelem)
54 struct hash_elem *elem, *tmp;
57 table = hash_create (nelem);
59 for (i = 0; i < ptr->nelem; i++) {
60 for (elem = ptr->table[i]; elem;) {
63 hash_insert (table, tmp->key, tmp->data, 1);
73 /* table hash table to update
75 * data data to associate with `key'
76 * allow_dup if nonzero, duplicate keys are allowed in the table
78 int hash_insert (HASH * table, const char *key, void *data, int allow_dup)
80 struct hash_elem *ptr;
83 ptr = (struct hash_elem *) safe_malloc (sizeof (struct hash_elem));
84 h = hash_string ((unsigned char *) key, table->nelem);
89 ptr->next = table->table[h];
90 table->table[h] = ptr;
94 struct hash_elem *tmp, *last;
97 for (tmp = table->table[h], last = NULL; tmp; last = tmp, tmp = tmp->next) {
98 r = mutt_strcmp (tmp->key, key);
109 table->table[h] = ptr;
116 void *hash_find_hash (const HASH * table, int hash, const char *key)
118 struct hash_elem *ptr = table->table[hash];
120 for (; ptr; ptr = ptr->next) {
121 if (mutt_strcmp (key, ptr->key) == 0)
127 void hash_delete_hash (HASH * table, int hash, const char *key,
128 const void *data, void (*destroy) (void *))
130 struct hash_elem *ptr = table->table[hash];
131 struct hash_elem **last = &table->table[hash];
133 for (; ptr; last = &ptr->next, ptr = ptr->next) {
134 /* if `data' is given, look for a matching ->data member. this is
135 * required for the case where we have multiple entries with the same
138 if ((data == ptr->data) || (!data && mutt_strcmp (ptr->key, key) == 0)) {
149 /* ptr pointer to the hash table to be freed
150 * destroy() function to call to free the ->data member (optional)
152 void hash_destroy (HASH ** ptr, void (*destroy) (void *))
156 struct hash_elem *elem, *tmp;
158 for (i = 0; i < pptr->nelem; i++) {
159 for (elem = pptr->table[i]; elem;) {