Page 1 of 1

Swap for Lists, Arrays and Maps

Posted: Tue Nov 06, 2012 11:47 pm
by Regenduft
German speakers may also be interessted in the german forum thread covering this theme (Link).

A SwapLists() function would be nice, since lists cannot be swaped like (e.g.) arrays, using the Swap keyword (this would just swap the two active elements).

At the moment swapping lists with a good performance is only possible by using either this combo...

Code: Select all

FirstElement(ListA())
MergeLists(ListB(), ListA(), #PB_List_Before)
SplitList(ListA(), ListB(), #False)
...or by using structured lists like this (inspired by NicTheQuick):

Code: Select all

Structure StrucList
  List s.s()
EndStructure

Define *ListA.StrucList = AllocateMemory(SizeOf(StrucList))
Define *ListB.StrucList = AllocateMemory(SizeOf(StrucList))

InitializeStructure(*ListA, StrucList)
InitializeStructure(*ListB, StrucList)

AddElement(*ListA\s()) : *ListA\s() = "a1"
AddElement(*ListA\s()) : *ListA\s() = "a2"

AddElement(*ListB\s()) : *ListB\s() = "b1"
AddElement(*ListB\s()) : *ListB\s() = "b2"

Swap *ListA, *ListB

Debug "=== ListA ==="
ForEach *ListA\s()
  Debug *ListA\s()
Next
Debug ""
Debug "=== ListB ==="
ForEach *ListB\s()
  Debug *ListB\s()
Next

Re: Swap for Lists, Arrays and Maps

Posted: Sun Mar 06, 2016 8:55 pm
by Regenduft
Unfortunately it isn't possible anymore to swap whole arrays using the keyword Swap (PB 5.42).

I really would appreciate if this feature would be reintroduced with the addition to swap lists and maps.

Code: Select all

; Once uppon a time, this codes was working... :ยด(

Dim a(0)
Dim b(0)

Swap a(), b()