y_stringhash.inc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. // Old.
  76. #define _H_0(%0) HASH@bernstein(%0))
  77. #define _H_2(%0) HASHi@bernstein(%0))
  78. // New.
  79. #define _H_1:_H_0<%0> hb:@Ba:H_Nba(%0)H_Eb:|||-1|||)
  80. #define _H_3:_H_2<%0> hc:@Ca:H_Nca(%0)H_Ec:|||-1|||)
  81. // Entry.
  82. #define _H@b (_:_H0@b:_H1@b
  83. #define _I@b (_:_H2@b:_H3@b
  84. // Old.
  85. #define _H1@b(%0) HASH@bernstein(%0))
  86. #define _H3@b(%0) HASHi@bernstein(%0))
  87. // New.
  88. #define _H0@b:_H1@b<%0> hb:@Ba:H_Nba(%0)H_Eb:|||-1|||)
  89. #define _H2@b:_H3@b<%0> hc:@Ca:H_Nca(%0)H_Ec:|||-1|||)
  90. // Entry.
  91. #define _H@f (_:_H0@f:_H1@f
  92. #define _I@f (_:_H2@f:_H3@f
  93. // Old.
  94. #define _H1@f(%0) HASH@fnv1(%0))
  95. #define _H3@f(%0) HASHi@fnv1(%0))
  96. // New.
  97. #define _H0@f:_H1@f<%0> hg:@Ga:H_Nga(%0)H_Eg:|||2166136261|||)
  98. #define _H2@f:_H3@f<%0> hf:@Fa:H_Nfa(%0)H_Ef:|||2166136261|||)
  99. // Entry.
  100. #define _H@a (_:_H0@a:_H1@a
  101. #define _I@a (_:_H2@a:_H3@a
  102. // Old.
  103. #define _H1@a(%0) HASH@fnv1a(%0))
  104. #define _H3@a(%0) HASHi@fnv1a(%0))
  105. // New.
  106. #define _H0@a:_H1@a<%0> ha:@Aa:H_Naa(%0)H_Ea:|||2166136261|||)
  107. #define _H2@a:_H3@a<%0> hd:@Da:H_Nda(%0)H_Ed:|||2166136261|||)
  108. // Entry.
  109. #define HASH (_:_H_4:_H_5
  110. #define HASHi (_:_H_6:_H_7
  111. // Old.
  112. #define _H_5(%0,%1) HASH@%0(%1))
  113. #define _H_7(%0,%1) HASHi@%0(%1))
  114. // New.
  115. #define _H_4:_H_5:%0<%1> HASh@%0(%1)
  116. #define _H_6:_H_7:%0<%1> HAShi@%0(%1)
  117. #define HASh@bernstein(%0) hb:@Ba:H_Nba(%0)H_Eb:|||-1|||)
  118. #define HAShi@bernstein(%0) hc:@Ca:H_Nca(%0)H_Ec:|||-1|||)
  119. #define HASh@fnv1(%0) hg:@Ga:H_Nga(%0)H_Eg:|||2166136261|||)
  120. #define HAShi@fnv1(%0) hf:@Fa:H_Nfa(%0)H_Ef:|||2166136261|||)
  121. #define HASh@fnv1a(%0) ha:@Aa:H_Naa(%0)H_Ea:|||2166136261|||)
  122. #define HAShi@fnv1a(%0) hd:@Da:H_Nda(%0)H_Ed:|||2166136261|||)
  123. // ============
  124. // Other code
  125. // ============
  126. enum e_HASH_TYPE
  127. {
  128. hash_bernstein,
  129. hash_fnv1,
  130. hash_fnv1a
  131. }
  132. /**--------------------------------------------------------------------------**\
  133. <summary>Hash</summary>
  134. <param name="str">The string to hash.</param>
  135. <param name="sensitive">Wether the hash is case sensitive or not.</param>
  136. <returns>
  137. The reverse Bernstein hash of the string.
  138. </returns>
  139. <remarks>
  140. Based on Bernstein hash, but backwards to match the macros. The only
  141. characters which can be used in the compile time version of this code are:
  142. a-z, A-Z, 0-9, _ and space.
  143. native Hash(str[]);
  144. NOW FORWARD AGAIN.
  145. </remarks>
  146. \**--------------------------------------------------------------------------**/
  147. stock YHash(const str[], bool:sensitive = true, e_HASH_TYPE:type = hash_bernstein, len = -1, pack = false)
  148. {
  149. #pragma unused pack
  150. P:3("YHash called: \"%s\", %i, %i, %i, %i", str, _:sensitive, _:type, len, pack);
  151. //pack = str[0] > 255;
  152. if (len < 0) len = strlen(str);
  153. else len = min(len, strlen(str));
  154. new
  155. i = 0;
  156. if (str[0] > 255)
  157. {
  158. switch (type)
  159. {
  160. case hash_bernstein:
  161. {
  162. new
  163. hash = -1;
  164. if (sensitive)
  165. {
  166. while (i != len)
  167. {
  168. hash = hash * 33 + str{i++};
  169. }
  170. }
  171. else
  172. {
  173. while (i != len)
  174. {
  175. hash = hash * 33 + toupper(str{i++});
  176. }
  177. }
  178. return hash;
  179. }
  180. case hash_fnv1:
  181. {
  182. new
  183. hash = 2166136261;
  184. if (sensitive)
  185. {
  186. while (i != len)
  187. {
  188. hash = hash * 16777619 ^ str{i++};
  189. }
  190. }
  191. else
  192. {
  193. while (i != len)
  194. {
  195. hash = hash * 16777619 ^ toupper(str{i++});
  196. }
  197. }
  198. return hash;
  199. }
  200. case hash_fnv1a:
  201. {
  202. new
  203. hash = 2166136261;
  204. if (sensitive)
  205. {
  206. while (i != len)
  207. {
  208. hash = (hash ^ str{i++}) * 16777619;
  209. }
  210. }
  211. else
  212. {
  213. while (i != len)
  214. {
  215. hash = (hash ^ toupper(str{i++})) * 16777619;
  216. }
  217. }
  218. return hash;
  219. }
  220. }
  221. }
  222. else
  223. {
  224. switch (type)
  225. {
  226. case hash_bernstein:
  227. {
  228. new
  229. hash = -1;
  230. if (sensitive)
  231. {
  232. while (i != len)
  233. {
  234. hash = hash * 33 + str[i++];
  235. }
  236. }
  237. else
  238. {
  239. while (i != len)
  240. {
  241. //printf("hash: %d, old: %d, add: %d", hash, str[i], toupper(str[i]));
  242. hash = hash * 33 + toupper(str[i++]);
  243. }
  244. }
  245. return hash;
  246. }
  247. case hash_fnv1:
  248. {
  249. new
  250. hash = 2166136261;
  251. if (sensitive)
  252. {
  253. while (i != len)
  254. {
  255. hash = hash * 16777619 ^ str[i++];
  256. }
  257. }
  258. else
  259. {
  260. while (i != len)
  261. {
  262. hash = hash * 16777619 ^ toupper(str[i++]);
  263. }
  264. }
  265. return hash;
  266. }
  267. case hash_fnv1a:
  268. {
  269. new
  270. hash = 2166136261;
  271. if (sensitive)
  272. {
  273. while (i != len)
  274. {
  275. hash = (hash ^ str[i++]) * 16777619;
  276. }
  277. }
  278. else
  279. {
  280. while (i != len)
  281. {
  282. hash = (hash ^ toupper(str[i++])) * 16777619;
  283. }
  284. }
  285. return hash;
  286. }
  287. }
  288. }
  289. return -1;
  290. }
  291. #include "y_stringhash/bernstein"
  292. #include "y_stringhash/fnv1a"
  293. #include "y_stringhash/fnv1"
  294. #if defined YSI_TESTS
  295. #include "..\YSI_Core\y_testing"
  296. #include "y_stringhash/tests"
  297. #endif