Actually, it can be easily done as a 2 step operation with the use of a
lookup table of addresses.

0: address of (0)
1:   "
2:   "
3:   "
4: address if (2)
etc.

Then do Jump (lookup(foo)).  You get the picture.

However, the limitations of this and other switch statement comes when you
add a new case to your business requirements.  *Chances* are you will need
to fix up lots of switch statements and lookup tables.  With OO you
add/register another class to your factory ... trivial.

Dennis.

> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
> Behalf Of Matt Powell
> Sent: Wednesday, 16 February 2000 10:39
> To: Multiple recipients of list delphi
> Subject: Re: [DUG]: VB Like Case statement
>
>
> > 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
>

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to