C files should #include only the header files they need, not
[bpt/guile.git] / libguile / hash.c
1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41 \f
42
43 #include <stdio.h>
44 #include "_scm.h"
45 #include "chars.h"
46
47 #include "hash.h"
48 \f
49
50 #ifndef floor
51 extern double floor();
52 #endif
53
54 #ifdef __STDC__
55 unsigned long
56 scm_hasher(SCM obj, unsigned long n, scm_sizet d)
57 #else
58 unsigned long
59 scm_hasher(obj, n, d)
60 SCM obj;
61 unsigned long n;
62 scm_sizet d;
63 #endif
64 {
65 switch (7 & (int) obj) {
66 case 2: case 6: /* SCM_INUMP(obj) */
67 return SCM_INUM(obj) % n;
68 case 4:
69 if SCM_ICHRP(obj)
70 return (unsigned)(scm_downcase(SCM_ICHR(obj))) % n;
71 switch ((int) obj) {
72 #ifndef SICP
73 case (int) SCM_EOL: d = 256; break;
74 #endif
75 case (int) SCM_BOOL_T: d = 257; break;
76 case (int) SCM_BOOL_F: d = 258; break;
77 case (int) SCM_EOF_VAL: d = 259; break;
78 default: d = 263; /* perhaps should be error */
79 }
80 return d % n;
81 default: return 263 % n; /* perhaps should be error */
82 case 0:
83 switch SCM_TYP7(obj) {
84 default: return 263 % n;
85 case scm_tc7_smob:
86 switch SCM_TYP16(obj) {
87 case scm_tcs_bignums:
88 bighash: return SCM_INUM(scm_modulo(obj, SCM_MAKINUM(n)));
89 default: return 263 % n;
90 #ifdef SCM_FLOATS
91 case scm_tc16_flo:
92 if SCM_REALP(obj) {
93 double r = SCM_REALPART(obj);
94 if (floor(r)==r) {
95 obj = scm_inexact_to_exact (obj);
96 if SCM_IMP(obj) return SCM_INUM(obj) % n;
97 goto bighash;
98 }
99 }
100 obj = scm_number_to_string(obj, SCM_MAKINUM(10));
101 #endif
102 }
103 case scm_tcs_symbols:
104 case scm_tc7_string:
105 case scm_tc7_mb_string:
106 case scm_tc7_substring:
107 case scm_tc7_mb_substring:
108 return scm_strhash(SCM_ROUCHARS(obj), (scm_sizet) SCM_ROLENGTH(obj), n);
109 case scm_tc7_wvect:
110 case scm_tc7_vector:
111 {
112 scm_sizet len = SCM_LENGTH(obj);
113 SCM *data = SCM_VELTS(obj);
114 if (len>5)
115 {
116 scm_sizet i = d/2;
117 unsigned long h = 1;
118 while (i--) h = ((h<<8) + (scm_hasher(data[h % len], n, 2))) % n;
119 return h;
120 }
121 else
122 {
123 scm_sizet i = len;
124 unsigned long h = (n)-1;
125 while (i--) h = ((h<<8) + (scm_hasher(data[i], n, d/len))) % n;
126 return h;
127 }
128 }
129 case scm_tcs_cons_imcar: case scm_tcs_cons_nimcar:
130 if (d) return (scm_hasher(SCM_CAR(obj), n, d/2)+scm_hasher(SCM_CDR(obj), n, d/2)) % n;
131 else return 1;
132 case scm_tc7_port:
133 return ((SCM_RDNG & SCM_CAR(obj)) ? 260 : 261) % n;
134 case scm_tcs_closures: case scm_tc7_contin: case scm_tcs_subrs:
135 return 262 % n;
136 }
137 }
138 }
139
140
141 \f
142
143 #ifdef __STDC__
144 unsigned int
145 scm_ihashq (SCM obj, unsigned int n)
146 #else
147 unsigned int
148 scm_ihashq (obj, n)
149 SCM obj;
150 unsigned int n;
151 #endif
152 {
153 return (((unsigned int) obj) >> 1) % n;
154 }
155
156
157 SCM_PROC(s_hashq, "hashq", 2, 0, 0, scm_hashq);
158 #ifdef __STDC__
159 SCM
160 scm_hashq(SCM obj, SCM n)
161 #else
162 SCM
163 scm_hashq(obj, n)
164 SCM obj;
165 SCM n;
166 #endif
167 {
168 SCM_ASSERT(SCM_INUMP(n) && 0 <= n, n, SCM_ARG2, s_hashq);
169 return SCM_MAKINUM(scm_ihashq (obj, SCM_INUM (n)));
170 }
171
172
173 \f
174
175 #ifdef __STDC__
176 unsigned int
177 scm_ihashv (SCM obj, unsigned int n)
178 #else
179 unsigned int
180 scm_ihashv (obj, n)
181 SCM obj;
182 unsigned int n;
183 #endif
184 {
185 if (SCM_ICHRP(obj))
186 return ((unsigned int)(scm_downcase(SCM_ICHR(obj)))) % n; /* downcase!?!! */
187
188 if (SCM_NIMP(obj) && SCM_NUMP(obj))
189 return (unsigned int) scm_hasher(obj, n, 10);
190 else
191 return ((unsigned int)obj) % n;
192 }
193
194
195 SCM_PROC(s_hashv, "hashv", 2, 0, 0, scm_hashv);
196 #ifdef __STDC__
197 SCM
198 scm_hashv(SCM obj, SCM n)
199 #else
200 SCM
201 scm_hashv(obj, n)
202 SCM obj;
203 SCM n;
204 #endif
205 {
206 SCM_ASSERT(SCM_INUMP(n) && 0 <= n, n, SCM_ARG2, s_hashv);
207 return SCM_MAKINUM(scm_ihashv (obj, SCM_INUM (n)));
208 }
209
210
211 \f
212
213 #ifdef __STDC__
214 unsigned int
215 scm_ihash (SCM obj, unsigned int n)
216 #else
217 unsigned int
218 scm_ihash (obj, n)
219 SCM obj;
220 unsigned int n;
221 #endif
222 {
223 return (unsigned int)scm_hasher (obj, n, 10);
224 }
225
226 SCM_PROC(s_hash, "hash", 2, 0, 0, scm_hash);
227 #ifdef __STDC__
228 SCM
229 scm_hash(SCM obj, SCM n)
230 #else
231 SCM
232 scm_hash(obj, n)
233 SCM obj;
234 SCM n;
235 #endif
236 {
237 SCM_ASSERT(SCM_INUMP(n) && 0 <= n, n, SCM_ARG2, s_hash);
238 return SCM_MAKINUM(scm_ihash(obj, SCM_INUM(n)));
239 }
240
241
242 \f
243
244 #ifdef __STDC__
245 void
246 scm_init_hash (void)
247 #else
248 void
249 scm_init_hash ()
250 #endif
251 {
252 #include "hash.x"
253 }
254