Page 1 of 2

TruncList()

Posted: Thu Sep 15, 2022 11:30 pm
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???

Re: TruncList()

Posted: Fri Sep 16, 2022 12:22 am
by AZJIO
SplitList + FreeList ?

Re: TruncList()

Posted: Fri Sep 16, 2022 1:02 am
by jacdelad
...makes me need a dummy list.

Re: TruncList()

Posted: Fri Sep 16, 2022 1:35 am
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.

Re: TruncList()

Posted: Fri Sep 16, 2022 1:50 am
by jacdelad
But the "Nirvana"-Elements would still be in memory, because PB assumes they are linked to another list now.

Re: TruncList()

Posted: Fri Sep 16, 2022 2:03 am
by AZJIO
FreeList frees all memory. There will be a pointer to a variable that can be used to free the next list.

Re: TruncList()

Posted: Fri Sep 16, 2022 2:18 am
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.

Re: TruncList()

Posted: Fri Sep 16, 2022 10:27 am
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.

Re: TruncList()

Posted: Sun Sep 18, 2022 11:36 pm
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

Re: TruncList()

Posted: Sun Oct 02, 2022 7:59 pm
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

Re: TruncList()

Posted: Fri Sep 08, 2023 10:54 pm
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.

Re: TruncList()

Posted: Mon Sep 11, 2023 12:50 am
by Quin
Giving this a +1.

Re: TruncList()

Posted: Mon Sep 11, 2023 7:57 am
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.

Re: TruncList()

Posted: Mon Sep 11, 2023 10:04 am
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

Re: TruncList()

Posted: Mon Sep 11, 2023 11:49 am
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.