a2dc11e2b137f387e0c6dd91c3b16c33a2c49e4b
[apps/madmutt.git] / hash.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "mutt.h"
28
29 #define SOMEPRIME 149711
30
31 int hash_string (const unsigned char *s, int n)
32 {
33   int h = 0;
34
35 #if 0
36   while (*s)
37     h += *s++;
38 #else
39   while (*s)
40     h += (h << 7) + *s++;
41   h = (h * SOMEPRIME) % n;
42   h = (h >= 0) ? h : h + n;
43 #endif
44
45   return (h % n);
46 }
47
48 HASH *hash_create (int nelem)
49 {
50   HASH *table = safe_malloc (sizeof (HASH));
51
52   if (nelem == 0)
53     nelem = 2;
54   table->nelem = nelem;
55   table->curnelem = 0;
56   table->table = safe_calloc (nelem, sizeof (struct hash_elem *));
57   return table;
58 }
59
60 HASH *hash_resize (HASH * ptr, int nelem)
61 {
62   HASH *table;
63   struct hash_elem *elem, *tmp;
64   int i;
65
66   table = hash_create (nelem);
67
68   for (i = 0; i < ptr->nelem; i++) {
69     for (elem = ptr->table[i]; elem;) {
70       tmp = elem;
71       elem = elem->next;
72       hash_insert (table, tmp->key, tmp->data, 1);
73       FREE (&tmp);
74     }
75   }
76   FREE (&ptr->table);
77   FREE (&ptr);
78
79   return table;
80 }
81
82 /* table        hash table to update
83  * key          key to hash on
84  * data         data to associate with `key'
85  * allow_dup    if nonzero, duplicate keys are allowed in the table 
86  */
87 int hash_insert (HASH * table, const char *key, void *data, int allow_dup)
88 {
89   struct hash_elem *ptr;
90   int h;
91
92   ptr = (struct hash_elem *) safe_malloc (sizeof (struct hash_elem));
93   h = hash_string ((unsigned char *) key, table->nelem);
94   ptr->key = key;
95   ptr->data = data;
96
97   if (allow_dup) {
98     ptr->next = table->table[h];
99     table->table[h] = ptr;
100     table->curnelem++;
101   }
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 = mutt_strcmp (tmp->key, key);
108       if (r == 0) {
109         FREE (&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     ptr->next = tmp;
120     table->curnelem++;
121   }
122   return h;
123 }
124
125 void *hash_find_hash (const HASH * table, int hash, const char *key)
126 {
127   struct hash_elem *ptr = table->table[hash];
128
129   for (; ptr; ptr = ptr->next) {
130     if (mutt_strcmp (key, ptr->key) == 0)
131       return (ptr->data);
132   }
133   return NULL;
134 }
135
136 void hash_delete_hash (HASH * table, int hash, const char *key,
137                        const void *data, void (*destroy) (void *))
138 {
139   struct hash_elem *ptr = table->table[hash];
140   struct hash_elem **last = &table->table[hash];
141
142   for (; ptr; last = &ptr->next, ptr = ptr->next) {
143     /* if `data' is given, look for a matching ->data member.  this is
144      * required for the case where we have multiple entries with the same
145      * key
146      */
147     if ((data == ptr->data) || (!data && mutt_strcmp (ptr->key, key) == 0)) {
148       *last = ptr->next;
149       if (destroy)
150         destroy (ptr->data);
151       FREE (&ptr);
152       table->curnelem--;
153       return;
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       FREE (&tmp);
174     }
175   }
176   FREE (&pptr->table);
177   FREE (ptr);
178 }