[Tutorial] Foreach & the functions handling of an iterator
#1

Foreach & the functions handling of an iterator
Tutorial explaining the working of the include Foreach and the functions of iterations are being bonded.

This is my first english tutorial, so please correct me if syntaxes mean nothing.
This tutorial is available for Foreach 0.4.1.
Initiation:
Foreach is a keyword that is found in many languages simplified today, unfortunately the Pawn is not allowed. This is why ****** spawned us a pretty include that it is intended.
Foreach is simply a loop running with iterators, but that I will explain to you a little later in the tutorial.

Concretely what it changes "for" and "while"? Much optimizing my friends !

The keyword foreach and an iterator :
I shock you if I say you that foreach is neither more nor less than a macro using for ?
You're amazed, eh ?

But how "foreach" using "for", can it be faster than him ?
It comes with this story of iterators !

An iterator is simply an array in which each cell of this array contains the value -1, and are therefore invalid.
When you add a value positive and less than the size of your iterator, it will add to the first invalid cell of the array.

We must also take into account your array sorting the values of the smallest to the largest, this means that if you add the value 6 and then you add the value 5, your foreach will loop 2 times and come out first value 5, while we added after the value 6.

Here's an example, do not try to understand "Iter_Add" we will see it below :
PHP Code:
#include <a_samp>
#include <foreach>
new
    
Iterator:Test<10>;
main()
{
    
Iter_Add(Test6);
    
Iter_Add(Test5);
    foreach(new 
Test)
    {
        
printf("%d"i);
    }

This will print
Code:
5
6
What if we had used "for" we would have looped 10 times. Of course, speed is calculable in nanoseconds, but you can imagine on a loop 500 or 2000 ? "Foreach" is really a useful!

Well, I hope I was understood.

In fact, "foreach" is nothing without iterators, it is only a macro similar to some that if I return to my example:
PHP Code:
new
    
Test[10] = {-1, ...};
    
main()
{
    
Test[0] = 5;
    
Test[1] = 6;
    
    for(new 
010 && Test[i] != -1i++)
    {
        
printf("%d"i);
    }

Simply, you do not have to break your head with comparisons. The include may seem complex only at the instructions of the functions handling iterators. Fortunately, Dutheil studied them for you and will explain them to you, we are left after the pubclicity !
The functions handling iterators:
Function » Iter_Add:
This function adds a value to an iterator, it contains two parameters :
  1. The iterator name
  2. The value
It may return 2 values :
  • 0 - If the value is less than 0 or greater than the size of the iterator or iterator is already full
  • 1 - If the function was successfully executed
Example of use :
PHP Code:
Iter_Add(Test6); 
Here we add the value 6 to the iterator Test.
Function » Iter_Remove:
This function allows you to remove a value from an iterator, it contains two parameters :
  1. The iterator name
  2. The value
It may return 2 values :
  • 0 - If the value is less than 0 or greater than the size of or the iterator that the iterator is empty
  • 1 - If the function was successfully executed
Example of use :
PHP Code:
Iter_Remove(Test6); 
Here it removed the value 6 to the iterator Test.
Function » Iter_Free:
This function returns the smallest value that is not in the iterator, it contains one parameter :
  1. The iterator name
It can return two types of value :
  • -1 - If the iterator is full
  • The smallest value that is not in the iterator
Example of use :
PHP Code:
new
    
variable Iter_Free(Test); 
If the iterator contains the values 0, 1, 3, 5, 8, the function will return 2.
Function » Iter_Contains:
This function of whether an iterator contains a specified value, it contains two parameters :
  1. The iterator name
  2. The value
It may return 2 values :
  • 0 - If the iterator contains no value
  • 1 - If the iterator contains the value
Example of use :
PHP Code:
if(Iter_Contains(Test6))
{
    print(
"Yes !");

If the iterator contains the values 0, 2, 4, 6, 8, the function will return 1 and code therefore display "Yes !".
Function » Iter_Count:
This function returns the number of values in an iterator, it contains one parameter :
  1. The iterator name
Example of use :
PHP Code:
printf("%d"Iter_Count(Test)); 
If the iterator contains the values 0, 1, 3, 4, 7, the function will return 5.
Function » Iter_Random:
This function returns a random value contained in an iterator, it contains one parameter :
  1. The iterator name
It can return two types of value :
  • -1 - If the iterator is empty
  • A random value contained in the iterator
Example of use :
PHP Code:
print("%d"Iter_Random(Test)); 
Well here I can not tell you what the function returns, it is random !
Iterators ready for use:
Here is a list of iterators ready to be used with foreach :
  • Player - Iterator of connected players.
  • Bot - Iterator of connected bots.
  • NPC - Alias of Bot.
  • Character - Iterator of connected players and bots.
Additionals constants:
Two constants are available to you, they are to be defined before including foreach.

FOREACH_NO_BOTS
Does not create the iterators Bot NPC and Character
FOREACH_NO_PLAYERS
Does not create the iterator Player and Character
__________________________________________________ __
Tutorial over, if you think I forgot an important point, let me know after the subject.

Go kisses!
Reply
#2

Awesome. Today I learn how to use Iter_Count
+rep for ur hard work on this thread and for creating this tutorial
Reply
#3

Nice, tnx for the effort, +Rep
Reply
#4

I am disappointed this is not on top. It is a really valuable tutorial for everyone to use.
Reply
#5

Quote:
Originally Posted by Private200
View Post
I am disappointed this is not on top. It is a really valuable tutorial for everyone to use.
And u bump it :3
Reply
#6

Quote:
Originally Posted by Humza
View Post
And u bump it :3
I gave you the reason of my bump. If you got a complain about it, just report. Acting smart around the forums won't get you anywhere, Humza.
Reply
#7

Quote:
Originally Posted by Eoussama
View Post
Nice, tnx for the effort, +Rep
Quote:
Originally Posted by Private200
View Post
I am disappointed this is not on top. It is a really valuable tutorial for everyone to use.
Thank you !
Reply
#8

Quote:
Originally Posted by Private200
View Post
I gave you the reason of my bump. If you got a complain about it, just report. Acting smart around the forums won't get you anywhere, Humza.
Seriously. I don't need to be anywhere in this forum. It's just you who is guessing that I need to have go somewhere.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)