[Include] foreach 0.4.1 standalone include
#28

Here is my version of foreach:

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)]
Faster than ******'s as per my speed tests and have some good functions to work around with.

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;
}
Reply


Messages In This Thread
foreach 19 standalone include - by Kar - 14.04.2015, 10:02
Re: foreach 0.4.1 standalone include - by RaeF - 14.04.2015, 10:07
Re: foreach 0.4.1 standalone include - by lukewid - 14.04.2015, 10:53
Re: foreach 0.4.1 standalone include - by Crayder - 14.04.2015, 11:31
Re: foreach 0.4.1 standalone include - by Gammix - 14.04.2015, 13:43
Re: foreach 0.4.1 standalone include - by Kar - 14.04.2015, 14:32
Re: foreach 0.4.1 standalone include - by Crayder - 14.04.2015, 20:44
Re: foreach 0.4.1 standalone include - by Kar - 14.04.2015, 23:35
Re: foreach 0.4.1 standalone include - by Emmet_ - 14.04.2015, 23:40
Re: foreach 0.4.1 standalone include - by Crayder - 15.04.2015, 00:10
Re: foreach 0.4.1 standalone include - by Abagail - 15.04.2015, 01:06
Re: foreach 0.4.1 standalone include - by Sasino97 - 05.09.2015, 10:27
Re: foreach 0.4.1 standalone include - by n0minal - 28.09.2015, 23:34
Re: foreach 0.4.1 standalone include - by Crayder - 28.09.2015, 23:53
Re: foreach 0.4.1 standalone include - by n0minal - 29.09.2015, 00:04
Re: foreach 0.4.1 standalone include - by Kar - 29.09.2015, 00:36
Re: foreach 0.4.1 standalone include - by Abagail - 29.09.2015, 02:17
Re: foreach 0.4.1 standalone include - by n0minal - 29.09.2015, 02:23
Re: foreach 0.4.1 standalone include - by Crayder - 29.09.2015, 03:50
Re: foreach 0.4.1 standalone include - by Droxx - 07.01.2016, 13:44
Re: foreach 0.4.1 standalone include - by Kar - 13.07.2016, 05:20
Re: foreach 0.4.1 standalone include - by justice96 - 13.07.2016, 05:23
Re: foreach 0.4.1 standalone include - by Liberation - 29.08.2016, 23:46
Re: foreach 0.4.1 standalone include - by Kar - 30.08.2016, 00:00
Re: foreach 0.4.1 standalone include - by Kar - 30.08.2016, 01:01
Re: foreach 0.4.1 standalone include - by Crayder - 30.08.2016, 06:32
Re: foreach 0.4.1 standalone include - by Kar - 31.08.2016, 00:22
Re: foreach 0.4.1 standalone include - by Gammix - 09.09.2016, 15:56
Re: foreach 0.4.1 standalone include - by Stinged - 09.09.2016, 17:10
Re: foreach 0.4.1 standalone include - by Gammix - 09.09.2016, 17:23
Re: foreach 0.4.1 standalone include - by GhostHacker - 10.09.2016, 04:25
Re: foreach 0.4.1 standalone include - by Spmn - 10.09.2016, 22:04
Re: foreach 0.4.1 standalone include - by GhostHacker - 11.09.2016, 02:12
Re: foreach 0.4.1 standalone include - by SyS - 13.09.2016, 15:10
Re: foreach 0.4.1 standalone include - by SyS - 15.09.2016, 02:09
Re: foreach 0.4.1 standalone include - by GhostHacker - 15.09.2016, 04:02
Re: foreach 0.4.1 standalone include - by SyS - 15.09.2016, 04:05
Re: foreach 0.4.1 standalone include - by ZiGGi - 15.09.2016, 04:36
Re: foreach 0.4.1 standalone include - by Crayder - 15.09.2016, 05:12
Re: foreach 0.4.1 standalone include - by Konstantinos - 15.09.2016, 07:47
Re: foreach 0.4.1 standalone include - by ZiGGi - 15.09.2016, 11:23
Re: foreach 0.4.1 standalone include - by Crayder - 15.09.2016, 11:49
Re: foreach 0.4.1 standalone include - by ZiGGi - 15.09.2016, 12:28
Re: foreach 0.4.1 standalone include - by KokyZ - 10.11.2016, 18:15
Re: foreach 0.4.1 standalone include - by Konstantinos - 10.11.2016, 18:28
Re: foreach 0.4.1 standalone include - by KokyZ - 10.11.2016, 18:34
Respuesta: foreach 0.4.1 standalone include - by iSanchez - 18.03.2017, 23:18
Re: Respuesta: foreach 0.4.1 standalone include - by ISmokezU - 18.03.2017, 23:35
Re: Respuesta: foreach 0.4.1 standalone include - by Crayder - 19.03.2017, 20:08
Re: foreach 0.4.1 standalone include - by Gammix - 24.06.2017, 05:53
Re: foreach 0.4.1 standalone include - by Unrea1 - 27.08.2017, 00:04
Re: foreach 0.4.1 standalone include - by Kar - 02.10.2017, 19:27

Forum Jump:


Users browsing this thread: 2 Guest(s)