add some very efficient utf8 routines. more to come.
[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 **e;
98
99         for (e = &table->table[h]; *e; e = &(*e)->next) {
100             int r = m_strcmp((*e)->key, key);
101
102             if (r == 0) {
103                 p_delete(&ptr);
104                 return (-1);
105             }
106             if (r > 0)
107                 break;
108         }
109         ptr->next = *e;
110         *e = ptr;
111         table->curnelem++;
112     }
113     return h;
114 }
115
116 void *hash_find_hash(const HASH *table, int hash, const char *key)
117 {
118     struct hash_elem *ptr;
119
120     for (ptr = table->table[hash]; ptr; ptr = ptr->next) {
121         if (m_strcmp(key, ptr->key) == 0)
122             return (ptr->data);
123     }
124     return NULL;
125 }
126
127 void hash_delete_hash(HASH *table, int hash, const char *key, const void *data,
128                       void (*destroy)(void *))
129 {
130     struct hash_elem *ptr   = table->table[hash];
131     struct hash_elem **last = &table->table[hash];
132
133     while (ptr) {
134         if ((data == ptr->data || !data) && m_strcmp(ptr->key, key) == 0) {
135             *last = ptr->next;
136             if (destroy)
137                 destroy (ptr->data);
138             p_delete(&ptr);
139
140             ptr = *last;
141         } else {
142             last = &ptr->next;
143             ptr = ptr->next;
144         }
145     }
146 }
147
148 /* ptr          pointer to the hash table to be freed
149  * destroy()    function to call to free the ->data member (optional) 
150  */
151 void hash_destroy(HASH **ptr, void (*destroy)(void *))
152 {
153     int i;
154     HASH *pptr = *ptr;
155     struct hash_elem *elem, *tmp;
156
157     for (i = 0; i < pptr->nelem; i++) {
158         for (elem = pptr->table[i]; elem;) {
159             tmp = elem;
160             elem = elem->next;
161             if (destroy)
162                 destroy (tmp->data);
163             p_delete(&tmp);
164         }
165     }
166     p_delete(&pptr->table);
167     p_delete(ptr);
168 }
169
170 void hash_map(HASH *table,
171               void (*mapfunc)(const char* key, void* data, unsigned long more),
172               unsigned long more)
173 {
174     int i;
175
176     if (!table || !mapfunc)
177         return;
178
179     for (i = 0; i < table->nelem; i++) {
180         struct hash_elem *elem;
181
182         for (elem = table->table[i]; elem; elem = elem->next) {
183             mapfunc (elem->key, elem->data, more);
184         }
185     }
186 }