y_stringhash.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /**--------------------------------------------------------------------------**\
  2. ========================================
  3. y_stringhash - Fast string comparisons
  4. ========================================
  5. Description:
  6. Allows you to hash strings at compile time to use them in a switch.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the YSI stringhash include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 2.0
  43. Changelog:
  44. 06/03/11:
  45. Changed the order of some letters to better support numbers in _I.
  46. 01/03/11:
  47. Rewrote compile-time hashes to not require commas.
  48. 25/10/10:
  49. Updated comments.
  50. Added to YSI 1.0.
  51. Added FNV1 and FNV1a hashes to avoid collisions.
  52. 19/08/10:
  53. First version.
  54. Functions:
  55. stock:
  56. YHash - Generate a string hash at run time.
  57. Definitions:
  58. _H - Generate a string hash at compile time.
  59. _I - Generate a case insensitive string hash at compile time.
  60. \**--------------------------------------------------------------------------**/
  61. #if defined _INC_y_stringhash
  62. #endinput
  63. #endif
  64. #define _INC_y_stringhash
  65. #include "..\YSI_Internal\y_version"
  66. #include "..\YSI_Core\y_debug"
  67. // Note: H_a, H_b, H_c, H_d, H_f and H_g are defined here, H_e in y_amx, H_z in
  68. // y_masteronce, H_u in y_users, H_y in y_timers.
  69. // ===================
  70. // Determine version
  71. // ===================
  72. // Entry.
  73. #define _H (_:_H_1:_H_0
  74. #define _I (_:_H_3:_H_2
  75. #define _H@b (_:_H_1:_H_0
  76. #define _I@b (_:_H_3:_H_2
  77. // Old.
  78. #define _H_0(%0) HASH@bernstein(%0))
  79. #define _H_2(%0) HASHi@bernstein(%0))
  80. // New.
  81. #define _H_1:_H_0<%0> HASh@bernstein(%0))
  82. #define _H_3:_H_2<%0> HAShi@bernstein(%0))
  83. // Entry.
  84. #define _H@f (_:_H0@f:_H1@f
  85. #define _I@f (_:_H2@f:_H3@f
  86. // Old.
  87. #define _H1@f(%0) HASH@fnv1(%0))
  88. #define _H3@f(%0) HASHi@fnv1(%0))
  89. // New.
  90. #define _H0@f:_H1@f<%0> HASh@fnv1(%0))
  91. #define _H2@f:_H3@f<%0> HAShi@fnv1(%0))
  92. // Entry.
  93. #define _H@a (_:_H0@a:_H1@a
  94. #define _I@a (_:_H2@a:_H3@a
  95. // Old.
  96. #define _H1@a(%0) HASH@fnv1a(%0))
  97. #define _H3@a(%0) HASHi@fnv1a(%0))
  98. // New.
  99. #define _H0@a:_H1@a<%0> HASh@fnv1a(%0))
  100. #define _H2@a:_H3@a<%0> HAShi@fnv1a(%0))
  101. // Entry.
  102. #define HASH (_:_H_4:_H_5
  103. #define HASHi (_:_H_6:_H_7
  104. // Old.
  105. #define _H_5(%0,%1) HASH@%0(%1))
  106. #define _H_7(%0,%1) HASHi@%0(%1))
  107. // New.
  108. #define _H_4:_H_5:%0<%1> HASh@%0(%1))
  109. #define _H_6:_H_7:%0<%1> HAShi@%0(%1))
  110. // Empty strings.
  111. #define HASH_:%0(,@,@) 2166136261
  112. #define _Y_HASH_H(%0,%1)(%9) @B%0:@A0:@A1:@A2:@A3:@A4:@A5:@A6:@A7:@A8:@A9:@Aa:@Ab:@Ac:@Ad:@Ae:@Af:@Ag:@Ah:@Ai:@Aj:@Ak:@Al:@Am:@An:@Ao:@Ap:@Aq:@Ar:@As:@At:@Au:@Av:@Aw:@Ax:@Ay:@Az:@AA:@AB:@AC:@AD:@AE:@AF:@AG:@AH:@AI:@AJ:@AK:@AL:@AM:@AN:@AO:@AP:@AQ:@AR:@AS:@AT:@AU:@AV:@AW:@AX:@AY:@AZ:@A_:@A@:@B_:@B%1$(%9)$
  113. #define _Y_HASH_I(%0,%1)(%9) @B%0:@A0:@A1:@A2:@A3:@A4:@A5:@A6:@A7:@A8:@A9:@Ba:@Bb:@Bc:@Bd:@Be:@Bf:@Bg:@Bh:@Bi:@Bj:@Bk:@Bl:@Bm:@Bn:@Bo:@Bp:@Bq:@Br:@Bs:@Bt:@Bu:@Bv:@Bw:@Bx:@By:@Bz:@AA:@AB:@AC:@AD:@AE:@AF:@AG:@AH:@AI:@AJ:@AK:@AL:@AM:@AN:@AO:@AP:@AQ:@AR:@AS:@AT:@AU:@AV:@AW:@AX:@AY:@AZ:@A_:@A@:@B_:@B%1$(%9)$
  114. // Bernstein
  115. #define @B0:%9$()$ -1
  116. #define @B1$(<%0>%1) @B2:%0@B3:<>%1
  117. #define @B2:%9<%0>%1) %9(@B2:%0)*33+%1)
  118. #define @B3:(%0)*%1$ -%1
  119. // FNV1
  120. #define @B4:%9$()$ 2166136261
  121. #define @B5$(<%0>%1) @B6:%0@B7:<>%1
  122. #define @B6:%9<%0>%1) %9(@B6:%0)*16777619^%1)
  123. #define @B7:(%0)*%9^%1$ 84696351^%1
  124. // FNV1a
  125. #define @B8:%9$()$ 2166136261
  126. #define @B9$(<%0>%1) (@BA:%0@BB:<>%1>
  127. #define @BA:%9<%0>%1>%2) %9(@BA:%0^%1)*16777619>%2)
  128. #define @BB:(%0^%1)%2>$ 2166136261^%1)%2
  129. // Entries
  130. #define HASh@bernstein(%9) _Y_HASH_H(0,1)(%9)
  131. #define HAShi@bernstein(%9) _Y_HASH_I(0,1)(%9)
  132. #define HASh@fnv1(%9) _Y_HASH_H(4,5)(%9)
  133. #define HAShi@fnv1(%9) _Y_HASH_I(4,5)(%9)
  134. #define HASh@fnv1a(%9) _Y_HASH_H(8,9)(%9)
  135. #define HAShi@fnv1a(%9) _Y_HASH_I(8,9)(%9)
  136. /*
  137. 888b 88 ,ad8888ba, 88
  138. 8888b 88 d8"' `"8b 88
  139. 88 `8b 88 d8' 88
  140. 88 `8b 88 ,adPPYba, 8b db d8 88 ,adPPYYba, ,adPPYba, ,adPPYba, ,adPPYb,88
  141. 88 `8b 88 a8P_____88 `8b d88b d8' 88 "" `Y8 I8[ "" a8P_____88 a8" `Y88
  142. 88 `8b 88 8PP""""""" `8b d8'`8b d8' Y8, ,adPPPPP88 `"Y8ba, 8PP""""""" 8b 88
  143. 88 `8888 "8b, ,aa `8bd8' `8bd8' Y8a. .a8P 88, ,88 aa ]8I "8b, ,aa "8a, ,d88
  144. 88 `888 `"Ybbd8"' YP YP `"Y8888Y"' `"8bbdP"Y8 `"YbbdP"' `"Ybbd8"' `"8bbdP"Y8
  145. */
  146. // ================
  147. // Case sensitive
  148. // ================
  149. // Test for the current character. Numbers MUST be first!
  150. #define @A0:%9$(%10%0)$ @A0:%9$(<%1>'0'%0)$
  151. #define @A1:%9$(%11%0)$ @A1:%9$(<%1>'1'%0)$
  152. #define @A2:%9$(%12%0)$ @A2:%9$(<%1>'2'%0)$
  153. #define @A3:%9$(%13%0)$ @A3:%9$(<%1>'3'%0)$
  154. #define @A4:%9$(%14%0)$ @A4:%9$(<%1>'4'%0)$
  155. #define @A5:%9$(%15%0)$ @A5:%9$(<%1>'5'%0)$
  156. #define @A6:%9$(%16%0)$ @A6:%9$(<%1>'6'%0)$
  157. #define @A7:%9$(%17%0)$ @A7:%9$(<%1>'7'%0)$
  158. #define @A8:%9$(%18%0)$ @A8:%9$(<%1>'8'%0)$
  159. #define @A9:%9$(%19%0)$ @A9:%9$(<%1>'9'%0)$
  160. #define @Aa:%9$(%1a%0)$ @Aa:%9$(<%1>97%0)$
  161. #define @Ab:%9$(%1b%0)$ @Ab:%9$(<%1>98%0)$
  162. #define @Ac:%9$(%1c%0)$ @Ac:%9$(<%1>99%0)$
  163. #define @Ad:%9$(%1d%0)$ @Ad:%9$(<%1>100%0)$
  164. #define @Ae:%9$(%1e%0)$ @Ae:%9$(<%1>101%0)$
  165. #define @Af:%9$(%1f%0)$ @Af:%9$(<%1>102%0)$
  166. #define @Ag:%9$(%1g%0)$ @Ag:%9$(<%1>103%0)$
  167. #define @Ah:%9$(%1h%0)$ @Ah:%9$(<%1>104%0)$
  168. #define @Ai:%9$(%1i%0)$ @Ai:%9$(<%1>105%0)$
  169. #define @Aj:%9$(%1j%0)$ @Aj:%9$(<%1>106%0)$
  170. #define @Ak:%9$(%1k%0)$ @Ak:%9$(<%1>107%0)$
  171. #define @Al:%9$(%1l%0)$ @Al:%9$(<%1>108%0)$
  172. #define @Am:%9$(%1m%0)$ @Am:%9$(<%1>109%0)$
  173. #define @An:%9$(%1n%0)$ @An:%9$(<%1>110%0)$
  174. #define @Ao:%9$(%1o%0)$ @Ao:%9$(<%1>111%0)$
  175. #define @Ap:%9$(%1p%0)$ @Ap:%9$(<%1>112%0)$
  176. #define @Aq:%9$(%1q%0)$ @Aq:%9$(<%1>113%0)$
  177. #define @Ar:%9$(%1r%0)$ @Ar:%9$(<%1>114%0)$
  178. #define @As:%9$(%1s%0)$ @As:%9$(<%1>115%0)$
  179. #define @At:%9$(%1t%0)$ @At:%9$(<%1>116%0)$
  180. #define @Au:%9$(%1u%0)$ @Au:%9$(<%1>117%0)$
  181. #define @Av:%9$(%1v%0)$ @Av:%9$(<%1>118%0)$
  182. #define @Aw:%9$(%1w%0)$ @Aw:%9$(<%1>119%0)$
  183. #define @Ax:%9$(%1x%0)$ @Ax:%9$(<%1>120%0)$
  184. #define @Ay:%9$(%1y%0)$ @Ay:%9$(<%1>121%0)$
  185. #define @Az:%9$(%1z%0)$ @Az:%9$(<%1>122%0)$
  186. #define @AA:%9$(%1A%0)$ @AA:%9$(<%1>65%0)$
  187. #define @AB:%9$(%1B%0)$ @AB:%9$(<%1>66%0)$
  188. #define @AC:%9$(%1C%0)$ @AC:%9$(<%1>67%0)$
  189. #define @AD:%9$(%1D%0)$ @AD:%9$(<%1>68%0)$
  190. #define @AE:%9$(%1E%0)$ @AE:%9$(<%1>69%0)$
  191. #define @AF:%9$(%1F%0)$ @AF:%9$(<%1>70%0)$
  192. #define @AG:%9$(%1G%0)$ @AG:%9$(<%1>71%0)$
  193. #define @AH:%9$(%1H%0)$ @AH:%9$(<%1>72%0)$
  194. #define @AI:%9$(%1I%0)$ @AI:%9$(<%1>73%0)$
  195. #define @AJ:%9$(%1J%0)$ @AJ:%9$(<%1>74%0)$
  196. #define @AK:%9$(%1K%0)$ @AK:%9$(<%1>75%0)$
  197. #define @AL:%9$(%1L%0)$ @AL:%9$(<%1>76%0)$
  198. #define @AM:%9$(%1M%0)$ @AM:%9$(<%1>77%0)$
  199. #define @AN:%9$(%1N%0)$ @AN:%9$(<%1>78%0)$
  200. #define @AO:%9$(%1O%0)$ @AO:%9$(<%1>79%0)$
  201. #define @AP:%9$(%1P%0)$ @AP:%9$(<%1>80%0)$
  202. #define @AQ:%9$(%1Q%0)$ @AQ:%9$(<%1>81%0)$
  203. #define @AR:%9$(%1R%0)$ @AR:%9$(<%1>82%0)$
  204. #define @AS:%9$(%1S%0)$ @AS:%9$(<%1>83%0)$
  205. #define @AT:%9$(%1T%0)$ @AT:%9$(<%1>84%0)$
  206. #define @AU:%9$(%1U%0)$ @AU:%9$(<%1>85%0)$
  207. #define @AV:%9$(%1V%0)$ @AV:%9$(<%1>86%0)$
  208. #define @AW:%9$(%1W%0)$ @AW:%9$(<%1>87%0)$
  209. #define @AX:%9$(%1X%0)$ @AX:%9$(<%1>88%0)$
  210. #define @AY:%9$(%1Y%0)$ @AY:%9$(<%1>89%0)$
  211. #define @AZ:%9$(%1Z%0)$ @AZ:%9$(<%1>90%0)$
  212. #define @A_:%9$(%1_%0)$ @A_:%9$(<%1>95%0)$
  213. #define @A@:%9$(%1@%0)$ @A@:%9$(<%1>64%0)$
  214. #define @B_:%9$(%1\32;%0)$ @B_:%9$(<%1>32%0)$
  215. // ==================
  216. // Case insensitive
  217. // ==================
  218. #define @Ba:%9$(%1a%0)$ @Ba:%9$(<%1>65%0)$
  219. #define @Bb:%9$(%1b%0)$ @Bb:%9$(<%1>66%0)$
  220. #define @Bc:%9$(%1c%0)$ @Bc:%9$(<%1>67%0)$
  221. #define @Bd:%9$(%1d%0)$ @Bd:%9$(<%1>68%0)$
  222. #define @Be:%9$(%1e%0)$ @Be:%9$(<%1>69%0)$
  223. #define @Bf:%9$(%1f%0)$ @Bf:%9$(<%1>70%0)$
  224. #define @Bg:%9$(%1g%0)$ @Bg:%9$(<%1>71%0)$
  225. #define @Bh:%9$(%1h%0)$ @Bh:%9$(<%1>72%0)$
  226. #define @Bi:%9$(%1i%0)$ @Bi:%9$(<%1>73%0)$
  227. #define @Bj:%9$(%1j%0)$ @Bj:%9$(<%1>74%0)$
  228. #define @Bk:%9$(%1k%0)$ @Bk:%9$(<%1>75%0)$
  229. #define @Bl:%9$(%1l%0)$ @Bl:%9$(<%1>76%0)$
  230. #define @Bm:%9$(%1m%0)$ @Bm:%9$(<%1>77%0)$
  231. #define @Bn:%9$(%1n%0)$ @Bn:%9$(<%1>78%0)$
  232. #define @Bo:%9$(%1o%0)$ @Bo:%9$(<%1>79%0)$
  233. #define @Bp:%9$(%1p%0)$ @Bp:%9$(<%1>80%0)$
  234. #define @Bq:%9$(%1q%0)$ @Bq:%9$(<%1>81%0)$
  235. #define @Br:%9$(%1r%0)$ @Br:%9$(<%1>82%0)$
  236. #define @Bs:%9$(%1s%0)$ @Bs:%9$(<%1>83%0)$
  237. #define @Bt:%9$(%1t%0)$ @Bt:%9$(<%1>84%0)$
  238. #define @Bu:%9$(%1u%0)$ @Bu:%9$(<%1>85%0)$
  239. #define @Bv:%9$(%1v%0)$ @Bv:%9$(<%1>86%0)$
  240. #define @Bw:%9$(%1w%0)$ @Bw:%9$(<%1>87%0)$
  241. #define @Bx:%9$(%1x%0)$ @Bx:%9$(<%1>88%0)$
  242. #define @By:%9$(%1y%0)$ @By:%9$(<%1>89%0)$
  243. #define @Bz:%9$(%1z%0)$ @Bz:%9$(<%1>90%0)$
  244. // ============
  245. // Other code
  246. // ============
  247. enum e_HASH_TYPE
  248. {
  249. hash_bernstein,
  250. hash_fnv1,
  251. hash_fnv1a
  252. }
  253. /**--------------------------------------------------------------------------**\
  254. <summary>Hash</summary>
  255. <param name="str">The string to hash.</param>
  256. <param name="sensitive">Wether the hash is case sensitive or not.</param>
  257. <returns>
  258. The reverse Bernstein hash of the string.
  259. </returns>
  260. <remarks>
  261. Based on Bernstein hash, but backwards to match the macros. The only
  262. characters which can be used in the compile time version of this code are:
  263. a-z, A-Z, 0-9, _ and space.
  264. native Hash(str[]);
  265. NOW FORWARD AGAIN.
  266. </remarks>
  267. \**--------------------------------------------------------------------------**/
  268. stock YHash(const str[], bool:sensitive = true, e_HASH_TYPE:type = hash_bernstein, len = -1, pack = false)
  269. {
  270. #pragma unused pack
  271. P:3("YHash called: \"%s\", %i, %i, %i, %i", str, _:sensitive, _:type, len, pack);
  272. //pack = str[0] > 255;
  273. if (len < 0) len = strlen(str);
  274. else len = min(len, strlen(str));
  275. new
  276. i = 0;
  277. if (str[0] > 255)
  278. {
  279. switch (type)
  280. {
  281. case hash_bernstein:
  282. {
  283. new
  284. hash = -1;
  285. if (sensitive)
  286. {
  287. while (i != len)
  288. {
  289. hash = hash * 33 + str{i++};
  290. }
  291. }
  292. else
  293. {
  294. while (i != len)
  295. {
  296. hash = hash * 33 + toupper(str{i++});
  297. }
  298. }
  299. return hash;
  300. }
  301. case hash_fnv1:
  302. {
  303. new
  304. hash = 2166136261;
  305. if (sensitive)
  306. {
  307. while (i != len)
  308. {
  309. hash = hash * 16777619 ^ str{i++};
  310. }
  311. }
  312. else
  313. {
  314. while (i != len)
  315. {
  316. hash = hash * 16777619 ^ toupper(str{i++});
  317. }
  318. }
  319. return hash;
  320. }
  321. case hash_fnv1a:
  322. {
  323. new
  324. hash = 2166136261;
  325. if (sensitive)
  326. {
  327. while (i != len)
  328. {
  329. hash = (hash ^ str{i++}) * 16777619;
  330. }
  331. }
  332. else
  333. {
  334. while (i != len)
  335. {
  336. hash = (hash ^ toupper(str{i++})) * 16777619;
  337. }
  338. }
  339. return hash;
  340. }
  341. }
  342. }
  343. else
  344. {
  345. switch (type)
  346. {
  347. case hash_bernstein:
  348. {
  349. new
  350. hash = -1;
  351. if (sensitive)
  352. {
  353. while (i != len)
  354. {
  355. hash = hash * 33 + str[i++];
  356. }
  357. }
  358. else
  359. {
  360. while (i != len)
  361. {
  362. //printf("hash: %d, old: %d, add: %d", hash, str[i], toupper(str[i]));
  363. hash = hash * 33 + toupper(str[i++]);
  364. }
  365. }
  366. return hash;
  367. }
  368. case hash_fnv1:
  369. {
  370. new
  371. hash = 2166136261;
  372. if (sensitive)
  373. {
  374. while (i != len)
  375. {
  376. hash = hash * 16777619 ^ str[i++];
  377. }
  378. }
  379. else
  380. {
  381. while (i != len)
  382. {
  383. hash = hash * 16777619 ^ toupper(str[i++]);
  384. }
  385. }
  386. return hash;
  387. }
  388. case hash_fnv1a:
  389. {
  390. new
  391. hash = 2166136261;
  392. if (sensitive)
  393. {
  394. while (i != len)
  395. {
  396. hash = (hash ^ str[i++]) * 16777619;
  397. }
  398. }
  399. else
  400. {
  401. while (i != len)
  402. {
  403. hash = (hash ^ toupper(str[i++])) * 16777619;
  404. }
  405. }
  406. return hash;
  407. }
  408. }
  409. }
  410. return -1;
  411. }
  412. #include "y_stringhash/bernstein"
  413. #include "y_stringhash/fnv1a"
  414. #include "y_stringhash/fnv1"
  415. #if defined YSI_TESTS
  416. #include "..\YSI_Core\y_testing"
  417. #include "y_stringhash/tests"
  418. #endif