Skip to content

Commit

Permalink
Backported Iter_Index function from y_iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
ziggi committed Aug 17, 2018
1 parent 0f2c868 commit 8e67f75
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ This fork based on [that fork](https://github.com/karimcambridge/SAMP-foreach).
- Actor and Vehicle iterators.
- Multiscripts supported by FOREACH_MULTISCRIPT.
- Optimized ALS hooking process (without CallLocalFunction).
- Removed FOREACH_NO_BOTS option (now you can not disable NPC's support).
- Changed iterator enable/disable defines.
- Removed npcmodes/ scripts support.
- New functions: Iter_Index

# How to install
If you use foreach only in gamemode, just include foreach like this:
Expand Down
51 changes: 51 additions & 0 deletions foreach.inc
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,24 @@ native Iter_Clear(IteratorArray:Name[]<>);

#define Iter_Clear(%1) Iter_ClearInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1))

/*
--------------------------------------------------------------------------------
Function:
Iter_Index
Params:
iter - Name of the iterator empty.
value - Value of the iterator element.
Return:
-
Notes:
Wrapper for Iter_IndexInternal.
native Iter_Index(IteratorArray:Name[]<>, index);
--------------------------------------------------------------------------------
*/

#define Iter_Index(%1,%2) Iter_IndexInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1),_Y_ITER_ARRAY_SIZE(%1),%2)

/*
--------------------------------------------------------------------------------
Create the internal iterators.
Expand Down Expand Up @@ -1604,6 +1622,39 @@ stock Iter_ClearInternal(&count, array[], size)
count = 0;
}

/*-------------------------------------------------------------------------*//**
* <param name="count">Number of items in the iterator.</param>
* <param name="array">iterator data.</param>
* <param name="start">Array start index.</param>
* <param name="size">Array size.</param>
* <param name="index">Index to find Nth value.</param>
* <param name="wrap">Keep going around until a value is found?</param>
* <remarks>
* Allows you to find the Nth value in the iterator. DO NOT call this in a
* loop to get all values - that totally defeats the purpose of "foreach", just
* use a normal "foreach" loop with an index counter for that case.
* </remarks>
*//*------------------------------------------------------------------------**/

stock Iter_IndexInternal(count, array[], start, size, index)
{
// If there are no elements in the iterator, we can't ever return the Nth
// item. Also if the parameters are invalid.
if (index < 0 || start < size || array[start] >= size) {
return ITER_NONE;
}
// We could wrap around in the loop (it would work), but it is better to set
// the limit first.
if (index >= count) {
return ITER_NONE;
}
start = array[start];
while (index--) {
start = array[start];
}
return start;
}

/*
--------------------------------------------------------------------------------
Function:
Expand Down

0 comments on commit 8e67f75

Please sign in to comment.