more useful list primitives
[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 <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "mem.h"
30 #include "str.h"
31 #include "hash.h"
32
33 #define SOMEPRIME 149711
34
35 int hash_string(const unsigned char *s, int n)
36 {
37     int h = 0;
38
39     while (*s) {
40         h += (h << 7) + *s++;
41     }
42     h = (h * SOMEPRIME) % n;
43     h = (h >= 0) ? h : h + n;
44
45     return (h % n);
46 }
47
48 HASH *hash_create(int nelem)
49 {
50     HASH *table = p_new(HASH, 1);
51
52     if (nelem == 0)
53         nelem = 2;
54
55     table->nelem    = nelem;
56     table->curnelem = 0;
57     table->table    = p_new(struct hash_elem *, nelem);
58     return table;
59 }
60
61 HASH *hash_resize(HASH *ptr, int nelem)
62 {
63     HASH *table;
64     struct hash_elem *elem, *tmp;
65     int i;
66
67     table = hash_create (nelem);
68
69     for (i = 0; i < ptr->nelem; i++) {
70         for (elem = ptr->table[i]; elem;) {
71             tmp = elem;
72             elem = elem->next;
73             hash_insert(table, tmp->key, tmp->data, 1);
74             p_delete(&tmp);
75         }
76     }
77     p_delete(&ptr->table);
78     p_delete(&ptr);
79
80     return table;
81 }
82
83 /* table        hash table to update
84  * key          key to hash on
85  * data         data to associate with `key'
86  * allow_dup    if nonzero, duplicate keys are allowed in the table 
87  */
88 int hash_insert(HASH *table, const char *key, void *data, int allow_dup)
89 {
90     struct hash_elem *ptr;
91     int h;
92
93     ptr = p_new(struct hash_elem, 1);
94     h = hash_string((unsigned char *)key, table->nelem);
95     ptr->key = key;
96     ptr->data = data;
97
98     if (allow_dup) {
99         ptr->next = table->table[h];
100         table->table[h] = ptr;
101         table->curnelem++;
102     } else {
103         struct hash_elem *tmp, *last;
104         int r;
105
106         for (tmp = table->table[h], last = NULL; tmp; last = tmp, tmp = tmp->next) {
107             r = m_strcmp(tmp->key, key);
108             if (r == 0) {
109                 p_delete(&ptr);
110                 return (-1);
111             }
112             if (r > 0)
113                 break;
114         }
115         if (last) {
116             last->next = ptr;
117         } else {
118             table->table[h] = ptr;
119         }
120         ptr->next = tmp;
121         table->curnelem++;
122     }
123     return h;
124 }
125
126 void *hash_find_hash(const HASH *table, int hash, const char *key)
127 {
128     struct hash_elem *ptr;
129
130     for (ptr = table->table[hash]; ptr; ptr = ptr->next) {
131         if (m_strcmp(key, ptr->key) == 0)
132             return (ptr->data);
133     }
134     return NULL;
135 }
136
137 void hash_delete_hash(HASH *table, int hash, const char *key, const void *data,
138                       void (*destroy)(void *))
139 {
140     struct hash_elem *ptr   = table->table[hash];
141     struct hash_elem **last = &table->table[hash];
142
143     while (ptr) {
144         if ((data == ptr->data || !data) && m_strcmp(ptr->key, key) == 0) {
145             *last = ptr->next;
146             if (destroy)
147                 destroy (ptr->data);
148             p_delete(&ptr);
149
150             ptr = *last;
151         } else {
152             last = &ptr->next;
153             ptr = ptr->next;
154         }
155     }
156 }
157
158 /* ptr          pointer to the hash table to be freed
159  * destroy()    function to call to free the ->data member (optional) 
160  */
161 void hash_destroy(HASH **ptr, void (*destroy)(void *))
162 {
163     int i;
164     HASH *pptr = *ptr;
165     struct hash_elem *elem, *tmp;
166
167     for (i = 0; i < pptr->nelem; i++) {
168         for (elem = pptr->table[i]; elem;) {
169             tmp = elem;
170             elem = elem->next;
171             if (destroy)
172                 destroy (tmp->data);
173             p_delete(&tmp);
174         }
175     }
176     p_delete(&pptr->table);
177     p_delete(ptr);
178 }
179
180 void hash_map(HASH *table,
181               void (*mapfunc)(const char* key, void* data, unsigned long more),
182               unsigned long more)
183 {
184     int i;
185
186     if (!table || !mapfunc)
187         return;
188
189     for (i = 0; i < table->nelem; i++) {
190         struct hash_elem *elem;
191
192         for (elem = table->table[i]; elem; elem = elem->next) {
193             mapfunc (elem->key, elem->data, more);
194         }
195     }
196 }