From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fcf8ea94b94d6941 X-Google-Attributes: gid103376,public From: Ole-Hjalmar Kristensen Subject: Re: Modular type. What is it and why? Date: 1999/04/08 Message-ID: #1/1 X-Deja-AN: 463976126 X-NNTP-Posting-Host: fwall.clustra.com References: <7ean3c$79m$1@eol.dd.chalmers.se> <7eb2iq$jc3@hobbes.crc.com> <7ede2p$pd1$1@its.hooked.net> <370A4B3A.5EDB3B37@pwfl.com> <7edk6p$1j7$1@its.hooked.net> <7eftdm$7rn$1@its.hooked.net> Newsgroups: comp.lang.ada X-Complaints-To: abuse@telia.no Date: 1999-04-08T00:00:00+00:00 List-Id: "Mike Silva" writes: > Ole-Hjalmar Kristensen wrote in message ... > >> but you can't do: > >> > >> index = ( index - 1 ) % NUM_ELEMENTS; /* won't handle 0 -> > >> NUM_ELEMENTS - 1 correctly */ > > > >Actually, if index is unsigned, then it *will* handle 0 -> > >NUM_ELEMENTS - 1 correctly. > You're right, of course. I was just thinking about the wrap from 0 to 65535. > No, if you roll e.g. a 16 bit unsigned int from 0 to 65535, the value 65535 > mod N will not in general be N - 1, as we want. (e.g., by inspection, 65535 > mod 10 is 5) > > > >> As Ole-Hjalmar Kristensen commented, the nice Ada solution doesn't work > if > >> NUM_ELEMENTS varies at runtime (at least, it appears that a modular type > >> can't be defined dynamically -- correct?), but that's not the usual case > >> I run across. > > > >Agreed, I just thought I would mention it. > > Actually, the more I thought about it the more the static limitation on > modular types makes my idea worthless. Even if NUM_ELEMENTS is static for > each menu, I'd want the same routine to handle multiple menus, each with a > different NUM_ELEMENTS. Oh well... > > Mike > > > > I suppose you could make the routine generic? Then it will work if NUM_ELMENTS is known at compile time. -- E pluribus Unix