The program below prints greetings to the world in a number of human languages. The greetings are stored in a table, or hashed map. The map associates every greeting (a value) with a language code (a key). That is, you can use language codes as keys to find greeting values in the table.

The elements in the map are constant strings of international characters, or really, pointers to such constant strings. A package Regional is used to set up both the language IDs and an instance of Ada.Containers.Hashed_Maps.

The next insertions use so called distinguished receiver notation which you can use in Ada 2005. (It's O-O parlance. While the Insert call involves all of: a Container object (greetings), a Key object (EN), and a New_Item object (new Wide_String'("Hello, World!")), the Container object is distinguished from the others in that the Insert call provides it (and only it) with the other objects. In this case the Container object will be modified by the call, using arguments named Key and New_Item for the modification.)

After the table is set up, the program goes on to print all the greetings contained in the table. It does so employing a cursor that runs along the elements in the table in some order. The typical scheme is to obtain a cursor, here using First, and then to iterate the following calls:

Has_Element, for checking whether the cursor is at an element

Element, to get the element and

Next, to move the cursor to another element

When there is no more element left, the cursor will have the special value No_Element. Actually, this is an iteration scheme that can be used with all containers in child packages of Ada.Containers.

The next program shows how to pick a value from the map, given a key. Actually, you will provide the key. The program is like the previous one, except that it doesn't just print all the elements in the map, but picks one based on a Language_ID value that it reads from standard input.

Let's take bean counting literally. Red beans, green beans, and white beans. (Yes, white beans really do exist.) Your job will be to collect a number of beans, weigh them, and then determine the average weight of red, green, and white beans, respectively. Here is one approach.

Again, we need a package, this time for storing vegetable related information. Introducing the Beans package (the Grams type doesn't belong in a vegetable package, but it's there to keep things simple):

All container operations take place in function average_weight. To find the mean weight of beans of the same color, the function is looking at all beans in order. If a bean has the right color, average_weight adds its weight to the total weight, and increases the number of beans counted by 1.

The computation visits all beans. The iteration that is necessary for going from one bean to the next and then performing the above steps is best left to the Iterate procedure which is part of all container packages. To do so, wrap the above steps inside some procedure and pass this procedure to Iterate. The effect is that Iterate calls your procedure for each element in the vector, passing a cursor value to your procedure, one for each element.

Having the container machinery do the iteration can also be faster than moving and checking the cursor yourself, as was done in the Hello_World_Extended example.

This approach is straightforward. However, imagine larger vectors. average_weight will visit all elements repeatedly for each color. If there are M colors and N beans, average_weight will be called M * N times, and with each new color, N more calls are necessary. A possible alternative is to collect all information about a bean once it is visited. However, this will likely need more variables, and you will have to find a way to return more than one result (one average for each color), etc. Try it!

A different approach might be better. One is to copy beans of different colors to separate vector objects. (Remembering Cinderella.) Then average_weight must visit each element only one time. The following procedure does this, using a new type from Beans, called Bean_Pots.

with Beans;with average_weight;with gather_into_pots;with Ada.Wide_Text_IO;procedure bean_counting isuse Beans, Ada;
buffer: Bean_Vecs.Vector;
bowls: Bean_Pots;procedure read_input(buf:inout Bean_Vecs.Vector)isseparate;-- collect information from a series of bean measurements into `buf`begin-- bean_counting
read_input(buffer);-- now everything is set up for computing some statistical data.-- Gather the beans into the right pot by color.-- Then find the average weight of beans in each pot.
gather_into_pots(buffer, bowls);for color in Bean_Color loop
Wide_Text_IO.Put_Line
(Bean_Color'Wide_Image(color)
& " ø ="
& Grams'Wide_Image(average_weight(bowls(color), color)));endloop;end bean_counting;

As a side effect of having chosen one vector per color, we can determine the number of beans in each vector by calling the Length function. But average_weight, too, computes the number of elements in the vector. Hence, a summing function might replace average_weight here.

The following program first calls read_input to fill a buffer with beans. Then, information about these beans is stored in a table, mapping bean properties to numbers of occurrence. The processing that starts at Iterate uses chained procedure calls typical of the Ada.Containers iteration mechanism.

The Beans package in this example instantiates another generic library unit, Ada.Containers.Ordered_Maps. Where the Ada.Containers.Hashed_Maps require a hashing function, Ada.Containers.Ordered_Maps require a comparison function. We provide one, "<", which sorts beans first by color, then by weight. It will automatically be associated with the corresponding generic formal function, as its name, "<", matches that of the generic formal function, "<".

...
function "<"(a, b: Bean)return Boolean;-- order beans, first by color, then by weightpackage Bean_Statistics
-- instances will map beans of a particular color and weight to the-- number of times they have been inserted.isnew Ada.Containers.Ordered_Maps
(Element_Type => Natural,
Key_Type => Bean);
...

Where the previous examples have withed subprograms, this variation on bean_counting packs them all as local subprograms.

Like in the greetings example, you can pick values from the table. This time the values tell the number of occurrences of beans with certain properties. The stats_cw table is ordered by key, that is by bean properties. Given particular properties, you can use the Floor and Ceiling functions to approximate the bean in the table that most closely matches the desired properties.

It is now easy to print a histogram showing the frequency with which each kind of bean has occurred. If N is the number of beans of a kind, then print N characters on a line, or draw a graphical bar of length N, etc. A histogram showing the number of beans per color can be drawn after computing the sum of beans of this color, using groups like in the previous example. You can delete beans of a color from the table using the same technique.

Finally, think of marshalling the beans in order starting at the least frequently occurring kind. That is, construct a vector appending first beans that have occurred just once, followed by beans that have occurred twice, if any, and so on. Starting from the table is possible, but be sure to have a look at the sorting functions of Ada.Containers.