Pointerlist vs SortStructuredList

Just starting out? Need help? Post your questions and find answers here.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Pointerlist vs SortStructuredList

Post by gnasen »

Hey guys,

I just wanted to use some old source of me and stumbled over this problem:

Code: Select all

Structure mystructure
  a.i
  b.i
EndStructure


NewList *mylist.mystructure()

item1.mystructure
item1\a = 3

item2.mystructure
item2\a = 2

AddElement(*mylist())
*mylist() = @item1
debug *mylist()\a

AddElement(*mylist())
*mylist() = @item2
debug *mylist()\a

SortStructuredList(*mylist(),#PB_Sort_Ascending,OffsetOf(mystructure\a),TypeOf(mystructure\a))
This results in the "new" error message

Code: Select all

SortStructeredList() can not be used with a non-structured list
This new features seems to be added with this statement

Code: Select all

Added: Debugger check for SortStructuredList() and SortList() to ensure the specified list is of correct type
So whats now the way to go?
Last edited by gnasen on Sun Dec 08, 2013 6:18 pm, edited 1 time in total.
pb 5.11
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Pointerlist vs SortStructuredList

Post by luis »

It's the asterisk in the name, I don't now what a NewList *name should mean.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
skywalk
Addict
Addict
Posts: 4213
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Pointerlist vs SortStructuredList

Post by skywalk »

Code: Select all

Structure mystructure
  a.i
  b.i
EndStructure
NewList mylist.mystructure()
SortStructuredList(myList(),#PB_Sort_Ascending,OffsetOf(mystructure\a),TypeOf(mystructure\a))
Procedure sortme(List *mylist.mystructure())
  SortStructuredList(*mylist(),#PB_Sort_Ascending,OffsetOf(mystructure\a),TypeOf(mystructure\a))
EndProcedure
sortme(mylist())
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Pointerlist vs SortStructuredList

Post by gnasen »

luis wrote:It's the asterisk in the name, I don't now what a NewList *name should mean.
It is a list of pointers, I use it like this

Code: Select all

Structure mystructure
  a.i
  b.i
EndStructure


NewList *mylist.mystructure()

item1.mystructure
item1\a = 3

item2.mystructure
item2\a = 2

AddElement(*mylist())
*mylist() = @item1
debug *mylist()\a

AddElement(*mylist())
*mylist() = @item2
debug *mylist()\a

SortStructuredList(*mylist(),#PB_Sort_Ascending,OffsetOf(mystructure\a),TypeOf(mystructure\a))
So I have a bunch of objects and my list holds pointers to some of them. In earlier versions it was possible to sort this pointer list by the way I posted :?

@skywalk: I dont see how this solves the problem with the pointer list as mentioned before. Do I miss something?
Last edited by gnasen on Sun Dec 08, 2013 6:17 pm, edited 1 time in total.
pb 5.11
User avatar
skywalk
Addict
Addict
Posts: 4213
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Pointerlist vs SortStructuredList

Post by skywalk »

Sorry, I don't use pointers to structured lists in this manner. Have a look at the manual under InitializeStructure.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Pointerlist vs SortStructuredList

Post by luis »

gnasen wrote:
luis wrote:It's the asterisk in the name, I don't now what a NewList *name should mean.
It is a list of pointers, I use it like this
I never knew that syntax was supported. Looking at the help about newlist does not seem possible.
Looking at the help it seem you can just use a name (without *) and then add items to the list by making a copy of them inside the list.
If using an * in the name makes the AddElement to copy the address of the object instead of making a whole copy, and then accessing the *list makes the compiler to generate code to reference the original data available elsewhere then I never knew that.

If that's the case, it should be mentioned in the manual. Knowing that I would have probably written some past code in a different way.
At least an example like this, using *list() should be added to the list of the example and one could guess it's possible (and supported).
Last edited by luis on Sun Dec 08, 2013 8:06 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Pointerlist vs SortStructuredList

Post by gnasen »

skywalk wrote:Sorry, I don't use pointers to structured lists in this manner. Have a look at the manual under InitializeStructure.
No I think we talk past each other. I dont want to initialize the elements I add to the list, because they already exist.
Imagine you have object1 ... object10 in your memory and you chose 3 of them. Further you want them sorted.
So you dont want to copy them, because they already exist.

Edit:
@Luis: Exactly, I think now it should be more clear where the "error" is to find.

At the moment I see 2 answers:
1. This was an unsupported feature.
2. The new PB versions broke a feature.

Edit2: I updated the above examples to make it more clear.
pb 5.11
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Pointerlist vs SortStructuredList

Post by freak »

Yes, pointer lists are allowed (and supported). It is just a list of pointer sized values.

However, sorting a pointer list with SortStructuredList() makes no sense and has actually never worked the way you expect. The command sorts the list by what is contained directly in the list element (the pointer). It does not follow where the pointer points to. Try the above example in an older PB version and you will actually see that the values don't get sorted by the structure content, but by the values of each pointer!

> SortStructuredList(*mylist(),#PB_Sort_Ascending,OffsetOf(mystructure\a),TypeOf(mystructure\a))

This asks the command to sort the list by what can be found in the list element at OffsetOf(mystructure\a). Since this offset is 0, and the list element only contains a single pointer, the command sorts the list by the values of the pointer! If you used OffsetOf(mystructure\b) you would get even weirder results, because the resulting offset is outside of the list element's memory!

So the new debugger check is correct: Such a list does not contain a structure in its elements, so there is no structure to sort by. The command is not designed to follow any pointers within its elements.
quidquid Latine dictum sit altum videtur
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Pointerlist vs SortStructuredList

Post by gnasen »

This is really weird, I will install an old version of PB again and try it out. Pighead and so!
Your explanation makes totally sense, however why did it actually work in my old code? Seems like a bug which has never fallen back on me. Dont know if I should feel happy or sad :shock:
pb 5.11
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Pointerlist vs SortStructuredList

Post by luis »

So if this is supported

Code: Select all

Structure mystructure
  a.i
  b.i
EndStructure


NewList *mylist.mystructure()

item1.mystructure
item1\a = 1

item2.mystructure
item2\a = 2

AddElement(*mylist())
*mylist() = @item1

AddElement(*mylist())
*mylist() = @item2

item1\a = 100
item2\a = 200

ResetList(*mylist())
While NextElement(*mylist())
    Debug *mylist()\a
Wend
and considering the help says about AddElement()

"Returns non-zero if the new element was created and zero otherwise. The value returned is a pointer to the new element data. "

This means using that return value with a list-of-pointers returns the address of the pointer referencing the actual data you want to use, and the code above becomes:

Code: Select all

Structure mystructure
  a.i
  b.i
EndStructure


NewList *mylist.mystructure()

item1.mystructure
item1\a = 1

item2.mystructure
item2\a = 2

Define *p.Integer

*p = AddElement(*mylist()) ; the returned value is the address of the pointer referencing the actual data
*p\i = @item1 ; store the address of the actual data inside the pointer

*p = AddElement(*mylist())
*p\i = @item2

item1\a = 100
item2\a = 200

ResetList(*mylist())
While NextElement(*mylist())
    Debug *mylist()\a
Wend

OK, nice to know but I think it should be mentioned in the manual. I learned it just today.

Until now, for me a list of pointers was this: NewList MyList.i()

A list of numbers, maybe of addresses to something.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Pointerlist vs SortStructuredList

Post by netmaestro »

However, sorting a pointer list with SortStructuredList() makes no sense
I wouldn't go quite that far although I see your point. For now it doesn't make sense. Because PB does not notice that it's pointers being sorted, drill down to the content and then sort the pointers such that the content would be ordered if you stepped through the list via ForEach or such, sorting pointers is not supported for now. But behavior like I just described would be possible to implement, and imho it makes reasonable fodder for a feature request. (I'm not going to make one because I don't think I've ever made a list of pointers :mrgreen: )
BERESHEIT
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Pointerlist vs SortStructuredList

Post by freak »

You are right. What i meant to say was "makes no sense given how the command works".
quidquid Latine dictum sit altum videtur
User avatar
STARGÅTE
Addict
Addict
Posts: 2228
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Pointerlist vs SortStructuredList

Post by STARGÅTE »

Here is an example with my CustomSortList()

Code: Select all

Prototype.i CustomSortListComparisonCallback(*Element1, *Element2)

Macro CustomSortList(ListName, ComparisonCallback)
  CustomSortList_ComparisonCallback.CustomSortListComparisonCallback = ComparisonCallback
  Repeat
    CustomSortList_Quit = #True
    ForEach ListName
      *CustomSortList_Element = @ListName
      While NextElement(ListName)
        If CustomSortList_ComparisonCallback(*CustomSortList_Element, @ListName)
          SwapElements(ListName, *CustomSortList_Element, @ListName)
          CustomSortList_Quit = #False
        EndIf
      Wend
      ChangeCurrentElement(ListName, *CustomSortList_Element)
    Next
  Until CustomSortList_Quit
EndMacro



Structure mystructure
  a.i
  b.i
EndStructure

Procedure Comparison(*Pointer1, *Pointer2)
  Protected *Element1.mystructure = PeekI(*Pointer1)
  Protected *Element2.mystructure = PeekI(*Pointer2)
  If *Element1\a > *Element2\a
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

NewList *mylist.mystructure()

item1.mystructure
item1\a = 3

item2.mystructure
item2\a = 2

AddElement(*mylist())
*mylist() = @item1
Debug *mylist()\a

AddElement(*mylist())
*mylist() = @item2
Debug *mylist()\a

CustomSortList(*mylist(),@Comparison())

Debug "---"
ForEach *mylist()
  Debug *mylist()\a
Next
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
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: Pointerlist vs SortStructuredList

Post by graph100 »

@Alexi : though that does the job, it's wasting memory and time !

@STARGÅTE : thanks for posting this, it will save me great time

@freak : indeed it could be used ! I stumbled on this post because just now I had the problem gnasen had.
I use list of pointer for a long time, et a lot to sort things stored in another place :

On one side I have the resources with all the data
and on the other side the list that address the data in a specified order.

And on my case it took time for me to find the problem because the error was not found by the compiler.
Cause I put it like that :

Code: Select all

Structure my_test
	List test.POINT()
	
	List *pt_test.POINT()
EndStructure

Define VAR_glb.my_test
InitializeStructure(VAR_glb, my_test)


For a = 0 To 100 Step 20
	AddElement(VAR_glb\test())
	AddElement(VAR_glb\pt_test())
	
	VAR_glb\test()\x = a
	VAR_glb\pt_test() = VAR_glb\test()
	
Next

For a = -100 To 0 Step 20
	AddElement(VAR_glb\test())
	AddElement(VAR_glb\pt_test())
	
	VAR_glb\test()\x = a
	VAR_glb\pt_test() = VAR_glb\test()
	
Next



SortStructuredList(VAR_glb\test(), #PB_Sort_Descending, OffsetOf(POINT\x), #PB_Long)

ForEach VAR_glb\test()
	Debug VAR_glb\test()\x
Next

Debug "----"

ForEach VAR_glb\pt_test()
	Debug VAR_glb\pt_test()\x
Next

Debug "####"

SortStructuredList(VAR_glb\pt_test(), #PB_Sort_Descending, OffsetOf(POINT\x), #PB_Long)


ForEach VAR_glb\test()
	Debug VAR_glb\test()\x
Next

Debug "----"

ForEach VAR_glb\pt_test()
	Debug VAR_glb\pt_test()\x
Next
this is an example code, so there is a reason I coded it that way in my project.
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
Post Reply