Guest User

foreach.inc

a guest
May 10th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.03 KB | None | 0 0
  1. #if defined _foreach_included
  2. #endinput
  3. #endif
  4. #define _foreach_included
  5.  
  6. #if !defined _samp_included
  7. #error "Please include a_samp or a_npc before foreach"
  8. #endif
  9.  
  10. #if defined SendChat || defined FOREACH_NO_PLAYERS
  11. #define BOTSYNC_IS_BOT (true)
  12. #endif
  13.  
  14. #if defined IsPlayerNPC
  15. #define _FOREACH_BOT
  16. #endif
  17.  
  18. #if !defined BOTSYNC_IS_BOT
  19. static
  20. bool:YSI_g_OPC = false,
  21. bool:YSI_g_OPDC = false;
  22. #endif
  23.  
  24. #if defined YSI_ITTER_NO_SORT
  25. #error "YSI_ITTER_NO_SORT is no longer supported by foreach"
  26. #endif
  27.  
  28. /*----------------------------------------------------------------------------*-
  29. Function:
  30. Itter_Create2
  31. Params:
  32. name - Itterator identifier.
  33. size0 - Number of iterators.
  34. size1 - Number of items per iterator.
  35. Return:
  36. -
  37. Notes:
  38. Creates a new array of itterator start/array pair.
  39. -*----------------------------------------------------------------------------*/
  40.  
  41. #define Iter_Create2 Itter_Create2
  42. #define Itter_Create2(%1,%2,%3) \
  43. new \
  44. YSI_g%1S[%2] = {-1, ...}, \
  45. YSI_g%1C[%2] = {0}, \
  46. YSI_g%1A[%2][%3]
  47.  
  48. /*----------------------------------------------------------------------------*-
  49. Function:
  50. Itter_Init2
  51. Params:
  52. itter - Name of the itterator array to initialise.
  53. Return:
  54. -
  55. Notes:
  56. Wrapper for Itter_InitInternal.
  57. -*----------------------------------------------------------------------------*/
  58.  
  59. #define Iter_Init2 Itter_Init2
  60. #define Itter_Init2(%1) \
  61. Itter_InitInternal(YSI_g%1A, sizeof (YSI_g%1A), sizeof (YSI_g%1A[]))
  62.  
  63. /*----------------------------------------------------------------------------*-
  64. Function:
  65. Itter_Add2
  66. Params:
  67. itter - Name of the itterator array to add the data to.
  68. index - Index of the iterator.
  69. value - Value to add to the itterator.
  70. Return:
  71. -
  72. Notes:
  73. Wrapper for Itter_AddInternal.
  74. -*----------------------------------------------------------------------------*/
  75.  
  76. #define Iter_Add2 Itter_Add2
  77. #define Itter_Add2(%1,%2,%3) \
  78. Itter_AddInternal(YSI_g%1S[%2], YSI_g%1C[%2], YSI_g%1A[%2], %3)
  79.  
  80. /*----------------------------------------------------------------------------*-
  81. Function:
  82. Itter_Free2
  83. Params:
  84. itter - Name of the itterator to get the first free slot in.
  85. index - Index of the iterator.
  86. Return:
  87. -
  88. Notes:
  89. Wrapper for Itter_FreeInternal.
  90. -*----------------------------------------------------------------------------*/
  91.  
  92. #define Iter_Free2 Itter_Free2
  93. #define Itter_Free2(%1,%2) \
  94. Itter_FreeInternal(YSI_g%1S[%2], YSI_g%1C[%2], YSI_g%1A[%2], sizeof (YSI_g%1A[]))
  95.  
  96. /*----------------------------------------------------------------------------*-
  97. Function:
  98. Itter_Remove2
  99. Params:
  100. itter - Name of the itterator to remove data from.
  101. index - Index of the iterator.
  102. value - Data to remove.
  103. Return:
  104. -
  105. Notes:
  106. Wrapper for Itter_RemoveInternal.
  107. -*----------------------------------------------------------------------------*/
  108.  
  109. #define Iter_Remove2 Itter_Remove2
  110. #define Itter_Remove2(%1,%2,%3) \
  111. Itter_RemoveInternal(YSI_g%1S[%2], YSI_g%1C[%2], YSI_g%1A[%2], %3)
  112.  
  113. /*----------------------------------------------------------------------------*-
  114. Function:
  115. Itter_Random2
  116. Params:
  117. itter - Name of the itterator to get a random slot from.
  118. index - Index of the iterator.
  119. Return:
  120. -
  121. Notes:
  122. Wrapper for Itter_RandomInternal.
  123. -*----------------------------------------------------------------------------*/
  124.  
  125. #define Iter_Random2 Itter_Random2
  126. #define Itter_Random2(%1,%2) \
  127. Itter_RandomInternal(YSI_g%1S[%2], YSI_g%1C[%2], YSI_g%1A[%2])
  128.  
  129. /*----------------------------------------------------------------------------*-
  130. Function:
  131. Itter_Debug2
  132. Params:
  133. itter - Name of the itterator to output debug information from.
  134. index - Index of the iterator.
  135. Return:
  136. -
  137. Notes:
  138. Wrapper for Itter_ShowArray.
  139. -*----------------------------------------------------------------------------*/
  140.  
  141. #define Iter_Debug2 Itter_Debug2
  142. #define Itter_Debug2(%1,%2) \
  143. Itter_ShowArray(YSI_g%1S[%2], YSI_g%1A[%2], YSI_g%1C[%2])
  144.  
  145. /*----------------------------------------------------------------------------*-
  146. Function:
  147. Itter_Count2
  148. Params:
  149. itter - Name of the itterator to get a random slot from4.
  150. index - Index of the iterator.
  151. Return:
  152. -
  153. Notes:
  154. Returns the number of items in this itterator.
  155. -*----------------------------------------------------------------------------*/
  156.  
  157. #define Iter_Count2 Itter_Count2
  158. #define Itter_Count2(%1,%2) \
  159. YSI_g%1C[%2]
  160.  
  161. /*----------------------------------------------------------------------------*-
  162. Function:
  163. foreach2
  164. Params:
  165. data - Data to itterate through.
  166. index - Index of the iterator.
  167. as - Variable to set value to.
  168. Return:
  169. -
  170. Notes:
  171. Not exactly the same as PHP foreach, just itterates through a list and
  172. returns the value of the current slot but uses that slot as the next index
  173. too. Variables must be in the form YSI_g<name>S for the start index and
  174. YSI_g<name>A for the data array where <name> is what's entered in data.
  175. -*----------------------------------------------------------------------------*/
  176.  
  177. #define foreach2(%1,%2,%3) \
  178. for (new %3 = YSI_g%1S[%2]; %3 != -1; %3 = YSI_g%1A[%2][%3])
  179.  
  180. /*----------------------------------------------------------------------------*-
  181. Function:
  182. foreachex2
  183. Params:
  184. data - Data to itterate through.
  185. index - Index of the iterator.
  186. as - Variable to set value to.
  187. Return:
  188. -
  189. Notes:
  190. Similar to foreach but doesn't declare a new variable for the itterator.
  191. -*----------------------------------------------------------------------------*/
  192.  
  193. #define foreachex2(%1,%2,%3) \
  194. for (%3 = YSI_g%1S[%2]; %3 != -1; %3 = YSI_g%1A[%2][%3])
  195.  
  196. /*----------------------------------------------------------------------------*-
  197. Function:
  198. Itter_Create
  199. Params:
  200. name - Itterator identifier.
  201. size - Number of values.
  202. Return:
  203. -
  204. Notes:
  205. Creates a new itterator start/array pair.
  206. -*----------------------------------------------------------------------------*/
  207.  
  208. #define Iter_Create Itter_Create
  209. #define Itter_Create(%1,%2) \
  210. new \
  211. YSI_g%1S = -1, \
  212. YSI_g%1C = 0, \
  213. YSI_g%1A[%2] = {-1, ...}
  214.  
  215. /*----------------------------------------------------------------------------*-
  216. Function:
  217. Itter_Add
  218. Params:
  219. itter - Name of the itterator to add the data to.
  220. value - Value to add to the itterator.
  221. Return:
  222. -
  223. Notes:
  224. Wrapper for Itter_AddInternal.
  225. -*----------------------------------------------------------------------------*/
  226.  
  227. #define Iter_Add Itter_Add
  228. #define Itter_Add(%1,%2) \
  229. Itter_AddInternal(YSI_g%1S, YSI_g%1C, YSI_g%1A, %2)
  230.  
  231. /*----------------------------------------------------------------------------*-
  232. Function:
  233. Itter_Free
  234. Params:
  235. itter - Name of the itterator to get the first free slot in.
  236. Return:
  237. -
  238. Notes:
  239. Wrapper for Itter_FreeInternal.
  240. -*----------------------------------------------------------------------------*/
  241.  
  242. #define Iter_Free Itter_Free
  243. #define Itter_Free(%1) \
  244. Itter_FreeInternal(YSI_g%1S, YSI_g%1C, YSI_g%1A, sizeof (YSI_g%1A))
  245.  
  246. /*----------------------------------------------------------------------------*-
  247. Function:
  248. Itter_Remove
  249. Params:
  250. itter - Name of the itterator to remove data from.
  251. value - Data to remove.
  252. Return:
  253. -
  254. Notes:
  255. Wrapper for Itter_RemoveInternal.
  256. -*----------------------------------------------------------------------------*/
  257.  
  258. #define Iter_Remove Itter_Remove
  259. #define Itter_Remove(%1,%2) \
  260. Itter_RemoveInternal(YSI_g%1S, YSI_g%1C, YSI_g%1A, %2)
  261.  
  262. /*----------------------------------------------------------------------------*-
  263. Function:
  264. Itter_Random
  265. Params:
  266. itter - Name of the itterator to get a random slot from.
  267. Return:
  268. -
  269. Notes:
  270. Wrapper for Itter_RandomInternal.
  271. -*----------------------------------------------------------------------------*/
  272.  
  273. #define Iter_Random Itter_Random
  274. #define Itter_Random(%1) \
  275. Itter_RandomInternal(YSI_g%1S, YSI_g%1C, YSI_g%1A)
  276.  
  277. /*----------------------------------------------------------------------------*-
  278. Function:
  279. Itter_Debug
  280. Params:
  281. itter - Name of the itterator to output debug information from.
  282. Return:
  283. -
  284. Notes:
  285. Wrapper for Itter_ShowArray.
  286. -*----------------------------------------------------------------------------*/
  287.  
  288. #define Iter_Debug Itter_Debug
  289. #define Itter_Debug(%1) \
  290. Itter_ShowArray(YSI_g%1S, YSI_g%1A, YSI_g%1C)
  291.  
  292. /*----------------------------------------------------------------------------*-
  293. Function:
  294. Itter_Count
  295. Params:
  296. itter - Name of the itterator to get a random slot from4.
  297. Return:
  298. -
  299. Notes:
  300. Returns the number of items in this itterator.
  301. -*----------------------------------------------------------------------------*/
  302.  
  303. #define Iter_Count Itter_Count
  304. #define Itter_Count(%1) \
  305. YSI_g%1C
  306.  
  307. /*----------------------------------------------------------------------------*-
  308. Create the internal itterators.
  309. -*----------------------------------------------------------------------------*/
  310.  
  311. #if !defined BOTSYNC_IS_BOT
  312. Itter_Create(Player, MAX_PLAYERS);
  313.  
  314. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  315. Itter_Create(Bot, MAX_PLAYERS);
  316. Itter_Create(Character, MAX_PLAYERS);
  317.  
  318. #define YSI_gNPCS YSI_gBotS
  319. #define YSI_gNPCC YSI_gBotC
  320. #define YSI_gNPCA YSI_gBotA
  321. #endif
  322. #endif
  323.  
  324. /*----------------------------------------------------------------------------*-
  325. Function:
  326. foreach
  327. Params:
  328. data - Data to itterate through.
  329. as - Variable to set value to.
  330. Return:
  331. -
  332. Notes:
  333. Not exactly the same as PHP foreach, just itterates through a list and
  334. returns the value of the current slot but uses that slot as the next index
  335. too. Variables must be in the form YSI_g<name>S for the start index and
  336. YSI_g<name>A for the data array where <name> is what's entered in data.
  337. -*----------------------------------------------------------------------------*/
  338.  
  339. #define foreach(%1,%2) \
  340. for (new %2 = YSI_g%1S; %2 != -1; %2 = YSI_g%1A[%2])
  341.  
  342. /*----------------------------------------------------------------------------*-
  343. Function:
  344. foreachex
  345. Params:
  346. data - Data to itterate through.
  347. as - Variable to set value to.
  348. Return:
  349. -
  350. Notes:
  351. Similar to foreach but doesn't declare a new variable for the itterator.
  352. -*----------------------------------------------------------------------------*/
  353.  
  354. #define foreachex(%1,%2) \
  355. for (%2 = YSI_g%1S; %2 != -1; %2 = YSI_g%1A[%2])
  356.  
  357. /*----------------------------------------------------------------------------*-
  358. Function:
  359. Itter_OnPlayerConnect
  360. Params:
  361. playerid - Player who joined.
  362. Return:
  363. -
  364. Notes:
  365. Adds a player to the loop data. Now sorts the list too. Note that I found
  366. the most bizzare bug ever (I *think* it may be a compiler but, but it
  367. requires further investigation), basically it seems that multiple variables
  368. were being treated as the same variable (namely YSI_gBotS and
  369. YSI_gCharacterS were the same and YSI_gBotC and YSI_gCharacterC were the
  370. same). Adding print statements which reference these variables seem to fix
  371. the problem, and I've tried to make sure that the values will never actually
  372. get printed.
  373. -*----------------------------------------------------------------------------*/
  374.  
  375. #if !defined BOTSYNC_IS_BOT
  376. public
  377. OnPlayerConnect(playerid)
  378. {
  379. #if defined _FOREACH_BOT
  380. if (!IsPlayerNPC(playerid))
  381. {
  382. Itter_Add(Player, playerid);
  383. }
  384. #if !defined FOREACH_NO_BOTS
  385. else
  386. {
  387. Itter_Add(Bot, playerid);
  388. }
  389. #pragma tabsize 4
  390. Itter_Add(Character, playerid);
  391. #endif
  392. #else
  393. Itter_Add(Player, playerid);
  394. #endif
  395. if (YSI_g_OPC)
  396. {
  397. return CallLocalFunction("Itter_OnPlayerConnect", "i", playerid);
  398. }
  399. return 1;
  400. }
  401.  
  402. #if defined _ALS_OnPlayerConnect
  403. #undef OnPlayerConnect
  404. #else
  405. #define _ALS_OnPlayerConnect
  406. #endif
  407. #define OnPlayerConnect Itter_OnPlayerConnect
  408.  
  409. forward
  410. Itter_OnPlayerConnect(playerid);
  411. #endif
  412.  
  413. /*----------------------------------------------------------------------------*-
  414. Function:
  415. Itter_OnGameModeInit
  416. Params:
  417. -
  418. Return:
  419. -
  420. Notes:
  421. There are WIERD bugs in this script, seemingly caused by the compiler, so
  422. this hopefully fixes them. The OnFilterScriptInit code is written to be
  423. very fast by utilising the internal array structure instead of the regular
  424. Add functions.
  425. -*----------------------------------------------------------------------------*/
  426.  
  427. #if !defined BOTSYNC_IS_BOT
  428. #if defined FILTERSCRIPT
  429. public
  430. OnFilterScriptInit()
  431. {
  432. if (YSI_gPlayerC)
  433. {
  434. print("foreach error: Something went wrong again! Please tell Y_less");
  435. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  436. printf("%d", YSI_gBotS);
  437. printf("%d", YSI_gBotC);
  438. printf("%d", YSI_gCharacterS);
  439. printf("%d", YSI_gCharacterC);
  440. #endif
  441. printf("%d", YSI_gPlayerS);
  442. printf("%d", YSI_gPlayerC);
  443. }
  444. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  445. new
  446. lastBot = -1,
  447. lastCharacter = -1;
  448. #endif
  449. new
  450. lastPlayer = -1;
  451. for (new i = 0; i != MAX_PLAYERS; ++i)
  452. {
  453. if (IsPlayerConnected(i))
  454. {
  455. #if defined _FOREACH_BOT
  456. if (!IsPlayerNPC(i))
  457. {
  458. if (lastPlayer == -1)
  459. {
  460. YSI_gPlayerS = i;
  461. }
  462. else
  463. {
  464. YSI_gPlayerA[lastPlayer] = i;
  465. }
  466. ++YSI_gPlayerC;
  467. lastPlayer = i;
  468. }
  469. #if !defined FOREACH_NO_BOTS
  470. else
  471. {
  472. if (lastBot == -1)
  473. {
  474. YSI_gBotS = i;
  475. }
  476. else
  477. {
  478. YSI_gBotA[lastBot] = i;
  479. }
  480. ++YSI_gBotC;
  481. lastBot = i;
  482. }
  483. #pragma tabsize 4
  484. if (lastCharacter == -1)
  485. {
  486. YSI_gCharacterS = i;
  487. }
  488. else
  489. {
  490. YSI_gCharacterA[lastCharacter] = i;
  491. }
  492. ++YSI_gCharacterC;
  493. lastCharacter = i;
  494. #endif
  495. #else
  496. if (lastPlayer == -1)
  497. {
  498. YSI_gPlayerS = i;
  499. }
  500. else
  501. {
  502. YSI_gPlayerA[lastPlayer] = i;
  503. }
  504. ++YSI_gPlayerC;
  505. lastPlayer = i;
  506. #endif
  507. }
  508. }
  509. YSI_g_OPC = (funcidx("Itter_OnPlayerConnect") != -1);
  510. YSI_g_OPDC = (funcidx("Itter_OnPlayerDisconnect") != -1);
  511. CallLocalFunction("Itter_OnFilterScriptInit", "");
  512. }
  513.  
  514. #if defined _ALS_OnFilterScriptInit
  515. #undef OnFilterScriptInit
  516. #else
  517. #define _ALS_OnFilterScriptInit
  518. #endif
  519. #define OnFilterScriptInit Itter_OnFilterScriptInit
  520.  
  521. forward Itter_OnFilterScriptInit();
  522. #else
  523. public
  524. OnGameModeInit()
  525. {
  526. if (YSI_gPlayerC)
  527. {
  528. print("foreach error: Something went wrong again! Please tell Y_less");
  529. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  530. printf("%d", YSI_gBotS);
  531. printf("%d", YSI_gBotC);
  532. printf("%d", YSI_gCharacterS);
  533. printf("%d", YSI_gCharacterC);
  534. #endif
  535. printf("%d", YSI_gPlayerS);
  536. printf("%d", YSI_gPlayerC);
  537. }
  538. YSI_g_OPC = (funcidx("Itter_OnPlayerConnect") != -1);
  539. YSI_g_OPDC = (funcidx("Itter_OnPlayerDisconnect") != -1);
  540. CallLocalFunction("Itter_OnGameModeInit", "");
  541. }
  542.  
  543. #if defined _ALS_OnGameModeInit
  544. #undef OnGameModeInit
  545. #else
  546. #define _ALS_OnGameModeInit
  547. #endif
  548. #define OnGameModeInit Itter_OnGameModeInit
  549.  
  550. forward
  551. Itter_OnGameModeInit();
  552. #endif
  553. #endif
  554.  
  555. /*----------------------------------------------------------------------------*-
  556. Function:
  557. Itter_OnPlayerDisconnect
  558. Params:
  559. playerid - Player who left.
  560. Return:
  561. -
  562. Notes:
  563. Removes a player from the loop data.
  564. -*----------------------------------------------------------------------------*/
  565.  
  566. #if !defined BOTSYNC_IS_BOT
  567. public
  568. OnPlayerDisconnect(playerid, reason)
  569. {
  570. #if defined _FOREACH_BOT
  571. if (!IsPlayerNPC(playerid))
  572. {
  573. Itter_Remove(Player, playerid);
  574. }
  575. #if !defined FOREACH_NO_BOTS
  576. else
  577. {
  578. Itter_Remove(Bot, playerid);
  579. }
  580. #pragma tabsize 4
  581. Itter_Remove(Character, playerid);
  582. #endif
  583. #else
  584. Itter_Remove(Player, playerid);
  585. #endif
  586. if (YSI_g_OPDC)
  587. {
  588. return CallLocalFunction("Itter_OnPlayerDisconnect", "ii", playerid, reason);
  589. }
  590. return 1;
  591. }
  592.  
  593. #if defined _ALS_OnPlayerDisconnect
  594. #undef OnPlayerDisconnect
  595. #else
  596. #define _ALS_OnPlayerDisconnect
  597. #endif
  598. #define OnPlayerDisconnect Itter_OnPlayerDisconnect
  599.  
  600. forward
  601. Itter_OnPlayerDisconnect(playerid, reason);
  602. #endif
  603.  
  604. /*----------------------------------------------------------------------------*-
  605. Function:
  606. Itter_ShowArray
  607. Params:
  608. start - Itterator start point.
  609. members[] - Itterator contents.
  610. size - Number of itterator values
  611. Return:
  612. -
  613. Notes:
  614. Pure debug function. Has regular prints not debug prints
  615. as it's only called when debug is on.
  616. -*----------------------------------------------------------------------------*/
  617.  
  618. stock
  619. Itter_ShowArray(start, members[], size)
  620. {
  621. static
  622. sString[61];
  623. new
  624. i,
  625. j = 10;
  626. printf("Start: %d", start);
  627. printf("Size: %d", size);
  628. while (i < size)
  629. {
  630. sString[0] = '\0';
  631. while (i < j && i < size)
  632. {
  633. format(sString, sizeof (sString), "%s, %d", sString, members[i]);
  634. i++;
  635. }
  636. printf("Array (%d): %s", j, sString);
  637. j += 10;
  638. }
  639. }
  640.  
  641. /*----------------------------------------------------------------------------*-
  642. Function:
  643. Itter_RandomInternal
  644. Params:
  645. start - Array start index.
  646. count - Number of items in the itterator.
  647. array[] - Itterator data.
  648. Return:
  649. -
  650. Notes:
  651. Returns a random value from an iterator.
  652. -*----------------------------------------------------------------------------*/
  653.  
  654. stock
  655. Itter_RandomInternal(start, count, array[])
  656. {
  657. if (count == 0)
  658. {
  659. return -1;
  660. }
  661. new
  662. rnd = random(count),
  663. cur = start;
  664. while (cur != -1)
  665. {
  666. if (rnd--)
  667. {
  668. cur = array[cur];
  669. }
  670. else
  671. {
  672. return cur;
  673. }
  674. }
  675. return -1;
  676. }
  677.  
  678. /*----------------------------------------------------------------------------*-
  679. Function:
  680. Itter_FreeInternal
  681. Params:
  682. start - Array start index.
  683. count - Number of items in the itterator.
  684. array[] - Itterator data.
  685. size - Size of the itterator.
  686. Return:
  687. -
  688. Notes:
  689. Finds the first free slot in the itterator. Itterators now HAVE to be
  690. sorted for this function to work correctly as it uses that fact to decide
  691. wether a slot is unused or the last one. If you want to use the slot
  692. straight after finding it the itterator will need to re-find it to add in
  693. the data.
  694. -*----------------------------------------------------------------------------*/
  695.  
  696. stock
  697. Itter_FreeInternal(start, count, array[], size)
  698. {
  699. if (count == size)
  700. {
  701. return -1;
  702. }
  703. else if (count == 0)
  704. {
  705. return 0;
  706. }
  707. new
  708. first = 0;
  709. while (first != -1)
  710. {
  711. if (first == start)
  712. {
  713. start = array[start];
  714. }
  715. else if (array[first] == -1)
  716. {
  717. return first;
  718. }
  719. ++first;
  720. }
  721. return -1;
  722. }
  723.  
  724. /*----------------------------------------------------------------------------*-
  725. Function:
  726. Itter_AddInternal
  727. Params:
  728. &start - Array start index.
  729. &count - Number of items in the itterator.
  730. array[] - Itterator data.
  731. value - Item to add.
  732. Return:
  733. -
  734. Notes:
  735. Adds a value to a given itterator set.
  736. -*----------------------------------------------------------------------------*/
  737.  
  738. stock
  739. Itter_AddInternal(&start, &count, array[], value)
  740. {
  741. if (array[value] != -1)
  742. {
  743. return 0;
  744. }
  745. ++count;
  746. if (start == -1)
  747. {
  748. start = value;
  749. }
  750. else if (start > value)
  751. {
  752. array[value] = start;
  753. start = value;
  754. }
  755. else
  756. {
  757. new
  758. cur = start,
  759. last;
  760. do
  761. {
  762. last = cur;
  763. cur = array[cur];
  764. if (cur > value)
  765. {
  766. array[value] = cur;
  767. array[last] = value;
  768. return 1;
  769. }
  770. }
  771. while (cur != -1);
  772. array[last] = value;
  773. }
  774. return 1;
  775. }
  776.  
  777. /*----------------------------------------------------------------------------*-
  778. Function:
  779. Itter_RemoveInternal
  780. Params:
  781. &start - Array start index.
  782. &count - Number of items in the itterator.
  783. array[] - Itterator data.
  784. value - Item to remove.
  785. Return:
  786. -
  787. Notes:
  788. Removes a value from an itterator.
  789. -*----------------------------------------------------------------------------*/
  790.  
  791. stock
  792. Itter_RemoveInternal(&start, &count, array[], value)
  793. {
  794. if (start == -1)
  795. {
  796. return 0;
  797. }
  798. if (start == value)
  799. {
  800. start = array[value];
  801. }
  802. else
  803. {
  804. new
  805. cur = start;
  806. while (array[cur] != value)
  807. {
  808. cur = array[cur];
  809. if (cur == -1)
  810. {
  811. return 0;
  812. }
  813. }
  814. array[cur] = array[value];
  815. }
  816. array[value] = -1;
  817. --count;
  818. return 1;
  819. }
  820.  
  821. /*----------------------------------------------------------------------------*-
  822. Function:
  823. Itter_InitInternal
  824. Params:
  825. array[][] - Itterator array to initialise.
  826. s0 - Size of first dimension.
  827. s1 - Size of second dimension.
  828. Return:
  829. -
  830. Notes:
  831. Multi-dimensional arrays can't be initialised at compile time, so need to be
  832. done at run time, which is slightly annoying.
  833. -*----------------------------------------------------------------------------*/
  834.  
  835. stock
  836. Itter_InitInternal(arr[][], s0, s1)
  837. {
  838. for (new i = 0; i != s0; ++i)
  839. {
  840. for (new j = 0; j != s1; ++j)
  841. {
  842. arr[i][j] = -1;
  843. }
  844. }
  845. }
Advertisement
Add Comment
Please, Sign In to add comment