[Include] Vector implementation in PAWN
#1

Vector implementation in PAWN


Description
This is a vector data structure implementation in PAWN.


Project
The source code and documentation are located at GitHub: https://github.com/BigETI/pawn-vector
Reply
#2

Good job!
Reply
#3

Quote:
Originally Posted by TroS
View Post
Good job!
Quote:
Originally Posted by [HLF]Southclaw
View Post
Finally, my favourite data structure! (sorry, unsorted_map)
thank you
Reply
#4

BigETI making PAWN slightly more bearable one include at a time. :^)
Reply
#5

Quote:
Originally Posted by TommyB
View Post
BigETI making PAWN slightly more bearable one include at a time. :^)
thank brother <3
Reply
#6

Mashallah
Reply
#7

for what system its useful in samp, because samp is so basic
and i really doubt that code written in pawn, does the same level things as other programming languages.
Reply
#8

Quote:
Originally Posted by CodeStyle175
View Post
for what system its useful in samp, because samp is so basic
and i really doubt that code written in pawn, does the same level things as other programming languages.
what you trying to say?
Reply
#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
#10

Is it possible to set float values?
Reply
#11

Quote:
Originally Posted by 2Col
Посмотреть сообщение
Is it possible to set float values?
anything
Reply
#12

Thanks. And is it possible to set array like this?
Код:
enum PlayerEnum
{
	id,
	Float:health,
	Float:armour
}

new PlayerData[][PlayerEnum] =
{
	{1, 2.0, 3.0},
	{4, 5.0, 6.0}
};

new Vector:PlayerVector;

for(new i = 0; i < sizeof(PlayerData); i += 1)
{
	VECTOR_push_back_arr(PlayerVector, PlayerData[i]);
}
If I try to compile this code, I get this warning:
Код:
warning 229: index tag mismatch (symbol "PlayerData"): expected tag none ("_"), but found "PlayerEnum"
Reply
#13

enum adds a tag to your variable, so you should just disable that tag either at the enumerator with
pawn Код:
enum _:PlayerEnum
or inside the function with
pawn Код:
VECTOR_push_back_arr(PlayerVector, _:PlayerData[i]);
Reply
#14

Thanks. Great include.
Reply
#15

Awesome! Thank you!
Reply
#16

Sorry for bothering you, but I have another question.
Код:
enum PlayerEnum
{
	id,
	Float:health,
	Float:armour
}

new Vector:PlayerVector[PlayerEnum];

VECTOR_push_back_val(PlayerVector[id], 0);
VECTOR_push_back_val(PlayerVector[health], 0.0);
VECTOR_push_back_val(PlayerVector[armour], 0.0);
Код:
warning 213: tag mismatch: expected tag "Vector", but found none ("_")

warning 213: tag mismatch: expected tag "Vector", but found "Float"
warning 213: tag mismatch: expected tag none ("_"), but found "Float"

warning 213: tag mismatch: expected tag "Vector", but found "Float"
warning 213: tag mismatch: expected tag none ("_"), but found "Float"
Is this the correct way to fix this?
Код:
enum PlayerEnum
{
	id,
	Float:health,
	Float:armour
}

new Vector:PlayerVector[PlayerEnum];

VECTOR_push_back_val(Vector:PlayerVector[id], 0);
VECTOR_push_back_val(Vector:PlayerVector[health], _:0.0);
VECTOR_push_back_val(Vector:PlayerVector[armour], _:0.0);
Reply
#17

What are you even trying to do?
Reply
#18

Quote:
Originally Posted by BigETI
Посмотреть сообщение
What are you even trying to do?
Something like this, but I want to use enums for more readable code.
Код:
new Vector:PlayerVector[3];

VECTOR_push_back_val(PlayerVector[0], 0);
VECTOR_push_back_val(PlayerVector[1], _:0.0);
VECTOR_push_back_val(PlayerVector[2], _:0.0);
Reply
#19

Are you trying to push player data into a single vector or are you trying to structurize single player data as a vector?
Reply
#20

Quote:
Originally Posted by BigETI
Посмотреть сообщение
Are you trying to push player data into a single vector or are you trying to structurize single player data as a vector?
Structurize.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)