1
0

impl.inc 10 KB

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