09.09.2016, 15:56
(
Последний раз редактировалось Gammix; 09.09.2016 в 19:54.
)
Here is my version of foreach:
Faster than ******'s as per my speed tests and have some good functions to work around with.
Example:
pawn Код:
/*
native Iter_Loop(&variable)<type> { }
native Iter_LoopEx(&variable, &index, start, end)<type> { }
native _
native Iter_Size<type>;
native Iter_PoolSize<type>;
native Iter_IsFull<type>;
native _
native Iter_Add(value)<type>;
native Iter_Remove(value)<type>;
native Iter_Value(index)<type>;
native Iter_Index(value)<type>;
native Iter_IsValidValue(index)<type>;
native _
native Iter_Random<type>;
*/
#define Iter:%1<%2> \
IterData_%1[%2] = {-1, ...}, IterSize_%1 = %2, IterPoolSize_%1 = 0
#define Iter_Loop(%1)<%2> \
for (new %1_index = 0, %1 = IterData_%2[0]; %1_index < IterPoolSize_%2; %1 = IterData_%2[++%1_index])
#define Iter_LoopEx(%1,%3,%4,%5)<%2> \
for (new %3 = %4, %1 = IterData_%2[%4], %1_limit = (((%5 < 0 && %5 >= IterPoolSize_%2) || (%5 < %4)) ? (IterPoolSize_%2) : (%5)); %3 < %1_limit; %1 = IterData_%2[++%3])
#define Iter_Size<%2> \
IterSize_%2
#define Iter_PoolSize<%2> \
(IterPoolSize_%2 - 1)
#define Iter_IsFull<%2> \
((IterPoolSize_%2 + 1) == IterSize_%2)
#define Iter_Add(%1)<%2> \
if (_Iter_Add(IterData_%2, IterSize_%2, IterPoolSize_%2, %1)) IterPoolSize_%2++
stock bool: _Iter_Add(array[], size, index, value)
{
if (index >= size)
return false;
array[index] = value;
return true;
}
#define Iter_Remove(%1)<%2> \
if (_Iter_Remove(IterData_%2, IterPoolSize_%2, %1)) IterPoolSize_%2--
stock bool: _Iter_Remove(array[], poolsize, value)
{
new bool: _shift;
for (new _i; _i < poolsize; _i++)
{
if (!_shift)
{
if (array[_i] == value)
_shift = true;
}
if (_shift)
{
if ((_i + 1) < poolsize)
array[_i] = array[_i + 1];
}
}
if (!_shift)
return false;
array[poolsize] = -1;
return true;
}
#define Iter_Value(%1)<%2> \
((%1 < IterPoolSize_%2 && %1 >= 0) ? (IterData_%2[%1]) : (-1))
#define Iter_Index(%1)<%2> \
_Iter_Index(IterData_%2, IterSize_%2, %1)
stock _Iter_Index(array[], size, check)
{
for (new _i; _i < size; _i++)
{
if (array[_i] == check)
return _i;
}
return -1;
}
#define Iter_IsValidValue(%1)<%2> \
_Iter_IsValidValue(IterData_%2, IterSize_%2, IterPoolSize_%2, %1)
stock bool: _Iter_IsValidValue(array[], size, poolsize, check)
{
if (poolsize == 0)
return false;
for (new _i; _i < size; _i++)
{
if (array[_i] == check)
return true;
}
return false;
}
#define Iter_Random<%2> \
IterData_%2[random(IterPoolSize_%2)]
Example:
pawn Код:
#include <a_samp>
#include <g_foreach>
new Iter:PLAYERS<MAX_PLAYERS>;
public OnPlayerConnect(playerid)
{
Iter_Add(playerid)<PLAYERS>;
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
Iter_Remove(playerid)<PLAYERS>;
return 1;
}
forward OnCall();
public OnCall()
{
Iter_Loop(i)<PLAYERS>
{
printf("PlayerId: %i", i);
}
return 1;
}