Iterator v1.1 -
GaByM - 26.02.2018
Kind of foreach, but it uses string functions (strcat, strfind, strdel) creating an array with no gaps and
this include does not replace y_iterate/foreach!
Advantages:
- The size of the iterator is not necessary to be the maximum value you want to add (if your minigame system can hold 10 players, you can create your iterator with size 10, and not MAX_PLAYERS)
- You can add values between 0 and 16777214.
- The values are saved in order they joined
- No need to initialize multidimensional iterators.
- Inside loop() you can use StepIter_ to check the index.
- Each iteration of the loop() should be as fast as foreach's.
Disadvantages:
- You can have the same value multiple times inside the iterator.
- Adding and removing a value from iterators is slower than Iter_Add/Iter_Remove.
Syntax:
Here is an example of a loop:
PHP код:
new Iter:hi<4>;
AddIter(hi, 5);
AddIter(hi, 0);
AddIter(hi, 19);
AddIter(hi, 19);
loop(new i : hi)
{
printf("hi[%i] = %i", StepIter_i, i);
}
Which prints this:
Код:
hi[0] = 5
hi[1] = 0
hi[2] = 19
hi[3] = 19
Functions:
AddIter(iterator, value) - add a value inside the iterator
RemoveIter(iterator, value) - removes a value from the iterator
SafeRemoveIter(iterator, value, contor) - removes a value from the iterator inside a loop
PHP код:
// This is how it should be done
loop(new i : my_iter)
{
SafeRemoveIter(my_iter, i, StepIter_i);
}
CheckSlotIter(iterator, slot) - the value of the iterator at index
slot, since the values are stored consecutive inside the array, the index matters.
ContainsIter(iterator, value) - 1 if the value is inside the iterator, 0 otherwise
CountIter(iterator) - the number of values inside the iterator
RandomIter(iterator) - returns a random value from the iterator
ClearIter(iterator) - resets the iterator
When to use:
- Creating a query (e.g.: for a saving system)
You don't want to save all players at the same time, so you decide to save 1 player at 6 seconds
PHP код:
main()
{
SetTimer("save", 6_000, 1);
}
forward save();
public save()
{
lastSaved++; //increment the last player saved
lastSaved %= MAX_PLAYERS; //make sure we don't overflow
SavePlayer(lastSaved);
}
This method looks fine, but you can save an account which just logged in a few seconds ago. Why would a newly joined player need a save, when there are so many players whose account were not saved for minutes?
So now you create an array in which you store the timestamp when you saved their accounts, and everytime save() is called, you check which account wasn't saved for a long period of time.
Or you can do this:
PHP код:
new Iter:saved<MAX_PLAYERS>;
main()
{
SetTimer("save", 6_000, 1);
}
forward save();
public save()
{
new player = CheckSlotIter(saved, 0); //check who is next in the query
SavePlayer(player);
RemoveIter(saved, player); //remove player from the que
AddIter(saved, player); //adds them at the end of the que
}
hook OnPlayerConnect(playerid)
{
AddIter(saved, playerid); // now at the end of the query
}
hook OnPlayerDisconnect(playerid, reason)
{
RemoveIter(saved, playerid);
}
Changelog:
- Iter_Step is now StepIter_
counter
- CheckSlot is now CheckSlotIter
Notes:
-
Default iterators and those mentioned in this post are not included.
- Strcat & strfind also works with negative numbers (not 0) but mixing positive and negative values inside the same iterator does not work correctly.
Download:
https://pastebin.com/0MXKajZi
Re: String iterator (foreach2?) -
N0FeaR - 26.02.2018
Looks good i will test it.
Re: String iterator (foreach2?) -
GaByM - 27.02.2018
Much of the issues were fixed. The syntax is still
PHP код:
loop(new i : Iter)
with this exact spacing. (I will fix this in a future update)
Quote:
Originally Posted by ******
You can't write the code wrong, then use that incorrect code as an argument that it is inefficient.
|
Example removed, I didn't knew about that. It's my fault I wasn't documented enough about foreach...
Re: String iterator (foreach2?) -
CodeStyle175 - 28.02.2018
why are you even trying?
Re: String iterator (foreach2?) -
iKarim - 28.02.2018
Quote:
Originally Posted by CodeStyle175
why are you even trying?
|
Because why not? This is indeed a useful library for anyone choose not to use YSI. The concept is nice as well.
Re: String iterator (foreach2?) -
CodeStyle175 - 02.03.2018
When you don't have enough skills to write better thing then don't even start and just learn and learn.
Re: String iterator (foreach2?) -
iKarim - 02.03.2018
Quote:
Originally Posted by CodeStyle175
When you don't have enough skills to write better thing then don't even start and just learn and learn.
|
Sorry that's completely nonsense and illogical, are you aware of what you just said?
Re: String iterator (foreach2?) -
RogueDrifter - 02.03.2018
Quote:
Originally Posted by CodeStyle175
When you don't have enough skills to write better thing then don't even start and just learn and learn.
|
Why did you create an anti weapon cheat when many existed? why did you create a speedometer when many existed? ... i can go on, what you just said makes no sense.