Advertisement
Y_Less

foreach 0.1.8

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