PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/libguile/eq.c

#
C | 397 lines | 330 code | 33 blank | 34 comment | 46 complexity | 5c4edb4bb21d8d4d290a50ade354d2c5 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, GPL-2.0
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001,2003, 2004, 2006, 2009, 2010, 2011 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 License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <math.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/array-map.h"
  24. #include "libguile/stackchk.h"
  25. #include "libguile/strorder.h"
  26. #include "libguile/async.h"
  27. #include "libguile/root.h"
  28. #include "libguile/smob.h"
  29. #include "libguile/arrays.h"
  30. #include "libguile/vectors.h"
  31. #include "libguile/hashtab.h"
  32. #include "libguile/bytevectors.h"
  33. #include "libguile/struct.h"
  34. #include "libguile/goops.h"
  35. #include "libguile/validate.h"
  36. #include "libguile/eq.h"
  37. #include "libguile/private-options.h"
  38. #ifdef HAVE_STRING_H
  39. #include <string.h>
  40. #endif
  41. static SCM scm_i_eq_p (SCM x, SCM y, SCM rest);
  42. SCM_DEFINE (scm_i_eq_p, "eq?", 0, 2, 1,
  43. (SCM x, SCM y, SCM rest),
  44. "Return @code{#t} if @var{x} and @var{y} are the same object,\n"
  45. "except for numbers and characters. For example,\n"
  46. "\n"
  47. "@example\n"
  48. "(define x (vector 1 2 3))\n"
  49. "(define y (vector 1 2 3))\n"
  50. "\n"
  51. "(eq? x x) @result{} #t\n"
  52. "(eq? x y) @result{} #f\n"
  53. "@end example\n"
  54. "\n"
  55. "Numbers and characters are not equal to any other object, but\n"
  56. "the problem is they're not necessarily @code{eq?} to themselves\n"
  57. "either. This is even so when the number comes directly from a\n"
  58. "variable,\n"
  59. "\n"
  60. "@example\n"
  61. "(let ((n (+ 2 3)))\n"
  62. " (eq? n n)) @result{} *unspecified*\n"
  63. "@end example\n"
  64. "\n"
  65. "Generally @code{eqv?} should be used when comparing numbers or\n"
  66. "characters. @code{=} or @code{char=?} can be used too.\n"
  67. "\n"
  68. "It's worth noting that end-of-list @code{()}, @code{#t},\n"
  69. "@code{#f}, a symbol of a given name, and a keyword of a given\n"
  70. "name, are unique objects. There's just one of each, so for\n"
  71. "instance no matter how @code{()} arises in a program, it's the\n"
  72. "same object and can be compared with @code{eq?},\n"
  73. "\n"
  74. "@example\n"
  75. "(define x (cdr '(123)))\n"
  76. "(define y (cdr '(456)))\n"
  77. "(eq? x y) @result{} #t\n"
  78. "\n"
  79. "(define x (string->symbol \"foo\"))\n"
  80. "(eq? x 'foo) @result{} #t\n"
  81. "@end example")
  82. #define FUNC_NAME s_scm_i_eq_p
  83. {
  84. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  85. return SCM_BOOL_T;
  86. while (scm_is_pair (rest))
  87. {
  88. if (!scm_is_eq (x, y))
  89. return SCM_BOOL_F;
  90. x = y;
  91. y = scm_car (rest);
  92. rest = scm_cdr (rest);
  93. }
  94. return scm_from_bool (scm_is_eq (x, y));
  95. }
  96. #undef FUNC_NAME
  97. SCM
  98. scm_eq_p (SCM x, SCM y)
  99. {
  100. return scm_from_bool (scm_is_eq (x, y));
  101. }
  102. /* We compare doubles in a special way for 'eqv?' to be able to
  103. distinguish plus and minus zero and to identify NaNs.
  104. */
  105. static int
  106. real_eqv (double x, double y)
  107. {
  108. return !memcmp (&x, &y, sizeof(double))
  109. || (SCM_UNLIKELY (isnan (x)) && SCM_UNLIKELY (isnan (y)));
  110. }
  111. SCM
  112. scm_real_equalp (SCM x, SCM y)
  113. {
  114. return scm_from_bool (real_eqv (SCM_REAL_VALUE (x),
  115. SCM_REAL_VALUE (y)));
  116. }
  117. SCM
  118. scm_bigequal (SCM x, SCM y)
  119. {
  120. return scm_from_bool (scm_i_bigcmp (x, y) == 0);
  121. }
  122. SCM
  123. scm_complex_equalp (SCM x, SCM y)
  124. {
  125. return scm_from_bool (real_eqv (SCM_COMPLEX_REAL (x),
  126. SCM_COMPLEX_REAL (y))
  127. && real_eqv (SCM_COMPLEX_IMAG (x),
  128. SCM_COMPLEX_IMAG (y)));
  129. }
  130. SCM
  131. scm_i_fraction_equalp (SCM x, SCM y)
  132. {
  133. return scm_from_bool
  134. (scm_is_true (scm_equal_p (SCM_FRACTION_NUMERATOR (x),
  135. SCM_FRACTION_NUMERATOR (y)))
  136. && scm_is_true (scm_equal_p (SCM_FRACTION_DENOMINATOR (x),
  137. SCM_FRACTION_DENOMINATOR (y))));
  138. }
  139. static SCM scm_i_eqv_p (SCM x, SCM y, SCM rest);
  140. #include <stdio.h>
  141. SCM_DEFINE (scm_i_eqv_p, "eqv?", 0, 2, 1,
  142. (SCM x, SCM y, SCM rest),
  143. "Return @code{#t} if @var{x} and @var{y} are the same object, or\n"
  144. "for characters and numbers the same value.\n"
  145. "\n"
  146. "On objects except characters and numbers, @code{eqv?} is the\n"
  147. "same as @code{eq?}, it's true if @var{x} and @var{y} are the\n"
  148. "same object.\n"
  149. "\n"
  150. "If @var{x} and @var{y} are numbers or characters, @code{eqv?}\n"
  151. "compares their type and value. An exact number is not\n"
  152. "@code{eqv?} to an inexact number (even if their value is the\n"
  153. "same).\n"
  154. "\n"
  155. "@example\n"
  156. "(eqv? 3 (+ 1 2)) @result{} #t\n"
  157. "(eqv? 1 1.0) @result{} #f\n"
  158. "@end example")
  159. #define FUNC_NAME s_scm_i_eqv_p
  160. {
  161. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  162. return SCM_BOOL_T;
  163. while (!scm_is_null (rest))
  164. {
  165. if (!scm_is_true (scm_eqv_p (x, y)))
  166. return SCM_BOOL_F;
  167. x = y;
  168. y = scm_car (rest);
  169. rest = scm_cdr (rest);
  170. }
  171. return scm_eqv_p (x, y);
  172. }
  173. #undef FUNC_NAME
  174. SCM scm_eqv_p (SCM x, SCM y)
  175. #define FUNC_NAME s_scm_i_eqv_p
  176. {
  177. if (scm_is_eq (x, y))
  178. return SCM_BOOL_T;
  179. if (SCM_IMP (x))
  180. return SCM_BOOL_F;
  181. if (SCM_IMP (y))
  182. return SCM_BOOL_F;
  183. /* this ensures that types and scm_length are the same. */
  184. if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
  185. return SCM_BOOL_F;
  186. switch (SCM_TYP7 (x))
  187. {
  188. default:
  189. break;
  190. case scm_tc7_number:
  191. switch SCM_TYP16 (x)
  192. {
  193. case scm_tc16_big:
  194. return scm_bigequal (x, y);
  195. case scm_tc16_real:
  196. return scm_real_equalp (x, y);
  197. case scm_tc16_complex:
  198. return scm_complex_equalp (x, y);
  199. case scm_tc16_fraction:
  200. return scm_i_fraction_equalp (x, y);
  201. }
  202. }
  203. return SCM_BOOL_F;
  204. }
  205. #undef FUNC_NAME
  206. static SCM scm_i_equal_p (SCM, SCM, SCM);
  207. SCM_PRIMITIVE_GENERIC (scm_i_equal_p, "equal?", 0, 2, 1,
  208. (SCM x, SCM y, SCM rest),
  209. "Return @code{#t} if @var{x} and @var{y} are the same type, and\n"
  210. "their contents or value are equal.\n"
  211. "\n"
  212. "For a pair, string, vector or array, @code{equal?} compares the\n"
  213. "contents, and does so using using the same @code{equal?}\n"
  214. "recursively, so a deep structure can be traversed.\n"
  215. "\n"
  216. "@example\n"
  217. "(equal? (list 1 2 3) (list 1 2 3)) @result{} #t\n"
  218. "(equal? (list 1 2 3) (vector 1 2 3)) @result{} #f\n"
  219. "@end example\n"
  220. "\n"
  221. "For other objects, @code{equal?} compares as per @code{eqv?},\n"
  222. "which means characters and numbers are compared by type and\n"
  223. "value (and like @code{eqv?}, exact and inexact numbers are not\n"
  224. "@code{equal?}, even if their value is the same).\n"
  225. "\n"
  226. "@example\n"
  227. "(equal? 3 (+ 1 2)) @result{} #t\n"
  228. "(equal? 1 1.0) @result{} #f\n"
  229. "@end example\n"
  230. "\n"
  231. "Hash tables are currently only compared as per @code{eq?}, so\n"
  232. "two different tables are not @code{equal?}, even if their\n"
  233. "contents are the same.\n"
  234. "\n"
  235. "@code{equal?} does not support circular data structures, it may\n"
  236. "go into an infinite loop if asked to compare two circular lists\n"
  237. "or similar.\n"
  238. "\n"
  239. "New application-defined object types (Smobs) have an\n"
  240. "@code{equalp} handler which is called by @code{equal?}. This\n"
  241. "lets an application traverse the contents or control what is\n"
  242. "considered @code{equal?} for two such objects. If there's no\n"
  243. "handler, the default is to just compare as per @code{eq?}.")
  244. #define FUNC_NAME s_scm_i_equal_p
  245. {
  246. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  247. return SCM_BOOL_T;
  248. while (!scm_is_null (rest))
  249. {
  250. if (!scm_is_true (scm_equal_p (x, y)))
  251. return SCM_BOOL_F;
  252. x = y;
  253. y = scm_car (rest);
  254. rest = SCM_CDR (rest);
  255. }
  256. return scm_equal_p (x, y);
  257. }
  258. #undef FUNC_NAME
  259. SCM
  260. scm_equal_p (SCM x, SCM y)
  261. #define FUNC_NAME s_scm_i_equal_p
  262. {
  263. SCM_CHECK_STACK;
  264. tailrecurse:
  265. SCM_TICK;
  266. if (scm_is_eq (x, y))
  267. return SCM_BOOL_T;
  268. if (SCM_IMP (x))
  269. return SCM_BOOL_F;
  270. if (SCM_IMP (y))
  271. return SCM_BOOL_F;
  272. if (scm_is_pair (x) && scm_is_pair (y))
  273. {
  274. if (scm_is_false (scm_equal_p (SCM_CAR (x), SCM_CAR (y))))
  275. return SCM_BOOL_F;
  276. x = SCM_CDR(x);
  277. y = SCM_CDR(y);
  278. goto tailrecurse;
  279. }
  280. if (SCM_TYP7 (x) == scm_tc7_smob && SCM_TYP16 (x) == SCM_TYP16 (y))
  281. {
  282. int i = SCM_SMOBNUM (x);
  283. if (!(i < scm_numsmob))
  284. return SCM_BOOL_F;
  285. if (scm_smobs[i].equalp)
  286. return (scm_smobs[i].equalp) (x, y);
  287. else
  288. goto generic_equal;
  289. }
  290. /* This ensures that types and scm_length are the same. */
  291. if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
  292. {
  293. /* Vectors can be equal to one-dimensional arrays.
  294. */
  295. if (scm_is_array (x) && scm_is_array (y))
  296. return scm_array_equal_p (x, y);
  297. return SCM_BOOL_F;
  298. }
  299. switch (SCM_TYP7 (x))
  300. {
  301. default:
  302. /* Check equality between structs of equal type (see cell-type test above). */
  303. if (SCM_STRUCTP (x))
  304. {
  305. if (SCM_INSTANCEP (x))
  306. goto generic_equal;
  307. else
  308. return scm_i_struct_equalp (x, y);
  309. }
  310. break;
  311. case scm_tc7_number:
  312. switch SCM_TYP16 (x)
  313. {
  314. case scm_tc16_big:
  315. return scm_bigequal (x, y);
  316. case scm_tc16_real:
  317. return scm_real_equalp (x, y);
  318. case scm_tc16_complex:
  319. return scm_complex_equalp (x, y);
  320. case scm_tc16_fraction:
  321. return scm_i_fraction_equalp (x, y);
  322. default:
  323. /* assert not reached? */
  324. return SCM_BOOL_F;
  325. }
  326. case scm_tc7_pointer:
  327. return scm_from_bool (SCM_POINTER_VALUE (x) == SCM_POINTER_VALUE (y));
  328. case scm_tc7_string:
  329. return scm_string_equal_p (x, y);
  330. case scm_tc7_bytevector:
  331. return scm_bytevector_eq_p (x, y);
  332. case scm_tc7_array:
  333. return scm_array_equal_p (x, y);
  334. case scm_tc7_bitvector:
  335. return scm_i_bitvector_equal_p (x, y);
  336. case scm_tc7_vector:
  337. case scm_tc7_wvect:
  338. return scm_i_vector_equal_p (x, y);
  339. }
  340. /* Otherwise just return false. Dispatching to the generic is the wrong thing
  341. here, as we can hit this case for any two objects of the same type that we
  342. think are distinct, like different symbols. */
  343. return SCM_BOOL_F;
  344. generic_equal:
  345. if (SCM_UNPACK (g_scm_i_equal_p))
  346. return scm_call_2 (g_scm_i_equal_p, x, y);
  347. else
  348. return SCM_BOOL_F;
  349. }
  350. #undef FUNC_NAME
  351. void
  352. scm_init_eq ()
  353. {
  354. #include "libguile/eq.x"
  355. }
  356. /*
  357. Local Variables:
  358. c-file-style: "gnu"
  359. End:
  360. */