23.07.2011, 11:03
Code:
1. /*----------------------------------------------------------------------------*- 2. =========================== 3. foreach efficient looping 4. =========================== 5. Description: 6. Provides efficient looping through sparse data sets, such as connected 7. players. Significantly improved from the original version to be a generic 8. loop system, rather then purely a player loop system. When used for 9. players this has constant time O(n) for number of connected players (n), 10. unlike standard player loops which are O(MAX_PLAYERS), regardless of the 11. actual number of connected players. Even when n is MAX_PLAYERS this is 12. still faster. 13. Legal: 14. Copyright © 2009 Alex "Y_Less" Cole 15. 16. The contents of this file are subject to the Mozilla Public License Version 17. 1.1 (the "License"); you may not use this file except in compliance with 18. the License. You may obtain a copy of the License at 19. http://www.mozilla.org/MPL/ 20. 21. Software distributed under the License is distributed on an "AS IS" basis, 22. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 23. for the specific language governing rights and limitations under the 24. License. 25. 26. The Original Code is the SA:MP foreach iterator code. 27. 28. The Initial Developer of the Original Code is Alex "Y_Less" Cole. 29. Version: 30. 0.1.8 31. Changelog: 32. 05/03/11: 33. Added tag-free code. 34. Updated to YSI y_iterate compatible code. 35. Added self-fixing code for internal errors. 36. 16/08/10: 37. Removed all the "2" versions of the functions. 38. 14/08/10: 39. Added Iter_Clear to reset an array. 40. 06/08/10: 41. Added special array declaration format. 42. 18/12/09: 43. Added Itter_Func2 functions for multi-dimensional iterators. 44. Renamed foreact et al as keywords in the documentation. 45. Changed licensing from GPL to MPL. 46. 02/09/09: 47. Fixed (again) for 0.3. 48. Added free slot finding. 49. 21/08/09: 50. Updated to include random functions. 51. Made entirely stand alone. 52. Ported to 0.3 (separate version). 53. Added automatic callback hook code. 54. Removed debug information from stand alone version. 55. 06/01/08: 56. Added debug information. 57. 09/10/07: 58. Moved to system. 59. 16/09/07: 60. Added list sorting. 61. Made this part of Y SeRver Includes, not Y Sever Includes. 62. Made list sorting optional. 63. Fixed version number. 64. 08/09/07: 65. First version. 66. Functions: 67. Public: 68. OnPlayerDisconnect - Called when a player leaves to remove them. 69. OnPlayerConnect - Called when a player connects to add them. 70. Core: 71. - 72. Stock: 73. Itter_ShowArray - Displays the contents of the array. 74. Itter_AddInternal - Add a value to an itterator. 75. Itter_RemoveInternal - Remove a value from an itterator. 76. Itter_RandomInternal - Get a random item from an itterator. 77. Itter_FreeInternal - Gets the first free slot in the itterator. 78. Itter_InitInternal - Initialises a multi-dimensional itterator. 79. Static: 80. - 81. Inline: 82. Itter_Create - Create a new itterator value set. 83. Itter_Add - Wraps Itter_AddInternal. 84. Itter_Remove - Wraps Itter_RemoveInternal. 85. Itter_Random - Wraps Itter_RandomInternal. 86. Itter_Count - Gets the number of items in an itterator. 87. Itter_Debug - Wraps around Itter_ShowArray. 88. Itter_Free - Wraps around Itter_FreeInternal. 89. Itter_Create2 - Create a new itterator array value set. 90. Itter_Add2 - Wraps Itter_AddInternal for arrays. 91. Itter_Remove2 - Wraps Itter_RemoveInternal for arrays. 92. Itter_Random2 - Wraps Itter_RandomInternal for arrays. 93. Itter_Count2 - Gets the number of items in an itterator array. 94. Itter_Debug2 - Wraps around Itter_ShowArray for arrays. 95. Itter_Free2 - Wraps around Itter_FreeInternal for arrays. 96. API: 97. - 98. Callbacks: 99. - 100. Hooks: 101. Itter_OnPlayerConnect - Hook for the OnPlayerConnect callback. 102. Itter_OnPlayerDisconnect - Hook for the OnPlayerDisconnect callback. 103. Itter_OnGameModeInit - Only exists to make the code compile correctly... 104. Definitions: 105. - 106. Enums: 107. - 108. Macros: 109. - 110. Keywords: 111. foreach - Command to loop an iterator. 112. foreachex - Like foreach but without a new variable. 113. foreach2 - Command to loop through an iterator array. 114. foreachex - Like foreach2 but without a new variable. 115. Tags: 116. Iterator - Declare an iterator. 117. Variables: 118. Global: 119. - 120. Static: 121. YSI_g_OPC - Records wether Itter_OnPlayerConnect exists for speed. 122. YSI_g_OPDC - Records wether Itter_OnPlayerDisconnect exists for speed. 123. Commands: 124. - 125. Compile options: 126. YSI_ITTER_NO_SORT - Removed. 127. FOREACH_NO_BOTS - Remove the bot iterators for smaller code. 128. FOREACH_NO_PLAYERS - Remove all default code for player itteration. 129. Operators: 130. - 131. Iterators: 132. Player - List of all players connected. 133. Bot - List of all bots (npcs) connected. 134. NPC - Alias of Bot. 135. Character - All players and bots. 136. -*----------------------------------------------------------------------------*/ 137. 138. #define _FOREACH_LOCAL_VERSION 2 139. 140. // Foreach is testing us. 141. #if defined _FOREACH_INC_TEST 142. #define _FOREACH_CUR_VERSION _FOREACH_LOCAL_VERSION 143. #endinput 144. #endif 145. #if !defined _FOREACH_NO_TEST 146. #define _FOREACH_INC_TEST 147. #tryinclude <YSI\y_iterate> 148. #undef _FOREACH_INC_TEST 149. // <y_iterate> exists - test which is newer. 150. #if defined _inc_y_iterate 151. #if !defined _FOREACH_CUR_VERSION 152. // y_iterate exists, but it's an old version - don't try use this 153. // system or the variables will conflict. 154. #endinput 155. #endif 156. #if _FOREACH_CUR_VERSION > _FOREACH_LOCAL_VERSION 157. // y_iterate is newer. 158. #undef _inc_y_iterate 159. #define _FOREACH_NO_TEST 160. #include <YSI\y_iterate> 161. #endinput 162. #endif 163. #endif 164. #endif 165. 166. #if !defined _samp_included 167. #error "Please include a_samp or a_npc before foreach" 168. #endif 169. 170. #if defined SendChat || defined FOREACH_NO_PLAYERS 171. #define BOTSYNC_IS_BOT (true) 172. #endif 173. 174. #if defined IsPlayerNPC 175. #define _FOREACH_BOT 176. #endif 177. 178. #if !defined BOTSYNC_IS_BOT 179. static 180. bool:YSI_g_OPC = false, 181. bool:YSI_g_OPDC = false; 182. #endif 183. 184. #if defined YSI_ITTER_NO_SORT 185. #error YSI_ITTER_NO_SORT is no longer supported by foreach. 186. #endif 187. 188. /*----------------------------------------------------------------------------*- 189. Function: 190. Itter_Create2 191. Params: 192. name - Itterator identifier. 193. size0 - Number of iterators. 194. size1 - Number of items per iterator. 195. Return: 196. - 197. Notes: 198. Creates a new array of itterator start/array pair. 199. -*----------------------------------------------------------------------------*/ 200. 201. #define Iter_Create2 Itter_Create2 202. #define Itter_Create2(%1,%2,%3) \ 203. new \ 204. YSI_gS%1[%2] = {-1, ...}, \ 205. YSI_gC%1[%2] = {0}, \ 206. YSI_gA%1[%2][%3] 207. 208. #define IteratorArray:%1[%2]<%3> \ 209. YSI_gS%1[%2] = {-1, ...}, \ 210. YSI_gC%1[%2] = {0}, \ 211. YSI_gA%1[%2][%3] 212. 213. /*----------------------------------------------------------------------------*- 214. Function: 215. Itter_Init2 216. Params: 217. itter - Name of the itterator array to initialise. 218. Return: 219. - 220. Notes: 221. Wrapper for Itter_InitInternal. 222. 223. native Iter_Init(IteratorArray:Name[]<>); 224. 225. -*----------------------------------------------------------------------------*/ 226. 227. #define Iter_Init Itter_Init 228. #define Itter_Init(%1) \ 229. Itter_InitInternal(YSI_gA%1, sizeof (YSI_gA%1), sizeof (YSI_gA%1[])) 230. 231. /*----------------------------------------------------------------------------*- 232. Function: 233. Itter_Create 234. Params: 235. name - Itterator identifier. 236. size - Number of values. 237. Return: 238. - 239. Notes: 240. Creates a new itterator start/array pair. 241. -*----------------------------------------------------------------------------*/ 242. 243. #define Iter_Create Itter_Create 244. #define Itter_Create(%1,%2) \ 245. new \ 246. YSI_gS%1 = -1, \ 247. YSI_gC%1 = 0, \ 248. YSI_gA%1[%2] = {-1, ...} 249. 250. /*----------------------------------------------------------------------------*- 251. Array: 252. Iterator 253. Notes: 254. Creates a new itterator start/array pair. 255. -*----------------------------------------------------------------------------*/ 256. 257. #define Iterator:%1<%2> \ 258. YSI_gS%1 = -1, \ 259. YSI_gC%1 = 0, \ 260. YSI_gA%1[%2] = {-1, ...} 261. 262. /*----------------------------------------------------------------------------*- 263. Function: 264. Itter_Add 265. Params: 266. itter - Name of the itterator to add the data to. 267. value - Value to add to the itterator. 268. Return: 269. - 270. Notes: 271. Wrapper for Itter_AddInternal. 272. 273. native Iter_Add(Iterator:Name<>, value); 274. 275. -*----------------------------------------------------------------------------*/ 276. 277. #define Iter_Add Itter_Add 278. #define Itter_Add(%1,%2) \ 279. Itter_AddInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1, %2) 280. 281. /*----------------------------------------------------------------------------*- 282. Function: 283. Itter_Free 284. Params: 285. itter - Name of the itterator to get the first free slot in. 286. Return: 287. - 288. Notes: 289. Wrapper for Itter_FreeInternal. 290. 291. native Iter_Free(Iterator:Name<>); 292. 293. -*----------------------------------------------------------------------------*/ 294. 295. #define Iter_Free Itter_Free 296. #define Itter_Free(%1) \ 297. Itter_FreeInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1, sizeof (YSI_gA%1)) 298. 299. /*----------------------------------------------------------------------------*- 300. Function: 301. Itter_Remove 302. Params: 303. itter - Name of the itterator to remove data from. 304. value - Data to remove. 305. Return: 306. - 307. Notes: 308. Wrapper for Itter_RemoveInternal. 309. 310. native Iter_Remove(Iterator:Name<>, value); 311. 312. -*----------------------------------------------------------------------------*/ 313. 314. #define Iter_Remove Itter_Remove 315. #define Itter_Remove(%1,%2) \ 316. Itter_RemoveInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1, %2) 317. 318. /*----------------------------------------------------------------------------*- 319. Function: 320. Itter_Random 321. Params: 322. itter - Name of the itterator to get a random slot from. 323. Return: 324. - 325. Notes: 326. Wrapper for Itter_RandomInternal. 327. 328. native Iter_Random(Iterator:Name<>); 329. 330. -*----------------------------------------------------------------------------*/ 331. 332. #define Iter_Random Itter_Random 333. #define Itter_Random(%1) \ 334. Itter_RandomInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1) 335. 336. /*----------------------------------------------------------------------------*- 337. Function: 338. Itter_Debug 339. Params: 340. itter - Name of the itterator to output debug information from. 341. Return: 342. - 343. Notes: 344. Wrapper for Itter_ShowArray. 345. -*----------------------------------------------------------------------------*/ 346. 347. #define Iter_Debug Itter_Debug 348. #define Itter_Debug(%1) \ 349. Itter_ShowArray(YSI_gS%1, YSI_gA%1, YSI_gC%1) 350. 351. /*----------------------------------------------------------------------------*- 352. Function: 353. Itter_Count 354. Params: 355. itter - Name of the itterator to get a random slot from4. 356. Return: 357. - 358. Notes: 359. Returns the number of items in this itterator. 360. 361. native Iter_Count(Iterator:Name<>); 362. 363. -*----------------------------------------------------------------------------*/ 364. 365. #define Iter_Count Itter_Count 366. #define Itter_Count(%1) \ 367. YSI_gC%1 368. 369. /*----------------------------------------------------------------------------*- 370. Function: 371. Itter_Clear 372. Params: 373. itter - Name of the itterator empty. 374. Return: 375. - 376. Notes: 377. Wrapper for Itter_ClearInternal. 378. 379. native Iter_Clear(IteratorArray:Name[]<>); 380. 381. -*----------------------------------------------------------------------------*/ 382. 383. #define Iter_Clear Itter_Clear 384. #define Itter_Clear(%1) \ 385. Itter_ClearInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1) 386. 387. /*----------------------------------------------------------------------------*- 388. Create the internal itterators. 389. -*----------------------------------------------------------------------------*/ 390. 391. #if !defined BOTSYNC_IS_BOT 392. new 393. Iterator:Player<MAX_PLAYERS>; 394. 395. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS 396. new 397. Iterator:Bot<MAX_PLAYERS>, 398. Iterator:Character<MAX_PLAYERS>; 399. 400. #define YSI_gNPCS YSI_gBotS 401. #define YSI_gNPCC YSI_gBotC 402. #define YSI_gNPCA YSI_gBotA 403. #endif 404. #endif 405. 406. /*----------------------------------------------------------------------------*- 407. Function: 408. foreach 409. Params: 410. data - Data to itterate through. 411. as - Variable to set value to. 412. Return: 413. - 414. Notes: 415. Not exactly the same as PHP foreach, just itterates through a list and 416. returns the value of the current slot but uses that slot as the next index 417. too. Variables must be in the form YSI_g<name>S for the start index and 418. YSI_g<name>A for the data array where <name> is what's entered in data. 419. -*----------------------------------------------------------------------------*/ 420. 421. #define foreach(%1,%2) \ 422. for (new %2 = YSI_gS%1; _:%2 != -1; %2 = YSI_gA%1[%2]) 423. 424. /*----------------------------------------------------------------------------*- 425. Function: 426. foreachex 427. Params: 428. data - Data to itterate through. 429. as - Variable to set value to. 430. Return: 431. - 432. Notes: 433. Similar to foreach but doesn't declare a new variable for the itterator. 434. -*----------------------------------------------------------------------------*/ 435. 436. #define foreachex(%1,%2) \ 437. for (%2 = YSI_gS%1; _:%2 != -1; %2 = YSI_gA%1[%2]) 438. 439. /*----------------------------------------------------------------------------*- 440. Function: 441. Itter_OnPlayerConnect 442. Params: 443. playerid - Player who joined. 444. Return: 445. - 446. Notes: 447. Adds a player to the loop data. Now sorts the list too. Note that I found 448. the most bizzare bug ever (I *think* it may be a compiler but, but it 449. requires further investigation), basically it seems that multiple variables 450. were being treated as the same variable (namely YSI_gBotS and 451. YSI_gCharacterS were the same and YSI_gBotC and YSI_gCharacterC were the 452. same). Adding print statements which reference these variables seem to fix 453. the problem, and I've tried to make sure that the values will never actually 454. get printed. 455. -*----------------------------------------------------------------------------*/ 456. 457. #if !defined BOTSYNC_IS_BOT 458. public 459. OnPlayerConnect(playerid) 460. { 461. #if defined _FOREACH_BOT 462. if (!IsPlayerNPC(playerid)) 463. { 464. Itter_Add(Player, playerid); 465. } 466. #if !defined FOREACH_NO_BOTS 467. else 468. { 469. Itter_Add(Bot, playerid); 470. } 471. #pragma tabsize 4 472. Itter_Add(Character, playerid); 473. #endif 474. #else 475. Itter_Add(Player, playerid); 476. #endif 477. if (YSI_g_OPC) 478. { 479. return CallLocalFunction("Itter_OnPlayerConnect", "i", playerid); 480. } 481. return 1; 482. } 483. 484. #if defined _ALS_OnPlayerConnect 485. #undef OnPlayerConnect 486. #else 487. #define _ALS_OnPlayerConnect 488. #endif 489. #define OnPlayerConnect Itter_OnPlayerConnect 490. 491. forward 492. Itter_OnPlayerConnect(playerid); 493. #endif 494. 495. /*----------------------------------------------------------------------------*- 496. Function: 497. Itter_OnGameModeInit 498. Params: 499. - 500. Return: 501. - 502. Notes: 503. There are WIERD bugs in this script, seemingly caused by the compiler, so 504. this hopefully fixes them. The OnFilterScriptInit code is written to be 505. very fast by utilising the internal array structure instead of the regular 506. Add functions. 507. -*----------------------------------------------------------------------------*/ 508. 509. #if !defined BOTSYNC_IS_BOT 510. #if defined FILTERSCRIPT 511. public 512. OnFilterScriptInit() 513. { 514. if (YSI_gCPlayer) 515. { 516. print("foreach error: Something went wrong again! Please tell Y_less"); 517. // Try reset. 518. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS 519. printf("%d", YSI_gSBot); 520. printf("%d", YSI_gCBot); 521. printf("%d", YSI_gSCharacter); 522. printf("%d", YSI_gCCharacter); 523. 524. #endif 525. printf("%d", YSI_gSPlayer); 526. printf("%d", YSI_gCPlayer); 527. } 528. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS 529. new 530. lastBot = -1, 531. lastCharacter = -1; 532. #endif 533. new 534. lastPlayer = -1; 535. for (new i = 0; i != MAX_PLAYERS; ++i) 536. { 537. if (IsPlayerConnected(i)) 538. { 539. #if defined _FOREACH_BOT 540. if (!IsPlayerNPC(i)) 541. { 542. if (lastPlayer == -1) 543. { 544. YSI_gSPlayer = i; 545. } 546. else 547. { 548. YSI_gAPlayer[lastPlayer] = i; 549. } 550. ++YSI_gCPlayer; 551. lastPlayer = i; 552. } 553. #if !defined FOREACH_NO_BOTS 554. else 555. { 556. if (lastBot == -1) 557. { 558. YSI_gSBot = i; 559. } 560. else 561. { 562. YSI_gABot[lastBot] = i; 563. } 564. ++YSI_gCBot; 565. lastBot = i; 566. } 567. #pragma tabsize 4 568. if (lastCharacter == -1) 569. { 570. YSI_gSCharacter = i; 571. } 572. else 573. { 574. YSI_gACharacter[lastCharacter] = i; 575. } 576. ++YSI_gCCharacter; 577. lastCharacter = i; 578. #endif 579. #else 580. if (lastPlayer == -1) 581. { 582. YSI_gSPlayer = i; 583. } 584. else 585. { 586. YSI_gAPlayer[lastPlayer] = i; 587. } 588. ++YSI_gCPlayer; 589. lastPlayer = i; 590. #endif 591. } 592. } 593. YSI_g_OPC = (funcidx("Itter_OnPlayerConnect") != -1); 594. YSI_g_OPDC = (funcidx("Itter_OnPlayerDisconnect") != -1); 595. CallLocalFunction("Itter_OnFilterScriptInit", ""); 596. } 597. 598. #if defined _ALS_OnFilterScriptInit 599. #undef OnFilterScriptInit 600. #else 601. #define _ALS_OnFilterScriptInit 602. #endif 603. #define OnFilterScriptInit Itter_OnFilterScriptInit 604. 605. forward Itter_OnFilterScriptInit(); 606. #else 607. public 608. OnGameModeInit() 609. { 610. if (YSI_gCPlayer) 611. { 612. print("foreach error: Something went wrong again! Is this a Filterscript and have you"); 613. print("foreach error: got \"#define FILTERSCRIPT\" above all your includes? Resetting..."); 614. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS 615. printf("%d", YSI_gSBot); 616. printf("%d", YSI_gCBot); 617. printf("%d", YSI_gSCharacter); 618. printf("%d", YSI_gCCharacter); 619. YSI_gCBot = 0; 620. YSI_gCCharacter = 0; 621. YSI_gSBot = -1; 622. YSI_gSCharacter = -1; 623. #endif 624. printf("%d", YSI_gSPlayer); 625. printf("%d", YSI_gCPlayer); 626. YSI_gCPlayer = 0; 627. YSI_gSPlayer = -1; 628. for (new i = 0; i != MAX_PLAYERS; ++i) 629. { 630. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS 631. YSI_gABot[i] = -1; 632. YSI_gACharacter[i] = -1; 633. #endif 634. YSI_gAPlayer[i] = -1; 635. } 636. } 637. YSI_g_OPC = (funcidx("Itter_OnPlayerConnect") != -1); 638. YSI_g_OPDC = (funcidx("Itter_OnPlayerDisconnect") != -1); 639. CallLocalFunction("Itter_OnGameModeInit", ""); 640. } 641. 642. #if defined _ALS_OnGameModeInit 643. #undef OnGameModeInit 644. #else 645. #define _ALS_OnGameModeInit 646. #endif 647. #define OnGameModeInit Itter_OnGameModeInit 648. 649. forward 650. Itter_OnGameModeInit(); 651. #endif 652. #endif 653. 654. /*----------------------------------------------------------------------------*- 655. Function: 656. Itter_OnPlayerDisconnect 657. Params: 658. playerid - Player who left. 659. Return: 660. - 661. Notes: 662. Removes a player from the loop data. 663. -*----------------------------------------------------------------------------*/ 664. 665. #if !defined BOTSYNC_IS_BOT 666. public 667. OnPlayerDisconnect(playerid, reason) 668. { 669. #if defined _FOREACH_BOT 670. if (!IsPlayerNPC(playerid)) 671. { 672. Itter_Remove(Player, playerid); 673. } 674. #if !defined FOREACH_NO_BOTS 675. else 676. { 677. Itter_Remove(Bot, playerid); 678. } 679. #pragma tabsize 4 680. Itter_Remove(Character, playerid); 681. #endif 682. #else 683. Itter_Remove(Player, playerid); 684. #endif 685. if (YSI_g_OPDC) 686. { 687. return CallLocalFunction("Itter_OnPlayerDisconnect", "ii", playerid, reason); 688. } 689. return 1; 690. } 691. 692. #if defined _ALS_OnPlayerDisconnect 693. #undef OnPlayerDisconnect 694. #else 695. #define _ALS_OnPlayerDisconnect 696. #endif 697. #define OnPlayerDisconnect Itter_OnPlayerDisconnect 698. 699. forward 700. Itter_OnPlayerDisconnect(playerid, reason); 701. #endif 702. 703. /*----------------------------------------------------------------------------*- 704. Function: 705. Itter_ShowArray 706. Params: 707. start - Itterator start point. 708. members[] - Itterator contents. 709. size - Number of itterator values 710. Return: 711. - 712. Notes: 713. Pure debug function. Has regular prints not debug prints 714. as it's only called when debug is on. 715. -*----------------------------------------------------------------------------*/ 716. 717. stock 718. Itter_ShowArray(start, members[], size) 719. { 720. static 721. sString[61]; 722. new 723. i, 724. j = 10; 725. printf("Start: %d", start); 726. printf("Size: %d", size); 727. while (i < size) 728. { 729. sString[0] = '\0'; 730. while (i < j && i < size) 731. { 732. format(sString, sizeof (sString), "%s, %d", sString, members[i]); 733. i++; 734. } 735. printf("Array (%d): %s", j, sString); 736. j += 10; 737. } 738. } 739. 740. /*----------------------------------------------------------------------------*- 741. Function: 742. Itter_RandomInternal 743. Params: 744. start - Array start index. 745. count - Number of items in the itterator. 746. array[] - Itterator data. 747. Return: 748. - 749. Notes: 750. Returns a random value from an iterator. 751. -*----------------------------------------------------------------------------*/ 752. 753. stock 754. Itter_RandomInternal(start, count, array[]) 755. { 756. if (count == 0) 757. { 758. return -1; 759. } 760. new 761. rnd = random(count), 762. cur = start; 763. while (cur != -1) 764. { 765. if (rnd--) 766. { 767. cur = array[cur]; 768. } 769. else 770. { 771. return cur; 772. } 773. } 774. return -1; 775. } 776. 777. /*----------------------------------------------------------------------------*- 778. Function: 779. Itter_FreeInternal 780. Params: 781. start - Array start index. 782. count - Number of items in the itterator. 783. array[] - Itterator data. 784. size - Size of the itterator. 785. Return: 786. - 787. Notes: 788. Finds the first free slot in the itterator. Itterators now HAVE to be 789. sorted for this function to work correctly as it uses that fact to decide 790. wether a slot is unused or the last one. If you want to use the slot 791. straight after finding it the itterator will need to re-find it to add in 792. the data. 793. -*----------------------------------------------------------------------------*/ 794. 795. stock 796. Itter_FreeInternal(start, count, array[], size) 797. { 798. if (count == size) 799. { 800. return -1; 801. } 802. else if (count == 0) 803. { 804. return 0; 805. } 806. new 807. first = 0; 808. while (first != -1) 809. { 810. if (first == start) 811. { 812. start = array[start]; 813. } 814. else if (array[first] == -1) 815. { 816. return first; 817. } 818. ++first; 819. } 820. return -1; 821. } 822. 823. /*----------------------------------------------------------------------------*- 824. Function: 825. Itter_AddInternal 826. Params: 827. &start - Array start index. 828. &count - Number of items in the itterator. 829. array[] - Itterator data. 830. value - Item to add. 831. Return: 832. - 833. Notes: 834. Adds a value to a given itterator set. 835. -*----------------------------------------------------------------------------*/ 836. 837. stock 838. Itter_AddInternal(&start, &count, array[], value) 839. { 840. if (array[value] != -1) 841. { 842. return 0; 843. } 844. ++count; 845. if (start == -1) 846. { 847. start = value; 848. } 849. else if (start > value) 850. { 851. array[value] = start; 852. start = value; 853. } 854. else 855. { 856. new 857. cur = start, 858. last; 859. do 860. { 861. last = cur; 862. cur = array[cur]; 863. if (cur > value) 864. { 865. array[value] = cur; 866. array[last] = value; 867. return 1; 868. } 869. } 870. while (cur != -1); 871. array[last] = value; 872. } 873. return 1; 874. } 875. 876. /*----------------------------------------------------------------------------*- 877. Function: 878. Itter_RemoveInternal 879. Params: 880. &start - Array start index. 881. &count - Number of items in the itterator. 882. array[] - Itterator data. 883. value - Item to remove. 884. Return: 885. - 886. Notes: 887. Removes a value from an itterator. 888. -*----------------------------------------------------------------------------*/ 889. 890. stock 891. Itter_RemoveInternal(&start, &count, array[], value) 892. { 893. if (start == -1) 894. { 895. return 0; 896. } 897. if (start == value) 898. { 899. start = array[value]; 900. } 901. else 902. { 903. new 904. cur = start; 905. while (array[cur] != value) 906. { 907. cur = array[cur]; 908. if (cur == -1) 909. { 910. return 0; 911. } 912. } 913. array[cur] = array[value]; 914. } 915. array[value] = -1; 916. --count; 917. return 1; 918. } 919. 920. /*----------------------------------------------------------------------------*- 921. Function: 922. Itter_ClearInternal 923. Params: 924. &start - Array start index. 925. &count - Number of items in the itterator. 926. array[] - Itterator data. 927. Return: 928. - 929. Notes: 930. Resets an iterator. 931. -*----------------------------------------------------------------------------*/ 932. 933. stock 934. Itter_ClearInternal(&start, &count, array[]) 935. { 936. if (start != -1) 937. { 938. new 939. cur = start, 940. next = array[cur]; 941. start = -1; 942. count = 0; 943. while (next != -1) 944. { 945. array[cur] = -1; 946. cur = next; 947. next = array[cur]; 948. } 949. } 950. } 951. 952. /*----------------------------------------------------------------------------*- 953. Function: 954. Itter_InitInternal 955. Params: 956. array[][] - Itterator array to initialise. 957. s0 - Size of first dimension. 958. s1 - Size of second dimension. 959. Return: 960. - 961. Notes: 962. Multi-dimensional arrays can't be initialised at compile time, so need to be 963. done at run time, which is slightly annoying. 964. -*----------------------------------------------------------------------------*/ 965. 966. stock 967. Itter_InitInternal(arr[][], s0, s1) 968. { 969. for (new i = 0; i != s0; ++i) 970. { 971. for (new j = 0; j != s1; ++j) 972. { 973. arr[i][j] = -1; 974. } 975. } 976. }
And should I go into word and save it as foreach.inc?
SHOULD THE NAME OF THE DOCUMENT BE FOREACH?