Examples
* Branch table *Alternatives
A multiway branch can, frequently, be replaced with an efficient indexed table lookup (using the data value itself or a calculated derivative of the data value, as the index of an array)"''...the implementation of a switch statement has been equated with that of a multiway branch. However, for many uses of the switch statement in real code, it is possible to avoid branching altogether and replace the switch with one or more table look-ups. For example, theHas30Days
example resented earliercan be implemented as the following: example''"A Superoptimizer Analysis of Multiway Branch Code Generation"
by Roger Anthony Sayleswitch (x) can be replaced, using a "safe-hashing" technique, with -unsigned int t = x , 2; switch (t) or it can be replaced, using anindex mapping Index mapping (or direct addressing, or a trivial hash function) in computer science describes using an array data structure, array, in which each position corresponds to a key in the Universe (mathematics), universe of possible values. The techniq ...table lookup, with -x %= 12; /* to ensure x is in range 0-11*/ static const int T 2= ; /* 0-based table 'if 30 days =1,else 0' */ return T /* return with boolean 1 = true, 0=false */ (in view of the simplicity of the latter case, it would be preferable to implement it in-line, since the overhead of using a function call may be greater than the indexed lookup itself.)
Quotations
See also
* Conditional (programming) * Lookup table
References
{{Reflist
External links
Coding Multiway Branches Using Customized Hash functions
by H. G. Dietz
Learning Python
By Mark Lutz
Programming in C++
By Nell B. Dale, Chip Weems
A Superoptimizer Analysis of Multiway Branch Code Generation
by Roger Anthony Sayle Conditional constructs