Pawn foreach - Provides foreach and iterator keywords to interact with this data structure and loop through iterators.
- Start using the library by creating a simple iterator.
iterator new MyIterator<10>;- You can use
itershortcut too
iter new MyIterator<10>;- Before using an iterator, make sure to initialize it:
iterator init(MyIterator);- After the iterator initialization, it's ready for a proper use - let's continue.
iterator add(MyIterator, 1);- We added a value
1to a iterator. So, when we do this:
foreach do(new i : MyIterator)
{
printf("%i", i);
}- The output will be:
1
- To remove a value, use
iterator remove:
iterator remove(MyIterator, 1);- So let's firstly explain how
iterator newworks, so:
iterator new MyIterator<10>;- This will declare an iterator, actually this is a macro, so in fact iterators are just arrays. This will code will generate into this:
enum IteratorData
{
returnvalues[SIZE]
};
new IteratorIndex;
new Iterator[IteratorData];IteratorDatais an enumerator holding the information about the iterator,IteratorIndexis an index ofreturnvaluesupdated afteriterator addis used.
iterator init(MyIterator);- This is REALLY important part, this actually prepares the iterator for it's iteration. If you don't initialize the iterator, your console will be full of zeros if you try to this:
foreach do(new i : MyIterator)
{
printf("%i", i);
}iterator add(MyIterator, 1);iterator remove(MyIterator, 1);- These two are literally the point of this library - add and remove the iteration values. The good thing is that you can add any value to the iterator, because the number between
<and>represents the number of slots. The slot number is actually the size of the iterator, but regardless its size any value can be added. So, this code is completely valid even if the size is for example only10:
iterator add(MyIterator, 2437);- Let's explain what
iterator adddoes, it just adds the provided number to an index ofreturnvaluesinIteratorData, that index is already mentionedIteratorIndexwhich is updated itself after the each iterator update. Removing the value is simple - almost the same process, just reversed.
- This one is completely possible and valid, but to remove the twice-applied value, you'll need to logically think - just remove the value twice. So if you did this:
iterator add(MyIterator, 1);
iterator add(MyIterator, 1);- To remove
1completely, just remove it twice:
iterator remove(MyIterator, 1);
iterator remove(MyIterator, 1);- This is possible since
iterator (add/remove)highly rely on the standardforloop.
- If you want to do an iterator cleanup for any reason, just re-initialize it using
iterator init.
- No notes yet.
- No warnings yet.