*** empty log message ***
[bpt/guile.git] / libguile / eq.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library 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 GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 \f
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include "libguile/_scm.h"
24 #include "libguile/ramap.h"
25 #include "libguile/stackchk.h"
26 #include "libguile/strorder.h"
27 #include "libguile/async.h"
28 #include "libguile/root.h"
29 #include "libguile/smob.h"
30 #include "libguile/unif.h"
31 #include "libguile/vectors.h"
32
33 #include "libguile/validate.h"
34 #include "libguile/eq.h"
35 \f
36
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 \f
41
42 SCM_DEFINE1 (scm_eq_p, "eq?", scm_tc7_rpsubr,
43 (SCM x, SCM y),
44 "Return @code{#t} iff @var{x} references the same object as @var{y}.\n"
45 "@code{eq?} is similar to @code{eqv?} except that in some cases it is\n"
46 "capable of discerning distinctions finer than those detectable by\n"
47 "@code{eqv?}.")
48 #define FUNC_NAME s_scm_eq_p
49 {
50 return SCM_BOOL (SCM_EQ_P (x, y));
51 }
52 #undef FUNC_NAME
53
54 /* We compare doubles in a special way for 'eqv?' to be able to
55 distinguish plus and minus zero and to identify NaNs.
56 */
57
58 static int
59 real_eqv (double x, double y)
60 {
61 return !memcmp (&x, &y, sizeof(double));
62 }
63
64 SCM_DEFINE1 (scm_eqv_p, "eqv?", scm_tc7_rpsubr,
65 (SCM x, SCM y),
66 "The @code{eqv?} procedure defines a useful equivalence relation on objects.\n"
67 "Briefly, it returns @code{#t} if @var{x} and @var{y} should normally be\n"
68 "regarded as the same object. This relation is left slightly open to\n"
69 "interpretation, but works for comparing immediate integers, characters,\n"
70 "and inexact numbers.")
71 #define FUNC_NAME s_scm_eqv_p
72 {
73 if (SCM_EQ_P (x, y))
74 return SCM_BOOL_T;
75 if (SCM_IMP (x))
76 return SCM_BOOL_F;
77 if (SCM_IMP (y))
78 return SCM_BOOL_F;
79 /* this ensures that types and scm_length are the same. */
80 if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
81 {
82 /* treat mixes of real and complex types specially */
83 if (SCM_SLOPPY_INEXACTP (x))
84 {
85 if (SCM_SLOPPY_REALP (x))
86 return SCM_BOOL (SCM_SLOPPY_COMPLEXP (y)
87 && real_eqv (SCM_REAL_VALUE (x),
88 SCM_COMPLEX_REAL (y))
89 && 0.0 == SCM_COMPLEX_IMAG (y));
90 else
91 return SCM_BOOL (SCM_SLOPPY_REALP (y)
92 && real_eqv (SCM_COMPLEX_REAL (x),
93 SCM_REAL_VALUE (y))
94 && SCM_COMPLEX_IMAG (x) == 0.0);
95 }
96 return SCM_BOOL_F;
97 }
98 if (SCM_NUMP (x))
99 {
100 if (SCM_BIGP (x)) {
101 return SCM_BOOL (0 == scm_i_bigcmp (x, y));
102 } else if (SCM_SLOPPY_REALP (x)) {
103 return SCM_BOOL (real_eqv (SCM_REAL_VALUE (x), SCM_REAL_VALUE (y)));
104 } else { /* complex */
105 return SCM_BOOL (real_eqv (SCM_COMPLEX_REAL (x),
106 SCM_COMPLEX_REAL (y))
107 && real_eqv (SCM_COMPLEX_IMAG (x),
108 SCM_COMPLEX_IMAG (y)));
109 }
110 }
111 return SCM_BOOL_F;
112 }
113 #undef FUNC_NAME
114
115
116 SCM_PRIMITIVE_GENERIC_1 (scm_equal_p, "equal?", scm_tc7_rpsubr,
117 (SCM x, SCM y),
118 "Return @code{#t} iff @var{x} and @var{y} are recursively @code{eqv?} equivalent.\n"
119 "@code{equal?} recursively compares the contents of pairs,\n"
120 "vectors, and strings, applying @code{eqv?} on other objects such as\n"
121 "numbers and symbols. A rule of thumb is that objects are generally\n"
122 "@code{equal?} if they print the same. @code{equal?} may fail to\n"
123 "terminate if its arguments are circular data structures.")
124 #define FUNC_NAME s_scm_equal_p
125 {
126 SCM_CHECK_STACK;
127 tailrecurse:
128 SCM_TICK;
129 if (SCM_EQ_P (x, y))
130 return SCM_BOOL_T;
131 if (SCM_IMP (x))
132 return SCM_BOOL_F;
133 if (SCM_IMP (y))
134 return SCM_BOOL_F;
135 if (SCM_CONSP (x) && SCM_CONSP (y))
136 {
137 if (SCM_FALSEP (scm_equal_p (SCM_CAR (x), SCM_CAR (y))))
138 return SCM_BOOL_F;
139 x = SCM_CDR(x);
140 y = SCM_CDR(y);
141 goto tailrecurse;
142 }
143 if (SCM_TYP7S (x) == scm_tc7_string && SCM_TYP7S (y) == scm_tc7_string)
144 return scm_string_equal_p (x, y);
145 /* This ensures that types and scm_length are the same. */
146 if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
147 {
148 /* treat mixes of real and complex types specially */
149 if (SCM_SLOPPY_INEXACTP (x))
150 {
151 if (SCM_SLOPPY_REALP (x))
152 return SCM_BOOL (SCM_SLOPPY_COMPLEXP (y)
153 && SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y)
154 && 0.0 == SCM_COMPLEX_IMAG (y));
155 else
156 return SCM_BOOL (SCM_SLOPPY_REALP (y)
157 && SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y)
158 && SCM_COMPLEX_IMAG (x) == 0.0);
159 }
160 return SCM_BOOL_F;
161 }
162 switch (SCM_TYP7 (x))
163 {
164 default:
165 break;
166 case scm_tc7_vector:
167 case scm_tc7_wvect:
168 return scm_vector_equal_p (x, y);
169 case scm_tc7_smob:
170 {
171 int i = SCM_SMOBNUM (x);
172 if (!(i < scm_numsmob))
173 return SCM_BOOL_F;
174 if (scm_smobs[i].equalp)
175 return (scm_smobs[i].equalp) (x, y);
176 else
177 break;
178 }
179 #if SCM_HAVE_ARRAYS
180 case scm_tc7_bvect: case scm_tc7_uvect: case scm_tc7_ivect:
181 case scm_tc7_fvect: case scm_tc7_cvect: case scm_tc7_dvect:
182 case scm_tc7_svect:
183 #if SCM_SIZEOF_LONG_LONG != 0
184 case scm_tc7_llvect:
185 #endif
186 case scm_tc7_byvect:
187 if (scm_tc16_array && scm_smobs[SCM_TC2SMOBNUM (scm_tc16_array)].equalp)
188 return scm_array_equal_p (x, y);
189 #endif
190 }
191 if (SCM_UNPACK (g_scm_equal_p))
192 return scm_call_generic_2 (g_scm_equal_p, x, y);
193 else
194 return SCM_BOOL_F;
195 }
196 #undef FUNC_NAME
197
198
199 \f
200
201
202
203 void
204 scm_init_eq ()
205 {
206 #include "libguile/eq.x"
207 }
208
209
210 /*
211 Local Variables:
212 c-file-style: "gnu"
213 End:
214 */