comp.lang.ada
 help / color / mirror / Atom feed
* Processing array subsections, a newbie question.
@ 2010-06-12 19:11 Peter C. Chapin
  2010-06-12 19:38 ` Yannick Duchêne (Hibou57)
                   ` (4 more replies)
  0 siblings, 5 replies; 28+ messages in thread
From: Peter C. Chapin @ 2010-06-12 19:11 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2010-06-16 19:22 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-12 19:11 Processing array subsections, a newbie question Peter C. Chapin
2010-06-12 19:38 ` Yannick Duchêne (Hibou57)
2010-06-12 19:41 ` Yannick Duchêne (Hibou57)
2010-06-12 20:54 ` Ludovic Brenta
2010-06-13  1:20   ` Gene
2010-06-13 14:01     ` Peter C. Chapin
2010-06-13 15:48       ` Yannick Duchêne (Hibou57)
2010-06-13 16:57       ` Phil Thornley
2010-06-13 18:39         ` Yannick Duchêne (Hibou57)
2010-06-13 19:04           ` Phil Thornley
2010-06-13 18:58         ` Peter C. Chapin
2010-06-13  6:28   ` Niklas Holsti
2010-06-13  6:54     ` Jeffrey R. Carter
2010-06-16 19:03       ` Niklas Holsti
2010-06-16 19:22       ` Ludovic Brenta
2010-06-13 14:09     ` Peter C. Chapin
2010-06-13 11:00 ` Stephen Leake
2010-06-13 11:04   ` Simon Wright
2010-06-14  1:45     ` Stephen Leake
2010-06-14 18:23 ` Colin Paul Gloster
2010-06-14 19:41   ` Simon Wright
2010-06-14 23:54     ` Peter C. Chapin
2010-06-15  3:28       ` Jeffrey R. Carter
2010-06-15  6:13       ` Simon Wright
2010-06-15 11:24         ` Peter C. Chapin
2010-06-15  9:45       ` Phil Thornley
2010-06-15 11:27         ` Peter C. Chapin
2010-06-15 12:11           ` Yannick Duchêne (Hibou57)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox