FB_HashTableCtrl
The hash table can be used as an efficient tool for finding individual data element among a larger number of data elements. The data objects must have a unique key. The key enables the data objects to be identified unambiguously and found quickly in the table.
The function block FB_HashTableCtrl can be used to realise a simple hash table in the PLC project, using the hashing with chaining (separate chaining) procedure.
The maximum number of data elements cannot be changed at runtime and must be specified in advance. Adding/removing/finding of data elements is controlled through action calls. The function block features the following tasks:
- A_Add (adds a new data element to the table (key/value). If an element with the same key already exists, it is overwritten!)
- A_GetFirst (reads the first table data element. If successful, getValue supplies the associated value.)
- A_GetNext (reads the next table data element. The address putPosPtr must point to the previous data element!)
- A_Lookup (looks for a data element matching the key. If successful, getValue supplies the associated value.)
- A_Remove (removes a data element matching the key.)
- A_RemoveAll (removes all data elements)
- A_RemoveFirst (removes the first data element)
- A_reset (deletes all data elements and resets the table.)
- A_GetIndexAtPosPtr (from TwinCAT v2.10 Build >= 1339 and TwinCATv2.11 Build >= 1524) (returns the array-index data element at address putPosPtr. If successfull the output variable getValue retuns the zero based array-index number. The value putValue is not used! )
VAR_IN_OUT
VAR_IN_OUT
hTable : T_HHASHTABLE;
END_VAR
hTable: Hash table handle. The handle must be initialized once with the function F_CreateHashTableHnd before it can be used. A corresponding instance of the handle variable must be created and initialized for each table.
VAR_INPUT
VAR_INPUT
key : DWORD := 0;
putValue : DWORD := 0;
putPosPtr : POINTER TO T_HashTableEntry := 0;
END_VAR
key: (32 bit). This key enables a data object to be identified and found quickly in the table.
putValue: Value/data element (32 bit).
putPosPtr: Address for a previous data element.
VAR_OUTPUT
VAR_OUTPUT
bOk : BOOL := FALSE;
getValue : DWORD := 0;
getPosPtr : POINTER TO T_HashTableEntry := 0;
END_VAR
bOk: Returns TRUE if a new data element was added to/removed from or found in the table. Returns FALSE, if the searched data element was not found, the table is empty, or an overflow occurred (table has no free data elements).
getValue: The value matching the key.
getPosPtr: The address for the data element.
Requirements
Development environment | Target platform | PLC libraries to be linked |
---|---|---|
TwinCAT v2.10.0 Build > 1332 | PC or CX (x86, ARM) | TcUtilities.Lib |