Examples
*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"''...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
exampleresented earlier Resentment (also called ranklement or bitterness) is a complex, multilayered emotion that has been described as a mixture of disappointment, disgust and anger. Other psychologists consider it a mood or as a secondary emotion (including cognitiv ...can 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, in which each position corresponds to a key in the universe of possible values. The technique is most effective when the universe of keys ...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) In computer science, conditionals (that is, conditional statements, conditional expressions and conditional constructs,) are programming language commands for handling decisions. Specifically, conditionals perform different computations or act ...*Lookup table In computer science, a lookup table (LUT) is an array that replaces runtime computation with a simpler array indexing operation. The process is termed as "direct addressing" and LUTs differ from hash tables in a way that, to retrieve a value v w ...
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