simplify hashes.
[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, int allow_dup)
43 {
44     HASH *table = p_new(HASH, 1);
45
46     table->dupes    = allow_dup;
47     table->nelem    = MIN(nelem, 2);
48     table->curnelem = 0;
49     table->table    = p_new(struct hash_elem *, table->nelem);
50
51     return table;
52 }
53
54 void hash_resize(HASH *ptr, int nelem)
55 {
56     HASH *table;
57     struct hash_elem *elem, *tmp;
58     int i;
59
60     /* XXX hack: we know the has was correct, no dupe checks is fater */
61     table = hash_create(nelem, 1);
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);
68             p_delete(&tmp);
69         }
70     }
71
72     p_delete(&ptr->table);
73     ptr->nelem = table->nelem;
74     ptr->table = table->table;
75     p_delete(&table);
76 }
77
78 /* table        hash table to update
79  * key          key to hash on
80  * data         data to associate with `key'
81  * allow_dup    if nonzero, duplicate keys are allowed in the table 
82  */
83 int hash_insert(HASH *table, const char *key, void *data)
84 {
85     struct hash_elem *ptr;
86     int h;
87
88     ptr = p_new(struct hash_elem, 1);
89     h = hash_string((unsigned char *)key, table->nelem);
90     ptr->key = key;
91     ptr->data = data;
92
93     if (table->dupes) {
94         ptr->next = table->table[h];
95         table->table[h] = ptr;
96         table->curnelem++;
97     } else {
98         struct hash_elem **e;
99
100         for (e = &table->table[h]; *e; e = &(*e)->next) {
101             int r = m_strcmp((*e)->key, key);
102
103             if (r == 0) {
104                 p_delete(&ptr);
105                 return (-1);
106             }
107             if (r > 0)
108                 break;
109         }
110         ptr->next = *e;
111         *e = ptr;
112         table->curnelem++;
113     }
114     return h;
115 }
116
117 void *hash_find_hash(const HASH *table, int hash, const char *key)
118 {
119     struct hash_elem *ptr;
120
121     for (ptr = table->table[hash]; ptr; ptr = ptr->next) {
122         if (m_strcmp(key, ptr->key) == 0)
123             return (ptr->data);
124     }
125     return NULL;
126 }
127
128 void hash_delete_hash(HASH *table, int hash, const char *key, const void *data,
129                       void (*destroy)(void *))
130 {
131     struct hash_elem *ptr   = table->table[hash];
132     struct hash_elem **last = &table->table[hash];
133
134     while (ptr) {
135         if ((data == ptr->data || !data) && m_strcmp(ptr->key, key) == 0) {
136             *last = ptr->next;
137             if (destroy)
138                 destroy (ptr->data);
139             p_delete(&ptr);
140
141             ptr = *last;
142         } else {
143             last = &ptr->next;
144             ptr = ptr->next;
145         }
146     }
147 }
148
149 /* ptr          pointer to the hash table to be freed
150  * destroy()    function to call to free the ->data member (optional) 
151  */
152 void hash_destroy(HASH **ptr, void (*destroy)(void *))
153 {
154     int i;
155     HASH *pptr = *ptr;
156     struct hash_elem *elem, *tmp;
157
158     for (i = 0; i < pptr->nelem; i++) {
159         for (elem = pptr->table[i]; elem;) {
160             tmp = elem;
161             elem = elem->next;
162             if (destroy)
163                 destroy (tmp->data);
164             p_delete(&tmp);
165         }
166     }
167     p_delete(&pptr->table);
168     p_delete(ptr);
169 }
170
171 void hash_map(HASH *table,
172               void (*mapfunc)(const char* key, void* data, unsigned long more),
173               unsigned long more)
174 {
175     int i;
176
177     if (!table || !mapfunc)
178         return;
179
180     for (i = 0; i < table->nelem; i++) {
181         struct hash_elem *elem;
182
183         for (elem = table->table[i]; elem; elem = elem->next) {
184             mapfunc (elem->key, elem->data, more);
185         }
186     }
187 }