TruncList()
TruncList()
Hi #PB_All,
I wish we had a TruncList()-function, which truncates a list after the currently active element? Or do we already have???
I wish we had a TruncList()-function, which truncates a list after the currently active element? Or do we already have???
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: TruncList()
...makes me need a dummy list.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: TruncList()
I don't know how right I am, but it forces you to create a pointer, which will be assigned to the beginning of the list, then deleting this list. I don't think there will be an enumeration of the list elements when splitting. When deleting a string list there will be an enum and the others have an explicit size. You can use NextElement + DeleteElement. But this will probably be the same in speed if a string list is used.
These are my assumptions, I cannot confirm this.
These are my assumptions, I cannot confirm this.
Re: TruncList()
But the "Nirvana"-Elements would still be in memory, because PB assumes they are linked to another list now.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: TruncList()
FreeList frees all memory. There will be a pointer to a variable that can be used to free the next list.
Re: TruncList()
Yeah you're right, but afaik FreeList() doesn't accept pointers, I have to define a list.
However, I fell like this is too complicated for just truncating a list.
However, I fell like this is too complicated for just truncating a list.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: TruncList()
Simple macro ...
The internal function FreeList cleans up the resourses better/faster than if you make them yourself with DeleteElement.
Code: Select all
;-TOP
Macro TruncList(_List_)
NewList _List_#Destroy()
SplitList(_List_#(), _List_#Destroy(), #True)
FreeList(_List_#Destroy())
EndMacro
NewList A()
NewList B.s()
For i = 0 To 10
AddElement(A())
A() = i
Next i
For i = 100 To 110
AddElement(B())
B() = "Number " + i
Next i
SelectElement(A(), 5)
TruncList(A)
SelectElement(B(),6)
TruncList(b)
Debug " -- A() -- "
ForEach A()
Debug A()
Next
Debug " -- B() -- "
ForEach B()
Debug B()
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: TruncList()
To truncate a list, A(), at the current element, simply use the following code:
While NextElement(A()) : DeleteElement(A()) : Wend
Example:
While NextElement(A()) : DeleteElement(A()) : Wend
Example:
Code: Select all
NewList A.i()
For i = 0 To 99 ; create 100 elements
AddElement(A())
Next
Debug ListSize(A()) ; verify size
SelectElement(A(), 49) ; select current element
;******************************************************************
While NextElement(A()) ;
DeleteElement(A()) ; truncate list at current element
Wend ;
;******************************************************************
Debug ListSize(A()) ; verify truncated size
Re: TruncList()
All is question of element type...
Code: Select all
Structure iList
List i.i()
EndStructure
Procedure iListNew()
Protected *this.iList = AllocateMemory(SizeOf(iList) )
InitializeStructure(*this, iList)
ProcedureReturn *this
EndProcedure
Procedure iListDel(*this.iList)
ClearStructure(*this, iList)
FreeMemory(*this)
EndProcedure
Procedure iListTruncate(*this.iList, n)
Protected *garbage.iList = iListNew()
SelectElement(*this\i(), n)
SplitList(*this\i(), *garbage\i() )
iListDel(*garbage)
EndProcedure
Define *myList.iList = iListNew()
With *myList
For i = 0 To 10
AddElement(\i() )
\i() = i
Next
EndWith
iListTruncate(*myList, 5)
ForEach \i()
Debug \i()
Next
EndWith
Re: TruncList()
This does not work for lists within structures. I still think a native command would be the best solution.mk-soft wrote: ↑Fri Sep 16, 2022 10:27 am Simple macro ...
The internal function FreeList cleans up the resourses better/faster than if you make them yourself with DeleteElement.Code: Select all
;-TOP Macro TruncList(_List_) NewList _List_#Destroy() SplitList(_List_#(), _List_#Destroy(), #True) FreeList(_List_#Destroy()) EndMacro NewList A() NewList B.s() For i = 0 To 10 AddElement(A()) A() = i Next i For i = 100 To 110 AddElement(B()) B() = "Number " + i Next i SelectElement(A(), 5) TruncList(A) SelectElement(B(),6) TruncList(b) Debug " -- A() -- " ForEach A() Debug A() Next Debug " -- B() -- " ForEach B() Debug B() Next
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: TruncList()
Giving this a +1.
PB v5.40/6.10, Windows 10 64-bit.
16-core AMD Ryzen 9 5950X, 128 GB DDR5.
16-core AMD Ryzen 9 5950X, 128 GB DDR5.
Re: TruncList()
Using a global list works though.jacdelad wrote: ↑Fri Sep 08, 2023 10:54 pmThis does not work for lists within structures. I still think a native command would be the best solution.mk-soft wrote: ↑Fri Sep 16, 2022 10:27 am Simple macro ...
The internal function FreeList cleans up the resourses better/faster than if you make them yourself with DeleteElement.Code: Select all
;-TOP Macro TruncList(_List_) NewList _List_#Destroy() SplitList(_List_#(), _List_#Destroy(), #True) FreeList(_List_#Destroy()) EndMacro NewList A() NewList B.s() For i = 0 To 10 AddElement(A()) A() = i Next i For i = 100 To 110 AddElement(B()) B() = "Number " + i Next i SelectElement(A(), 5) TruncList(A) SelectElement(B(),6) TruncList(b) Debug " -- A() -- " ForEach A() Debug A() Next Debug " -- B() -- " ForEach B() Debug B() Next
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: TruncList()
TruncList extended with parameter vartype or structure
Code: Select all
;-TOP
Macro TruncList(_List_, _Structure_=i)
NewList _List_#Destroy._Structure_()
SplitList(_List_#(), _List_#Destroy(), #True)
FreeList(_List_#Destroy())
EndMacro
Structure udtData
iVal.i
sVal.s
EndStructure
NewList A.udtData()
NewList B.s()
For i = 0 To 10
AddElement(A())
A()\iVal = i
Next i
For i = 100 To 110
AddElement(B())
B() = "Number " + i
Next i
SelectElement(A(), 5)
TruncList(A, udtData)
SelectElement(B(),6)
TruncList(b, s)
Debug " -- A() -- "
ForEach A()
Debug A()\iVal
Next
Debug " -- B() -- "
ForEach B()
Debug B()
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: TruncList()
Yes that's the one, wrap it in a macro or Procedure and it's done.Tawbie wrote: ↑Sun Sep 18, 2022 11:36 pm To truncate a list, A(), at the current element, simply use the following code:
While NextElement(A()) : DeleteElement(A()) : Wend
Example:Code: Select all
NewList A.i() For i = 0 To 99 ; create 100 elements AddElement(A()) Next Debug ListSize(A()) ; verify size SelectElement(A(), 49) ; select current element ;****************************************************************** While NextElement(A()) ; DeleteElement(A()) ; truncate list at current element Wend ; ;****************************************************************** Debug ListSize(A()) ; verify truncated size