2
0

646-hashcode-string.patch 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. From 64042bb91aea5f854ca8a8938e2b3f7d1935e4f1 Mon Sep 17 00:00:00 2001
  2. From: Bruno Haible <[email protected]>
  3. Date: Wed, 30 Apr 2025 12:47:37 +0200
  4. Subject: [PATCH] New module hashcode-string1.
  5. * lib/hashcode-string1.h: New file.
  6. * lib/hashcode-string1.c: New file, based on lib/hash.c.
  7. * modules/hashcode-string1: New file.
  8. * lib/hash.h: Include hashcode-string1.h.
  9. (hash_string): Remove declaration.
  10. * lib/hash.c (hash_string): Remove function.
  11. * modules/hash (Depends-on): Add hashcode-string1.
  12. * lib/exclude.c: Include hashcode-string1.h.
  13. * modules/exclude (Depends-on): Add hashcode-string1.
  14. ---
  15. ChangeLog | 13 +++++++++
  16. lib/exclude.c | 1 +
  17. lib/hash.c | 59 ++++++--------------------------------
  18. lib/hash.h | 11 +++----
  19. lib/hashcode-string1.c | 62 ++++++++++++++++++++++++++++++++++++++++
  20. lib/hashcode-string1.h | 38 ++++++++++++++++++++++++
  21. modules/exclude | 1 +
  22. modules/hash | 1 +
  23. modules/hashcode-string1 | 24 ++++++++++++++++
  24. 9 files changed, 154 insertions(+), 56 deletions(-)
  25. create mode 100644 lib/hashcode-string1.c
  26. create mode 100644 lib/hashcode-string1.h
  27. create mode 100644 modules/hashcode-string1
  28. --- a/lib/exclude.c
  29. +++ b/lib/exclude.c
  30. @@ -36,6 +36,7 @@
  31. #include "filename.h"
  32. #include <fnmatch.h>
  33. #include "hash.h"
  34. +#include "hashcode-string1.h"
  35. #if GNULIB_MCEL_PREFER
  36. # include "mcel.h"
  37. #else
  38. --- a/lib/hash.c
  39. +++ b/lib/hash.c
  40. @@ -345,57 +345,6 @@ hash_do_for_each (const Hash_table *tabl
  41. return counter;
  42. }
  43. -/* Allocation and clean-up. */
  44. -
  45. -#if USE_DIFF_HASH
  46. -
  47. -/* About hashings, Paul Eggert writes to me (FP), on 1994-01-01: "Please see
  48. - B. J. McKenzie, R. Harries & T. Bell, Selecting a hashing algorithm,
  49. - Software--practice & experience 20, 2 (Feb 1990), 209-224. Good hash
  50. - algorithms tend to be domain-specific, so what's good for [diffutils'] io.c
  51. - may not be good for your application." */
  52. -
  53. -size_t
  54. -hash_string (const char *string, size_t n_buckets)
  55. -{
  56. -# define HASH_ONE_CHAR(Value, Byte) \
  57. - ((Byte) + rotl_sz (Value, 7))
  58. -
  59. - size_t value = 0;
  60. - unsigned char ch;
  61. -
  62. - for (; (ch = *string); string++)
  63. - value = HASH_ONE_CHAR (value, ch);
  64. - return value % n_buckets;
  65. -
  66. -# undef HASH_ONE_CHAR
  67. -}
  68. -
  69. -#else /* not USE_DIFF_HASH */
  70. -
  71. -/* This one comes from 'recode', and performs a bit better than the above as
  72. - per a few experiments. It is inspired from a hashing routine found in the
  73. - very old Cyber 'snoop', itself written in typical Greg Mansfield style.
  74. - (By the way, what happened to this excellent man? Is he still alive?) */
  75. -
  76. -size_t
  77. -hash_string (const char *string, size_t n_buckets)
  78. -{
  79. - size_t value = 0;
  80. - unsigned char ch;
  81. -
  82. - for (; (ch = *string); string++)
  83. - value = (value * 31 + ch) % n_buckets;
  84. - return value;
  85. -}
  86. -
  87. -#endif /* not USE_DIFF_HASH */
  88. -
  89. -void
  90. -hash_reset_tuning (Hash_tuning *tuning)
  91. -{
  92. - *tuning = default_tuning;
  93. -}
  94. /* If the user passes a NULL hasher, we hash the raw pointer. */
  95. static size_t
  96. @@ -418,6 +367,14 @@ raw_comparator (const void *a, const voi
  97. }
  98. +/* Allocation and clean-up. */
  99. +
  100. +void
  101. +hash_reset_tuning (Hash_tuning *tuning)
  102. +{
  103. + *tuning = default_tuning;
  104. +}
  105. +
  106. /* For the given hash TABLE, check the user supplied tuning structure for
  107. reasonable values, and return true if there is no gross error with it.
  108. Otherwise, definitively reset the TUNING field to some acceptable default
  109. --- a/lib/hash.h
  110. +++ b/lib/hash.h
  111. @@ -134,11 +134,6 @@ extern size_t hash_do_for_each (const Ha
  112. * Allocation and clean-up.
  113. */
  114. -/* Return a hash index for a NUL-terminated STRING between 0 and N_BUCKETS-1.
  115. - This is a convenience routine for constructing other hashing functions. */
  116. -extern size_t hash_string (const char *string, size_t n_buckets)
  117. - _GL_ATTRIBUTE_PURE;
  118. -
  119. extern void hash_reset_tuning (Hash_tuning *tuning);
  120. typedef size_t (*Hash_hasher) (const void *entry, size_t table_size);
  121. @@ -266,6 +261,12 @@ extern void *hash_remove (Hash_table *ta
  122. _GL_ATTRIBUTE_DEPRECATED
  123. extern void *hash_delete (Hash_table *table, const void *entry);
  124. +
  125. +# if GNULIB_HASHCODE_STRING1
  126. +/* Include declarations of module 'hashcode-string1'. */
  127. +# include "hashcode-string1.h"
  128. +# endif
  129. +
  130. # ifdef __cplusplus
  131. }
  132. # endif
  133. --- /dev/null
  134. +++ b/lib/hashcode-string1.c
  135. @@ -0,0 +1,62 @@
  136. +/* hashcode-string1.c -- compute a hash value from a NUL-terminated string.
  137. +
  138. + Copyright (C) 1998-2004, 2006-2007, 2009-2025 Free Software Foundation, Inc.
  139. +
  140. + This file is free software: you can redistribute it and/or modify
  141. + it under the terms of the GNU Lesser General Public License as
  142. + published by the Free Software Foundation; either version 2.1 of the
  143. + License, or (at your option) any later version.
  144. +
  145. + This file is distributed in the hope that it will be useful,
  146. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  147. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  148. + GNU Lesser General Public License for more details.
  149. +
  150. + You should have received a copy of the GNU Lesser General Public License
  151. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  152. +
  153. +#include <config.h>
  154. +
  155. +/* Specification. */
  156. +#include "hashcode-string1.h"
  157. +
  158. +#if USE_DIFF_HASH
  159. +
  160. +# include "bitrotate.h"
  161. +
  162. +/* About hashings, Paul Eggert writes to me (FP), on 1994-01-01: "Please see
  163. + B. J. McKenzie, R. Harries & T. Bell, Selecting a hashing algorithm,
  164. + Software--practice & experience 20, 2 (Feb 1990), 209-224. Good hash
  165. + algorithms tend to be domain-specific, so what's good for [diffutils'] io.c
  166. + may not be good for your application." */
  167. +
  168. +size_t
  169. +hash_string (const char *string, size_t tablesize)
  170. +{
  171. + size_t value = 0;
  172. + unsigned char ch;
  173. +
  174. + for (; (ch = *string); string++)
  175. + value = ch + rotl_sz (value, 7);
  176. + return value % tablesize;
  177. +}
  178. +
  179. +#else /* not USE_DIFF_HASH */
  180. +
  181. +/* This one comes from 'recode', and performs a bit better than the above as
  182. + per a few experiments. It is inspired from a hashing routine found in the
  183. + very old Cyber 'snoop', itself written in typical Greg Mansfield style.
  184. + (By the way, what happened to this excellent man? Is he still alive?) */
  185. +
  186. +size_t
  187. +hash_string (const char *string, size_t tablesize)
  188. +{
  189. + size_t value = 0;
  190. + unsigned char ch;
  191. +
  192. + for (; (ch = *string); string++)
  193. + value = (value * 31 + ch) % tablesize;
  194. + return value;
  195. +}
  196. +
  197. +#endif /* not USE_DIFF_HASH */
  198. --- /dev/null
  199. +++ b/lib/hashcode-string1.h
  200. @@ -0,0 +1,38 @@
  201. +/* hashcode-string1.h -- declaration for a simple hash function
  202. + Copyright (C) 1998-2004, 2006-2007, 2009-2025 Free Software Foundation, Inc.
  203. +
  204. + This file is free software: you can redistribute it and/or modify
  205. + it under the terms of the GNU Lesser General Public License as
  206. + published by the Free Software Foundation; either version 2.1 of the
  207. + License, or (at your option) any later version.
  208. +
  209. + This file is distributed in the hope that it will be useful,
  210. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  211. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  212. + GNU Lesser General Public License for more details.
  213. +
  214. + You should have received a copy of the GNU Lesser General Public License
  215. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  216. +
  217. +/* This file uses _GL_ATTRIBUTE_PURE. */
  218. +#if !_GL_CONFIG_H_INCLUDED
  219. + #error "Please include config.h first."
  220. +#endif
  221. +
  222. +#include <stddef.h>
  223. +
  224. +#ifdef __cplusplus
  225. +extern "C" {
  226. +#endif
  227. +
  228. +
  229. +/* Compute a hash code for a NUL-terminated string S,
  230. + and return the hash code modulo TABLESIZE.
  231. + The result is platform dependent: it depends on the size of the 'size_t'
  232. + type. */
  233. +extern size_t hash_string (char const *s, size_t tablesize) _GL_ATTRIBUTE_PURE;
  234. +
  235. +
  236. +#ifdef __cplusplus
  237. +}
  238. +#endif
  239. --- a/modules/exclude
  240. +++ b/modules/exclude
  241. @@ -12,6 +12,7 @@ filename
  242. fnmatch
  243. fopen-gnu
  244. hash
  245. +hashcode-string1
  246. mbscasecmp
  247. mbuiter [test "$GNULIB_MCEL_PREFER" != yes]
  248. nullptr
  249. --- a/modules/hash
  250. +++ b/modules/hash
  251. @@ -14,6 +14,7 @@ next-prime
  252. bool
  253. stdint-h
  254. xalloc-oversized
  255. +hashcode-string1
  256. configure.ac:
  257. --- /dev/null
  258. +++ b/modules/hashcode-string1
  259. @@ -0,0 +1,24 @@
  260. +Description:
  261. +Compute a hash value for a NUL-terminated string.
  262. +
  263. +Files:
  264. +lib/hashcode-string1.h
  265. +lib/hashcode-string1.c
  266. +
  267. +Depends-on:
  268. +bitrotate
  269. +
  270. +configure.ac:
  271. +gl_MODULE_INDICATOR([hashcode-string1])
  272. +
  273. +Makefile.am:
  274. +lib_SOURCES += hashcode-string1.h hashcode-string1.c
  275. +
  276. +Include:
  277. +"hashcode-string1.h"
  278. +
  279. +License:
  280. +LGPLv2+
  281. +
  282. +Maintainer:
  283. +Jim Meyering