TruncList()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

TruncList()

Post by jacdelad »

Hi #PB_All,
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
AZJIO
Addict
Addict
Posts: 1313
Joined: Sun May 14, 2017 1:48 am

Re: TruncList()

Post by AZJIO »

SplitList + FreeList ?
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: TruncList()

Post by jacdelad »

...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
AZJIO
Addict
Addict
Posts: 1313
Joined: Sun May 14, 2017 1:48 am

Re: TruncList()

Post by AZJIO »

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.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: TruncList()

Post by jacdelad »

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
AZJIO
Addict
Addict
Posts: 1313
Joined: Sun May 14, 2017 1:48 am

Re: TruncList()

Post by AZJIO »

FreeList frees all memory. There will be a pointer to a variable that can be used to free the next list.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: TruncList()

Post by jacdelad »

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.
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
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: TruncList()

Post by mk-soft »

Simple macro ...

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
The internal function FreeList cleans up the resourses better/faster than if you make them yourself with DeleteElement.
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
Tawbie
User
User
Posts: 26
Joined: Fri Jul 10, 2020 2:36 am

Re: TruncList()

Post by Tawbie »

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
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: TruncList()

Post by Olli »

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
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: TruncList()

Post by jacdelad »

mk-soft wrote: Fri Sep 16, 2022 10:27 am Simple macro ...

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
The internal function FreeList cleans up the resourses better/faster than if you make them yourself with DeleteElement.
This does not work for lists within structures. I still think a native command would be the best solution.
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
Quin
Enthusiast
Enthusiast
Posts: 279
Joined: Thu Mar 31, 2022 7:03 pm
Location: United States
Contact:

Re: TruncList()

Post by Quin »

Giving this a +1.
PB v5.40/6.10, Windows 10 64-bit.
16-core AMD Ryzen 9 5950X, 128 GB DDR5.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: TruncList()

Post by jacdelad »

jacdelad wrote: Fri Sep 08, 2023 10:54 pm
mk-soft wrote: Fri Sep 16, 2022 10:27 am Simple macro ...

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
The internal function FreeList cleans up the resourses better/faster than if you make them yourself with DeleteElement.
This does not work for lists within structures. I still think a native command would be the best solution.
Using a global list works though.
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
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: TruncList()

Post by mk-soft »

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
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: TruncList()

Post by idle »

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
Yes that's the one, wrap it in a macro or Procedure and it's done.
Post Reply