Advertisement
DraKiNs

[Include] Foreach V2

Sep 9th, 2011
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.25 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //             Foreach V2 (fast loop ir arrays pawn)
  4. //  Author: Bruno da Silva (thanks SlashPT)
  5. //       www.ips-team.blogspot.com
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. #define Iterator:%0<%1>     ipsArray%0[%1] = {-1, ...}, ipsInit%0 = -1, ipsFinal%0 = 0, ipsItens%0 = 0
  10. #define IteratorArray:%0[%1]<%2>    ipsArray%0[%1][%2], ipsInit%0[%1] = {-1, ...}, ipsFinal%0[%1], ipsItens%0[%1]; functionArray_Init(ipsArray%0, %1, %2)
  11.  
  12. #define Iter_Add(%0,%1)     functionChangeIter(ipsArray%0, %1, ipsInit%0, ipsFinal%0, ipsItens%0)
  13. #define Iter_Remove(%0,%1)  functionDeleteIter(ipsArray%0, %1, ipsInit%0, ipsFinal%0, ipsItens%0)
  14.  
  15. #define Iter_Count(%0)      _:ipsItens%0
  16. #define Iter_Random(%0)     functionRandomIterArray(ipsInit%0, ipsFinal%0, ipsArray%0)
  17.  
  18. #define Iter_Get(%0, %1)        ipsArray%0[%1]
  19.  
  20. #define foreach(%0,%1)      for(new %1 = ipsInit%0 ; %1 != -1; %1 = ipsArray%0[%1])
  21. #define foreachex(%0,%1)        for(%1 = ipsInit%0 ; %1 != -1; %1 = ipsArray%0[%1])
  22.  
  23. new Iterator:Player<MAX_PLAYERS>;
  24.  
  25. functionArray_Init(array[][], max1, max2)
  26. {
  27.     for (new i; max1 != i; ++i) for (new j = 0; max2 != j; ++j)
  28.     {
  29.         array[i][j] = -1;
  30.     }
  31.     return true;
  32. }
  33.  
  34. functionRandomIterArray(iniciar, finalizar,array[])
  35. {
  36.    
  37.     if(iniciar == -1) return -1; // não deixar travar servidor
  38.    
  39.     new retornarValor = -1;
  40.    
  41.     inicializarBusca:
  42.  
  43.     retornarValor = array[random(finalizar)];
  44.  
  45.     if(retornarValor == -1) goto inicializarBusca;
  46.  
  47.     return retornarValor;
  48.    
  49. }
  50.  
  51. functionChangeIter(array[], valor, &inicio, &ultimoNumeroVar, &count)
  52. {
  53.     if(valor == -1 || array[valor] != -1)
  54.     {  
  55.         return false;
  56.     }
  57.    
  58.     ++count;
  59.     if(inicio == -1)
  60.     {      
  61.         return inicio = valor, ultimoNumeroVar = valor;
  62.     }
  63.    
  64.     if(valor < inicio)
  65.     {
  66.        
  67.         return array[valor] = inicio, inicio = valor, true;
  68.     }
  69.    
  70.     if(array[inicio] == -1)
  71.     {
  72.         return array[inicio] = valor, ultimoNumeroVar = valor;
  73.     }
  74.    
  75.     return array[ultimoNumeroVar] = valor, ultimoNumeroVar = valor, true;
  76. }
  77.  
  78. functionDeleteIter(array[], value, &inicio, &ultimoNumeroVar, &count)
  79. {  
  80.     if(inicio == -1 || value == -1)
  81.     {
  82.         return false;
  83.     }
  84.     --count;
  85.     if(inicio == value)
  86.     {
  87.         return inicio = -1, array[value] = -1, true;
  88.     }
  89.    
  90.     ultimoNumeroVar = 0;
  91.    
  92.     for( ; array[ultimoNumeroVar] != value; ultimoNumeroVar = array[ultimoNumeroVar])
  93.     {
  94.         if(ultimoNumeroVar == -1) return false;
  95.     }
  96.    
  97.     return array[ultimoNumeroVar] = array[value], array[value] = -1, true;
  98.  
  99. }
  100.  
  101. public OnPlayerConnect(playerid)
  102. {
  103.     Iter_Add(Player, playerid);
  104.     return CallLocalFunction("CallOnPlayerConnect", "i", playerid);
  105. }
  106.  
  107. public OnPlayerDisconnect(playerid, reason)
  108. {
  109.     Iter_Remove(Player, playerid);
  110.     return CallLocalFunction("CallOnPlayerDisconnect", "ii", playerid, reason);
  111. }
  112.  
  113.  
  114. #if defined HookOnPlayerConnect
  115.     #undef OnPlayerConnect
  116. #else
  117.     #define HookOnPlayerConnect
  118. #endif
  119.  
  120. #define OnPlayerConnect CallOnPlayerConnect
  121.  
  122. #if defined HookOnPlayerDisconnect
  123.     #undef OnPlayerDisconnect
  124. #else
  125.     #define HookOnPlayerDisconnect
  126. #endif
  127.  
  128. #define OnPlayerDisconnect CallOnPlayerDisconnect
  129.  
  130. forward OnPlayerConnect(playerid);
  131. forward OnPlayerDisconnect(playerid, reason);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement