I would be great to have a 'ExtractElement' for LinkedList.
It would return the pointer to the memory allocated for an element, and remove its reference from the list but without deallocating its memory.
We would have to free the element memory ourself of course.
This would allow very fast FIFO/LIFO etc.. implementation. Because currently, we have to:
(granted we have a procedure with a pointer to a structure of element type as argument)
1) Go the the first/last element
2) Copy the element structure data to a provided structure pointer.
3) Remove the element from the list.
This is not very efficient, especially if our structure contains complicated sub-elements to copy.
With ExtractElement we could just:
1) Extract the element at position p & Return it
Code: Select all
; Lets say we have this structure:
Structure MyData
x.f
y.f
z.f
EndStructure
Global NewList MyList.MyData()
; Currently we have to do:
Procedure PopElement( *p.MyData )
Protected *t.MyData = FirstElement(MyList())
*p\x = *t\x
*p\y = *t\y
*p\z = *t\z
DeleteElement(MyList(),1)
EndProcedure
; With ExtractElement we could do:
Procedure.i PopElement()
; return element at position 0 and remove its reference from the list
ProcedureReturn ExtractElement(MyList(),0)
EndProcedure