Moving Element from one list to a other

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Moving Element from one list to a other

Post by Josh »

Would be nice to have a native way to move an element from one list to a other. Doing it by hand, is a little bit complicated.
sorry for my bad english
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Moving Element from one list to a other

Post by DK_PETER »

? What's wrong with CopyStructure()?
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Moving Element from one list to a other

Post by Josh »

DK_PETER wrote:? What's wrong with CopyStructure()?
Pointers to the listelement are lost in this case
sorry for my bad english
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Moving Element from one list to a other

Post by DK_PETER »

Hmmm..Okay..
How about this?

Code: Select all

Declare CopyTheElement(*src, *dest)

Structure ELEMENT_DATA
  id.i
  num.i
  var.s
EndStructure

Global NewList List1.ELEMENT_DATA()
Global NewList List2.ELEMENT_DATA()

Procedure CopyTheElement(*src, *dest)
  CopyStructure(*src, *dest, ELEMENT_DATA)
EndProcedure


For x = 0 To 100
  AddElement(List1())
  List1()\id = Random(255,10)
  List1()\num = Random(1000,100)
  List1()\var = "This is a string " + Str(x)
Next x

SelectElement(List1(),55)
AddElement(List2())
CopyTheElement(@List1(), @List2())

Debug Str(List2()\id)
Debug List2()\var
Wouldn't that work?
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Moving Element from one list to a other

Post by STARGÅTE »

CopyStructure is to slow for big list elements.

MoveElement() change only the next- and previous element pointer, but only in one list.
PB 4.60 B1 - why MoveElement() isn't across lists?

We need some like TransferElement(SourceList(), DestinationList(), Location [, *RelativeElement])
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Demivec
Addict
Addict
Posts: 4261
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Moving Element from one list to a other

Post by Demivec »

You could section a larger list into two portions and keep track of where they start and stop. Then you only have to use MoveElement() to make the transition between the two 'logical' lists.

Sorting can still be done by specifying the start and end for the list 'portion' you were interested in and you could iterate through their elements by using While/Wend or Repeat/Until instead of a ForEach/Next.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Moving Element from one list to a other

Post by Josh »

There is a way, but you have to create a temporary list and then you have to do this:

Code: Select all

ChangeCurrentElement (List1(), *Element)
MoveElement          (List1(), #PB_List_Last)
SplitList            (List1(), ListTemp())
MergeLists           (ListTemp(), List2)
I think Pb should do this by an easier native way.
sorry for my bad english
User avatar
Demivec
Addict
Addict
Posts: 4261
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Moving Element from one list to a other

Post by Demivec »

Josh wrote:There is a way, but you have to create a temporary list and then you have to do this:

Code: Select all

ChangeCurrentElement (List1(), *Element)
MoveElement          (List1(), #PB_List_Last)
SplitList            (List1(), ListTemp())
MergeLists           (ListTemp(), List2)
I think Pb should do this by an easier native way.
That's a great idea.
Post Reply