Hello,
I'm coding graph charts.
I try to have a data structure with a pointer to a global linked list, in order to share common lists between several charts (a list for colors used by the graph, a list of values, etc...).
Does anybody know how PureBasic manages linked lists to store list informations such as listsize, first element, last element?
I know that PB adds 2 pointers (*previous and *next) in a header of each element, but I don't know where are stored other informations.
Thanks!
linked list pointer
Re: linked list pointer
You can have several pointers to a common list that is stored in a structure but the list can have only one set of attributes, such as current element.
Code: Select all
Structure commonList
List myList.i()
EndStructure
Global colors.commonList
Define *graph1.commonList = @colors, *graph2.commonList = @colors
AddElement(colors\myList()): colors\myList() = RGB(10, 10, 10)
AddElement(colors\myList()): colors\myList() = RGB(20, 20, 20)
AddElement(colors\myList()): colors\myList() = RGB(30, 30, 30)
AddElement(colors\myList()): colors\myList() = RGB(40, 40, 40)
AddElement(colors\myList()): colors\myList() = RGB(50, 50, 50)
AddElement(colors\myList()): colors\myList() = RGB(60, 60, 60)
AddElement(colors\myList()): colors\myList() = RGB(70, 70, 80)
AddElement(colors\myList()): colors\myList() = RGB(80, 80, 80)
AddElement(colors\myList()): colors\myList() = RGB(90, 90, 90)
AddElement(colors\myList()): colors\myList() = RGB(100, 100, 100)
FirstElement(*graph1\myList())
Debug ListSize(*graph1\myList())
ForEach *graph2\myList()
Debug "color: " + Hex(*graph2\myList(), #PB_Long)
Next
;Since the list being pointed to is the same both indexes are the same, the index of the last element
Debug ListIndex(*graph1\myList())
Debug ListIndex(*graph2\myList())
;You can move through the common list if only reading elements by using
;nested PushListPosition()/PopListPosition(), but this cannot be done in a shuffled manner.
;If this is done in separate threads then a Mutex or other access control feature needs to be used also.
SelectElement(*graph1\myList(), 3) ;select element 3 of list using *graph1 pointer.
Debug "graph1 color: " + Hex(*graph1\myList(), #PB_Long)
PushListPosition(*graph1\myList()) ;save the current position, either pointer could be used (i.e. *graph2 or *graph1)
;cycle through all colors again using the list pointer *graph2
ForEach *graph2\myList()
Debug "graph2 color: " + Hex(*graph2\myList(), #PB_Long)
Next
PopListPosition(*graph1\myList()) ;restore the current position, either pointer could be used (i.e. *graph2 or *graph1)
Debug "graph1 color: " + Hex(*graph1\myList(), #PB_Long)
Last edited by Demivec on Fri Apr 20, 2018 2:52 am, edited 1 time in total.
Re: linked list pointer
Maybe you should explain what you have in mind before you want to go deep into PureBasic Internas in your first posting.ymerdy wrote:Hello,
I'm coding graph charts.
I try to have a data structure with a pointer to a global linked list, in order to share common lists between several charts (a list for colors used by the graph, a list of values, etc...).
Does anybody know how PureBasic manages linked lists to store list informations such as listsize, first element, last element?
I know that PB adds 2 pointers (*previous and *next) in a header of each element, but I don't know where are stored other informations.
Thanks!
sorry for my bad english
Re: linked list pointer
Messing with PB internal is a bad idea, it could break anytime in future version
Re: linked list pointer
For sure, but it'good to understand how things work in the background!Fred wrote:Messing with PB internal is a bad idea, it could break anytime in future version
Envoyé de mon Power_3 en utilisant Tapatalk
Re: linked list pointer
Hi Ymerdy,
I think you have exactly what you want here without internal overview.
Is it okay?
I think you have exactly what you want here without internal overview.
Is it okay?
Code: Select all
Structure LST
List Act.S()
EndStructure
Global Todo.LST
Structure LSTPTR
List *Act()
EndStructure
Procedure Ajouter(Quoi.S, *AQuoi.LSTPTR)
AddElement(Todo\Act() ) ; inchangé
Todo\Act() = Quoi
AddElement(*AQuoi\Act() )
*AQuoi\Act() = @Todo\Act()
EndProcedure.
Procedure Lire(*Quoi.LST)
ForEach(*Quoi\Act() )
ChangeCurrentElement(Todo(), *Quoi\Act() )
Debug Todo\Act()
Next
Debug " "
EndProcedure
Define Lundi.LSTPTR
Define Mardi.LSTPTR
Ajouter("Courses", Lundi)
Ajouter("Coiffeur", Mardi)
Ajouter("Dentiste", Mardi)
ForEach Todo()
Debug Todo()
Next
Debug " "
Lire(Lundi)
Lire(Mardi)
Re: linked list pointer
What kind of access to the list or lists is needed? Did you need to only read from the list or did you also need to add and delete items (possibly reordering them)?ymerdy wrote:I try to have a data structure with a pointer to a global linked list, in order to share common lists between several charts (a list for colors used by the graph, a list of values, etc...).
Did you need to access a list common to several charts for only one chart at a time or did you need to interleave or mix the access to the same list from more than one chart or object at the same time?
Can you give a brief outline of how you might use the type of access you are interested in?