Table lookups

Leo de Wit leo at philmds.UUCP
Sun Jul 3 17:37:54 AEST 1988


In article <1802 at loral.UUCP> jlh at loral.UUCP (The Mad Merkin Hunter) writes:
>Lets say I have a value that can range from 0-15 and I want to print
>an ascii string based on that value.  Normally I create an array of
>strings and index into it.  K&R does this with the names of the months
>when they talk about arrays of pointers.  Now lets say that instead of
>index values from 0-15 I have bit positions, i.e., the values are
>0x1, 0x2, 0x4, ..., 0x80.  I'd like to do a table lookup on these values
>also, as opposed to building a switch statement.  The only methods I
>can come up with take a lot longer and are much less clear than the
>switch statement (for example, logarithms).  Anyone got any ideas?

A general solution is a function that maps values to natural numbers;
for instance if you have a function log2(n) that returns the base 2 logarithm
of a number (leaving in the middle what should be returned if the number
is not a power of 2) then you have your problem solved. For instance:

      answer = table[log2(n)];

(You probably have to check the range of the function before using it).

This scheme is also useful as an alternative to switch cases; you could do
something like:

some_type firstf(), secondf(), thirdf(); 

some_type (*funcarr[])() = { firstf, secondf, thirdf };

some_type some_var;

    /* now use it ... */

    some_var = (*funcarr[log2(n)])(param);

especially when you have much code in the switch cases, this can be a real
nice alternative.

Consider a single key command editor; the mapping function degenerates
to a simple cast to unsigned char (if the input character value hadn't
already this type) - possibly preceded by a simple range check -, the
array of functions are the diverse actions to be performed for each input
(including 'invalid key' actions: sounding the bell), a parameter is
the 'current count' for instance, the return value is a boolean indicated
whether there was a problem.
Another nice example is a disassembler that uses the (first part of
the) opcode to index into an array of functions that handle moving,
shifting, logic, control flow, arithmetic etc. I've done that for a 68K
disassembler (hint:  use the first 4 bits of the instruction word to
index into an array of 16 functions).

As for the log2 function itself, some suggestion has already been made.

       Leo.



More information about the Comp.lang.c mailing list