Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old June 20th, 2008, 08:40 AM
Reinert Korsnes
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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


Reply With Quote
  #2  
Old June 20th, 2008, 12:20 PM
Stefan Bellon
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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


Reply With Quote
  #3  
Old June 20th, 2008, 05:01 PM
Reinert Korsnes
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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



Reply With Quote
  #4  
Old June 23rd, 2008, 06:20 AM
Stefan Bellon
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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


Reply With Quote
  #5  
Old June 23rd, 2008, 12:40 PM
Matthew Heaney
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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;

Reply With Quote
  #6  
Old June 23rd, 2008, 12:40 PM
Matthew Heaney
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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;



Reply With Quote
Reply

Viewing: Web Development Archives FAQs Programming > How to loop (elegant) throug a vector ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway