|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to loop (elegant) throug a vector ?
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 |
|
#2
|
|||
|
|||
|
Returning ranges from a function (was: How to loop (elegant) througa vector ?)
Fri, 20 Jun, Reinert Korsnes wrote:
-- I want something more beautiful than this loop: > for k in 1 Positive(X.Length) loop do_something(X.Element(k)); end loop; Sorry to high-jack this thread, but I'd like to comment on a somewhat related topic: For a very long time I was looking for some way to return a range from a function so that you can do something elegant like in the following: for I in Get_Range loop Do_Something (I); end loop; I always thought it to be a deficiency of Ada not being able to return a range from a function. A few weeks ago I realised the following solution, which I'd like to share: generic type Index_Type is (<>); package Generic_R is type Empty_Component is null record; for Empty_Component'Size use 0; type Range_Type is array (Index_Type range <>) of Empty_Component; pragma Pack (Range_Type); end Generic_R; And an instance for Integer ranges with Generic_R; package Integer_R is new Generic_R (Integer); Now you can do the following: function Get_Range return Integer_RRange_Type is begin return (42 128 =<>); end Get_Range; And then: for I in Get_Range'Range loop Do_Something (I); end loop; It's not as elegant as I wished (because of the additional 'Range), but it is simple and light-weight enough to be usable. -- Stefan Bellon |
|
#3
|
|||
|
|||
|
How to loop (elegant) throug a vector ?
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 |
|
#4
|
|||
|
|||
|
Returning ranges from a function (was: How to loop (elegant)throug a vector ?)
Sun, 22 Jun, Maciej Sobczak wrote:
What about "independent addressability" from 13.3? The text is a bit dense, but I conclude that the 'Size attribute does not have to be strictly obeyed by the implementation. I don't think this applies here as the array components are not aliased and thus there is no need for independent addressability. Perhaps the compiler should allocate memory for each component as soon as "aliased" is specified. But in any case my "range objects" do not rely on the compiler not allocating memory. This is just a nice and efficient side-effect of the compiler implementation. So, even if the compiler implementation does not have to optimise, the semantics of my "range objects" is guaranteed and for the GNAT implementation it is even optimal concerning memory. -- Stefan Bellon |
|
#5
|
|||
|
|||
|
How to loop (elegant) throug a vector ?
Jun 20, 9:19*am, Reinert Korsnes <a@b.nowrote:
* *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; |
|
#6
|
|||
|
|||
|
How to loop (elegant) throug a vector ?
Jun 20, 3:48*pm, Reinert Korsnes <a@b.nowrote:
> 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. Right. The first element has index Index_Type'First. You could also say: for K in X.First_Element X.Last_Element loop Query_Element (X, K, Do_Something'Access); end loop; |
![]() |
| Viewing: Web Development Archives > FAQs > Programming > How to loop (elegant) throug a vector ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|