[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
#2

Looks good i will test it.
Reply
#3

Much of the issues were fixed. The syntax is still
PHP код:
loop(new 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...
Reply
#4

why are you even trying?
Reply
#5

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.
Reply
#6

When you don't have enough skills to write better thing then don't even start and just learn and learn.
Reply
#7

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?
Reply
#8

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.
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)