|
Prev: GNAT.Serial_Communications ?
Next: Printing with GtkADA
From: Reinert Korsnes on 20 Jun 2008 09:19 I try to learn to use Ada.Containers. Assume the following code: package X_t is new Vectors(Positive,Item_t); use X_t; X : Vector; -- I want something more beautiful than this loop: for k in 1 .. Positive(X.Length) loop do_something(X.Element(k)); end loop; Could anybody suggest improvements for the three last lines in the code? reinert
From: Pascal Obry on 20 Jun 2008 10:49 Reinert Korsnes a �crit : > Could anybody suggest improvements for the three > last lines in the code? A solution using the Iterate routine: procedure Do_Something (P : X_t.Cursor) is E : constant Item_T := Element (P); begin ... end Do_Something; X.Iterate (Do_Something'Access); Another solution would be to loop using a cursor, something like: P : X_t.Cursor := X.First; while Has_Element (P) loop Do_Something (P); Next (P); -- With this loop it is possible to exit before the end -- exit when <some_condition>; end loop; Both example have not been compiled, so could have some syntax error but you get the idea. Pascal. -- --|------------------------------------------------------ --| Pascal Obry Team-Ada Member --| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE --|------------------------------------------------------ --| http://www.obry.net --| "The best way to travel is by means of imagination" --| --| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
From: Jeffrey R. Carter on 20 Jun 2008 15:21 Reinert Korsnes wrote: > > for k in 1 .. Positive(X.Length) loop > do_something(X.Element(k)); > end loop; > > Could anybody suggest improvements for the three > last lines in the code? for K in 1 .. X.Last_Index loop Do_Something (X.Element (K) ); end loop; Since the correct name for this package is Unbounded_Arrays, it makes sense to iterate over it in a manner to iterating over an array. -- Jeff Carter "C's solution to this [variable-sized array parameters] has real problems, and people who are complaining about safety definitely have a point." Dennis Ritchie 25
From: Reinert Korsnes on 20 Jun 2008 15:48 Jeffrey R. Carter wrote: > Reinert Korsnes wrote: >> >> for k in 1 .. Positive(X.Length) loop >> do_something(X.Element(k)); >> end loop; >> >> Could anybody suggest improvements for the three >> last lines in the code? > > for K in 1 .. X.Last_Index loop > Do_Something (X.Element (K) ); > end loop; > > Since the correct name for this package is Unbounded_Arrays, it makes > sense to iterate over it in a manner to iterating over an array. > Maybe better: for K in X.First_Index .. X.Last_Index loop Do_Something (X.Element (K) ); end loop; ? Just in case someone in the future change the vector indexing type. The first element need not correspond to index = 1. reinert
From: Matthew Heaney on 23 Jun 2008 13:08
On Jun 20, 9:19 am, Reinert Korsnes <a...(a)b.no> wrote: > for k in 1 .. Positive(X.Length) loop > do_something(X.Element(k)); > end loop; > > Could anybody suggest improvements for the three > last lines in the code? for K in X.First_Index .. X.Last_Index loop Do_Something (X.Element (K)); end loop; |