impl.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. // This is redefined below, don't worry. It's like this so the function
  65. // prototypes can use a familiar syntax.
  66. #define BitArray:%1<%2> Bit:%1[%2]
  67. #if cellbits == 32
  68. #define CELLSHIFT (5)
  69. #else
  70. #if cellbits == 64
  71. #define CELLSHIFT (6)
  72. #else
  73. #if cellbits == 16
  74. #define CELLSHIFT (4)
  75. #else
  76. #error Unkown cell size
  77. #endif
  78. #endif
  79. #endif
  80. /*-------------------------------------------------------------------------*//**
  81. * <param name="size">Number of bits required.</param>
  82. * <returns>
  83. * Number of cells required for the bit array.
  84. * </returns>
  85. *//*------------------------------------------------------------------------**/
  86. // If this ever changes, update the size reference in y_users.
  87. P:D(Bit_Bits(size));
  88. #define Bit_Bits(%1) (((%1)+cellbits-1)/cellbits)
  89. /*-------------------------------------------------------------------------*//**
  90. * <param name="value">Value to get the slot for.</param>
  91. * <returns>
  92. * The true array slot for this value.
  93. * </returns>
  94. *//*------------------------------------------------------------------------**/
  95. P:D(Bit_Slot(value));
  96. #define Bit_Slot(%1) ((_:%1)>>>CELLSHIFT)
  97. /*-------------------------------------------------------------------------*//**
  98. * <param name="value">Value to get the mask for</param>
  99. * <returns>
  100. * The bit in the array slot to use.
  101. * </returns>
  102. *//*------------------------------------------------------------------------**/
  103. P:D(Bit_Mask(value));
  104. #define Bit_Mask(%1) (Bit:(1<<((_:%1)&cellbits-1)))
  105. /*-------------------------------------------------------------------------*//**
  106. * <param name="array">Array of bits.</param>
  107. * <param name="slot">Bit slot.</param>
  108. * <returns>
  109. * State of the provided slot, 0 on fail.
  110. * </returns>
  111. * <remarks>
  112. * Unsafe but faster for when you're sure you're within range.
  113. * </remarks>
  114. *//*------------------------------------------------------------------------**/
  115. P:D(bool:Bit_GetBit(BitArray:array<>,slot));
  116. #define Bit_GetBit(%1,%2) (%1[(%2)>>>CELLSHIFT]&Bit:(1<<((%2)&cellbits-1)))
  117. /*-------------------------------------------------------------------------*//**
  118. * <param name="array">Array of bits.</param>
  119. * <param name="slot">Bit slot.</param>
  120. * <param name="size">Size of array.</param>
  121. * <returns>
  122. * State of the provided slot, 0 on fail.
  123. * </returns>
  124. * <remarks>
  125. * -
  126. *
  127. * native Bit_Get(BitArray:array&lt;&gt;, slot);
  128. *
  129. * </remarks>
  130. *//*------------------------------------------------------------------------**/
  131. P:D(bool:Bit_Get(BitArray:array<>,slot));
  132. #define Bit_Get(%1,%2) bool:Bit_GetBit(Bit:%1,_:%2)
  133. /*-------------------------------------------------------------------------*//**
  134. * <param name="array">Array of bits.</param>
  135. * <param name="slot">Bit slot.</param>
  136. * <remarks>
  137. * Sets the slot to 1.
  138. * </remarks>
  139. *//*------------------------------------------------------------------------**/
  140. P:D(Bit_Let(BitArray:array<>,slot));
  141. #define Bit_Let(%1,%2) %1[(%2)>>>CELLSHIFT]|=Bit:(1<<((%2)&cellbits-1))
  142. /*-------------------------------------------------------------------------*//**
  143. * <param name="array">Array of bits.</param>
  144. * <param name="slot">Bit slot.</param>
  145. * <remarks>
  146. * Sets the slot to 0.
  147. * </remarks>
  148. *//*------------------------------------------------------------------------**/
  149. P:D(Bit_Vet(BitArray:array<>,slot));
  150. #define Bit_Vet(%1,%2) %1[(%2)>>>CELLSHIFT]&=Bit:~(1<<((%2)&cellbits-1))
  151. /*-------------------------------------------------------------------------*//**
  152. * <param name="array">Array of bits.</param>
  153. * <param name="slot">Bit slot.</param>
  154. * <param name="set">State to set the slot to.</param>
  155. * <param name="size">Size of array.</param>
  156. *//*------------------------------------------------------------------------**/
  157. stock Bit_Set(BitArray:array<>, slot, bool:set)//, size = sizeof (array))
  158. {
  159. //if (slot >>> CELLSHIFT >= size) return;
  160. if (set) Bit_Let(array, slot);
  161. else Bit_Vet(array, slot);
  162. }
  163. /*-------------------------------------------------------------------------*//**
  164. * <param name="array">Array of bits.</param>
  165. * <param name="slot">Bit slot.</param>
  166. * <param name="set">State to set the slot to.</param>
  167. * <param name="size">Size of array.</param>
  168. * <remarks>
  169. * Exactly the same as "Bit_Set", but as a macro not a function.
  170. *
  171. * native Bit_FastSet(BitArray:array&lt;&gt;, slot, bool:set, size = sizeof (array));
  172. *
  173. * </remarks>
  174. *//*------------------------------------------------------------------------**/
  175. P:D(Bit_FastSet(BitArray:array<>,slot,bool:set,size = sizeof (array)));
  176. #define Bit_FastSet(%0,%1,%2) ((%2)?(Bit_Let(%0,(%1))):(Bit_Vet(%0,(%1))))
  177. /*-------------------------------------------------------------------------*//**
  178. * <param name="array">Array to set all values of.</param>
  179. * <param name="set">Wether to set them all 0 or 1.</param>
  180. * <param name="size">Size of array.</param>
  181. *//*------------------------------------------------------------------------**/
  182. stock Bit_SetAll(BitArray:array<>, bool:set, size = sizeof (array))
  183. {
  184. memset(_:array, set ? 0xFFFFFFFF : 0, size);
  185. }
  186. /*-------------------------------------------------------------------------*//**
  187. * <param name="array">Array to count.</param>
  188. * <param name="size">Size of array.</param>
  189. * <returns>
  190. * Number of 1s in the array.
  191. * </returns>
  192. * <remarks>
  193. * Code from:
  194. * <a href="http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel" />
  195. *
  196. * native Bit_Count(BitArray:array&lt;&gt;, size = sizeof (array));
  197. *
  198. * </remarks>
  199. *//*------------------------------------------------------------------------**/
  200. #define Bit_Count Bit_GetCount
  201. stock Bit_GetCount(BitArray:array<>, size = sizeof (array))
  202. {
  203. new
  204. count;
  205. for (new i = 0; i != size; ++i)
  206. {
  207. count += Cell_CountBits(array[i]);
  208. }
  209. return count;
  210. }
  211. stock Bit_Display(BitArray:array<>, size = sizeof (array))
  212. {
  213. new
  214. ret[YSI_MAX_STRING],
  215. val;
  216. while (size--)
  217. {
  218. val = Cell_ReverseBits(array[size]);
  219. format(ret, sizeof (ret), "%016b%016b%s", val >>> 16, val & 0xFFFF, ret);
  220. }
  221. //P:7("Bit_Display called: %s, %i", ret, size);
  222. return ret;
  223. }
  224. #define bitsof(%0) (sizeof(%0)*cellbits)
  225. stock Iter_Func@Bits(start, BitArray:data<>, size = sizeof (data))
  226. {
  227. P:3("Iter_Func@Bits called: %s, %i", Bit_Display(data, size), start);
  228. new
  229. cur,
  230. i = Bit_Slot(++start);
  231. if (i == size)
  232. {
  233. return -1;
  234. }
  235. // Blank out the lowest bits to get the lowest bit not yet used.
  236. if ((cur = _:(data[i] & ~(Bit_Mask(start) - Bit:1))))
  237. {
  238. P:7("Iter_Func@Bits: %d %d %d %d", cur, _:data[i], start, _:~(Bit_Mask(start) - Bit:1));
  239. // Bits left in the current cell.
  240. return Cell_GetLowestBit(cur) + (i << CELLSHIFT);
  241. }
  242. while (++i != size)
  243. {
  244. if ((cur = _:data[i]))
  245. {
  246. return Cell_GetLowestBit(cur) + (i << CELLSHIFT);
  247. }
  248. }
  249. return -1;
  250. }
  251. #define Iterator@Bits iterstart(-1)
  252. stock Iter_Func@Blanks(start, BitArray:data<>, size = sizeof (data))
  253. {
  254. P:3("Iter_Func@Blanks called: %s, %i", Bit_Display(data, size), start);
  255. new
  256. cur,
  257. i = Bit_Slot(++start);
  258. if (i == size)
  259. {
  260. return -1;
  261. }
  262. if ((cur = _:(~data[i] & ~(Bit_Mask(start) - Bit:1))))
  263. {
  264. // Bits left in the current cell.
  265. return Cell_GetLowestBit(cur) + (i << CELLSHIFT);
  266. }
  267. while (++i != size)
  268. {
  269. if ((cur = ~_:data[i]))
  270. {
  271. return Cell_GetLowestBit(cur) + (i << CELLSHIFT);
  272. }
  273. }
  274. return -1;
  275. }
  276. #define Iterator@Blanks iterstart(-1)
  277. #define bits<%1> Bit_Bits(%1)
  278. #undef BitArray
  279. #define BitArray:%1<%2> Bit:%1[bits<%2>]