Advertisement
ipsBruno

(Pawn) iEach development

Oct 13th, 2013
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1.  
  2.  
  3. /*
  4. *
  5.  * --- iEach ---
  6.  * Loops super velozes sobre arrays!
  7.  * Por Bruno da Silva
  8.  * [iPs]TeaM
  9.  * mixmusicas.com.br ipsbr.net
  10.  *
  11. */
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. #if defined _ieach_included
  19.   #endinput
  20. #endif
  21.  
  22. #define _ieach_included
  23.  
  24. #pragma library ieach
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. /*
  32.  * Função Each:%0[%1]
  33.  *
  34.  * Está função é servida para declarar novas arrays que usaram foreach
  35.  *
  36. */
  37.  
  38. #define Each:%0[%1] \
  39.                 g_IPSinicio%0 = -1, g_IPSfim%0 = -1, g_IPScount%0 , %0[%1] = {-1, ...}
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /*
  48. *
  49. * new Meach:array[x][y]
  50. *
  51. * Essa sintaxe serve para criar uma array multi-dimensional
  52. *
  53. */
  54.  
  55.  
  56. #define Meach:%0[%1][%2] \
  57.                 g_IPSinicio%0[%1] = {-1, ...}, g_IPSfim%0[%1] = {-1, ...}, g_IPScount%0[%1] = {-1, ...}, %0[%1][%2]
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. /*
  67.  * Função each(%0->new %1)
  68.  *
  69.  * Está função é servida para listar todos elementos ativo do loop
  70.  *
  71. */
  72.  
  73.  
  74. #define each(%1->new%0) \
  75.                 for( new %0  = g_IPSinicio%1; %0 ^ -1; %0 = %1[%0] )
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. /*
  84.  * Função eachex(%0->%1)
  85.  *
  86.  * Está função é servida para listar todos elementos ativo do loop
  87.  * A diferença, é que não declara variável!
  88. */
  89.  
  90.  
  91.  
  92. #define eachex(%1->%0) \
  93.                 for( %0  = g_IPSinicio%1; %0 ^ -1; %0 = %1[%0] )
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. /*
  101.  * Função EachAdd(%0,%1)
  102.  *
  103.  * Está função é servida para ativar itens da array
  104.  *
  105. */
  106.  
  107.  
  108. #define EachAdd(%0,%1) \
  109.             AdicionarElemento(%0, %1, g_IPSfim%0, g_IPSinicio%0, EachCount(%0))
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. /*
  117.  * Função EachRemove(%0,%1)
  118.  *
  119.  * Está função é para remover/desativar itens da array
  120.  *
  121. */
  122.  
  123.  
  124. #define EachRemove(%0,%1) \
  125.             RemoverElemento(%0, %1, g_IPSfim%0, g_IPSinicio%0, EachCount(%0))
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /*
  134.  * Função EachFree(%0)
  135.  *
  136.  * Está função limpa TODOS elementos da array, para o Each
  137.  *
  138. */
  139.  
  140.  
  141.  
  142. #define EachFree(%0) \
  143.             LimparElementos(%0, g_IPSfim%0, g_IPSinicio%0, EachCount(%0))
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. /*
  151.  * Função EachRemove(%0,%1)
  152.  *
  153.  * Está função é para contar o número de itens ativos na rray
  154.  *
  155. */
  156.  
  157.  
  158. #define EachCount(%0) \
  159.             g_IPScount%0
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. /*
  167. *
  168. * IsActive (%1, %0)
  169. *
  170. * Checa se um elemento da array está ativo
  171. *
  172. */
  173.  
  174.  
  175. #define IsActive(%1,%0) (!!(~%1[%0]) | (g_IPSfim%1 == %0))
  176.  
  177.  
  178.  
  179.  
  180.  
  181. /*
  182. *
  183. * RandomFromArray(array)
  184. * Pega um elemento randônomico da array
  185. *
  186. */
  187.  
  188.  
  189. #define RandomFromArray(%0) \
  190.             ElementoAleatorio(%0,  g_IPSinicio%0, EachCount(%0))
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. /*
  198.  * Funções internas do script, responsável por desativar elementos na array
  199.  *
  200.  */
  201.  
  202.  
  203. stock
  204.     ElementoAleatorio(arr[], inicio, totalelementos) {
  205.  
  206.     static r, i ;
  207.  
  208.     r = random(totalelementos);
  209.  
  210.     for( i  = inicio; i ^ -1; i = arr[i] ) {
  211.  
  212.         if(!r) break;
  213.  
  214.         --r;
  215.     }
  216.  
  217.     return i;
  218. }
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225. stock
  226.     LimparElementos(arr[], &ultimoelemento, &primeiroelemento, &totalelementos, size = sizeof arr)
  227. {
  228.  
  229.     for( new x = 0; x != size; x++) {
  230.         arr[x] = -1;
  231.     }
  232.    
  233.     ultimoelemento = -1;
  234.     primeiroelemento = -1;
  235.     totalelementos = 0;
  236.    
  237.     return true;
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245. stock
  246.  
  247.     RemoverElemento(arr[], id, &ultimoelemento, &primeiroelemento, &totalelementos, size = sizeof arr)
  248. {
  249.  
  250.     new origem = -1;
  251.  
  252.     if(~primeiroelemento) {
  253.  
  254.         if(id == primeiroelemento) {
  255.  
  256.             if(ultimoelemento == id) ultimoelemento = -1;
  257.  
  258.             primeiroelemento = arr[id];
  259.             arr[id] = -1;
  260.             --totalelementos;
  261.             return true;
  262.         }
  263.     }
  264.     else {
  265.         #if defined DEBUG
  266.         printf("[Erro] Array vazia, impossível deletar o elemento: %d", id);
  267.         #endif
  268.         return false;
  269.     }
  270.  
  271.     for( new i; i ^ size; i++) {
  272.         if(arr[i] == id) {
  273.             origem = i;
  274.             break;
  275.         }
  276.     }
  277.  
  278.  
  279.  
  280.     if(~origem) {
  281.  
  282.         if(~ultimoelemento) {
  283.             if(ultimoelemento == id) {
  284.                 ultimoelemento = origem;
  285.  
  286.             }
  287.         }
  288.  
  289.         arr[origem] = arr[id];
  290.         arr[id] = -1;
  291.         --totalelementos;
  292.         return true;
  293.     }
  294.     #if defined DEBUG
  295.     printf("[Erro] Elemento %d não está disponível na array.", id);
  296.     #endif
  297.     return false;
  298.  
  299. }
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306. stock
  307.  
  308.     AdicionarElemento(arr[], id, &ultimoelemento, &primeiroelemento, &totalelementos, size = sizeof arr)
  309. {
  310.  
  311.     if(!arr[id]) {
  312.         for(new x; x != size; x++) arr[x] = -1;
  313.     }
  314.  
  315.     if(id >= size){
  316.         #if defined DEBUG
  317.         printf("[Erro] ID acessado inválido: %d com tamanho arr[%d]", id, size);
  318.         #endif
  319.         return false;
  320.     }
  321.  
  322.     if(ultimoelemento == -1) {
  323.  
  324.         primeiroelemento = id;
  325.         ultimoelemento = id;
  326.     }
  327.     else {
  328.         if( primeiroelemento == id) {
  329.             #if defined DEBUG
  330.             printf("[Erro] O valor acesso já existe na array: %d", id);
  331.             #endif
  332.             return false;
  333.         }
  334.         for( new i; i != size; i++) {
  335.             if(arr[i] == id) {
  336.                 #if defined DEBUG
  337.                 printf("[Erro] O valor acesso já existe na array: %d", id);
  338.                 #endif
  339.                 return false;
  340.             }
  341.         }
  342.  
  343.         arr[ultimoelemento] = id;
  344.         ultimoelemento = id;
  345.  
  346.     }
  347.  
  348.     ++totalelementos;
  349.  
  350.     return true;
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement