SortStructuredList()

Just starting out? Need help? Post your questions and find answers here.
Splunk
User
User
Posts: 36
Joined: Wed Apr 21, 2021 6:53 pm

SortStructuredList()

Post by Splunk »

Hi guys,
I'm probably too stupid again, but I think this is a bug.

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table[3]), TypeOf(Anything\Table)
"Syntax Error" at SortStructuredList()

Why? (PB 5.41)

// Moved from "Bugs - Windows" to "Coding Questions" (Kiffi)
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: SortStructuredList()

Post by jacdelad »

Closing Bracket is missing and you are using OffsetOf wrong:

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table), TypeOf(Anything\Table))
Edit: You want to sort by the third element of Table.l (counting starts with "0", so "3" is the fourth item. if you need the fourth item, then use "+12"):

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table)+8, TypeOf(Anything\Table))
But I've never used the "[]" within structures, so I'm not 100% sure.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: SortStructuredList()

Post by mk-soft »

Not supported by 'OffsetOf', but you can calculate it yourself.

@jacdelad,
your calculation is wrong. First element is null. Table index 0 to 4.

Code: Select all

Structure Anything
  Dummy.i
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

Debug OffsetOf(Anything\Table) + 3 * SizeOf(long)

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table) + 3 * SizeOf(long), TypeOf(Anything\Table))
Last edited by mk-soft on Wed Mar 01, 2023 6:20 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kiffi
Addict
Addict
Posts: 1486
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: SortStructuredList()

Post by Kiffi »

Splunk wrote: Wed Mar 01, 2023 5:44 pm"Syntax Error" at SortStructuredList()
@Splunk: The bug forum is not for the bugs you make yourself.

Please read before submitting a bug report!
Hygge
Splunk
User
User
Posts: 36
Joined: Wed Apr 21, 2021 6:53 pm

Re: SortStructuredList()

Post by Splunk »

@Kiffi: I am still of the opinion that it is a bug. Because I can't read anywhere in the description that it doesn't work.Did you address the issue, or did you immediately read what "jacdelad" wrote and mistakenly assume I made a mistake?

Thanks to all
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: SortStructuredList()

Post by NicTheQuick »

Splunk wrote: Wed Mar 01, 2023 6:34 pm @Kiffi: I am still of the opinion that it is a bug. Because I can't read anywhere in the description that it doesn't work.Did you address the issue, or did you immediately read what "jacdelad" wrote and mistakenly assume I made a mistake?

Thanks to all
No, it is not a bug.

Your first error was the missing closing bracket.
Your second error was using OffsetOf wrongly. There is no documentation about the possibility to use the square brackets operator to calculate offsets of static arrays within structure members. But I understand that this would be a nice feature.

The only bug I could imagine is the result of TypeOf(Anything\Table) which is #PB_Long = 5, although in fact it is a static array. But on the other hand it is an static array of type #PB_Long. :wink:
However SizeOf(Anything\Table) returns 20 which is 5 times the size of a Long which is not consistent with TypeOf() then.

I was trying to do something like this as an workaround but because of that inconsistency it does not work:

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

SortStructuredList(void(),
 	#PB_Sort_Ascending,
  	OffsetOf(Anything\Table) + 3 * SizeOf(Anything\Table),
   TypeOf(Anything\Table))
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: SortStructuredList()

Post by NicTheQuick »

I added a feature request here: viewtopic.php?t=80981
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: SortStructuredList()

Post by mk-soft »

@NicTheQuick

Now you have made a mistake in calculating the offset!
The 'SizeOf(Anything\Table)' is the total size of the table. So 20 bytes ;)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Splunk
User
User
Posts: 36
Joined: Wed Apr 21, 2021 6:53 pm

Re: SortStructuredList()

Post by Splunk »

Thanks NicTheQuick..

Yes, the missing closing bracket was probably lost in the transfer to the forum and is not part of the actual problem.

Since there is (unfortunately) no "clean" solution, I fixed the problem with an auxiliary field.
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: SortStructuredList()

Post by mk-soft »

Splunk wrote: Wed Mar 01, 2023 7:57 pm Thanks NicTheQuick..

Yes, the missing closing bracket was probably lost in the transfer to the forum and is not part of the actual problem.

Since there is (unfortunately) no "clean" solution, I fixed the problem with an auxiliary field.
Of course it works with self-calculated offset ... My example not tried.

Here again with output.

Code: Select all

Structure Anything
  Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

Debug "set values"
value = 3
ForEach void()
  void()\Table[3] = value
  value - 1
  Debug void()\Table[3]
Next

SortStructuredList(void(), #PB_Sort_Ascending, OffsetOf(Anything\Table) + 3 * SizeOf(long), #PB_Long)

Debug "sort values"
ForEach void()
  Debug void()\Table[3]
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Splunk
User
User
Posts: 36
Joined: Wed Apr 21, 2021 6:53 pm

Re: SortStructuredList()

Post by Splunk »

@mk-soft:
Yes, I looked at your suggestion and it does work. But it is not really a "clean" solution.

As a clean solution I tried:

Code: Select all

Bytes = SizeOf(Anything\Table) / TypeOf(Anything\Table)
SortStructuredList(void(), #PB_Sort_Descending, OffsetOf(Anything\Table) +3*Bytes, Bytes)
PB does not accept this. But it is mathematically correct.
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: SortStructuredList()

Post by jacdelad »

Your "TypeOf"-Parameter for sorting is wrong:

Code: Select all

Structure Anything
	Table.l[5]
EndStructure

NewList void.Anything()

AddElement(void())
AddElement(void())
AddElement(void())

Bytes = SizeOf(Anything\Table) / TypeOf(Anything\Table)
SortStructuredList(void(), #PB_Sort_Descending, OffsetOf(Anything\Table) +3*Bytes, TypeOf(Anything\Table))
You gave the size of element, not the type.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: SortStructuredList()

Post by mk-soft »

Help, ...
Both wrong

TypeOf for an offset calculation is completely wrong !!! TypeOF does not return a size ...

Offset = offset_of_first_array_element + array_index * sizeof_element
Offset = 0 + 3 * 4
Offset = 12
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Splunk
User
User
Posts: 36
Joined: Wed Apr 21, 2021 6:53 pm

Re: SortStructuredList()

Post by Splunk »

mk-soft wrote: Wed Mar 01, 2023 11:19 pm Help, ...
Both wrong
:mrgreen:

I probably would have reacted the same way if I had read this bullshit. Coincidentally, "TypeOf()" gave the number of elements, which of course is wrong.

The bulletproof solution is:

Code: Select all

Index = 3
LenOfElement = @void()\Table[1] - @void()\Table[0]
SortStructuredList(void(), #PB_Sort_Descending, OffsetOf(Anything\Table) +Index*LenOfElement, TypeOf(Anything\Table))
BarryG
Addict
Addict
Posts: 4135
Joined: Thu Apr 18, 2019 8:17 am

Re: SortStructuredList()

Post by BarryG »

jacdelad wrote: Wed Mar 01, 2023 5:59 pmClosing Bracket is missing
That's why I requested this before -> viewtopic.php?t=76876
Post Reply