<
hughag...@yahoo.com> wrote in message
news:4de58c99-d87e-4851...@googlegroups.com...
> [...] Whenever I see programs featuring a big CASE, they are
> almost always C programs that have been ported to Forth
> and/or they were written by recalcitrant C programmers [...]
> --- the SWITCH statement is the foundation of C programming,
> where it is common to see a single SWITCH statement spanning
> hundreds of lines of source-code.
Well, it's definately not "the foundation" of C programming. Switches are
not as common in C code as other control-flow. However, it's definately
true some C programmers seem to embrace C's switch() much more than they
ever should. I see excessively switched programs from time to time too.
I also see programmers embrace C's for() too much. etc.
FYI, ANSI C89/90 allows 257 and ISO C99 allows 1023 items in a switch.
So, switches were designed to allow easy selection of a relatively "large"
number of items (at the time).
Personally, I think it's far more common to see a switch() with just a
handful of items, followed by maybe 20 to 50 items. From what I've seen,
the truly large switches are used with a large input set of non-contiguous
integer values. If the set of input integer values is contiguous, you don't
need a switch. You can use an array.
The largest switch I have in C has 356 items. Why so many? Well, there are
356 different valid input text strings. The switch is given an integer from
a hash function on the input string. This produces 356 different 32-bit
integers. The integer values are not adjacent to each other. There are no
hash collisions on that set of strings. That makes the switch() ideal.
Otherwise, you're using many if's.
That large switch is relatively recent. Most of mine are much smaller.
What I think is my next largest switch has 100 items. That's still many.
It too is tasked with separating strings from each other. It uses a simple
hash function which has a few collisions that must be corrected.
Another option in C is similar, switch based on the first character of the
string which reduces you to 26 or 52 switch items, but then you need to do a
bunch of string compares using if's, e.g., if you switched on 'r', you still
need to separate "return" from "register". 356/26=14. So, if the strings
were evenly distributed, which they aren't, you'd need an average of 14 if's
per case. Obviously, that means you're likely to see one case with 28 or
similar larger set of strings ... That would encourage using a hash
function in the first place.
The "worst" option in C is to loop through a string array and compare every
string until the one you're looking for is found. I.e., like searching the
dictionary in a simple interpreted Forth. The loop is to equate the string
to a sequential integer index, i.e., one value from a contiguous set of
integer values.
The need to determine one string from another and take action for a specific
string is needed for parsers and interpreters.
How would you handle code selection for 356 different input strings in
Forth?
Rod Pemberton