SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 22/11/2017, 10:27 AM   #1
BigETI
High-roller
 
BigETI's Avatar
 
Join Date: Mar 2010
Location: Germany
Posts: 1,005
Reputation: 323
Default Linked list implementation in PAWN

Linked list implementation in PAWN
A successor to http://forum.sa-mp.com/showthread.php?t=451962


Description
This is a linked list implementation in PAWN.


Project
The source code and documentation are located at GitHub: https://github.com/BigETI/pawn-list
BigETI is offline   Reply With Quote
Old 03/05/2018, 12:15 PM   #2
ball
Huge Clucker
 
ball's Avatar
 
Join Date: Jul 2012
Location: Poznan, Poland
Posts: 288
Reputation: 33
Default Re: Linked list implementation in PAWN

What is the difference between LIST_push_back_* and LIST_push_front_*? I read documentation of this, but still I don't get it, for example

Code:
new List:list;
LIST_push_back_str(list, "This is a test");
Code:
new List:list;
LIST_push_front_str(list, "This is a test");
What is the difference?

@down: Oh, thanks. I thought that there are indexes to store data like normal arrays.

Last edited by ball; 03/05/2018 at 01:46 PM.
ball is offline   Reply With Quote
Old 03/05/2018, 01:37 PM   #3
BigETI
High-roller
 
BigETI's Avatar
 
Join Date: Mar 2010
Location: Germany
Posts: 1,005
Reputation: 323
Default Re: Linked list implementation in PAWN

Quote:
Originally Posted by ball View Post
What is the difference between LIST_push_back_* and LIST_push_front_*? I read documentation of this, but still I don't get it, for example

Code:
new List:list;
LIST_push_back_str(list, "This is a test");
Code:
new List:list;
LIST_push_front_str(list, "This is a test");
What is the difference?
It's pretty easy to understand:
One pushes the value at the back (end) of the list, and the other one pushes the value at the front (begin) of the list.
Let's simulate a "push back" and a "push front":
Given a list:
Code:
A -> B -> C
Let's push "D" at the back of the list
Code:
A -> B -> C -> D
after that let's push "E" at the front of the list
Code:
E -> A -> B -> C -> D
BigETI is offline   Reply With Quote
Old 03/05/2018, 02:18 PM   #4
ball
Huge Clucker
 
ball's Avatar
 
Join Date: Jul 2012
Location: Poznan, Poland
Posts: 288
Reputation: 33
Default Re: Linked list implementation in PAWN

Thanks, now I got it. So I give it a try and got some code from documentation at github, there are some errors, not big, but if you want to know.

Code:
new List:list, value[16], m_v_sz, v_sz;
LIST_push_back_str(map, "This is a test);
LIST_push_back_str(map, "foo");
LIST_push_back_str(map, "bar");
LIST_foreach(v : list)
{
	v_sz = MEM_get_size(v);
	m_v_sz = ((v_sz < sizeof value) ? v_sz : sizeof value);
	MEM_zero(UnmanagedPointer:MEM_get_addr(value[0]), sizeof value);
	MEM_get_arr(v, _, value, m_v_sz);
	printf("0x%x, %d, \"%s\"", _:v, v_sz, value);
}
This is at LIST_foreach.

Code:
error 017: undefined symbol "map"
Code:
new List:list, value[16], m_v_sz, v_sz;
LIST_push_back_str(map, "This is a test); //also there is a lack of " near test
But I wanted to ask about this line, gives error

Code:
MEM_zero(UnmanagedPointer:MEM_get_addr(value[0]), sizeof value);
Code:
error 017: undefined symbol "MEM_get_addr"
I checked file memory.inc and there is lack of this function, but I commented this line and everything seems to be working as expected, is this line necessary?
ball is offline   Reply With Quote
Old 03/05/2018, 03:16 PM   #5
BigETI
High-roller
 
BigETI's Avatar
 
Join Date: Mar 2010
Location: Germany
Posts: 1,005
Reputation: 323
Default Re: Linked list implementation in PAWN

Code:
MEM_zero(UnmanagedPointer:MEM_get_addr(value[0]), sizeof value);
which should be
Code:
MEM_UM_zero(UnmanagedPointer:MEM_UM_get_addr(value[0]), sizeof value);
ensures, that "value" has been set to zero at all cells, because "m_v_sz" can be "v_sz" or "sizeof value", where "v_sz" is not always "sizeof value".

Documentation should be fixed now, thank you.

Last edited by BigETI; 04/05/2018 at 03:15 AM.
BigETI is offline   Reply With Quote
Old 03/05/2018, 05:30 PM   #6
ball
Huge Clucker
 
ball's Avatar
 
Join Date: Jul 2012
Location: Poznan, Poland
Posts: 288
Reputation: 33
Default Re: Linked list implementation in PAWN

Code:
MEM_zero(UnmanagedPointer:MEM_UM_get_addr(value[0]), sizeof value);
Now line gives warning "tag mismatch", but I changed tag to "Pointer" and warning disappeared.

Code:
MEM_zero(Pointer:MEM_UM_get_addr(value[0]), sizeof value);
ball is offline   Reply With Quote
Old 03/05/2018, 05:42 PM   #7
BigETI
High-roller
 
BigETI's Avatar
 
Join Date: Mar 2010
Location: Germany
Posts: 1,005
Reputation: 323
Default Re: Linked list implementation in PAWN

Either way MEM_zero will not work with a pointer, that wasn't created with MEM_new_* or MEM_clone.
MEM_UM_zero should have been used for this. My bad...
BigETI is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Linked lists in PAWN BigETI Includes 23 25/11/2017 02:45 PM
List of Pawn keywords and constants IllidanS4 Discussion 5 16/10/2017 08:30 PM
Quicksort algorithm implementation arad55 Scripting Help 2 26/07/2017 03:17 PM
[Tutorial] PAWN Errors & Warnings list. ** [ALL] GangsTa_ Tutorials 18 30/10/2011 11:46 PM
Streamer implementation problems rt-2 Scripting Help 3 07/10/2011 06:23 AM


All times are GMT. The time now is 04:08 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.