Advertisement
DraKiNs

[INC] i_foreach.inc FAST FOREACH

Aug 29th, 2011
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.46 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //   Foreach (fast loop in arrays)
  4. //     Author: Bruno da Silva
  5. //   www.ips-team.blogspot.com
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. #define newItterArray:%0<%1>        ipsArray%0[%1] = {-1, ...}, ipsInit%0 = -1, ipsFinal%0 = 0
  10.  
  11. #define addItterArray:%0<%1>        intervalChangeItter(ipsArray%0, %1, ipsInit%0, ipsFinal%0)
  12. #define delItterArray:%0<%1>        intervalDeleteItter(ipsArray%0, %1, ipsInit%0, ipsFinal%0)
  13.  
  14. #define getItterArray:%0<%1>        ipsArray%0[%1]
  15. #define foreach(%0,%1)              for(new %1 = ipsInit%0 ; %1 != -1; %1 = ipsArray%0[%1])
  16.  
  17. new newItterArray:Player<MAX_PLAYERS>;
  18.  
  19. intervalChangeItter(array[], valor, &inicio, &ultimoNumeroVar)
  20. {
  21.     if(array[valor] != -1 || valor == -1) {
  22.         return false;
  23.     }
  24.  
  25.     if(inicio == -1) {
  26.         return inicio = valor, true;
  27.     }
  28.  
  29.     if(valor < inicio) {
  30.         return array[valor] = inicio, inicio = valor, true;
  31.     }
  32.  
  33.     if(array[inicio] == -1) {
  34.         return array[inicio] = valor, ultimoNumeroVar = valor;
  35.     }
  36.  
  37.     return array[ultimoNumeroVar] = valor, ultimoNumeroVar = valor, true;
  38. }
  39.  
  40.  
  41. intervalDeleteItter(array[], value, &inicio, &ultimoNumeroVar)
  42. {
  43.     if(inicio == -1 || value == -1) {
  44.         return false;
  45.     }
  46.     if(inicio == value) {
  47.         return inicio = -1, array[value] = -1, true;
  48.     }
  49.  
  50.     ultimoNumeroVar = 0;
  51.  
  52.     for( ; array[ultimoNumeroVar] != value; ultimoNumeroVar = array[ultimoNumeroVar]) {
  53.         if(ultimoNumeroVar == -1) return false;
  54.     }
  55.  
  56.     return array[ultimoNumeroVar] = array[value], array[value] = -1, true;
  57.  
  58. }
  59.  
  60.  
  61. public OnPlayerConnect(playerid)
  62. {
  63.     addItterArray:Player<playerid>;
  64.     return CallLocalFunction("CallOnPlayerConnect", "i", playerid);
  65. }
  66.  
  67.  
  68. public OnPlayerDisconnect(playerid, reason)
  69. {
  70.     delItterArray:Player<playerid>;
  71.     return CallLocalFunction("CallOnPlayerDisconnect", "ii", playerid, reason);
  72. }
  73.  
  74.  
  75. #if defined HookOnPlayerConnect
  76. #undef OnPlayerConnect
  77. #else
  78. #define HookOnPlayerConnect
  79. #endif
  80.  
  81. #define OnPlayerConnect CallOnPlayerConnect
  82.  
  83. #if defined HookOnPlayerDisconnect
  84. #undef OnPlayerDisconnect
  85. #else
  86. #define HookOnPlayerDisconnect
  87. #endif
  88.  
  89. #define OnPlayerDisconnect CallOnPlayerDisconnect
  90.  
  91. forward OnPlayerConnect(playerid);
  92. forward OnPlayerDisconnect(playerid, reason);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement