annotate src/hash.c @ 272:c5d627a313b1 r21-0b34

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