img wangjiong

Table-Driven Methods

发表于2004/10/14 23:02:00  905人阅读

First, an example of table-driven method in P2

How to look up entries in the table
1. Direct Access
For example, you want to the days number for every month, store the days in an array and use month as the index.
2. Indexed Access
When you use indexes, you use the primary data to look up a key in an index table and then you use the value from the index table to look up the main data you're interested in.
3. Stair-Step Access
The general idea of stair-step structure is that entries in a table are valid for ranges of data rather than for distinct data points.
The stair-step approach categorizes each entry by determining the level at which it hits a "staircase".
To use the stair-step method, you put the upper end of each range into a table and then write a loop to check a score against the upper end of each range. When you find the point at which the score first exceeds the top of a range, you know what the grade is.
      Watch the endpoints.
      Consider using a binary search rather than a sequential search.
      Consider using indexed access instead of the stair-step technique.
      Put the stair-step table lookup into its own routine.


0 0


取 消