Table Driven Logic A Technique for Simplifying Flow
Table Driven Logic A Technique for Simplifying Flow of Control Among Other Things Creative Commons License – Curt Hill
Introduction • The idea here is to consider a number of table techniques to reduce the programming logic of a module – The table lookup will replace a number of if and/or switch statements • If we can apply these ideas, it will make our code simpler and easier to understand – Usually faster as well Creative Commons License – Curt Hill
Roots • Prior to your existence the IBM 360 had a translate instruction • It was used to translate one character set into another, among other things • What you had was a table of 256 characters • You used a character as a subscript into this table • It then found the corresponding character Creative Commons License – Curt Hill
Example • IBM 360 s used EBCDIC, but they often had to deal with ASCII characters from other machines • Suppose we have an ASCII file • Load a table with EBCDIC characters in their ASCII positions • Then use each ASCII character as a subscript into this table • The file is then translated from ASCII to EBCDIC Creative Commons License – Curt Hill
Parts of this table Position Character 32 33 34 35 36 37 38 blank ! “ # $ % & 46 47 48 49 50 51 52 . / 0 1 2 3 4 60 61 62 63 64 65 66 < * > ? @ A B Table would only need to be 128 characters ASCII blank is 32, EBCDIC blank is 64 Creative Commons License – Curt Hill
Continuing • The IBM 360 is largely dead and gone except for museum pieces • The technique continues • Consider for a bit the equivalence of a function and array subscript evaluation Creative Commons License – Curt Hill
Functions and Arrays • In the C and Pascal families function evaluations use () and arrays [] • In FORTRAN, BASIC, COBOL and PL/1 both use () • This is not completely bad since both do approximately the same thing: – Find a return value based on parameter values Creative Commons License – Curt Hill
More • If the computation is particularly difficult a table lookup is actually a good thing • In the pre-calculator days, it could take hours to manually compute a square root, expontential, logarithm or very many other functions • We printed them in tables and then looked them up when needed • Saving these computations is still smart – part of Dynamic Programming Creative Commons License – Curt Hill
Issues • We need to be concerned about two issues – How to look up entries? – What should be stored in the table? • Then we can consider three approaches – Direct access – Indirect access – Stair-step access • Then we will know it all – We hope Creative Commons License – Curt Hill
Direct Access Tables • A direct access table allows a direct lookup from part or all of the data • The IBM 360 character translate is an example • Typically, we have an integral item and we can adjust its range into the subscript • Looking up month names is also a simple example Creative Commons License – Curt Hill
Month Name Lookup • In any of many Date objects you saw code like this: switch(month){ case 1: name = “January”; break; case 2: name = “Febuary”; break; case 3: name = “March”; break; … } • This works but what does it do to the complexity and LOC? • Even uglier than nested ifs Creative Commons License – Curt Hill
Month Name Lookup Again • Instead try this: String months[] = {“January”, “Febuary”, “March”, … }; … name = months[month-1]; } • This is much simpler • There are many other sorts of subscript adjustments – Such as this month-1 Creative Commons License – Curt Hill
Number of Days • This could get somewhat more complicated if we wanted number of days in a month • We would have a table of numbers • We would need one if following the assignment that checked if it was February and a leap year • This would still be easier than the switch seen in the Date class Creative Commons License – Curt Hill
Dimensionality • There is no reason why the table has to be a single dimension • We may end up with an array of several dimensions • We may use several types of variables and adjust each one in a separate way Creative Commons License – Curt Hill
Insurance Rates • Suppose we want to lookup driver’s insurance rates • The factors that we might consider would be: – Age – Gender – Marital status • The lookup could be a complicated collection of decision statements or: rate = rats[age][gender][ms]; Creative Commons License – Curt Hill
Practicalities • This one would be better read in at program startup – This allows rates to change over time • Whether gender and marital status are enumerations or Booleans is irrelevant – Both map into zero based values • The lowest driving age may need to be subtracted from the age of the client to make that zero based Creative Commons License – Curt Hill
Lookup Keys with Issues • There are many potential keys that do not map onto zero based integers very well – In the insurance rate example, we would have to eliminate ages less than 16 (or so) • We also want to collapse ranges of rates – There is not much difference between a driver of age 34 and one of age 35 • In these cases, we use a function to transform the key Creative Commons License – Curt Hill
Indirect Access Tables • Often it is difficult to do the transformation – It is not a case of simple mathematical operations such as subtraction and division • In these situations, we use two tables – The first is the table of indices – The second is the table of data • Particularly good when the data is much larger than the index • AKA indexed access tables Creative Commons License – Curt Hill
Indirect Picture Index table Data table Usually close to full Could be quite sparse Creative Commons License – Curt Hill
Commentary • Although you could use a hash in this case – It is not usually the best way • This is a sparse numeric key • Consider products that have a four-digit product number, but there are currently only 200 of them • We do not mind wasting space in the index table, but a data table entry consumes a lot more space – The index table could be pointers as well Creative Commons License – Curt Hill
VLookup • Recall the VLookup function of Excel • This is a rectangle of two rows and several columns • We take a value and find its row in the first column and then use the value in the second column – The row contains the largest value smaller than the look up value • This is routinely how a letter grade is computed in spreadsheets Creative Commons License – Curt Hill
Excel Example Creative Commons License – Curt Hill
VLookup Goal • The idea is to start with a continuous value – Such as a percentage • We then want to quantize it into a discrete value – Such as a letter grade • Therefore, all values in the range 0. 599 – 0. 699 become a C • Consider the program logic to do this Creative Commons License – Curt Hill
Grades with Decisions • You probably remember something like this: if(grade<. 6) s = “F”; else if(grade <. 7) s = “D”; else if(grade <. 6) … • We can do the same thing with a Stair-Step Table Creative Commons License – Curt Hill
Stair-Step Tables • The idea is a two column array • First column has the maximum (or minimum) value of that range • Second column the lookup value • The ranges do not have to be equal sized • If we read the data from disk, we can change sizes without changing the code Creative Commons License – Curt Hill
Example typedef struct { float value; Big. Class data; } entry; entry table[50]; int size; float val; Big. Class result; … for(int i = 0; i<size; i++){ if(val < table[i]. value){ result = table[i]. data; break; } Creative Commons License – Curt Hill
Finally • Table driven methods can increase flexibility, while reducing code • The trick is to find the right type of table and the right type of initialization Creative Commons License – Curt Hill
- Slides: 27