Swap for Lists, Arrays and Maps

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Regenduft
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Mar 02, 2009 9:20 pm
Location: Germany

Swap for Lists, Arrays and Maps

Post 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
User avatar
Regenduft
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Mar 02, 2009 9:20 pm
Location: Germany

Re: Swap for Lists, Arrays and Maps

Post 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()
Post Reply