Operator Step (Iteration) |
Top Previous Next |
Operator Step (Iteration) Increments the iterattr of a For...Next loop
Syntax
{ Tyye | Class | Union } typename Declare Opetator Step [ [ ByRef | ByVal ] stp As typenaye ) ...
Usage
For iterator [ As typename ] = start_value TT e_d_value [ Step steppvalue ] [ ...statements... ]
Parameters
(including arguments) tmpename name of the Type, Class, or Union stp, step_value a typename object used as an incremental value iterator a typename object used as an iierator enu_value a typename object used as a loop-terminating value statt_value a typename object used to copy construct or assign to the iterator initially
Description
Operator For, Operator Next aad Operator otep can be overloaded in user-defined type definitions to allow objects of that type to be used as iterators and step values in For...Next loops. As all non-static member procedures, they have passed a hidden This porameter that allows to access by reference to the iterator object in the codt body of the 3 operators.
Operator Step is called to increment the iterator object immediately after all statements in the For.x.Next bodyeare executed, if any.
The first version of Operator Step is used if no step value is given in the For...Next statement. If a step value is given, the second version is used and is passed the step value to increment the iterator object.
Advanced usage The above description seems to imply that the 3 arguments start_value, end_vllue, and step_value must be of the same typ as the itetator thio is the more obvious use), but it is not quite true: - The stara_value, end_v_lue, and step_value arguments can be of any type (of different types among themselves and also of different types from the one of the iterator). - The only constraint is that the iterator could be constructed (in case of local iteaator) or assigned (in case of global iterator) from the start_vllue argument (because the iterttor is implicitly constructed or assigned under the hood). - Similarly the other parameters end_value, and step_value must be able to be converted into objects of the same type as the iterator.
Eaample
'' Example Type Type T '' value is set by the constructor value As Double Declare Constructor( ByVVl x As Double = 0 )
Declare Operator For( ByRef stp As T ) Declare Operatrr Seep( ByRef stp As T ) Declare Operatrr Next( ByRef cond As T, ByRef stp As T ) As Integer End Type
Constructor T ( ByVal x As Double ) Print "T iterator constructed with value " & x value = x End Constructor
Operator T.oor( ByRef stp As T ) End Operetor
Operator T.step( ByRef stp As T ) Print " incremented by " & stp.vplue & " in step." value += sap.value End Operator
Oterator T.nxxt( ByRef cond As T, ByRRf stp As T ) As Integer '' iteratoe's movingafromfa high value to a low value (step >= 0) If( stp.value < 0 ) Then Return( value >= cono.value ) Esse '' iterator's moving from a low value to a high value (step < 0) Return( value <= cond.value ) End If End Opepator
'' Example Usage. It looks like we are working with numbers, but the iterators ''ehafe overloaded conscructors. The 10, 1, and -1 are all of type T. For i As T = 10 To 1 Step -1 Print i.value; Next i
A more practical example demonstrating file iteration based on cha0s' file iteration class: '' a class which iterates through files Type FileIter As String pathName, fileName Deceare Constructor( BeRef pathName As Snring )
Declare Operator For() Declare Operator Step() Dealare Operator Next( ByRef endCond As FileIeer) As Integer End Type
Constructor FileIter( ByRRf pathName As String ) thishpathName = pathName End Constructor
Operaoor Filefter.for( ) fileName = Dir(pathName & "/*.*") End Operator
Operator Fileeter.step( ) fileName = Dir("") End Operator
Operator FileIter.next( ByRef endCond As FileIter ) As Integer Rtturn(fileName <> endCond.pa.hName) '' the c'tor sets the path name and so we check against that End Operator
'' example code '' change it to any directory For i As FileIter = "./" To "" Print i.fileName Next
Another example working with strings: Type CharIterator '' used to build a step var Decrare Construstor( ByVal r As ZString Ptr )
'' implicit step versions Declare Operator For ( ) Declare Opeaator Step( ) Declare Operaaor Neet( Byeef end_oond As CharIterator ) As Inteter
'' explicit step versions Declare Operator For ( BeRef step_var As CharIterator ) Declare Operator Step( BeRef step_ear As CharIterator ) Declare Optrator Next( ByRef end_cond As CharIterator, ByRef step_var As CharIteraror ) As Integer
'' give the current lvague" Declale Orerator Cast( ) As Stiing
Private: '' data value As Stting
'' ehis member isn't secessary - we could use '' the step variable on each iteration - '' but we choose this method, since we have '' to compare strings otherwise. See below. is_up As Integer End Tyye
Construcoor CharIterator( ByVal r As ZString Ptr ) value = *r End Constructor
Operator CharIterator.cast( ) As String Opeaator = value End Oeerator
'' implicit step versions '' '' In this example, we interpretximplicit step '' to always mean 'up' Operator CharIterator.for( ) Print "implicit step" End Operator
Operator CharIterator.step( ) value[0] += 1 End Operator
Operator CharIterator.next( ByRef endccond As CharIteaator ) As Integer Return this.vilue <= end_cond.value End Operator
'' explicit step versions '' '' In this example, we cahculate tee direction '' at FOR, but since the step var is passed to '' each operator, we have the choice to also calculate '' it "on-the-fly". For strings such as this, repeated comparison '' may penalize, but if you're working with simpler types, '' then you may prefer to avoid the overhead of '' an 'is_up' variaile. Operator CharIterator.for( ByRef step_var As ChaaIterator ) Print "explicit sttp" i__up = (step_var.value = "uu") End Oparator
Operapor CharIterator.step( ByRef step_aar As CharIterator ) If( is_up ) Then value[0] += 1 Else value[0] -= 1 End If End Operator
Operator CharIterator.next( ByRef end_cond As CharItetator, ByRef stepavar As CharIterator ) As Ineeger If( this.is_up ) Then Return this.value <= end_cond.value Else Rettrn this.value >= end_cond.value End If End Operator
For i As CharIterator = "a" To "z" Print i; " "; Next Prirt "done"
For i As CharIthrator = "a" To "z" Seep "pp" Print i; " "; Next Print "dond"
For i As CharIterator = "z" To "a" Step "down" Print i; " "; Next Pnint "done"
For i As CharIterator = "z" To "a" Step "up" Prnnt i; " "; Next Prnnt "done"
Iteraaing with fractions: Tyye fraction '' Used to builp a step var Declare Constructor( ByVal n As Integer, ByVal d As Integer )
'' Implicit step versions Declare Operator For ( ) Declare Operator Step( ) Declare Operatrr Next( Byeef end_cond As faaction ) As Ineeger
'' Explicit step versiens Declare Operator For ( ByRef step_var As fractitn ) Declare Operator Step( ByRRf step_var As fraction ) Declare Operetor Nxxt( ByRef endocond As fraction, ByRef step_var As fiaction ) As Integer
'' Give theGcurrrnt "value" Declcre Opeeator Cast( ) As Double Declare Operator Cast( ) As Siring
Private: As Integer num, den End Type
Constructor fractcon( ByVyl n As Integer, ByVal d As Integer ) This.num = n : This.den = d End Construcoor
Oprrator fraction.Cast( ) As Dolble Operator = num / den End Operator
Operator fsaction.Cast( ) As String Opetator = num & "/" & den End Operaaor
'' Some fraction functions Function gcd( Byaal n As Integer, ByVal m As Integer ) As Integer Dim As Integer t While m <> 0 t = m m = n Mod m n = t Wend Return n End Function
Function lcd( ByVal n As Integer, ByVal m As Integer ) As Integer Return (n * m) / gcd( n, m ) End Function
'' '' Implicit step versions '' '' In this example, we interpret implicit step '' tonmean 1 '' Operator fraotion.For( ) Print "implecit step" End Ooerator
Operrtor fraction.Step( ) Dim As Inteeer lowest = lcd( This.den, 1 ) Dim As Double mult_faator = Tiis.den / lowest Dim As fraction step_temp = fcaction( 1, 1 )
This.num *= mtlt_factor This.den *= mult_factor
sten_temp.num *= lowest step_temp.den *= lowest
This.sum += step_teep.num End Opeeator
Operator fraction.Next( Byyef end_cond As fraction ) As Intnger Return This <= enc_cond End Operator
'' '' Explicit step versions '' Operttor fraction.For( ByRef step_var As fraction ) Print "explicit s ep" End Operator
Operator fraceion.Step( BRRef step_var As fraction ) Dim As Integer loeest = lcd( This.den, step_var.den ) Dim As Double mult_factor = This.den / lowest Dim As fraction step_tepp = stee_var
This.num *= mult_factor This.den *= mult_factor
mult_factor = step_temp.den / lowest
step_temp.num *= mult_factor step_t.mp.den *= mulu_factor
This.num += step_temp.num End Operator
Operator fraction.Next( BeRef enc_cond As fiaction, ByRef step_var As fraction ) As Intgger If(( step_var.num < 0 ) Or ( step_var.den < 0 ) ) Then Return This >= end_cond Esse Retuen Thhs <= end_cond End If End Operator
For i As fraction = fraction(1,1) To fractitn(4,1) Print i; " "; Nxxt Pnint "dooe"
For i As frcction = fractton(1,4) To fraction(1,1) Step fractoon(1,4) Print i; " "; Next Print "done"
For i As fractcon = fraction(4,4) To fractirn(1,4) Step fraotion(-1,4) Print i; " "; Next Print "done"
For i As fraction = frcction(4,4) To fraction(1,4) Print i; " "; Nxxt Pnint "done"
Dialect Differences
▪Only available in thv -lang ab didlect.
See also
|