Another variation of the macro version.
Code: Select all
EnableExplicit
Macro ReverseList(_list_)
CompilerIf #PB_Compiler_Procedure = ""
Define _listidx_#MacroExpandedCount, _prevElement1_#MacroExpandedCount, _prevElement2_#MacroExpandedCount, _mid_#MacroExpandedCount, _listsz_#MacroExpandedCount= ListSize(_list_)
CompilerElse
Protected _listidx_#MacroExpandedCount, _prevElement1_#MacroExpandedCount, _prevElement2_#MacroExpandedCount, _mid_#MacroExpandedCount, _listsz_#MacroExpandedCount = ListSize(_list_)
CompilerEndIf
If _listsz_#MacroExpandedCount > 1
_mid_#MacroExpandedCount = (_listsz_#MacroExpandedCount - 2) / 2
_prevElement1_#MacroExpandedCount = FirstElement(_list_)
MoveElement(_list_, #PB_List_Last)
_prevElement2_#MacroExpandedCount = PreviousElement(_list_)
MoveElement(_list_, #PB_List_First)
For _listidx_#MacroExpandedCount = 1 To _mid_#MacroExpandedCount
;If NextElement(_list_) = 0 : Break : EndIf
NextElement(_list_)
MoveElement(_list_, #PB_List_Before, _prevElement1_#MacroExpandedCount)
_prevElement1_#MacroExpandedCount = @_list_
;If PreviousElement(_list_) = 0 : Break : EndIf
PreviousElement(_list_)
MoveElement(_list_, #PB_List_After, _prevElement2_#MacroExpandedCount)
_prevElement2_#MacroExpandedCount = @_list_
Next
EndIf
EndMacro
Procedure x()
Protected NewList NList1.s()
Protected NewList NList2.i()
Protected NewList NList3.i()
Protected i
Debug "In Procedure"
For i = 1 To 11
AddElement(NList1())
NList1() = Str(i)
Next
ReverseList(NList1())
ForEach NList1()
Debug NList1()
Next
Debug "----------------------"
For i = 1 To 4
AddElement(NList2())
NList2() = i
Next
ReverseList(NList2())
ForEach NList2()
Debug NList2()
Next
Debug "----------------------"
For i = 1 To 2
AddElement(NList3())
NList3() = i
Next
ReverseList(NList3())
ForEach NList3()
Debug NList3()
Next
EndProcedure
NewList NList1.s()
NewList NList2.i()
NewList NList3.i()
Define i
Debug "Main"
For i = 1 To 11
AddElement(NList1())
NList1() = Str(i)
Next
ReverseList(NList1())
ForEach NList1()
Debug NList1()
Next
Debug "----------------------"
For i = 1 To 4
AddElement(NList2())
NList2() = i
Next
ReverseList(NList2())
ForEach NList2()
Debug NList2()
Next
Debug "----------------------"
For i = 1 To 2
AddElement(NList3())
NList3() = i
Next
ReverseList(NList3())
ForEach NList3()
Debug NList3()
Next
Debug "----------------------"
x()
A version that reuses variables that have already been defined. (Using a 'While' loop makes it a bit faster.)
Code: Select all
Macro ReverseList(_list_)
CompilerIf #PB_Compiler_Procedure = ""
CompilerIf Not Defined(_listsz_, #PB_Variable)
Define _prevElement1_, _prevElement2_, _listsz_
CompilerEndIf
CompilerElse
CompilerIf Not Defined(_listsz_, #PB_Variable)
Protected _prevElement1_, _prevElement2_, _listsz_
CompilerEndIf
CompilerEndIf
_listsz_= ListSize(_list_)
If _listsz_ > 1
_listsz_ = (_listsz_ - 2) / 2
_prevElement1_ = FirstElement(_list_)
MoveElement(_list_, #PB_List_Last)
_prevElement2_ = PreviousElement(_list_)
MoveElement(_list_, #PB_List_First)
While _listsz_ > 0
NextElement(_list_)
MoveElement(_list_, #PB_List_Before, _prevElement1_)
_prevElement1_ = @_list_
PreviousElement(_list_)
MoveElement(_list_, #PB_List_After, _prevElement2_)
_prevElement2_ = @_list_
_listsz_ - 1
Wend
EndIf
EndMacro
Edit:
A faster way.
Code: Select all
EnableExplicit
Macro ReverseList(_list_)
CompilerIf #PB_Compiler_Procedure = ""
CompilerIf Not Defined(_listsz_, #PB_Variable)
Define _listsz_, _prevElement1_, _prevElement2_, _prevElement3_, _prevElement4_
CompilerEndIf
CompilerElse
CompilerIf Not Defined(_listsz_, #PB_Variable)
Protected _listsz_, _prevElement1_, _prevElement2_, _prevElement3_, _prevElement4_
CompilerEndIf
CompilerEndIf
_listsz_= ListSize(_list_)
If _listsz_ > 1
_listsz_ = _listsz_ / 2
_prevElement1_ = FirstElement(_list_)
_prevElement2_ = NextElement(_list_)
_prevElement4_ = LastElement(_list_)
_prevElement3_ = PreviousElement(_list_)
While _listsz_ > 0
SwapElements(_list_, _prevElement1_, _prevElement4_)
ChangeCurrentElement(_list_, _prevElement2_)
_prevElement1_ = _prevElement2_
_prevElement2_ = NextElement(_list_)
ChangeCurrentElement(_list_, _prevElement3_)
_prevElement4_ = _prevElement3_
_prevElement3_ = PreviousElement(_list_)
_listsz_ - 1
Wend
EndIf
EndMacro
Macro loop(_max_)
NewList NList#_max_#.i()
For i = 1 To _max_
AddElement(NList#_max_#())
NList#_max_#() = i
Next
ReverseList(NList#_max_#())
ForEach NList#_max_#()
Debug NList#_max_#()
Next
Debug "----------------------"
EndMacro
Procedure x()
Protected i
Debug "In Procedure"
loop(1)
loop(2)
loop(3)
loop(4)
loop(5)
loop(6)
loop(7)
loop(8)
loop(9)
loop(10)
loop(11)
loop(11)
EndProcedure
Define i
Debug "Main"
loop(1)
loop(2)
loop(3)
loop(4)
loop(5)
loop(6)
loop(7)
loop(8)
loop(9)
loop(10)
loop(11)
x()