Control table: Difference between revisions

Content deleted Content added
C language: Split C section into C-stuff and non-C-stuff
Examples of control tables: Normalize structure->add general section
Line 92:
The examples below have been chosen partly to illustrate potential performance gains that may not only ''compensate'' significantly for the additional tier of abstraction, but also ''improve'' upon – what otherwise might have been – less efficient, less maintainable and lengthier code. Although the examples given are for a 'low level' assembly language and for the [[C (language)|C language]], it can be seen, in both cases, that very few lines of code are required to implement the control table approach and yet can achieve very significant [[constant time]] performance improvements, reduce repetitive source coding and aid clarity, as compared with [[verbose]] conventional program language constructs. See also the [[Control table#Quotations|quotations]] by [[Donald Knuth]], concerning tables and the efficiency of [[multiway branch]]ing in this article.
 
==Examples of control tables==
===General===
The following examples are [[arbitrary]] (and based upon just a single input for simplicity), however the intention is merely to demonstrate how control flow can be effected via the use of tables instead of regular program statements. It should be clear that this technique can easily be extended to deal with multiple inputs, either by increasing the number of columns or utilizing multiple table entries (with optional and/or operator). Similarly, by using (hierarchical) 'linked' control tables, [[structured programming]] can be accomplished (optionally using indentation to help highlight subordinate control tables).
"CT1" is an example of a control table that is a simple [[lookup table]]. The first column represents the input value to be tested (by an implied 'IF input1 = x') and, if TRUE, the corresponding 2nd column (the 'action') contains a subroutine address to perform by a [[System call|call]] (or [[goto|jump]] to – similar to a [[Switch statement|SWITCH]] statement). It is, in effect, a [[multiway branch]] with return (a form of "[[dynamic dispatch]]"). The last entry is the default case where no match is found. For a programming language that supports pointers within [[data structure]]s, CT1 can be used to direct [[control flow]] to an [[subroutine]] according to matching value from the table.
 
"CT1" is an example of a control table that is a simple [[lookup table]]. The first column represents the input value to be tested (by an implied 'IF input1 = x') and, if TRUE, the corresponding 2nd column (the 'action') contains a subroutine address to perform by a [[System call|call]] (or [[goto|jump]] to – similar to a [[Switch statement|SWITCH]] statement). It is, in effect, a [[multiway branch]] with return (a form of "[[dynamic dispatch]]"). The last entry is the default case where no match is found.
 
:{| class="wikitable"
Line 111 ⟶ 110:
| {{mono|?}} || → ||Default
|}
For programming languages that support pointers within [[data structure]]s alongside other data values, the above table (CT1) can be used to direct [[control flow]] to an appropriate [[subroutine]]s according to matching value from the table (without a column to indicate otherwise, equality is assumed in this simple case).
 
=== Assembly language ===