impl.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. Legal:
  3. Version: MPL 1.1
  4. The contents of this file are subject to the Mozilla Public License Version
  5. 1.1 the "License"; you may not use this file except in compliance with
  6. the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. The Original Code is the YSI framework.
  13. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  14. Portions created by the Initial Developer are Copyright C 2011
  15. the Initial Developer. All Rights Reserved.
  16. Contributors:
  17. Y_Less
  18. koolk
  19. JoeBullet/Google63
  20. g_aSlice/Slice
  21. Misiur
  22. samphunter
  23. tianmeta
  24. maddinat0r
  25. spacemud
  26. Crayder
  27. Dayvison
  28. Ahmad45123
  29. Zeex
  30. irinel1996
  31. Yiin-
  32. Chaprnks
  33. Konstantinos
  34. Masterchen09
  35. Southclaws
  36. PatchwerkQWER
  37. m0k1
  38. paulommu
  39. udan111
  40. Thanks:
  41. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  42. ZeeX - Very productive conversations.
  43. koolk - IsPlayerinAreaEx code.
  44. TheAlpha - Danish translation.
  45. breadfish - German translation.
  46. Fireburn - Dutch translation.
  47. yom - French translation.
  48. 50p - Polish translation.
  49. Zamaroht - Spanish translation.
  50. Los - Portuguese translation.
  51. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  52. me to strive to better.
  53. Pixels^ - Running XScripters where the idea was born.
  54. Matite - Pestering me to release it and using it.
  55. Very special thanks to:
  56. Thiadmer - PAWN, whose limits continue to amaze me!
  57. Kye/Kalcor - SA:MP.
  58. SA:MP Team past, present and future - SA:MP.
  59. Optional plugins:
  60. Gamer_Z - GPS.
  61. Incognito - Streamer.
  62. Me - sscanf2, fixes2, Whirlpool.
  63. */
  64. #define HASH_MAP_SIZE (256)
  65. #define HASH_MAP_PTR (HASH_MAP_SIZE)
  66. #define HASH_MAP_SIZE_1 (HASH_MAP_SIZE + 1)
  67. #define HASH_MAP_SIZE_2 (HASH_MAP_SIZE + 2)
  68. #define HASH_MAP_SIZE_3 (HASH_MAP_SIZE + 3)
  69. #define HashMap:%0<%1> %0[HASH_MAP_SIZE + 4]
  70. #define HASH_MAP_DIR_LEFT (4)
  71. #define HASH_MAP_DIR_RIGHT (8)
  72. // There are three pieces of hash map data associated with each stored string.
  73. // In byte offsets, these are:
  74. //
  75. // 0 - The hash of the stored string.
  76. // 4 - The left pointer for the binary tree.
  77. // 8 - The right pointer for the binary tree.
  78. //
  79. #define HASH_MAP_DATA (3)
  80. /*-------------------------------------------------------------------------*//**
  81. * <param name="str">String to hash.</param>
  82. * <param name="hash">Desination of the hash.</param>
  83. * <remarks>
  84. * Quickly hashes the string using Bernstein. Caters for both packed and
  85. * unpacked strings.
  86. * </remarks>
  87. *//*------------------------------------------------------------------------**/
  88. P:D(HashMap_Hash(str[],&hash));
  89. #define HashMap_Hash(%0,%1) (%1=YHash(%0))
  90. /*-------------------------------------------------------------------------*//**
  91. * <param name="m">Hash map to initialise.</param>
  92. * <param name="target">Array to point in to.</param>
  93. * <param name="slot">Second dimension slot of the hashed data.</param>
  94. * <remarks>
  95. * Finds the location of the hash map linked list data in the passed array data
  96. * and uses that to read the data through pointers subsequently. It doesn't
  97. * matter WHERE in the enum the hash map data is, and if its not there you'll
  98. * get an error, or at least a warning.
  99. * </remarks>
  100. *//*------------------------------------------------------------------------**/
  101. P:D(HashMap_Init(HashMap:m<>, target[][], slot));
  102. /*-------------------------------------------------------------------------*//**
  103. * <param name="m">Hash map to initialise.</param>
  104. * <param name="target">Address of the hashmap data.</param>
  105. * <param name="size1">Number of entries.</param>
  106. * <param name="size2">Total Size of each entry IN BYTES.</param>
  107. * <param name="t2">Address of the name AND data start.</param>
  108. * <remarks>
  109. * Finds the location of the hash map linked list data in the passed array data
  110. * and uses that to read the data through pointers subsequently. It doesn't
  111. * matter WHERE in the enum the hash map data is, and if its not there you'll
  112. * get an error, or at least a warning.
  113. * </remarks>
  114. *//*------------------------------------------------------------------------**/
  115. stock _HashMap_Init(HashMap:m<>, &target, size1, size2, &t2)
  116. {
  117. static
  118. HashMap:sInit<> = {0, ...};
  119. m = sInit;
  120. new
  121. ptr;
  122. // Save the pointer.
  123. #emit LOAD.S.pri target
  124. #emit STOR.S.pri ptr
  125. m[HASH_MAP_PTR] = ptr,
  126. // Store the number of elements in the array.
  127. m[HASH_MAP_SIZE_1] = size1,
  128. // Store the size of each element.
  129. m[HASH_MAP_SIZE_2] = size2;
  130. // Store the size of "_E_HASH_MAP_NAME" in bytes.
  131. #emit LOAD.S.pri target
  132. #emit LOAD.S.alt t2
  133. #emit SUB
  134. #emit STOR.S.pri ptr
  135. m[HASH_MAP_SIZE_3] = ptr;
  136. }
  137. // Uses "%2 - %2" to make a tagged 0 without knowing the tag.
  138. #define HashMap_Init(%0,%1,%2) _HashMap_Init(%0, %1[0][(%2)], sizeof (%1), sizeof (%1[]) * 4, %1[0][(%2) - (%2)])
  139. /*-------------------------------------------------------------------------*//**
  140. * <param name="str">String to get the size of.</param>
  141. * <returns>
  142. * The number of BYTES this string takes up including the NULL.
  143. * </returns>
  144. * <remarks>
  145. * Caters for both packed and unpacked strings. The weirdness is basically
  146. * just: <code>ispacked(str) ? (* 1) : (* 4)</code>.
  147. * </remarks>
  148. *//*------------------------------------------------------------------------**/
  149. P:D(HashMap_ByteLen(str[]));
  150. #define HashMap_ByteLen(%0) ((strlen((%0)) + 1) << (2 * _:!ispacked((%0))))
  151. /*-------------------------------------------------------------------------*//**
  152. * <param name="m"></param>
  153. * <param name="str"></param>
  154. * <param name="value">More like the target slot.</param>
  155. * <remarks>
  156. * Adds a value to the given hash map under the given string key.
  157. *
  158. * Actually more like adding an index, not a value...
  159. * </remarks>
  160. *//*------------------------------------------------------------------------**/
  161. stock bool:HashMap_Add(HashMap:m<>, const str[], value, bool:ignorecase = false)
  162. {
  163. P:3("HashMap_Add called: %d <= %d < %d", 0, value, m[HASH_MAP_SIZE_1]);
  164. if (0 <= value < m[HASH_MAP_SIZE_1])
  165. {
  166. // Check if the index is already in a hash map. Doesn't work if the
  167. // entry is a leaf, since it will be a member, but also have both
  168. // pointers as 0... I don't think there is a nice way to do this.
  169. // if (AMX_Read(ptr + 4) || AMX_Read(ptr + 8)) return false;
  170. static
  171. ptr,
  172. hash,
  173. res;
  174. hash = YHash(str, .sensitive = !ignorecase);
  175. // Add this entry to the correct binary tree.
  176. P:5("HashMap_Add: mask = %d", hash & 0xFF);
  177. new
  178. prev = ref(m[hash & 0xFF]),
  179. next;
  180. for ( ; ; )
  181. {
  182. #emit LREF.S.pri prev
  183. #emit STOR.S.pri next
  184. if (!next)
  185. break;
  186. {}
  187. #emit LREF.S.pri next
  188. #emit STOR.pri res
  189. if (hash == res)
  190. {
  191. // It doesn't matter which way we go on matching hashes, as long
  192. // as the direction is consistent.
  193. #emit PUSH.C 0x7FFFFFFF
  194. #emit PUSH.S ignorecase
  195. #emit PUSH.S str
  196. #emit LOAD.S.pri m
  197. #emit ADD.C 1036 // 256 * 4 + 3 * 4
  198. #emit LOAD.I
  199. #emit LOAD.S.alt next
  200. #emit SUB.alt
  201. #emit PUSH.pri
  202. #emit PUSH.C 16
  203. #emit SYSREQ.C strcmp
  204. #emit STACK 20
  205. #emit STOR.pri ptr
  206. if (ptr == 0)
  207. {
  208. P:E("Trying to add two copies of a string to a hashmap: \"%s\", %d", str, value);
  209. return false;
  210. }
  211. else if (ptr < 0) // Lower, move left.
  212. prev = next + HASH_MAP_DIR_LEFT;
  213. else // Higher, move right.
  214. prev = next + HASH_MAP_DIR_RIGHT;
  215. }
  216. else if (hash < res) // Lower, move left.
  217. prev = next + HASH_MAP_DIR_LEFT;
  218. else // Higher, move right.
  219. prev = next + HASH_MAP_DIR_RIGHT;
  220. }
  221. P:6("HashMap_Add: used = %d", AMX_Read(m[HASH_MAP_PTR] + value * m[HASH_MAP_SIZE_2]));
  222. // Get the address of this structure.
  223. ptr = m[HASH_MAP_PTR] + value * m[HASH_MAP_SIZE_2],
  224. // Copy the hashed value.
  225. AMX_Write(ptr, hash),
  226. AMX_Write(ptr + HASH_MAP_DIR_LEFT, 0),
  227. AMX_Write(ptr + HASH_MAP_DIR_RIGHT, 0),
  228. // Add this hash to the hash list.
  229. AMX_Write(prev, ptr),
  230. // Get the hashed string destination size, and copy the string.
  231. next = m[HASH_MAP_SIZE_3],
  232. rawMemcpy(ptr - next, ref(str), next);
  233. return true;
  234. }
  235. return false;
  236. }
  237. /*-------------------------------------------------------------------------*//**
  238. * <param name="m">The hash map to search.</param>
  239. * <param name="str">The key to find.</param>
  240. * <returns>
  241. * The value associated with this key in the given hash map.
  242. * </returns>
  243. *//*------------------------------------------------------------------------**/
  244. stock HashMap_Get(HashMap:m<>, const str[], bool:ignorecase = false)
  245. {
  246. return HashMap_GetWithHash(m, str, YHash(str, .sensitive = !ignorecase), ignorecase);
  247. }
  248. /*-------------------------------------------------------------------------*//**
  249. * <param name="m">The hash map to search.</param>
  250. * <param name="str">The key to find.</param>
  251. * <param name="hash">The hashed key.</param>
  252. * <returns>
  253. * The value associated with this key in the given hash map.
  254. * </returns>
  255. *//*------------------------------------------------------------------------**/
  256. stock HashMap_GetWithHash(HashMap:m<>, const str[], hash, bool:ignorecase = false)
  257. {
  258. P:3("HashMap_Get called: mask = %d (%d)", hash, hash & 0xFF);
  259. new
  260. prev = ref(m[hash & 0xFF]),
  261. next;
  262. static
  263. res;
  264. for ( ; ; )
  265. {
  266. #emit LREF.S.pri prev
  267. #emit STOR.S.pri next
  268. if (!next)
  269. break;
  270. {}
  271. #emit LREF.S.pri next
  272. #emit STOR.pri res
  273. if (hash == res)
  274. {
  275. // It doesn't matter which way we go on matching hashes, as long
  276. // as the direction is consistent.
  277. #emit PUSH.C 0x7FFFFFFF
  278. #emit PUSH.S ignorecase
  279. #emit PUSH.S str
  280. #emit LOAD.S.pri m
  281. #emit ADD.C 1036 // 256 * 4 + 3 * 4
  282. #emit LOAD.I
  283. #emit LOAD.S.alt next
  284. #emit SUB.alt
  285. #emit PUSH.pri
  286. #emit PUSH.C 16
  287. #emit SYSREQ.C strcmp
  288. #emit STACK 20
  289. #emit STOR.pri res
  290. if (res == 0)
  291. {
  292. return (next - m[HASH_MAP_PTR]) / m[HASH_MAP_SIZE_2];
  293. }
  294. else if (res < 0) // Lower, move left.
  295. prev = next + HASH_MAP_DIR_LEFT;
  296. else // Higher, move right.
  297. prev = next + HASH_MAP_DIR_RIGHT;
  298. }
  299. else if (hash < res) // Lower, move left.
  300. prev = next + HASH_MAP_DIR_LEFT;
  301. else // Higher, move right.
  302. prev = next + HASH_MAP_DIR_RIGHT;
  303. }
  304. return -1;
  305. }
  306. /*-------------------------------------------------------------------------*//**
  307. * <param name="m">The hash map to modify.</param>
  308. * <param name="str">The key to remove from the hash map.</param>
  309. * <remarks>
  310. * Removes a given key and its associated value from the given hash map (if it
  311. * can be found in the map in the first place).
  312. * </remarks>
  313. *//*------------------------------------------------------------------------**/
  314. stock bool:HashMap_RemoveKeyWithHash(HashMap:m<>, const str[], hash, bool:ignorecase = false)
  315. {
  316. // First, find the key and it's parent.
  317. new
  318. prev = ref(m[hash & 0xFF]),
  319. next;
  320. static
  321. res;
  322. // First, find this key in the hashmap.
  323. for ( ; ; )
  324. {
  325. #emit LREF.S.pri prev
  326. #emit STOR.S.pri next
  327. if (!next)
  328. return false;
  329. {}
  330. #emit LREF.S.pri next
  331. #emit STOR.pri res
  332. if (hash == res)
  333. {
  334. // It doesn't matter which way we go on matching hashes, as long
  335. // as the direction is consistent.
  336. #emit PUSH.C 0x7FFFFFFF
  337. #emit PUSH.S ignorecase
  338. #emit PUSH.S str
  339. #emit LOAD.S.pri m
  340. #emit ADD.C 1036 // 256 * 4 + 3 * 4
  341. #emit LOAD.I
  342. #emit LOAD.S.alt next
  343. #emit SUB.alt
  344. #emit PUSH.pri
  345. #emit PUSH.C 16
  346. #emit SYSREQ.C strcmp
  347. #emit STACK 20
  348. #emit STOR.pri res
  349. if (res == 0)
  350. {
  351. break;
  352. }
  353. else if (res < 0) // Lower, move left.
  354. prev = next + HASH_MAP_DIR_LEFT;
  355. else // Higher, move right.
  356. prev = next + HASH_MAP_DIR_RIGHT;
  357. }
  358. else if (hash < res) // Lower, move left.
  359. prev = next + HASH_MAP_DIR_LEFT;
  360. else // Higher, move right.
  361. prev = next + HASH_MAP_DIR_RIGHT;
  362. }
  363. // The LEFT/RIGHT apparent swap below is correct. We want the right-most
  364. // value on the left branch, and vice-versa.
  365. new
  366. left = AMX_Read(next + HASH_MAP_DIR_LEFT),
  367. right = AMX_Read(next + HASH_MAP_DIR_RIGHT);
  368. // Find an empty branch, or the smallest branch.
  369. if (!left)
  370. {
  371. if (!right)
  372. AMX_Write(prev, 0);
  373. else
  374. AMX_Write(prev, right);
  375. }
  376. else if (!right)
  377. {
  378. AMX_Write(prev, left);
  379. }
  380. else
  381. {
  382. new
  383. lHeight = 1,
  384. rHeight = 1,
  385. lParent = 0,
  386. rParent = 0,
  387. lVal = HashMap_GetBranchEnd(left, lHeight, lParent, HASH_MAP_DIR_RIGHT),
  388. rVal = HashMap_GetBranchEnd(right, rHeight, rParent, HASH_MAP_DIR_LEFT);
  389. if (lHeight < rHeight)
  390. {
  391. // Reduce the right (larger) branch. If this branch is bigger, and
  392. // no branch is empty, then this branch MUST be at least two nodes
  393. // high, and so MUST have a parent.
  394. AMX_Write(prev, rVal),
  395. AMX_Write(rParent + HASH_MAP_DIR_LEFT, AMX_Read(rVal + HASH_MAP_DIR_RIGHT)),
  396. AMX_Write(rVal + HASH_MAP_DIR_RIGHT, right),
  397. AMX_Write(rVal + HASH_MAP_DIR_LEFT, left);
  398. }
  399. else
  400. {
  401. // Reduce the left (larger) branch. This branch MAY NOT have a
  402. // parent, if both branches are equal at one node high.
  403. AMX_Write(prev, lVal);
  404. if (lParent)
  405. {
  406. AMX_Write(lParent + HASH_MAP_DIR_RIGHT, AMX_Read(lVal + HASH_MAP_DIR_LEFT)),
  407. AMX_Write(lVal + HASH_MAP_DIR_RIGHT, right),
  408. AMX_Write(lVal + HASH_MAP_DIR_LEFT, left);
  409. }
  410. else
  411. {
  412. AMX_Write(lVal + HASH_MAP_DIR_RIGHT, right),
  413. AMX_Write(lVal + HASH_MAP_DIR_LEFT, 0);
  414. }
  415. }
  416. }
  417. // Clear the removed node's pointers and string.
  418. AMX_Write(next + HASH_MAP_DIR_LEFT, 0),
  419. AMX_Write(next + HASH_MAP_DIR_RIGHT, 0),
  420. AMX_Write(next - m[HASH_MAP_SIZE_3], 0);
  421. return true;
  422. }
  423. stock bool:HashMap_RemoveKey(HashMap:m<>, const str[], bool:ignorecase = false)
  424. {
  425. return HashMap_RemoveKeyWithHash(m, str, YHash(str, .sensitive = !ignorecase), ignorecase);
  426. }
  427. static stock HashMap_GetBranchEnd(cur, &height, &parent, dir)
  428. {
  429. // Find the node as close in (hash) value to the current node as possible.
  430. static
  431. next;
  432. while ((next = AMX_Read(cur + dir)))
  433. {
  434. ++height,
  435. parent = cur,
  436. cur = next;
  437. }
  438. return cur;
  439. }
  440. /*-------------------------------------------------------------------------*//**
  441. * <param name="m">Hash map to modify.</param>
  442. * <param name="value">Value to remove.</param>
  443. * <remarks>
  444. * Removes a value from the hash map. First it gets the string key for the
  445. * value, then removes that (to update associated linked lists correctly).
  446. * </remarks>
  447. *//*------------------------------------------------------------------------**/
  448. stock bool:HashMap_RemoveValue(HashMap:m<>, value)
  449. {
  450. if (0 <= value < m[HASH_MAP_SIZE_1])
  451. {
  452. static
  453. sString[128 char];
  454. new
  455. size = m[HASH_MAP_SIZE_3];
  456. rawMemcpy(ref(sString), m[HASH_MAP_PTR] + value * m[HASH_MAP_SIZE_2] - size, min(size, sizeof (sString) * 4));
  457. return HashMap_RemoveKey(m, sString);
  458. }
  459. return false;
  460. }
  461. /*-------------------------------------------------------------------------*//**
  462. * <param name="m">The hash map to modify.</param>
  463. * <param name="str">The key to modify.</param>
  464. * <param name="value">The new value for the given key.</param>
  465. * <remarks>
  466. * If this key is already in the hash map it is removed, and then the new value
  467. * is added in its place. If the string already exists, its associated data is
  468. * removed. If the value already exists, it is removed as well.
  469. * </remarks>
  470. *//*------------------------------------------------------------------------**/
  471. stock HashMap_Set(HashMap:m<>, const str[], const value)
  472. {
  473. return
  474. HashMap_RemoveKey(m, str),
  475. HashMap_RemoveValue(m, value),
  476. HashMap_Add(m, str, value);
  477. }