[Include] Vector implementation in PAWN
#9

Very useful include. I can use it as a stack for a non-recursive backtracking.

For anyone interested, I needed a random shuffle for one of my vectors. It is based on Fisher-Yates's algorithm (modern version):
pawn Code:
VECTOR_random_shuffle(Vector: vector, first, last)
{
    for (new i = (last - first) - 1; i > 0; --i)
    {
        VECTOR_swap(vector, i, random(i + 1));
    }
}

VECTOR_swap(Vector: vector, a, b)
{
    new a_value = VECTOR_get_val(vector, a);
   
    VECTOR_set_val(vector, a, VECTOR_get_val(vector, b));
    VECTOR_set_val(vector, b, a_value);
}
pawn Code:
// Example:
public OnGameModeInit()
{
    new Vector: vector;
   
    for (new i = 1; i <= 10; i++)
    {
        VECTOR_push_back_val(vector, i);
    }
   
    VECTOR_random_shuffle(vector, 0, VECTOR_size(vector));
   
    VECTOR_foreach(v : vector)
    {
        printf("%d", MEM_get_val(v));
    }
    return 1;
}
Although it is still random shuffle, I prefer using MerRandom plugin.
Reply


Messages In This Thread
Vector implementation in PAWN - by BigETI - 16.07.2018, 20:06
Re: Vector implementation in PAWN - by TroS - 16.07.2018, 20:11
Re: Vector implementation in PAWN - by BigETI - 19.07.2018, 07:12
Re: Vector implementation in PAWN - by TommyB - 19.07.2018, 07:23
Re: Vector implementation in PAWN - by BigETI - 19.07.2018, 07:29
Re: Vector implementation in PAWN - by iLearner - 19.07.2018, 07:30
Re: Vector implementation in PAWN - by CodeStyle175 - 19.07.2018, 08:40
Re: Vector implementation in PAWN - by GhostHacker9 - 19.07.2018, 09:03
Re: Vector implementation in PAWN - by Calisthenics - 12.10.2018, 09:36
Re: Vector implementation in PAWN - by 2Col - 27.10.2018, 12:10
Re: Vector implementation in PAWN - by BigETI - 27.10.2018, 12:13
Re: Vector implementation in PAWN - by 2Col - 27.10.2018, 14:00
Re: Vector implementation in PAWN - by BigETI - 27.10.2018, 15:37
Re: Vector implementation in PAWN - by 2Col - 27.10.2018, 16:30
Re: Vector implementation in PAWN - by d3Pedro - 27.10.2018, 23:09
Re: Vector implementation in PAWN - by 2Col - 11.11.2018, 08:15
Re: Vector implementation in PAWN - by BigETI - 11.11.2018, 21:30
Re: Vector implementation in PAWN - by 2Col - 12.11.2018, 09:18
Re: Vector implementation in PAWN - by BigETI - 12.11.2018, 10:04
Re: Vector implementation in PAWN - by 2Col - 12.11.2018, 10:52
Re: Vector implementation in PAWN - by BigETI - 12.11.2018, 13:22

Forum Jump:


Users browsing this thread: 7 Guest(s)