# Ada Programming/Containers

This language feature is only available from Ada 2005 on.
Containers is an Ada (2005 or later) specification that provides facilities for storing collections of elements. There are several types of containers defined as child packages of Ada.Containers, each one with different features. All container packages are generic units, thus it is recommended to read Generics section of this Wikibook.
The following table list all containers available in the Containers package. The array type is provided by the language itself and is also listed for comparative purposes. The table describes the following features: the package or the container name; if the elements are associated to a specific index; if the elements are stored in a specific order.
Types of Ada Containers :
| Package/Name | Indexed | Ordered |
| array | By scalar type | No |
| Ada.Containers.Vectors | By scalar type | No |
| Ada.Containers.Doubly_Linked_Lists | No | No |
| Maps |||
| Ada.Containers.Hashed_Maps | By key type | No |
| Ada.Containers.Ordered_Maps | By key type | Yes |
| Sets |||
| Ada.Containers.Hashed_Sets | No | No |
| Ada.Containers.Ordered_Sets | No | Yes |
| Trees |||
| Ada.Containers.Multiway_Trees |
| Indefinites |||
| Ada.Containers.Indefinite_Vectors | By scalar type | No |
| Ada.Containers.Indefinite_Doubly_Linked_Lists | No | No |
| Ada.Containers.Indefinite_Hashed_Maps |
| Ada.Containers.Indefinite_Ordered_Maps | By key type | Yes |
| Ada.Containers.Indefinite_Hashed_Sets | No | No |
| Ada.Containers.Indefinite_Ordered_Sets | No | Yes |
| Ada.Containers.Indefinite_Multiway_Trees |
| Ada.Containers.Indefinite_Holders |
| Bounded |
| Ada.Containers.Bounded_Vectors | By scalar type | No |
| Ada.Containers.Bounded_Doubly_Linked_Lists | No | No |
| Ada.Containers.Bounded_Hashed_Maps | By key type | No |
| Ada.Containers.Bounded_Ordered_Maps | By key type | Yes |
| Ada.Containers.Bounded_Hashed_Sets | No | No |
| Ada.Containers.Bounded_Ordered_Sets | No | Yes |
| Ada.Containers.Bounded_Multiway_Trees |
| Array Sorting |||
| Ada.Containers.Synchronized_Queue_Interfaces |
| Ada.Containers.Unbounded_Synchronized_Queues |
| Ada.Containers.Bounded_Synchronized_Queues |
| Ada.Containers.Unbounded_Priority_Queues |
| Ada.Containers.Bounded_Priority_Queues |

## Vectors

A vector container is similar to an array. The difference is that the vector can expand automatically its capacity in run-time. It has the following features:
* It can contain several elements of only one specific type.In order to store elements of different types in a container, a new type must be created to support each of the types. For instance, by using an variant record where each component store an element of each type.
* The index must be of scalar type. It can be a subtype defined by the user.
* The capacity is expanded on run-time.
* An element contained can be accessed in any indexed position (random access).
* An element can be inserted or deleted in any indexed position.
** Specifically optimized for insertion/deletion at the high end.
* A Cursor type can be used to access the contained elements.
* A user-defined procedure can be used to iterate (and operate on) the contained elements.
* Find functions are provided.
* There is an "empty element" (a No_Element constant) for positions without an element assigned.
It is important to note that length and capacity are not the same. Length is the current amount of elements the containers possess. Its capacity is the maximum amount of elements it can currently contain. Once the capacity is exceeded, the vector expands it automatically.

### Creating a new Vector

To create a new vector, first a new instance package must be created from the generic package Ada.Containers.Vectors. There are three generic parameters: Index_Type which defines the scalar type of the index, Element_Type which is the type of the items to store, and the "=" function used to find and determine if an element is present on the vector. The "=" function can be optional, using the default equal operation of the Element_Type. For instance, the following code creates a new vector package that uses Positive numbers as index, Unbounded_String as elements, and the default "=" from Unbounded_String:
Now, a vector instance can be created in a variable declaration:

### Vector examples

A complete example using the codes shown above can be the following:

## Maps

Maps, sometimes called "associations" or "dictionaries", establish a relation between items of one type with items of another. One item is called the "key", and the associated item is the "value".
One key is limited to be associated to one value. If the developer requires to associate a key to different values, then it must encapsulate these into a data type such as an array, vector, or set. Thus, a key is associated to a container instance, which in turn, it possess all the values.
There are two types of maps: the ordered and hashed maps. The first one uses the function "<" to insert the keys in an order fashion, along with their values. The second one, uses a hash function to create a shorter or simpler hash value from the key. The hash value must be the same for the same key.
When inserting the same key twice with Insert, a Constraint_Error is raised. Instead, use the Replace procedure, which removes the last element associated to the key, and store the new one.

### Map creation

To create an ordered map, the generic Ada.Containers.Ordered_Maps package requires the following parameters: Key_Type, Element_Type, the "<" function for the key, and "=" function for the element. If the last two paramaters are not provided, then the default are used for the corresponding type.
To create a hashed map package, the generic Ada.Containers.Hashed_Maps package requires the following parameters: Key_Type, Element_Type, Hash, Equivalent_Keys.

### Map examples

A complete ordered map example using the code above.
A complete hashed map example.

## Sets

The set containers store elements of a specific type, but avoid storing duplicated elements. Therefore, it uses an equivalent function or operation of the element type to identify if it is already contained.
* There is no index.
* The capacity is expanded on run-time.
* An element contained can be accessed with the Cursor instance.
* An element can be inserted or removed from the set. No index is used in this procedures.
* A user-defined procedure can be used to iterate (and operate on) the contained elements.
* Find and contains functions are provided.
* Empty_Set and No_Element are provided. The latter is used for the Cursor instance as initial value if an element was not assigned to it.
There are two types of sets: the ordered set, and the hashed set. The orderer set maintain its elements sorted in the container. While inserting new elemnets, it search for the proper place with the "<" operation.
In any case, inserting the same element into a set without checking its existence previously will raise a Constraint_Error exception.

### Creating a new set

Similarly as vectors, the Ada.Containers.Ordered_Sets is a generic package. The parameters varies depending on the set used. The ordered set defines the Element_Type parameter, the "<" function to sort the elements, and the "=" function to detect if an element is already present. The hashed set defines the Element_Type, the hash function, the Equivalent_Elements function, and the "=" function.
The code to create an instace of an ordered set and hashed set is:

### Set examples

The following example is the complete code that uses an ordered set. After executing the program, the output is all the phrases sorted alphabetically.
A hashed set complete example. The output in this case is not sorted.

## Iterating

Sometimes it is required to execute code on all elements of a container. There are several ways to do this:
* By using for ... of This language feature has been introduced in Ada 2012..
* By retrieving a Cursor instance from the container and using the Next and Previous procedures on it.
* By defining a procedure with the code, and pass it to the container with the Iterate procedure.

### Iterating with for-of loops

This language feature has been introduced in Ada 2012.
Arrays and containers can be iterated with for-of loops.
The following example is a simple program to sort city names alphabetically. First, it insert city names to an Ordered_Set instance. This type of set maintains the order of the items contained, therefore internally, each element is inserted in the proper place. Second, it uses the for-of loop to print the city name on screen.
The Ordered_Set uses two functions named "<" and "=". When not provided, it uses the functions with the same name specified by the Element_Type. In this case, it uses the one defined for the Unbounded_String type. See Ada.Strings.Unbounded for declarations of these functions.
In this case, the Insert function uses the Ada 2005 style. These lines can be changed to support the Ada 95 style by transcribing them to the Insert (Cities, ...) notation.

## Demos

What follows are demos of some of the container types.

### Maps: Greetings

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.
Here is the program, details will be explained later.
The first of the Insert statements is written in an Ada 95 style:
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.

#### A slight variation: picking an element

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.
This time the Element function consumes a Key (Lang) not a Cursor. Actually, it consumes two values, the other value being Greetings, in distinguished receiver notation.

### Vectors and Maps: Bean Counting

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):
The Vectors instance offers a data structure similar to an array that can change its size at run time. It is called Vector. Each bean that is read will be appended to a Bean_Vecs.Vector object.
The following program first calls read_input to fill a buffer with beans. Next, it calls a function that computes the average weight of beans having the same color. This function:
Then the average value is printed for beans of each color and the program stops.
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.
Note how this plain array associates colors with Vectors. The procedure for getting the beans into the right bowls uses the bean color as array index for finding the right bowl (vector).
Everything is in place now.
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.

#### All In Just One Map!

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, "<".
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.

## See also
### Wikibook

* Ada Programming
* Ada Programming/Libraries/Ada.Containers

### Ada 2005 Reference Manual

* A.18.1 The Package Containers (Annotated)