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,effb80d4bb7716dd X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: Wanted: Ada STL. Reward: Ada's Future Date: 1999/02/03 Message-ID: #1/1 X-Deja-AN: 440250420 Sender: matt@mheaney.ni.net References: <790f4q$3l@bgtnsc01.worldnet.att.net> <36B856E4.D921C1D@bton.ac.uk> NNTP-Posting-Date: Wed, 03 Feb 1999 09:34:35 PDT Newsgroups: comp.lang.ada Date: 1999-02-03T00:00:00+00:00 List-Id: John English writes: > In Ada this isn't possible -- e.g. type Foo is array(Boolean) of Bar > has no "beyond the end" index that can be used to form non-inclusive > ranges [i,j). This makes it hard to support arrays as "just another > collection type" in the way that the STL does, so you can't easily > define algorithms which can be applied impartially to arrays or > vectors or maps or whatever. The problem in your example is your array index. The rule in Ada is that an enumeration type is its own base type, and type Boolean has no other values than True and False. If you need to fall off the beginning or end of an array, you just need to make sure the base type of the index subtype has at least one extra value on either end, something like: type Color_Base is (First, Red, Green, Blue, Last); subtype Color_Type is Color_Base range Color_Base'Succ (First) .. Color_Base'Pred (Last); Now declare your array: type Color_Map is array (Color_Type) of Integer; -- or whatever Map : Color_Map := ...; Index : Color_Base := Color_Map'First; begin loop Index := Color_Base'Succ (Index); exit when Index > Map'Last; end loop; end; Because Index has subtype Color_Base, not Color_Type, you can safely iterate pass the end of the array. What motivated the STL approach is that this is a very common idiom for iteration in C/C++. You iterate pass the end of the array - that's your loop termination condition: for (i = 0; i < len; i++) { ... a[i] ... This scheme works because your array has indices 0 .. len - 1, but the index goes from 0 to len. You can implement the exact same iteration scheme in Ada, using the technique I've shown above: declare a base type with an extra value on the end, and then declare a subtype without the extra value. Of course, there's no way to do this sort of thing using type Boolean, because Boolean is its own base type. That being said, the technique I've shown above is only rarely needed in Ada, because standard loop idioms are different. In Ada, I'd only need to do this: for Index in Map'Range loop end loop; If this looks simpler than my earlier formulation, that's because it's simpler. Loop iteration in Ada is less error prone than in C. This is yet another reason why I think that looking at the STL as a model for a standard Ada library is dangerous and misleading. The idioms in both languages are completely different. So forget about the STL as a model for an Ada library.