[Include] String iterator (foreach2?)
#1

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(hi5);
AddIter(hi0);
AddIter(hi19);
AddIter(hi19);
loop(new hi)
{
    
printf("hi[%i] = %i"StepIter_ii);

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 my_iter)
{
    
SafeRemoveIter(my_iteriStepIter_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_0001);
}
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_0001);
}
forward save();
public 
save()
{
    new 
player CheckSlotIter(saved0); //check who is next in the query
    
SavePlayer(player);
    
RemoveIter(savedplayer); //remove player from the que
    
AddIter(savedplayer); //adds them at the end of the que
}
hook OnPlayerConnect(playerid)
{
    
AddIter(savedplayerid); // now at the end of the query
}
hook OnPlayerDisconnect(playeridreason)
{
    
RemoveIter(savedplayerid);


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
Reply


Messages In This Thread
Iterator v1.1 - by GaByM - 26.02.2018, 16:02
Re: String iterator (foreach2?) - by N0FeaR - 26.02.2018, 17:14
Re: String iterator (foreach2?) - by GaByM - 27.02.2018, 18:36
Re: String iterator (foreach2?) - by CodeStyle175 - 28.02.2018, 19:07
Re: String iterator (foreach2?) - by iKarim - 28.02.2018, 20:07
Re: String iterator (foreach2?) - by CodeStyle175 - 02.03.2018, 09:08
Re: String iterator (foreach2?) - by iKarim - 02.03.2018, 10:54
Re: String iterator (foreach2?) - by RogueDrifter - 02.03.2018, 11:08

Forum Jump:


Users browsing this thread: 2 Guest(s)