tags:

views:

91

answers:

4

I have to do a table lookup to translate from input A to output A'. I have a function with input A which should return A'. Using databases or flat files are not possible for certain reasons. I have to hardcode the lookup in the program itself.

What would be the the most optimum (*space-wise and time-wise separately): Using a hashmap, with A as the key and A' as the value, or use switch case statements in the function?

The table is a string to string lookup with a size of about 60 entries.

+1  A: 

I believe that both the switch and the table-look up will be equivalent (although one should do some tests on the compiler being used). A modern C compiler will implement a big switch with a look-up table. The table look-up can be created more easily with a macro or a scripting language.

For both solutions the input A must be an integer. If this is not the case, one solution will be to implement a huge if-else statement.

If you have strings you can create two arrays - one for input and one for output (this will be inefficient if they aren't of the same size). Then you need to iterate the contents of the input array to find a match. Based on the index you find, you return the corresponding output string.

kgiannakakis
Why two arrays? Why not an array of pairs or similar structures? And iteration will be very slow.
anon
You are right about the iteration, it will be slow. Hashing the strings and sorting them will be faster.
kgiannakakis
+6  A: 

If speed is ultra ultra necessary, then I would consider perfect hashing. Otherwise I'd use an array/vector of string to string pairs, created statically in sort order and use binary search. I'd also write a small test program to check the speed and memory constraints were met.

anon
Thanks. But would switch-case implementation take up lesser memory since it wouldn't use a separate data-structure?
trex279
@trex279 You can't switch on strings. And it won't take up any less memory, plus or minus a few bytes.
anon
`gperf` is for sissies. Do it like Knuth: approach the problem like an interesting puzzle and create a perfect hashing function manually, for the fun of it. ;-)
Konrad Rudolph
A: 

Make a key that is fast to calculate, and hash

If the table is pretty static, unlikely to change in future, you could have a look-see if adding a few selected chars (with fix indexes) in the "key" string could get unique values (value K). From those insert the "value" strings into a hash_table by using the pre-calculated "K" value for each "key" string.

epatel
A: 

Although a hash method is fast, there is still the possibility of collision (two inputs generating the same hash value). A fast method depends on the data type of the input.

For integral types, the fastest table lookup method is an array. Use the incoming datum as an index into the array. One of the problems with this method is that the array must account for the entire spectrum of values for the fastest speed. Otherwise execution is slowed down by translating the original index into an index for the array (kind of like a hashing method).

For string input types, a nested look up may be the fastest. One example is to break up tables by length. The first array returns pointers to the table to search based on length, e.g. char * sub_table = First_Array[5] for a string of length 5. These can be configured for specialized input data.

Another method is to use a B-Tree, which is a binary tree of "pages". Behavior is similar to nested arrays.

If you let us know the input type, we can better answer your question.

Thomas Matthews