Advertisement
ipsBruno

(Pawn) Linked List V2

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