y_functional_funcs.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. stock Map(Func:cb<i>, const arr[], dest[], al = sizeof (arr), dl = sizeof (dest))
  65. {
  66. for (new len = min(al, dl), i; i != len; ++i)
  67. {
  68. dest[i] = @.cb(arr[i]);
  69. }
  70. }
  71. #define Map({%0}%1) LAMBDA_i<Map>{%0}(%1)
  72. stock Map_(Func:cb<i>, const arr[], len = sizeof (arr))
  73. {
  74. for (new i; i != len; ++i)
  75. {
  76. @.cb(arr[i]);
  77. }
  78. }
  79. #define Map_({%0}%1) LAMBDA_i<Map_>{%0}(%1)
  80. stock MapIdx(Func:cb<ii>, const arr[], dest[], al = sizeof (arr), dl = sizeof (dest))
  81. {
  82. for (new len = min(al, dl), i; i != len; ++i)
  83. {
  84. dest[i] = @.cb(i, arr[i]);
  85. }
  86. }
  87. #define MapIdx({%0}%1) LAMBDA_ii<MapIdx>{%0}(%1)
  88. stock MapIdx_(Func:cb<ii>, const arr[], len = sizeof (arr))
  89. {
  90. for (new i; i != len; ++i)
  91. {
  92. @.cb(i, arr[i]);
  93. }
  94. }
  95. #define MapIdx_({%0}%1) LAMBDA_ii<MapIdx_>{%0}(%1)
  96. stock ZipWith(Func:cb<ii>, const l[], const r[], dest[], ls = sizeof (l), rs = sizeof (r), ds = sizeof (dest))
  97. {
  98. for (new len = min(ds, min(ls, rs)), i; i != len; ++i)
  99. {
  100. dest[i] = @.cb(l[i], r[i]);
  101. }
  102. }
  103. #define ZipWith({%0}%1) LAMBDA_ii<ZipWith>{%0}(%1)
  104. stock ZipWith_(Func:cb<ii>, const l[], const r[], ls = sizeof (l), rs = sizeof (r))
  105. {
  106. for (new len = min(ls, rs), i; i != len; ++i)
  107. {
  108. @.cb(l[i], r[i]);
  109. }
  110. }
  111. #define ZipWith_({%0}%1) LAMBDA_ii<ZipWith_>{%0}(%1)
  112. stock ZipWith3(Func:cb<iii>, const l[], const m[], const r[], dest[], ls = sizeof (l), ms = sizeof (m), rs = sizeof (r), ds = sizeof (dest))
  113. {
  114. for (new len = min(min(ms, ds), min(ls, rs)), i; i != len; ++i)
  115. {
  116. dest[i] = @.cb(l[i], m[i], r[i]);
  117. }
  118. }
  119. #define ZipWith3({%0}%1) LAMBDA_iii<ZipWith3>{%0}(%1)
  120. stock ZipWith3_(Func:cb<iii>, const l[], const m[], const r[], ls = sizeof (l), ms = sizeof (m), rs = sizeof (r))
  121. {
  122. for (new len = min(ms, min(ls, rs)), i; i != len; ++i)
  123. {
  124. @.cb(l[i], m[i], r[i]);
  125. }
  126. }
  127. #define ZipWith3_({%0}%1) LAMBDA_iii<ZipWith3_>{%0}(%1)
  128. stock ZipWithIdx(Func:cb<iii>, const l[], const r[], dest[], ls = sizeof (l), rs = sizeof (r), ds = sizeof (dest))
  129. {
  130. for (new len = min(ds, min(ls, rs)), i; i != len; ++i)
  131. {
  132. dest[i] = @.cb(i, l[i], r[i]);
  133. }
  134. }
  135. #define ZipWithIdx({%0}%1) LAMBDA_iii<ZipWithIdx>{%0}(%1)
  136. stock ZipWithIdx_(Func:cb<iii>, const l[], const r[], ls = sizeof (l), rs = sizeof (r))
  137. {
  138. for (new len = min(ls, rs), i; i != len; ++i)
  139. {
  140. @.cb(i, l[i], r[i]);
  141. }
  142. }
  143. #define ZipWithIdx_({%0}%1) LAMBDA_iii<ZipWithIdx_>{%0}(%1)
  144. stock ZipWith3Idx(Func:cb<iiii>, const l[], const m[], const r[], dest[], ls = sizeof (l), ms = sizeof (m), rs = sizeof (r), ds = sizeof (dest))
  145. {
  146. for (new len = min(min(ms, ds), min(ls, rs)), i; i != len; ++i)
  147. {
  148. dest[i] = @.cb(i, l[i], m[i], r[i]);
  149. }
  150. }
  151. #define ZipWith3Idx({%0}%1) LAMBDA_iiii<ZipWith3Idx>{%0}(%1)
  152. stock ZipWith3Idx_(Func:cb<iiii>, const l[], const m[], const r[], ls = sizeof (l), ms = sizeof (m), rs = sizeof (r))
  153. {
  154. for (new len = min(ms, min(ls, rs)), i; i != len; ++i)
  155. {
  156. @.cb(i, l[i], m[i], r[i]);
  157. }
  158. }
  159. #define ZipWith3Idx_({%0}%1) LAMBDA_iiii<ZipWith3Idx_>{%0}(%1)
  160. stock FoldLIdx(Func:cb<iii>, n, const arr[], len = sizeof (arr))
  161. {
  162. new
  163. cur = n;
  164. for (new i = 0; i != len; ++i)
  165. {
  166. cur = @.cb(i, cur, arr[i]);
  167. }
  168. return cur;
  169. }
  170. #define FoldLIdx({%0}%1) LAMBDA_iii<FoldLIdx>{%0}(%1)
  171. stock ScanLIdx(Func:cb<iii>, n, const arr[], dest[], al = sizeof (arr), dl = sizeof (dest))
  172. {
  173. if (!dl) return 0;
  174. new
  175. len = min(al, dl - 1),
  176. i = -1,
  177. cur = n;
  178. while (++i != len)
  179. {
  180. dest[i] = cur,
  181. cur = @.cb(i, cur, arr[i]);
  182. }
  183. dest[i] = cur;
  184. return 1;
  185. }
  186. #define ScanLIdx({%0}%1) LAMBDA_iii<ScanLIdx>{%0}(%1)
  187. stock FoldRIdx(Func:cb<iii>, const arr[], n, len = sizeof (arr))
  188. {
  189. new
  190. cur = n;
  191. while (len--)
  192. {
  193. cur = @.cb(i, arr[len], cur);
  194. }
  195. return cur;
  196. }
  197. #define FoldRIdx({%0}%1) LAMBDA_iii<FoldRIdx>{%0}(%1)
  198. stock ScanRIdx(Func:cb<iii>, n, const arr[], dest[], al = sizeof (arr), dl = sizeof (dest))
  199. {
  200. if (!dl) return 0;
  201. new
  202. len = min(al, dl - 1),
  203. cur = n;
  204. dest[len] = cur;
  205. while (len--)
  206. {
  207. dest[len] = cur = @.cb(i, arr[len], cur);
  208. }
  209. return 1;
  210. }
  211. #define ScanRIdx({%0}%1) LAMBDA_iii<ScanRIdx>{%0}(%1)
  212. stock FoldL1Idx(Func:cb<iii>, const arr[], len = sizeof (arr))
  213. {
  214. assert(len > 0);
  215. new
  216. cur = arr[0];
  217. for (new i = 1; i != len; ++i)
  218. {
  219. cur = @.cb(i, cur, arr[i]);
  220. }
  221. return cur;
  222. }
  223. #define FoldL1Idx({%0}%1) LAMBDA_iii<FoldL1Idx>{%0}(%1)
  224. stock FoldR1Idx(Func:cb<iii>, const arr[], len = sizeof (arr))
  225. {
  226. assert(len > 0);
  227. new
  228. cur = arr[--len];
  229. while (len--)
  230. {
  231. cur = @.cb(i, arr[len], cur);
  232. }
  233. return cur;
  234. }
  235. #define FoldR1Idx({%0}%1) LAMBDA_iii<FoldR1Idx>{%0}(%1)
  236. stock FoldL(Func:cb<ii>, n, const arr[], len = sizeof (arr))
  237. {
  238. new
  239. cur = n;
  240. for (new i = 0; i != len; ++i)
  241. {
  242. cur = @.cb(cur, arr[i]);
  243. }
  244. return cur;
  245. }
  246. #define FoldL({%0}%1) LAMBDA_ii<FoldL>{%0}(%1)
  247. stock ScanL(Func:cb<ii>, n, const arr[], dest[], al = sizeof (arr), dl = sizeof (dest))
  248. {
  249. if (!dl) return 0;
  250. new
  251. len = min(al, dl - 1),
  252. i = -1,
  253. cur = n;
  254. while (++i != len)
  255. {
  256. dest[i] = cur,
  257. cur = @.cb(cur, arr[i]);
  258. }
  259. dest[i] = cur;
  260. return 1;
  261. }
  262. #define ScanL({%0}%1) LAMBDA_ii<ScanL>{%0}(%1)
  263. stock FoldR(Func:cb<ii>, const arr[], n, len = sizeof (arr))
  264. {
  265. new
  266. cur = n;
  267. while (len--)
  268. {
  269. cur = @.cb(arr[len], cur);
  270. }
  271. return cur;
  272. }
  273. #define FoldR({%0}%1) LAMBDA_ii<FoldR>{%0}(%1)
  274. stock ScanR(Func:cb<ii>, n, const arr[], dest[], al = sizeof (arr), dl = sizeof (dest))
  275. {
  276. if (!dl) return 0;
  277. new
  278. len = min(al, dl - 1),
  279. cur = n;
  280. dest[len] = cur;
  281. while (len--)
  282. {
  283. dest[len] = cur = @.cb(arr[len], cur);
  284. }
  285. return 1;
  286. }
  287. #define ScanR({%0}%1) LAMBDA_ii<ScanR>{%0}(%1)
  288. stock FoldL1(Func:cb<ii>, const arr[], len = sizeof (arr))
  289. {
  290. assert(len > 0);
  291. new
  292. cur = arr[0];
  293. for (new i = 1; i != len; ++i)
  294. {
  295. cur = @.cb(cur, arr[i]);
  296. }
  297. return cur;
  298. }
  299. #define FoldL1({%0}%1) LAMBDA_ii<FoldL1>{%0}(%1)
  300. stock FoldR1(Func:cb<ii>, const arr[], len = sizeof (arr))
  301. {
  302. assert(len > 0);
  303. new
  304. cur = arr[--len];
  305. while (len--)
  306. {
  307. cur = @.cb(arr[len], cur);
  308. }
  309. return cur;
  310. }
  311. #define FoldR1({%0}%1) LAMBDA_ii<FoldR1>{%0}(%1)
  312. stock bool:And(bool:arr[], len = sizeof (arr))
  313. {
  314. while (len--)
  315. {
  316. if (!arr[len]) return false;
  317. }
  318. return true;
  319. }
  320. stock bool:Or(bool:arr[], len = sizeof (arr))
  321. {
  322. while (len--)
  323. {
  324. if (arr[len]) return true;
  325. }
  326. return false;
  327. }
  328. stock bool:All(Func:cb<i>, const arr[], len = sizeof (arr))
  329. {
  330. for (new i; i != len; ++i)
  331. {
  332. if (!@.cb(arr[i])) return false;
  333. }
  334. return true;
  335. }
  336. #define All({%0}%1) LAMBDA_i<All>{%0}(%1)
  337. stock bool:Any(Func:cb<i>, const arr[], len = sizeof (arr))
  338. {
  339. for (new i; i != len; ++i)
  340. {
  341. if (@.cb(arr[i])) return true;
  342. }
  343. return false;
  344. }
  345. #define Any({%0}%1) LAMBDA_i<Any>{%0}(%1)
  346. stock bool:AllIdx(Func:cb<ii>, const arr[], len = sizeof (arr))
  347. {
  348. for (new i; i != len; ++i)
  349. {
  350. if (!@.cb(i, arr[i])) return false;
  351. }
  352. return true;
  353. }
  354. #define AllIdx({%0}%1) LAMBDA_ii<AllIdx>{%0}(%1)
  355. stock bool:AnyIdx(Func:cb<ii>, const arr[], len = sizeof (arr))
  356. {
  357. for (new i; i != len; ++i)
  358. {
  359. if (@.cb(i, arr[i])) return true;
  360. }
  361. return false;
  362. }
  363. #define AnyIdx({%0}%1) LAMBDA_ii<AnyIdx>{%0}(%1)
  364. stock Reverse(const arr[], len = sizeof (arr))
  365. {
  366. new
  367. temp;
  368. for (new i = 0; j = len - 1; i < j; ++i, --j)
  369. {
  370. temp = arr[i],
  371. arr[i] = arr[j],
  372. arr[j] = temp;
  373. }
  374. return 1;
  375. }
  376. stock bool:Elem(n, const arr[], len = sizeof (arr))
  377. {
  378. while (len--)
  379. {
  380. if (arr[len] == n) return true;
  381. }
  382. return false;
  383. }
  384. stock bool:NotElem(n, const arr[], len = sizeof (arr))
  385. {
  386. while (len--)
  387. {
  388. if (arr[len] == n) return false;
  389. }
  390. return true;
  391. }
  392. #define Just(%0) (YSI_gJust=_:%0,1)
  393. #define Nothing (0)