16.04.2015, 18:18
foreach 3.0
Introduction
This is a major new version of "foreach", I'm going to assume some familiarity with the existing version. If you don't have that, read this topic first: [MISSING LINK]
Breaking Changes
- Most of the internal names, prefixes, and macros have changed. This shouldn't be a problem though as you shouldn't be using the internals directly...
- "Iter_InternalArray" is now "Iter_TrueArray", and "Iter_InternalSize" is now "Iter_TrueCount". The use of "Internal" in the name was confusing, because they were designed as an external API to provide direct access to some implementation details.
- Special iterator functions have changed significantly. The old style looked like:
Код:iterfunc Tag:MyIterator(other, Tag:cur) { return Tag:-1; }
Код:#define iterstart@MyIterator -1 iterfunc Tag:MyIterator(cur, other) { return Tag:-1; }
- The return values of "Iter_Add", "Iter_Remove", and some other functions are no longer boolean. Instead, they return the value just added to/removed from the iterator, or -1 on failure.
- This now REQUIRES YSI - there are too many changes to keep two parallel versions going since many features would require twice the code without YSI.
New Features
- Multi-Iterators
Код:
new Iterator:OwnedVehicles[MAX_PLAYERS]<MAX_VEHICLES>; Iter_Init(OwnedVehicles); foreach (new i : OwnedVehicles[playerid]) { }
Код:
Iter_Add(OwnedVehicles[5], 5); Iter_Add(OwnedVehicles[7], 5);
A multi-iterator for this would look like:
Код:
new Iterator:OwnedVehicles<MAX_PLAYERS, MAX_VEHICLES>; foreach (new i : OwnedVehicles<playerid>) { }
Код:
Iter_Add(OwnedVehicles<5>, 5); Iter_Add(OwnedVehicles<7>, 5);
Код:
if (Iter_Add(OwnedVehicles<7>, 5) == -1) { // Can't add the vehicle to this iterator. }
Код:
if (Iter_Contains(MyIter<5>, 11)) { }
Код:
if (Iter_Contains(MyIter<>, 11)) { }
Код:
Iter_Add(MyIter<5>, 11); foreach (new i : MyIter<5>) printf("%d", i); Iter_Remove(MyIter<7>, 11); foreach (new i : MyIter<5>) printf("%d", i);
Код:
11 11
- Reverse Iterators
Код:
Iter_Add(MyIter, 4); Iter_Add(MyIter, 7); Iter_Add(MyIter, 20); foreach (new i : Reverse(MyIter)) { }
Код:
20 7 4
- New Iterators
Код:
foreach (new i : Range(4, 8)) { printf("%d", i); }
Код:
4 5 6 7
Код:
foreach (new i : Range(4, 8, 2)) { printf("%d", i); }
Код:
4 6
Код:
foreach (new i : Range(10, 6, -1)) { printf("%d", i); }
Код:
10 9 8 7
- YSI Iterators
Код:
new BitArray:x<100>; foreach (new i : Bits(x)) { // Get all the slots that are "1" in "x". }
Код:
new BitArray:x<100>; foreach (new i : Blanks(x)) { // Get all the slots that are "0" in "x". }
Код:
foreach (new i : Command()) { // Loop over all y_commands commands. }
Код:
foreach (new i : PlayerCommand(playerd)) { // Loop over all y_commands commands a given player can use. }
Код:
foreach (new Group:i : GroupChild(g)) { // Loop over all the child groups of y_groups Group:g. }
Код:
// Should be "PlayerGroup", sorry! foreach (new Group:i : PlayerGroups(playerid)) { // Loop over all the groups a player is in. }
Код:
foreach (new i : GroupMemeber(g)) { // Loop over all the players in a group. }
Код:
foreach (new Group:i : CreatedGroup()) { // Loop over all groups in y_groups. }
- Deprecation
- Nicer Errors
Код:
new Iterator:ArrayIter[10]<20>; Iter_Add(ArrayIter[5], 10);
Код:
Symbol is never used "Iter_Init@ArrayIter"
Код:
new Iterator:ArrayIter[10]<20>; Iter_Init(ArrayIter); Iter_Add(ArrayIter[5], 10);
Код:
new Iterator:ArrayIter[10]<20>; Iter_Add(ArrayIter[5], 10); Iter_Init(ArrayIter);
Код:
Undefined symbol "Iter_Multi@IterName"
Код:
Undefined symbol "Iter_Single@IterName"
- Forgiving Syntax
Код:
foreach (new i : Player)
Код:
foreach ( new i : Player )
- Iter_Alloc
Код:
new val = Iter_Alloc(MyIter);
- Correct Spellings
- Code Cleanup
- Tests
Download
This version of "foreach" is in the latest version of "YSI" on github:
https://github.com/Misiur/YSI-Includes/tree/YSI.tl