hash.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Copyright (C) 1995,1996,1997, 2000, 2002 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, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/chars.h"
  44. #include "libguile/ports.h"
  45. #include "libguile/vectors.h"
  46. #include "libguile/validate.h"
  47. #include "libguile/hash.h"
  48. #ifndef floor
  49. extern double floor();
  50. #endif
  51. /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */
  52. /* Dirk:FIXME:: scm_hasher could be made static. */
  53. unsigned long
  54. scm_hasher(SCM obj, unsigned long n, scm_sizet d)
  55. {
  56. switch (SCM_ITAG3 (obj)) {
  57. case scm_tc3_int_1:
  58. case scm_tc3_int_2:
  59. return SCM_INUM(obj) % n; /* SCM_INUMP(obj) */
  60. case scm_tc3_imm24:
  61. if (SCM_CHARP(obj))
  62. return (unsigned)(scm_downcase(SCM_CHAR(obj))) % n;
  63. switch (SCM_UNPACK (obj)) {
  64. #ifndef SICP
  65. case SCM_EOL:
  66. d = 256;
  67. break;
  68. #endif
  69. case SCM_BOOL_T:
  70. d = 257;
  71. break;
  72. case SCM_BOOL_F:
  73. d = 258;
  74. break;
  75. case SCM_EOF_VAL:
  76. d = 259;
  77. break;
  78. default:
  79. d = 263; /* perhaps should be error */
  80. }
  81. return d % n;
  82. default:
  83. return 263 % n; /* perhaps should be error */
  84. case scm_tc3_cons:
  85. switch SCM_TYP7(obj) {
  86. default:
  87. return 263 % n;
  88. case scm_tc7_smob:
  89. switch SCM_TYP16(obj) {
  90. case scm_tc16_big:
  91. return SCM_INUM(scm_modulo(obj, SCM_MAKINUM(n)));
  92. default:
  93. return 263 % n;
  94. case scm_tc16_real:
  95. {
  96. double r = SCM_REAL_VALUE(obj);
  97. if (floor(r)==r) {
  98. obj = scm_inexact_to_exact (obj);
  99. if SCM_IMP(obj) return SCM_INUM(obj) % n;
  100. return SCM_INUM(scm_modulo(obj, SCM_MAKINUM(n)));
  101. }
  102. }
  103. case scm_tc16_complex:
  104. obj = scm_number_to_string(obj, SCM_MAKINUM(10));
  105. }
  106. case scm_tcs_symbols:
  107. case scm_tc7_string:
  108. case scm_tc7_substring:
  109. return scm_strhash(SCM_ROUCHARS(obj), (scm_sizet) SCM_ROLENGTH(obj), n);
  110. case scm_tc7_wvect:
  111. case scm_tc7_vector:
  112. {
  113. scm_sizet len = SCM_LENGTH(obj);
  114. SCM *data = SCM_VELTS(obj);
  115. if (len>5)
  116. {
  117. scm_sizet i = d/2;
  118. unsigned long h = 1;
  119. while (i--) h = ((h<<8) + (scm_hasher(data[h % len], n, 2))) % n;
  120. return h;
  121. }
  122. else
  123. {
  124. scm_sizet i = len;
  125. unsigned long h = (n)-1;
  126. while (i--) h = ((h<<8) + (scm_hasher(data[i], n, d/len))) % n;
  127. return h;
  128. }
  129. }
  130. case scm_tcs_cons_imcar:
  131. case scm_tcs_cons_nimcar:
  132. if (d) return (scm_hasher(SCM_CAR(obj), n, d/2)+scm_hasher(SCM_CDR(obj), n, d/2)) % n;
  133. else return 1;
  134. case scm_tc7_port:
  135. return ((SCM_RDNG & SCM_CELL_WORD_0 (obj)) ? 260 : 261) % n;
  136. case scm_tcs_closures:
  137. case scm_tc7_contin:
  138. case scm_tcs_subrs:
  139. return 262 % n;
  140. }
  141. }
  142. }
  143. unsigned int
  144. scm_ihashq (SCM obj, unsigned int n)
  145. {
  146. return (SCM_UNPACK (obj) >> 1) % n;
  147. }
  148. SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0,
  149. (SCM key, SCM size),
  150. "Determine a hash value for KEY that is suitable for lookups in\n"
  151. "a hashtable of size SIZE, where eq? is used as the equality\n"
  152. "predicate. The function returns an integer in the range 0 to\n"
  153. "SIZE - 1. NOTE that `hashq' may use internal addresses.\n"
  154. "Thus two calls to hashq where the keys are eq? are not\n"
  155. "guaranteed to deliver the same value if the key object gets\n"
  156. "garbage collected in between. This can happen, for example\n"
  157. "with symbols: (hashq 'foo n) (gc) (hashq 'foo n) may produce two\n"
  158. "different values, since 'foo will be garbage collected.")
  159. #define FUNC_NAME s_scm_hashq
  160. {
  161. SCM_VALIDATE_INUM_MIN (2, size, 0);
  162. return SCM_MAKINUM (scm_ihashq (key, SCM_INUM (size)));
  163. }
  164. #undef FUNC_NAME
  165. unsigned int
  166. scm_ihashv (SCM obj, unsigned int n)
  167. {
  168. if (SCM_CHARP(obj))
  169. return ((unsigned int)(scm_downcase(SCM_CHAR(obj)))) % n; /* downcase!?!! */
  170. if (SCM_NUMP(obj))
  171. return (unsigned int) scm_hasher(obj, n, 10);
  172. else
  173. return SCM_UNPACK (obj) % n;
  174. }
  175. SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0,
  176. (SCM key, SCM size),
  177. "Determine a hash value for KEY that is suitable for lookups in\n"
  178. "a hashtable of size SIZE, where eqv? is used as the equality\n"
  179. "predicate. The function returns an integer in the range 0 to\n"
  180. "SIZE - 1. NOTE that (hashv key) may use internal addresses.\n"
  181. "Thus two calls to hashv where the keys are eqv? are not\n"
  182. "guaranteed to deliver the same value if the key object gets\n"
  183. "garbage collected in between. This can happen, for example\n"
  184. "with symbols: (hashv 'foo n) (gc) (hashv 'foo n) may produce two\n"
  185. "different values, since 'foo will be garbage collected.")
  186. #define FUNC_NAME s_scm_hashv
  187. {
  188. SCM_VALIDATE_INUM_MIN (2, size, 0);
  189. return SCM_MAKINUM (scm_ihashv (key, SCM_INUM (size)));
  190. }
  191. #undef FUNC_NAME
  192. unsigned int
  193. scm_ihash (SCM obj, unsigned int n)
  194. {
  195. return (unsigned int)scm_hasher (obj, n, 10);
  196. }
  197. SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
  198. (SCM key, SCM size),
  199. "Determine a hash value for KEY that is suitable for lookups in\n"
  200. "a hashtable of size SIZE, where equal? is used as the equality\n"
  201. "predicate. The function returns an integer in the range 0 to\n"
  202. "SIZE - 1.")
  203. #define FUNC_NAME s_scm_hash
  204. {
  205. SCM_VALIDATE_INUM_MIN (2, size, 0);
  206. return SCM_MAKINUM (scm_ihash (key, SCM_INUM (size)));
  207. }
  208. #undef FUNC_NAME
  209. void
  210. scm_init_hash ()
  211. {
  212. #include "libguile/hash.x"
  213. }
  214. /*
  215. Local Variables:
  216. c-file-style: "gnu"
  217. End:
  218. */