annotate src/hash.c @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children 8eaf7971accc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Hash tables.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 #include "lisp.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #define NULL_ENTRY (LISP_TO_VOID (Qnil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #else /* !emacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 #define NULL_ENTRY ((void *) 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 #endif /* !emacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 #include "hash.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 #include "elhash.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 static CONST int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 primes []={
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 13,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 761, 919, 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 8419, 10103, 12143, 14591, 17519, 21023, 25229, 30293, 36353, 43627, 52361,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 62851, 75431, 90523, 108631, 130363, 156437, 187751, 225307, 270371, 324449,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 1674319,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 2009191, 2411033, 2893249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 /* strings code */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 /* from base/generic-hash.cc, and hence from Dragon book, p436 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 unsigned long
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 string_hash (CONST void *xv)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 unsigned int h = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 unsigned int g;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 unsigned CONST char *x = (unsigned CONST char *) xv;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 if (!x) return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 while (*x != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 h = (h << 4) + *x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 if ((g = h & 0xf0000000) != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 h = (h ^ (g >> 24)) ^ g;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 return h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 unsigned long
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 memory_hash (CONST void *xv, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 unsigned int h = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 unsigned int g;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 unsigned CONST char *x = (unsigned CONST char *) xv;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 if (!x) return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 while (size > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 h = (h << 4) + *x++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 if ((g = h & 0xf0000000) != 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 h = (h ^ (g >> 24)) ^ g;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 size--;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 return h;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 string_eq (CONST void *st1v, CONST void *st2v)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 CONST char *st1 = (CONST char *)st1v;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 CONST char *st2 = (CONST char *)st2v;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 if (!st1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 return (st2)?0:1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 else if (!st2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 return 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 return !strcmp (st1, st2);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 static unsigned int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 prime_size (unsigned int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 unsigned int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 CONST int lim = countof (primes);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 for (i = 0; i < lim; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 if (size <= primes [i]) return primes [i];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 return primes [lim - 1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 static void rehash (hentry *harray, c_hashtable ht, unsigned int size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 #define KEYS_DIFFER_P(old, new, testfun) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ((testfun)?(((old) == (new))?0:(!(testfun ((old), new)))):((old) != (new)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 CONST void *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 gethash (CONST void *key, c_hashtable hash, CONST void **ret_value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 hentry *harray = hash->harray;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 int (*test_function) (CONST void *, CONST void *) = hash->test_function;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 unsigned int hsize = hash->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 unsigned int hcode_initial =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 unsigned int hcode = hcode_initial % hsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 hentry *e = &harray [hcode];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 CONST void *e_key = e->key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 if (!key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 *ret_value = hash->zero_entry;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 return (void *) hash->zero_set;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 if ((e_key)?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (KEYS_DIFFER_P (e_key, key, test_function)):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (e->contents == NULL_ENTRY))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 unsigned int h2 = hsize - 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 unsigned int incr = 1 + (hcode_initial % h2);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 do
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 hcode = hcode + incr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 if (hcode >= hsize) hcode = hcode - hsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 e = &harray [hcode];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 e_key = e->key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 while ((e_key)?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (KEYS_DIFFER_P (e_key, key, test_function)):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (e->contents == NULL_ENTRY));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 *ret_value = e->contents;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 return e->key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 clrhash (c_hashtable hash)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 memset (hash->harray, 0, sizeof (hentry) * hash->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 hash->zero_entry = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 hash->zero_set = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 hash->fullness = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 free_hashtable (c_hashtable hash)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 if (!NILP (hash->elisp_table))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 xfree (hash->harray);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 xfree (hash);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 c_hashtable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 make_hashtable (unsigned int hsize)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 c_hashtable res = (c_hashtable) xmalloc (sizeof (struct _C_hashtable));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 memset (res, 0, sizeof (struct _C_hashtable));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 res->size = prime_size ((13 * hsize) / 10);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 res->harray = (hentry *) xmalloc (sizeof (hentry) * res->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 res->elisp_table = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 clrhash (res);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 return res;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 c_hashtable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 make_general_hashtable (unsigned int hsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 unsigned long (*hash_function) (CONST void *),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 int (*test_function) (CONST void *, CONST void *))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 c_hashtable res = (c_hashtable) xmalloc (sizeof (struct _C_hashtable));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 memset (res, 0, sizeof (struct _C_hashtable));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 res->size = prime_size ((13 * hsize) / 10);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 res->harray = (hentry *) xmalloc (sizeof (hentry) * res->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 res->hash_function = hash_function;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 res->test_function = test_function;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 res->elisp_table = Qnil;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 clrhash (res);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 return res;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 c_hashtable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 make_strings_hashtable (unsigned int hsize)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 return make_general_hashtable (hsize, string_hash, string_eq);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 unsigned int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 compute_harray_size (unsigned int hsize)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 return prime_size ((13 * hsize) / 10);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 copy_hash (c_hashtable dest, c_hashtable src)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 /* if these are not the same, then we are losing here */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 if ((NILP (dest->elisp_table)) != (NILP (src->elisp_table)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 error ("Incompatible hashtable types to copy_hash.");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 if (dest->size != src->size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 if (!NILP (dest->elisp_table))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 elisp_hvector_free (dest->harray, dest->elisp_table);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 xfree (dest->harray);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 dest->size = src->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 if (!NILP (dest->elisp_table))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 dest->harray =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (hentry *) elisp_hvector_malloc
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (sizeof (hentry) * dest->size, dest->elisp_table);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 dest->harray = (hentry *) xmalloc (sizeof (hentry) * dest->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 dest->fullness = src->fullness;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 dest->zero_entry = src->zero_entry;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 dest->zero_set = src->zero_set;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 dest->hash_function = src->hash_function;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 dest->test_function = src->test_function;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 memcpy (dest->harray, src->harray, sizeof (hentry) * dest->size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 grow_hashtable (c_hashtable hash, unsigned int new_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 unsigned int old_hsize = hash->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 hentry *old_harray = hash->harray;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 unsigned int new_hsize = prime_size (new_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 hentry *new_harray;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 /* We test for Qzero to facilitate free-hook.c. That module creates
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 a hashtable very very early, at which point Qnil has not yet
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 been set and is thus all zeroes. Qzero is "automatically"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 initialized at startup because its correct value is also all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 zeroes. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 if (!NILP (hash->elisp_table) && !ZEROP (hash->elisp_table))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 new_harray = (hentry *) elisp_hvector_malloc (sizeof (hentry) * new_hsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 hash->elisp_table);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 new_harray =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (hentry *) xmalloc (sizeof (hentry) * new_hsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 hash->size = new_hsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 hash->harray = new_harray;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 /* do the rehash on the "grown" table */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 long old_zero_set = hash->zero_set;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 void *old_zero_entry = hash->zero_entry;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 clrhash (hash);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 hash->zero_set = old_zero_set;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 hash->zero_entry = old_zero_entry;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 rehash (old_harray, hash, old_hsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 #ifdef emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 if (!NILP (hash->elisp_table) && !ZEROP (hash->elisp_table))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 elisp_hvector_free (old_harray, hash->elisp_table);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 xfree (old_harray);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 expand_hashtable (c_hashtable hash, unsigned int needed_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 unsigned int hsize = hash->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 int comfortable_size = (13 * needed_size) / 10;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 if (hsize < comfortable_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 grow_hashtable (hash, comfortable_size + 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 puthash (CONST void *key, void *cont, c_hashtable hash)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 unsigned int hsize = hash->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 int (*test_function) (CONST void *, CONST void *) = hash->test_function;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 unsigned int fullness = hash->fullness;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 hentry *harray;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 CONST void *e_key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 hentry *e;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 unsigned int hcode_initial =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 unsigned int hcode;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 unsigned int incr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 unsigned int h2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 CONST void *oldcontents;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 if (!key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 hash->zero_entry = cont;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 hash->zero_set = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 if (hsize < (1 + ((13 * fullness) / 10)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 grow_hashtable (hash, hsize + 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 hsize = hash->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 fullness = hash->fullness;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 harray= hash->harray;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 h2 = hsize - 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 hcode = hcode_initial % hsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 e_key = harray [hcode].key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 if (e_key && (KEYS_DIFFER_P (e_key, key, test_function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 h2 = hsize - 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 incr = 1 + (hcode_initial % h2);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 do
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 hcode = hcode + incr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 if (hcode >= hsize) hcode = hcode - hsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 e_key = harray [hcode].key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 while (e_key && (KEYS_DIFFER_P (e_key, key, test_function)));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 oldcontents = harray [hcode].contents;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 harray [hcode].key = key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 harray [hcode].contents = cont;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 /* if the entry that we used was a deleted entry,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 check for a non deleted entry of the same key,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 then delete it */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 if (!e_key && (oldcontents == NULL_ENTRY))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 if (!incr) incr = 1 + ((unsigned long) key % h2);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 do
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 hcode = hcode + incr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 if (hcode >= hsize) hcode = hcode - hsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 e = &harray [hcode];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 e_key = e->key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 while ((e_key)?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 (KEYS_DIFFER_P (e_key, key, test_function)):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 (e->contents == NULL_ENTRY));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 if (e_key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 e->key = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 e->contents = NULL_ENTRY;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 /* only increment the fullness when we used up a new hentry */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 if (!e_key || (KEYS_DIFFER_P (e_key, key, test_function)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 hash->fullness++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 rehash (hentry *harray, c_hashtable hash, unsigned int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 hentry *limit = harray + size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 hentry *e;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 for (e = harray; e < limit; e++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 if (e->key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 puthash (e->key, e->contents, hash);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 remhash (CONST void *key, c_hashtable hash)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 hentry *harray = hash->harray;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 int (*test_function) (CONST void*, CONST void*) = hash->test_function;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 unsigned int hsize = hash->size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 unsigned int hcode_initial =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 unsigned int hcode = hcode_initial % hsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 hentry *e = &harray [hcode];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 CONST void *e_key = e->key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 if (!key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 hash->zero_entry = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 hash->zero_set = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 if ((e_key)?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 (KEYS_DIFFER_P (e_key, key, test_function)):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 (e->contents == NULL_ENTRY))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 unsigned int h2 = hsize - 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 unsigned int incr = 1 + (hcode_initial % h2);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 do
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 hcode = hcode + incr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 if (hcode >= hsize) hcode = hcode - hsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 e = &harray [hcode];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 e_key = e->key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 while ((e_key)?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 (KEYS_DIFFER_P (e_key, key, test_function)):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 (e->contents == NULL_ENTRY));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 if (e_key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 e->key = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 e->contents = NULL_ENTRY;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 /* Note: you can't do fullness-- here, it breaks the world. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 maphash (maphash_function mf, c_hashtable hash, void *arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 hentry *e;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 hentry *limit;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 if (hash->zero_set)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 ((*mf) (0, hash->zero_entry, arg));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 for (e = hash->harray, limit = e + hash->size; e < limit; e++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 if (e->key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 ((*mf) (e->key, e->contents, arg));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 map_remhash (remhash_predicate predicate, c_hashtable hash, void *arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 hentry *e;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 hentry *limit;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 if (hash->zero_set && ((*predicate) (0, hash->zero_entry, arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 hash->zero_set = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 hash->zero_entry = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 for (e = hash->harray, limit = e + hash->size; e < limit; e++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 if ((*predicate) (e->key, e->contents, arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 e->key = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 e->contents = NULL_ENTRY;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 }