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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,da5197b9dca0ed40,start X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!novia!news-out.readnews.com!postnews3.readnews.com!not-for-mail Message-Id: <4c13db30$0$2391$4d3efbfe@news.sover.net> From: "Peter C. Chapin" Subject: Processing array subsections, a newbie question. Newsgroups: comp.lang.ada Date: Sat, 12 Jun 2010 15:11:33 -0400 User-Agent: KNode/0.10.9 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit Organization: SoVerNet (sover.net) NNTP-Posting-Host: b3028cd5.news.sover.net X-Trace: DXC=9g_B4im^7IKLG@jU:39aJFK6_LM2JZB_C\d4Rg: This may seem like an elementary question but I'm not too proud to ask it anyway. :) I'm looking for the "cleanest" way to process sequential subsections of an array. To illustrate what I mean consider type Buffer_Type is array(Natural range <>) of Some_Type; The choice of Natural as an index type is perhaps not perfect, but the problem I'm talking about here remains even if a more constrained subtype is used. Now suppose I want to write a procedure that performs an operation on a subsection of a Buffer_Type object. Ultimately the code needs to be SPARKable so I can't use slices (true?). Let's say I do something like procedure Do_Something (Buffer : in Buffer_Type; Fst : in Natural; Lst : out Natural; Ok : out Boolean); Here 'Fst' is intended to be a valid index into Buffer where I want to start my processing. The processing will look over some number of Buffer's elements, but the precise number won't be known until run time. The procedure writes into 'Lst' the highest index value that it considers. The procedure is careful not to access the Buffer out of bounds and it also does some other checks on the validity of the data it finds. It writes 'True' or 'False' into Ok depending on how happy it is. Note that in real life this procedure does other things too... but that's not important here. I want to invoke this procedure repeatedly on a particular buffer such that each invocation picks up where the previous invocation left off. For example Ok : Boolean := True; Index_Fst : Natural := Buffer'First; Index_Lst : Natural; ... while Ok and Index_Fst <= Buffer'Last loop Do_Something(Buffer, Index_Fst, Index_Lst, Ok); Index_Fst := Index_Lst + 1; end loop; The problem with this code is that the assignment to Index_Fst inside the loop might raise Constraint_Error if Index_Lst = Buffer'Last after Do_Something finishes. I can work around this problem but my solutions tend to be rather ungainly looking. Surely there must be an easy way to handle this. Peter