- Joined
- Sep 26, 2009
- Messages
- 9,534
JASS:
library ForEach
//=======================================================================
// This library provides high-level functions at your normal high-level
// efficiency loss. Zinc has a built-in syntax which is very close to
// this, but Zinc also lacks a ton of features only vJass can provide.
//
// The premise is this:
//
// For each (from 0 to the specified exit-point), do (function).
// For range (start, end), do (function).
// For range (start, end, stride), do (function).
//
// Start may be higher than end. The stride will reverse itself in that
// case. The stride defaults to 1 (i = i + 1).
//
// cJass, now deprecated due to lack of proper care, had the "define"
// syntax which allowed optional parameters. Because of the lack of such
// functionality, ForEach is split into three functions:
//
// ForEach - takes an integer for how many times the loop should iterate.
// ForRange - takes a start and end parameter
// ForRangeEx - takes a start, end and stride parameter
//
// The last parameter will always be the function to be iterated through.
//
// The functionality of ForEach is this: you give it a function, the
// function must take an integer and return a boolean. A true boolean
// continues the loop as normal, a false boolean exits the loop ahead of
// schedule. You can decide which one you are more comfortable with by
// adjusting the constant "EXITWHEN".
//
// If you are iterating through a stack and you need to increment or
// decrement the current and end position, use:
//
// ForEach_RaiseStack()
// ForEach_LowerStack()
//
// Alternatively, you may set ForEach_A and ForEach_B directly. A is the
// running index, B is the end-position.
//
// A ForEach call may be nested within another ForEach call.
// Syntax:
//
// function theLoop takes integer i returns boolean
// call TriggerExecute(myTriggerArray[i])
// return true
// endfunction
// ...
// call ForEach(10, theLoop)
//
// # This runs the function "theLoop" exactly 10 times (0..9).
globals
// If the looping function returns this value, the loop ends:
public constant boolean EXITWHEN = false
public integer A = 0
public integer B = 0
endglobals
function ForEach_LowerStack takes nothing returns nothing
set A = A - 1
set B = B - 1
endfunction
function ForEach_RaiseStack takes nothing returns nothing
set A = A + 1
set B = B + 1
endfunction
function interface ForEachFunc takes integer i returns boolean
function ForRangeEx takes integer start, integer end, integer stride, ForEachFunc func returns nothing
local integer a = A
local integer b = B
set A = start
set B = end
if (start > end) then
set stride = -(stride)
endif
loop
exitwhen ((A == B) or (func.evaluate(A) == EXITWHEN))
set A = A + stride
endloop
set A = a
set B = b
endfunction
function ForRange takes integer start, integer end, ForEachFunc func returns nothing
call ForRangeEx(start, end, 1, func)
endfunction
function ForEach takes integer end, ForEachFunc func returns nothing
call ForRangeEx(0, end, 1, func)
endfunction
endlibrary
JASS:
library Example initializer init requires ForEach
globals
group array Groups
endglobals
private function clear takes integer i returns nothing
call GroupClear(Groups[i])
return true
endfunction
function ClearUnitGroups takes nothing returns nothing
call ForEach(bj_MAX_PLAYER_SLOTS, clear)
endfunction
private function fill takes integer i returns nothing
call GroupEnumUnitsOfPlayer(Groups[i], Player(i), null)
return true
endfunction
function FillUnitGroups takes nothing returns nothing
call ForEach(bj_MAX_PLAYER_SLOTS, fill)
endfunction
endlibrary
Last edited: