Chapter XVIII table-driven method

Method is a table-driven programming mode - information from the lookup table inside without using logical statements ( ifand case).

Table-driven method General Conditions of Use

In appropriate circumstances, the use of table-driven method, the generated code is more complex than logic code simpler, easier to modify, and more efficient.

Two table-driven method of problem

The use of table-driven method must solve two problems. First of all, you have to answer the question of how the query entry from the table. Second, consider what is stored in the table.

Direct access table

All queries and tables, direct access to the table instead of a more complex logic control structures. The reason that they are "direct access" is because you do not have a lot of complexity around the circle will be able to find the information you want in the table inside.

Index to access the table

Sometimes, only a simple mathematical calculation can not convert the data into a table key. Part of such cases can be solved by using the index access methods.

When you use an index, first find a key index from a table with a basic type of data, and then use this key identify the master data you're interested in.

Index access technology has two main advantages:

  1. First, if the main query each record in the table are large, then create a lot of wasted space indexed array of space used, it is necessary to create more than a waste of a lot of space in the main query table space used by much smaller;
  2. Secondly, even if you do not save memory space using the index after recording operation in the index of the recording operation is sometimes better than the main table is more convenient and cheaper;
  3. A final advantage of index access technology is common table query technology advantage in maintainability has.

Access ladder table

Details need to be aware of when using the technology ladder:

  • Pay attention to the endpoint;
  • Consider using binary search instead of sequential search;
  • Consider instead a plain silver ladder access technology;
  • The extraction step into a single table lookup operation subroutine.

Checklist: table-driven method

  • [] Have you considered the table-driven method as an alternative to complex logic of it?
  • [] Have you considered the table-driven method as an alternative to complex integrated structure of it?
  • [] Have you considered the outside and during the operation of reading the table data storage, so you can change these data without modifying the code do?
  • [] If you are unable to access the table with a simple array index, then you calculate the access key functions extracted into separate subroutine, instead of calculating the key in the code repeatedly do?

Points

  • Table provides a logical alternative to a complex and integrated structure;
  • A key decision to use the table is to decide how to access the table;
  • Another key decision to use the table is to decide what content should be put into a table.

Guess you like

Origin www.cnblogs.com/liam-ji/p/11544419.html