开发者

How can I check to see if a element of an array exists in Ada

How can I check the to see if an element of an array exists in Ada. Also is there any good documented site for Ada like python's or php's documentation sites, so that I can search for all the types of functions and it's uses. I am not able to find more information in google for some types of functions in Ada.

soldiers : array (0..max_number_of_soldiers - 1) o开发者_运维问答f soldier_type;
procedure Next (Index: in out Integer; Interval: Positive) is
     begin
        for I in 1..Interval loop
           loop
             Index := (Index + 1) mod Number_Of_Soldiers;
             exit when Soldiers(Index).Alive;
           end loop;
        end loop;
     end Next;

what does Soldiers(Index).Alive shows ? What is .alive ?


"what does Soldiers(Index).Alive shows ? what is .alive ?"

The exact type definition of soldier_type is missing, but apparently it's got a (boolean) alive field.

There's an array of max_number_of_soldiers soldiers. This code iterates through the array, and exits when it finds a soldier that's alive.

It starts at index Index, and loops through Interval items. If it reaches the end, it wraps and starts at the start of the array.

So, to answer your question, Soldiers(Index).Alive returns whether the solder at index is alive or not.


Arrays in Ada can be declared with any bounds the coder desires, even by a call to a routine (perhaps reading the proper size out from the user at runtime). The bounds can be retrived by the coder using attributes like 'first, 'last, and 'length. By convention arrays in Ada generally do not use terminator sentinal values.

What this all adds up to is that usually every element in an Ada array is assumed to contain a valid value. If there are some reasons why that may not always be true, then it is the coder's responsibility to come up with a scheme to keep track of which elements have good values in them.

In the case of the code you presented, it looks like every element in that array is assumed to have valid values of soldier_type, which apparently is a record type that has a boolean field named Alive. Other than the fact that he uses it to terminate his loop, there isn't much I can tell you about it.

Well, I suppose I can tell you that if no records in that entire array have the Alive flag set, then you've got an infinite loop. That probably isn't what you'd want to happen. Also, if it was me, I'd make the array go from 1..max_number_of_soldiers, just because it reads better. Either there's much more code dealing with that array that looks better when its 0-based, or someone is a C coder and is having trouble letting go of old habits.

For documentation, I'd suggest looking at the sites listed in the Ada tag's wiki. If you have a particular interest in language-defined functions and whatnot, then you should know that entire language reference manual is available online (unlike for many other languages I could name). I'd highly suggest looking over annexes A (Predefined Language Environment), K (Language-Defined Attributes), and L (Language-Defined Pragmas). K in particular should be read thouroughly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜