Page 1 of 1

[LinkedList] AttachElement

Posted: Sat Oct 20, 2012 4:07 pm
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)

Re: [LinkedList] AttachElement

Posted: Sat Oct 20, 2012 4:18 pm
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)

Re: [LinkedList] AttachElement

Posted: Sat Oct 20, 2012 4:24 pm
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.

Re: [LinkedList] AttachElement

Posted: Sat Oct 20, 2012 4:44 pm
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.

Re: [LinkedList] AttachElement

Posted: Sat Oct 20, 2012 7:04 pm
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.

Re: [LinkedList] AttachElement

Posted: Sat Oct 20, 2012 7:30 pm
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.

Re: [LinkedList] AttachElement

Posted: Wed Oct 24, 2012 4:27 am
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) 



Re: [LinkedList] AttachElement

Posted: Wed Oct 24, 2012 2:02 pm
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.

Re: [LinkedList] AttachElement

Posted: Wed Oct 24, 2012 2:21 pm
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.