Raised This Month: $32 Target: $400
 8% 

Dynamic Arrays in SourcePawn


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BAILOPAN
Join Date: Jan 2004
Old 08-07-2012 , 14:03   Dynamic Arrays in SourcePawn
Reply With Quote #1

Hi All. I am working on adding fully dynamic arrays to SourcePawn. I have three proposals in flight, and I would be grateful to get your input on them.

Overall Feature

In all three proposals, the following type of code will be possible:

PHP Code:
new array[];    // New resizable array of length 0.
array += [1];   // Add an element to the array.
new value = array.pop();  // Get 1 back. 
Resizing an array with a constant size would not work. In all proposals, you can create global, dynamic arrays, resize them, assign them, etc. In all proposals, some amount of garbage collection is required. In all proposals, arrays passed to functions are still passed by-reference.

Proposal 1: Arrays are references

In this proposal, we would break existing copy semantics. All arrays in Pawn would be treated as references, and you could pass them anywhere, a lot like pointers in Java or C.

PHP Code:
new Float:a[3] = {0.01.02.0};
new 
Float:b[3] = {4.05.06.0};

b;
a[0] = 10.0;
// b[0] is now 10.0! 
Currently in Pawn, the final assignment only changes a. In this proposal, it would change b as well, because a was changed to point to b.

Pros: Easy to implement, consistent behavior, makes sense for objects/structs/2D arrays. Assignment is fast.
Cons: Makes all 1D array creation much slower unless we implement analyses or excellent garbage collection to mitigate that.

Proposal 2: Arrays are values

In this proposal, we would preserve existing copy semantics. All arrays in Pawn would be treated as values, and assigning one array to another would fully copy its contents.

PHP Code:
new g_Players[];
MakePlayerList() {
    new 
players[];
    for (new 
1<= MaxClientsi++) {
        if (
IsClientInGame(i))
            
players += [i];
    }
    
g_Players players
In this example, the final assignment would copy the contents of players into g_Players, and changing one would not affect the other.

Pros: Preserves existing semantics, consistent behavior. 1D array creation does not need fancy internal magic to be fast.
Cons: Assigning an array anywhere becomes an expensive, unbounded copy. 2D+ arrays become extremely expensive to move around.

Proposal 3: Hybrid

In this proposal, we would try to capture the best of both worlds. Arrays with a constant size would continue to act as values, as in proposal 2. However, a new kind of reference array could be declared with:

PHP Code:
new g_Players[*];
MakePlayerList() {
    new 
players[*];
    for (new 
1<= MaxClientsi++) {
        if (
IsClientInGame(i))
            
players += [i];
    }
    
g_Players players
This creates the subtle difference that the new syntax would be needed to move references around:
PHP Code:
new String:dictionary[*][*];

Fill(String:dictionary[][], const String:str[]) {
    
dictionary += [str];  // Illegal!
}
Fill(String:dictionary[*][*], const String:str[]) {
    
dictionary += [str]; // Illegal!
}
Fill(String:dictionary[][], const String:str[*]) {
    
dictionary += [str]; // Illegal!
}

// This one works!
Fill(String:dictionary[*][*], const String:str[*]) {
    
dictionary += [str];

However, you could still pass them around as "normal" arrays, including into functions which expect non-dynamic arrays. Just assignment would not work (as it does not now).

Pros: Best of both worlds, fine-grained performance control.
Cons: Requires the programmer to understand the difference between reference and value arrays.
__________________
egg

Last edited by BAILOPAN; 08-07-2012 at 14:40.
BAILOPAN is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 08-07-2012 , 14:35   Re: Dynamic Arrays in SourcePawn
Reply With Quote #2

Speaking from a "user" (read: plugin coder) perspective...

Honestly, if I had to choose just one of the 3, I'd have to choose #1. The major reason being I'm already used to arrays being references like C and Java. This also adds consistency, as right now Strings appear to be passed by reference to functions; otherwise, Format and friends would break horribly.

#3 introduces a new syntax and it seems to me it would cause a lot of confusion. It's also not clear whether array[*] arrays could be passed to functions expecting array[] and vice versa.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
BAILOPAN
Join Date: Jan 2004
Old 08-07-2012 , 14:39   Re: Dynamic Arrays in SourcePawn
Reply With Quote #3

Arrays as function parameters would continue to be references in all proposals. That is not something we can or would change. The proposal only talks about assignment. And proposal #3 states that [*] arrays could be passed to functions expecting [].

I've clarified this just in case. Thanks!
__________________
egg

Last edited by BAILOPAN; 08-07-2012 at 14:42.
BAILOPAN is offline
BAILOPAN
Join Date: Jan 2004
Old 08-07-2012 , 15:15   Re: Dynamic Arrays in SourcePawn
Reply With Quote #4

A fourth proposal I'll mention in passing, is we could take #2 and implement reference semantics through an explicit pointer type. For example:

Code:
// Copies dynamic contents of y into dynamic contents of x. // Changing contents of x DOES NOT change contents of y. new x[], y[]; x = y; // Now, x and y are both pointers which point to arrays. x = y makes x point to y instead. // Changing contents of x changes contents of y. new @x[], @y[]; x = y;

This is a possible solution but I think it's the most exotic and unfamiliar.
__________________
egg

Last edited by BAILOPAN; 08-07-2012 at 15:18.
BAILOPAN is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 08-07-2012 , 16:38   Re: Dynamic Arrays in SourcePawn
Reply With Quote #5

What about keeping the array as it is and adding "list" as a collection-type? We all know that is the way it is solved in most languages, so what speaks against it?
Dr. Greg House is offline
BAILOPAN
Join Date: Jan 2004
Old 08-07-2012 , 16:58   Re: Dynamic Arrays in SourcePawn
Reply With Quote #6

Quote:
Originally Posted by Dr. Greg House View Post
What about keeping the array as it is and adding "list" as a collection-type? We all know that is the way it is solved in most languages, so what speaks against it?
Thanks for the feedback, Dr. House That falls into proposal #3: a new array type with a new syntax. I'm welcome to new syntax ideas, but "list" types as implemented in other static languages tend to be very, very verbose (read: Java or C#, which also need generic or dynamic type support, both routes that would greatly complicate this feature), whereas those languages should have made builtin-array types work like lists from the beginning (see: Python). It's certainly an option for us - to go the verbose route and avoid all confusion - but hopefully one of the above four proposals will appeal more.
__________________
egg

Last edited by BAILOPAN; 08-07-2012 at 17:03.
BAILOPAN is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 08-07-2012 , 17:14   Re: Dynamic Arrays in SourcePawn
Reply With Quote #7

It's just that the syntax of all those proposals is very unique (at least for me), and I'm wondering if this would actually create a lot of pitfalls for users, so adding a bit verboseity for the benefit of the user might be worth thinking about.
Actually I'd like to see how your approaches on inserting and removing values/nodes/whatever would look like with the your current solutions.
Dr. Greg House is offline
BAILOPAN
Join Date: Jan 2004
Old 08-07-2012 , 17:31   Re: Dynamic Arrays in SourcePawn
Reply With Quote #8

Dr. House: They'd just be methods on the array (like array.pop, array.remove, etc). If you have an alternate array syntax overall you'd like to propose, please feel free to PM me with some examples. Every idea helps
__________________
egg

Last edited by BAILOPAN; 08-07-2012 at 17:31.
BAILOPAN is offline
Pawemol12
Member
Join Date: Jun 2012
Location: Poland - Wojnowice
Old 08-07-2012 , 17:56   Re: Dynamic Arrays in SourcePawn
Reply With Quote #9

I really like 3 proposal. In general, well, that does not need to declare size. I write in lua quests, and there also does not need to declare the size of the array which is very easy to work. But that is also handy function that retrieves the size of the array. I mean not just strings.
Pawemol12 is offline
RedSword
SourceMod Plugin Approver
Join Date: Mar 2006
Location: Quebec, Canada
Old 08-07-2012 , 18:21   Re: Dynamic Arrays in SourcePawn
Reply With Quote #10

Can't you do something like first option, with the following possible :

Code:
new g_Players[]; MakePlayerList() {     new players[];     new cpt;     for (new i = 1; i <= MaxClients; i++) {         if (IsClientInGame(i))             players[cpt++] = i; //allowed because [cpt] is the next available array 'slot'     }     g_Players = players; //this would copy a ptr aiming at the array (java like)

I feel like it is way more "natural" (for C-ish-programmers).

Also, I don't like 3rd option, as it is way too ehm... new ? exotic ? I think I like 4 (could you use '&' rather than '@' ? ; are you avoiding char conflicts ?).
__________________
My plugins :
Red Maze
Afk Bomb
RAWR (per player/rounds Awp Restrict.)
Kill Assist
Be Medic

You can also Donate if you appreciate my work

Last edited by RedSword; 08-07-2012 at 18:21.
RedSword is offline
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

Forum Jump


All times are GMT -4. The time now is 13:12.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode