Linked lists are suitable as a flexible and modular way to implement simple and powerful code with ease.
This library will allow you to leverage the simplicity, power and flexibility of linked lists.
A linked list is a data structure that represents a sequence of nodes (Elements). This library implements a so called "double linked list", which simply means that the list gives each Element pointers to both the next Element and the previous Element. Unlike an array, a linked list does not provide constant time access to a particular “index” within the list. This means that if you’d like to find the K-th element in the list, you will need to iterate through K elements. The benefit of a linked list is that you can add and remove Elements from the list during runtime so this means you can very easily extend your code with new FB's without extra coding since itteration with Elements is always done in FOR and/or WHILE loops. This means that, by design your code doesn't need to be rewritten, as demonstrated in the example below 👇
Insert the Library Reference in your program's library manager.
Declare a new FB named FB_ExtendedElement
We need to either to
1) extend ~~~ FB_Element ~~~ since it has been decorated as ~~~ ABSTRACT ~~~ or
Any new FB that will be a part of the List should preferably use method 2) implement interface. This way FB_ExtendElement will act as a baseclass for your Element without being itself of type "FB_ELEMENT". EXTENDS means "IS OF CLASS .. ", while IMPLEMENTS means "MAKES USE OF INTERFACE .. ".
If you use encapsulation and composition for the appropriate interface, as a bonus you gain more freedom and more flexibility, it isn't a CLASS of! However, the penalty is that it will demand more programming effort since the Interface needs to be implemented.
FUNCTION_BLOCKFB_MyElementIMPLEMENTSLinkedList.IElement// We choose implement interface hereVAR_itfPrev:IElement;_itfNext:IElement;_itfList:IList;_uiCount:UINT:=0;END_VAR// BodyMETHODIsListMemberOf:BOOL// xMemberVAR_INPUTitfList:IList;END_VAR// BodyIsListMemberOf:=(itfList=THIS^._itfList);METHODRemoveElem:IElement// BodyIF_itfList=0THEN// This element is not a member of a listRemoveElem:=0;RETURN;END_IFRemoveElem:=_itfList.RemoveElem(THIS^);PROPERTYIsLinkedElem:BOOL// Get GetIsLinkedElem:=(_itfList<>0);// 0 = interface is not initialised (pointing to something)// Set not implemented PROPERTYList:IList// GetList:=_itfList;// Set_itfList:=List;PROPERTYNextElem:IElement// GetList:=_itfList;// Set_itfList:=List;PROPERTYPrevElem:IElement// GetPrevElem:=_itfPrev;// Set_itfPrev:=PrevElem;METHODReturnInstance:POINTERTOFB_MyElement// BodyReturnInstance:=THIS;METHODCount:UINT;// Body_uiCount:=uiCount+1;PROPERTYGetCount:UINT// GetGetCount:=_uiCount;
Always obtain access to elements via the functionblock instance of List.
Element require some burnerplate-code when compared to array's but they also more flexible due to their dynamic nature.
You can even claim dynamic Elements on the fly through use of the pre claimed dynamic memory pool factory class. This factory reserves a pre-defined number of elements for you which you can claim or release during runtime. Just check if a new element can be claimed (is there a free lement available from the pool?), claim it, add to the list with AddElement( .. ), ready.
By downloading and using our software you abide by the MIT License