> In theory OO programming never needs a switch statement.
No, but it can be written and compiled a lot more efficiently using a
switch statement. It need not take O(n/2) comparisons to switch, either.
It is possible to take O(log n) comparisons:
case foo of
0..3: blarg(1);
4, 11: blarg(2);
5: blarg(3);
6-8: blarg(4);
9: blarg(5);
10: blarg(6);
end;
...can be compiled as:
if foo >= 6 then goto 9;
0:
if foo > 3 then goto 4;
blarg(1);
4:
if foo = 5 then goto 5;
blarg(2);
goto end;
5:
blarg(3);
goto end;
6:
blarg(4);
goto end;
9:
if foo < 9 then goto 6;
if foo >= 10 then goto 10;
blarg(5);
goto end;
10:
if blarg = 11 then goto 4;
blarg(6);
end:
...effectively performing a binary search for the number you want.
This sort of binary-select is not difficult to do automatically - you
just have to look out for multiple disjoint ranges (like 4,11) that
share blocks of code. Does anyone know if any compilers actually
implement this method?
- Matt
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz