[LinkedList] AttachElement

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

[LinkedList] AttachElement

Post by grabiller »

In the same spirit as ExtractElement, it could be interesting to have a 'AttachElement':

Code: Select all

; Lets say we have this structure:
Structure MyData
  x.f
  y.f
  z.f
EndStructure

Global NewList MyList.MyData()

; User allocated structure element
Define *p.MyData = AllocateMemory(SizeOf(MyData))
*p\x = 0.1
*p\y = 0.2
*p\z = 0.3

'Insert' it in our List
AttachElement(MyList(),*p)
guy rabiller | radfac founder / ceo | raafal.org
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: [LinkedList] AttachElement

Post by STARGÅTE »

it can not attach an element with this way (AllocateMemory).

A LinkedList Element need 3*Integer space more for header-, previous- and next-pointer.

So it is easier to use CopyStructure:

Code: Select all

AddElement(MyList()) : CopyStructure(*p, @MyList(), MyData)
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
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [LinkedList] AttachElement

Post by grabiller »

I know it cannot attach elements with this way.

That's why I'm asking for this feature. :wink:

CopyStructure can't do deep copy so it does not solve the issue.
guy rabiller | radfac founder / ceo | raafal.org
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: [LinkedList] AttachElement

Post by STARGÅTE »

grabiller wrote:CopyStructure can't do deep copy so it does not solve the issue.
what?

CopyStructure() copy all structures including lists, maps, strings and so on.

the linked list element can not use "your" space, because an element needs at the beginning the header, and you / Pure Basic can not reallocate memorys forward, only at the end, so you need Move oder Copy.
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
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [LinkedList] AttachElement

Post by grabiller »

STARGÅTE wrote:
grabiller wrote:CopyStructure can't do deep copy so it does not solve the issue.
what?

CopyStructure() copy all structures including lists, maps, strings and so on.
No, not 'and so on'. It does not copy memory allocated to structure pointers, hence "it can't do deep copy".

And stop telling me "it can't" be done.

This is a "wishlist" and everything can be done.

If the PB team *does not* want to implement the wishes, that's another matter and that's fine with me.

But don't tell me *it can't be done*, this is useless and pointless.
guy rabiller | radfac founder / ceo | raafal.org
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: [LinkedList] AttachElement

Post by STARGÅTE »

grabiller wrote:It does not copy memory allocated to structure pointers, hence "it can't do deep copy".
That is also not necessary if you want attach your root-memory (with sub memorys) to the list, you have to copy only the root-memory, the pointers to sub-memorys remain valid, because the sub-memorys must not be copied, they dont need a header.
grabiller wrote:And stop telling me "it can't" be done.
This is a "wishlist" and everything can be done.
If the PB team *does not* want to implement the wishes, that's another matter and that's fine with me.
But don't tell me *it can't be done*, this is useless and pointless.
Well, then wait.
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
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: [LinkedList] AttachElement

Post by idle »

you mean something like this

Code: Select all

; Lets say we have this structure:
Structure MyData
  x.f
  y.f
  z.f
EndStructure

Global NewList MyList.i()

Macro AttachElement(alist,item,struct) 
   *temp = AllocateMemory(SizeOf(struct))
   CopyStructure(item,*temp,struct)
   AddElement(alist)
   alist = *temp 
EndMacro    

Macro ExtractElement(alist,item,Struct) 
   CopyStructure(alist,item,struct)
EndMacro  

Macro FreeListPointers(alist,struct) 
   ForEach alist 
      ClearStructure(alist,struct)
      FreeMemory(alist)
    Next   
 EndMacro 
 
*p.MyData = AllocateMemory(SizeOf(MyData))

For a = 0 To 100 ; User allocated structure element
 *p\x = a + 0.1 
  *p\y = a + 0.2
  *p\z = a + 0.3
;'Insert' it in our List
  AttachElement(MyList(),*p,MyData)
Next 

ForEach MyList() 
   ExtractElement(mylist(),*p,MyData) 
   Debug StrF(*p\x,1) + "  " + StrF(*p\y,1) + " " + StrF(*p\z,1) 
Next 

FreeListPointers(mylist(),MyData) 


Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: [LinkedList] AttachElement

Post by grabiller »

That's the idea (except for the ExtractElement which should remove the element from the list).

But the idea was also not to use any copy.

I was assuming that, internally, PureBasic is using something like this for each element:

Code: Select all

Structure Element
  *next
  *prev
  *value.ElementStructure ; a pointer to the element structure
EndStructure
But from what I understand now, PureBasic is rather using something like:

Code: Select all

Structure Element
  *next
  *prev
  value.ElementStructure_t ; element structure embedded
EndStructure
So indeed, with this last approach my wish can not be implemented without redesigning the all List feature.
guy rabiller | radfac founder / ceo | raafal.org
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: [LinkedList] AttachElement

Post by STARGÅTE »

@grabiller

you can use your structure "Element" with a pointer-list,
You can manage the memory of the data itself, and attach the address in the LinkedList:

Code: Select all

Procedure AttachElement(List *Pointer(), *Element)
	AddElement(*Pointer())
	*Pointer() = *Element
EndProcedure

Procedure ExtractElement(List *Pointer())
	Protected *Element = *Pointer()
	DeleteElement(*Pointer())
	ProcedureReturn *Element
EndProcedure



Structure Element
	Long.l
	Float.f
	String.s
EndStructure

Procedure NewElement(Long.l, Float.f, String.s)
	Protected *Buffer.Element = AllocateMemory(SizeOf(Element))
	*Buffer\Long = Long
	*Buffer\Float = Float
	*Buffer\String = String
	ProcedureReturn *Buffer
EndProcedure

Global NewList *Element()

*A = NewElement(123, #PI, "Hello")
*B = NewElement(456, #E, "World!")

AttachElement(*Element(), *B)
AttachElement(*Element(), *A)
*C.Element = ExtractElement(*Element())
Debug *C\String
*C.Element = ExtractElement(*Element())
Debug *C\String
This example can be extended.
You can add a Pointer-Map, to get an element by his "Name".
Or you can add a Pointer-Array, to get an element by his index.
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
Post Reply