annotate src/hash.c @ 394:7d59cb494b73 r21-2-12

Import from CVS: tag r21-2-12
author cvs
date Mon, 13 Aug 2007 11:11:37 +0200
parents 8626e4521993
children 74fd4e045ea6
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 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 #include "lisp.h"
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
25 #include "hash.h"
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
27 #define NULL_ENTRY ((void *) 0xdeadbeef)
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
28
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
29 #define COMFORTABLE_SIZE(size) (21 * (size) / 16)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
31 #define KEYS_DIFFER_P(old, new, testfun) \
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
32 (((old) != (new)) && (!(testfun) || !(testfun) ((old),(new))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
34 static void rehash (hentry *harray, struct hash_table *ht, hash_size_t size);
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
35
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
36 unsigned long
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
37 memory_hash (CONST void *xv, size_t size)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
38 {
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
39 unsigned int h = 0;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
40 unsigned CONST char *x = (unsigned CONST char *) xv;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
41
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
42 if (!x) return 0;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
43
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
44 while (size--)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
45 {
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
46 unsigned int g;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
47 h = (h << 4) + *x++;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
48 if ((g = h & 0xf0000000) != 0)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
49 h = (h ^ (g >> 24)) ^ g;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
50 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
51
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
52 return h;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
53 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
54
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
55 /* Return a suitable size for a hash table, with at least SIZE slots. */
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
56 static size_t
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
57 hash_table_size (size_t requested_size)
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
58 {
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
59 /* Return some prime near, but greater than or equal to, SIZE.
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
60 Decades from the time of writing, someone will have a system large
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
61 enough that the list below will be too short... */
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
62 static CONST size_t primes [] =
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
63 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
64 19, 29, 41, 59, 79, 107, 149, 197, 263, 347, 457, 599, 787, 1031,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
65 1361, 1777, 2333, 3037, 3967, 5167, 6719, 8737, 11369, 14783,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
66 19219, 24989, 32491, 42257, 54941, 71429, 92861, 120721, 156941,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
67 204047, 265271, 344857, 448321, 582821, 757693, 985003, 1280519,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
68 1664681, 2164111, 2813353, 3657361, 4754591, 6180989, 8035301,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
69 10445899, 13579681, 17653589, 22949669, 29834603, 38784989,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
70 50420551, 65546729, 85210757, 110774011, 144006217, 187208107,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
71 243370577, 316381771, 411296309, 534685237, 695090819, 903618083,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
72 1174703521, 1527114613, 1985248999, 2580823717UL, 3355070839UL
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
73 };
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
74 /* We've heard of binary search. */
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
75 int low, high;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
76 for (low = 0, high = countof (primes) - 1; high - low > 1;)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
77 {
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
78 /* Loop Invariant: size < primes [high] */
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
79 int mid = (low + high) / 2;
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
80 if (primes [mid] < requested_size)
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
81 low = mid;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
82 else
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
83 high = mid;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
84 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
85 return primes [high];
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
86 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
87
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
88 CONST void *
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
89 gethash (CONST void *key, struct hash_table *hash_table, CONST void **ret_value)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
90 {
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
91 if (!key)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
92 {
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
93 *ret_value = hash_table->zero_entry;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
94 return (void *) hash_table->zero_set;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
95 }
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
96 else
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
97 {
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
98 hentry *harray = hash_table->harray;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
99 hash_table_test_function test_function = hash_table->test_function;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
100 hash_size_t size = hash_table->size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
101 unsigned int hcode_initial =
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
102 hash_table->hash_function ?
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
103 hash_table->hash_function (key) :
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
104 (unsigned long) key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
105 unsigned int hcode = hcode_initial % size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
106 hentry *e = &harray [hcode];
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
107 CONST void *e_key = e->key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
108
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
109 if (e_key ?
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
110 KEYS_DIFFER_P (e_key, key, test_function) :
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
111 e->contents == NULL_ENTRY)
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
112 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
113 size_t h2 = size - 2;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
114 unsigned int incr = 1 + (hcode_initial % h2);
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
115 do
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
116 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
117 hcode += incr; if (hcode >= size) hcode -= size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
118 e = &harray [hcode];
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
119 e_key = e->key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
120 }
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
121 while (e_key ?
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
122 KEYS_DIFFER_P (e_key, key, test_function) :
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
123 e->contents == NULL_ENTRY);
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
124 }
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
125
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
126 *ret_value = e->contents;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
127 return e->key;
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
128 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
129 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
130
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
131 void
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
132 clrhash (struct hash_table *hash_table)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
133 {
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
134 memset (hash_table->harray, 0, sizeof (hentry) * hash_table->size);
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
135 hash_table->zero_entry = 0;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
136 hash_table->zero_set = 0;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
137 hash_table->fullness = 0;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
138 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
139
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
140 void
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
141 free_hash_table (struct hash_table *hash_table)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
142 {
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
143 xfree (hash_table->harray);
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
144 xfree (hash_table);
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
145 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
146
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
147 struct hash_table*
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
148 make_hash_table (hash_size_t size)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
149 {
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
150 struct hash_table *hash_table = xnew_and_zero (struct hash_table);
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
151 hash_table->size = hash_table_size (COMFORTABLE_SIZE (size));
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
152 hash_table->harray = xnew_array (hentry, hash_table->size);
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
153 clrhash (hash_table);
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
154 return hash_table;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
155 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
156
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
157 struct hash_table *
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
158 make_general_hash_table (hash_size_t size,
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
159 hash_table_hash_function hash_function,
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
160 hash_table_test_function test_function)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
161 {
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
162 struct hash_table* hash_table = make_hash_table (size);
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
163 hash_table->hash_function = hash_function;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
164 hash_table->test_function = test_function;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
165 return hash_table;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
166 }
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
167
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 static void
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
169 grow_hash_table (struct hash_table *hash_table, hash_size_t new_size)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 {
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
171 hash_size_t old_size = hash_table->size;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
172 hentry *old_harray = hash_table->harray;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
174 hash_table->size = hash_table_size (new_size);
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
175 hash_table->harray = xnew_array (hentry, hash_table->size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 /* do the rehash on the "grown" table */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 {
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
179 long old_zero_set = hash_table->zero_set;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
180 void *old_zero_entry = hash_table->zero_entry;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
181 clrhash (hash_table);
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
182 hash_table->zero_set = old_zero_set;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
183 hash_table->zero_entry = old_zero_entry;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
184 rehash (old_harray, hash_table, old_size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
187 xfree (old_harray);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 void
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
191 puthash (CONST void *key, void *contents, struct hash_table *hash_table)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 {
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 0
diff changeset
193 if (!key)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 {
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
195 hash_table->zero_entry = contents;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
196 hash_table->zero_set = 1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 }
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
198 else
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 {
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
200 hash_table_test_function test_function = hash_table->test_function;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
201 hash_size_t size = hash_table->size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
202 hentry *harray = hash_table->harray;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
203 unsigned int hcode_initial =
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
204 hash_table->hash_function ?
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
205 hash_table->hash_function (key) :
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
206 (unsigned long) key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
207 unsigned int hcode = hcode_initial % size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
208 size_t h2 = size - 2;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
209 unsigned int incr = 1 + (hcode_initial % h2);
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
210 CONST void *e_key = harray [hcode].key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
211 CONST void *oldcontents;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
213 if (e_key && KEYS_DIFFER_P (e_key, key, test_function))
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
214 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
215 do
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
216 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
217 hcode += incr; if (hcode >= size) hcode -= size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
218 e_key = harray [hcode].key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
219 }
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
220 while (e_key && KEYS_DIFFER_P (e_key, key, test_function));
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
221 }
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
222 oldcontents = harray [hcode].contents;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
223 harray [hcode].key = key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
224 harray [hcode].contents = contents;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
225 /* If the entry that we used was a deleted entry,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
226 check for a non deleted entry of the same key,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
227 then delete it. */
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
228 if (!e_key && oldcontents == NULL_ENTRY)
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
229 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
230 hentry *e;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
232 do
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
233 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
234 hcode += incr; if (hcode >= size) hcode -= size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
235 e = &harray [hcode];
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
236 e_key = e->key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
237 }
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
238 while (e_key ?
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
239 KEYS_DIFFER_P (e_key, key, test_function):
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
240 e->contents == NULL_ENTRY);
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
241
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
242 if (e_key)
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
243 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
244 e->key = 0;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
245 e->contents = NULL_ENTRY;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
246 }
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
247 }
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
248
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
249 /* only increment the fullness when we used up a new hentry */
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
250 if (!e_key || KEYS_DIFFER_P (e_key, key, test_function))
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
251 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
252 hash_size_t comfortable_size = COMFORTABLE_SIZE (++(hash_table->fullness));
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
253 if (hash_table->size < comfortable_size)
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
254 grow_hash_table (hash_table, comfortable_size + 1);
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
255 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 static void
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
260 rehash (hentry *harray, struct hash_table *hash_table, hash_size_t size)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 hentry *limit = harray + size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 hentry *e;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 for (e = harray; e < limit; e++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 if (e->key)
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
267 puthash (e->key, e->contents, hash_table);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 0
diff changeset
271 void
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
272 remhash (CONST void *key, struct hash_table *hash_table)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 {
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 0
diff changeset
274 if (!key)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 {
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
276 hash_table->zero_entry = 0;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
277 hash_table->zero_set = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 }
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
279 else
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 {
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
281 hentry *harray = hash_table->harray;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
282 hash_table_test_function test_function = hash_table->test_function;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
283 hash_size_t size = hash_table->size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
284 unsigned int hcode_initial =
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
285 (hash_table->hash_function) ?
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
286 (hash_table->hash_function (key)) :
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
287 ((unsigned long) key);
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
288 unsigned int hcode = hcode_initial % size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
289 hentry *e = &harray [hcode];
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
290 CONST void *e_key = e->key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
291
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
292 if (e_key ?
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
293 KEYS_DIFFER_P (e_key, key, test_function) :
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
294 e->contents == NULL_ENTRY)
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
295 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
296 size_t h2 = size - 2;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
297 unsigned int incr = 1 + (hcode_initial % h2);
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
298 do
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
299 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
300 hcode += incr; if (hcode >= size) hcode -= size;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
301 e = &harray [hcode];
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
302 e_key = e->key;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
303 }
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
304 while (e_key?
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
305 KEYS_DIFFER_P (e_key, key, test_function):
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
306 e->contents == NULL_ENTRY);
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
307 }
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
308 if (e_key)
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
309 {
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
310 e->key = 0;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
311 e->contents = NULL_ENTRY;
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
312 /* Note: you can't do fullness-- here, it breaks the world. */
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 380
diff changeset
313 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 }
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
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
318 maphash (maphash_function mf, struct hash_table *hash_table, void *arg)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 hentry *e;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 hentry *limit;
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 0
diff changeset
322
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
323 if (hash_table->zero_set)
241
f955c73f5258 Import from CVS: tag r20-5b19
cvs
parents: 185
diff changeset
324 {
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
325 if (mf (0, hash_table->zero_entry, arg))
241
f955c73f5258 Import from CVS: tag r20-5b19
cvs
parents: 185
diff changeset
326 return;
f955c73f5258 Import from CVS: tag r20-5b19
cvs
parents: 185
diff changeset
327 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
329 for (e = hash_table->harray, limit = e + hash_table->size; e < limit; e++)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 {
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
331 if (e->key && mf (e->key, e->contents, arg))
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
332 return;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 0
diff changeset
336 void
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
337 map_remhash (remhash_predicate predicate, struct hash_table *hash_table, void *arg)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 hentry *e;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 hentry *limit;
173
8eaf7971accc Import from CVS: tag r20-3b13
cvs
parents: 0
diff changeset
341
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
342 if (hash_table->zero_set && predicate (0, hash_table->zero_entry, arg))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 {
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
344 hash_table->zero_set = 0;
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
345 hash_table->zero_entry = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
348 for (e = hash_table->harray, limit = e + hash_table->size; e < limit; e++)
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
349 if (predicate (e->key, e->contents, arg))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 e->key = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 e->contents = NULL_ENTRY;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 }