fix the real bug, that was hidden in the .h
[apps/madmutt.git] / lib-lib / hash.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 /*
21  * Copyright notice from original mutt:
22  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
23  */
24
25 #include "lib-lib.h"
26
27 #define SOMEPRIME 149711
28
29 int hash_string(const unsigned char *s, int n)
30 {
31     int h = 0;
32
33     while (*s) {
34         h += (h << 7) + *s++;
35     }
36     h = (h * SOMEPRIME) % n;
37     h = (h >= 0) ? h : h + n;
38
39     return (h % n);
40 }
41
42 HASH *hash_create(int nelem)
43 {
44     HASH *table = p_new(HASH, 1);
45
46     if (nelem == 0)
47         nelem = 2;
48
49     table->nelem    = nelem;
50     table->curnelem = 0;
51     table->table    = p_new(struct hash_elem *, nelem);
52     return table;
53 }
54
55 HASH *hash_resize(HASH *ptr, int nelem)
56 {
57     HASH *table;
58     struct hash_elem *elem, *tmp;
59     int i;
60
61     table = hash_create (nelem);
62
63     for (i = 0; i < ptr->nelem; i++) {
64         for (elem = ptr->table[i]; elem;) {
65             tmp = elem;
66             elem = elem->next;
67             hash_insert(table, tmp->key, tmp->data, 1);
68             p_delete(&tmp);
69         }
70     }
71     p_delete(&ptr->table);
72     p_delete(&ptr);
73
74     return table;
75 }
76
77 /* table        hash table to update
78  * key          key to hash on
79  * data         data to associate with `key'
80  * allow_dup    if nonzero, duplicate keys are allowed in the table 
81  */
82 int hash_insert(HASH *table, const char *key, void *data, int allow_dup)
83 {
84     struct hash_elem *ptr;
85     int h;
86
87     ptr = p_new(struct hash_elem, 1);
88     h = hash_string((unsigned char *)key, table->nelem);
89     ptr->key = key;
90     ptr->data = data;
91
92     if (allow_dup) {
93         ptr->next = table->table[h];
94         table->table[h] = ptr;
95         table->curnelem++;
96     } else {
97         struct hash_elem *tmp, *last;
98         int r;
99
100         for (tmp = table->table[h], last = NULL; tmp; last = tmp, tmp = tmp->next) {
101             r = m_strcmp(tmp->key, key);
102             if (r == 0) {
103                 p_delete(&ptr);
104                 return (-1);
105             }
106             if (r > 0)
107                 break;
108         }
109         if (last) {
110             last->next = ptr;
111         } else {
112             table->table[h] = ptr;
113         }
114         ptr->next = tmp;
115         table->curnelem++;
116     }
117     return h;
118 }
119
120 void *hash_find_hash(const HASH *table, int hash, const char *key)
121 {
122     struct hash_elem *ptr;
123
124     for (ptr = table->table[hash]; ptr; ptr = ptr->next) {
125         if (m_strcmp(key, ptr->key) == 0)
126             return (ptr->data);
127     }
128     return NULL;
129 }
130
131 void hash_delete_hash(HASH *table, int hash, const char *key, const void *data,
132                       void (*destroy)(void *))
133 {
134     struct hash_elem *ptr   = table->table[hash];
135     struct hash_elem **last = &table->table[hash];
136
137     while (ptr) {
138         if ((data == ptr->data || !data) && m_strcmp(ptr->key, key) == 0) {
139             *last = ptr->next;
140             if (destroy)
141                 destroy (ptr->data);
142             p_delete(&ptr);
143
144             ptr = *last;
145         } else {
146             last = &ptr->next;
147             ptr = ptr->next;
148         }
149     }
150 }
151
152 /* ptr          pointer to the hash table to be freed
153  * destroy()    function to call to free the ->data member (optional) 
154  */
155 void hash_destroy(HASH **ptr, void (*destroy)(void *))
156 {
157     int i;
158     HASH *pptr = *ptr;
159     struct hash_elem *elem, *tmp;
160
161     for (i = 0; i < pptr->nelem; i++) {
162         for (elem = pptr->table[i]; elem;) {
163             tmp = elem;
164             elem = elem->next;
165             if (destroy)
166                 destroy (tmp->data);
167             p_delete(&tmp);
168         }
169     }
170     p_delete(&pptr->table);
171     p_delete(ptr);
172 }
173
174 void hash_map(HASH *table,
175               void (*mapfunc)(const char* key, void* data, unsigned long more),
176               unsigned long more)
177 {
178     int i;
179
180     if (!table || !mapfunc)
181         return;
182
183     for (i = 0; i < table->nelem; i++) {
184         struct hash_elem *elem;
185
186         for (elem = table->table[i]; elem; elem = elem->next) {
187             mapfunc (elem->key, elem->data, more);
188         }
189     }
190 }