[Ada] How do I use built-in data structures like dictionaries or maps in Ada?

In Ada, the built-in data structure that is equivalent to dictionaries or maps in other programming languages is called a "package" and it is called Ada.Containers. This package provides different container types, such as Ada.Containers.Hashed_Maps and Ada.Containers.Ordered_Maps, which can serve as dictionaries or maps depending on your requirements.

Here is an example of how to use Ada.Containers.Hashed_Maps:

 1with Ada.Containers.Hashed_Maps;
 2with Ada.Text_IO;
 4procedure Dictionary_Example is
 5   -- Use the generic package Ada.Containers.Hashed_Maps
 6   package My_Dictionary is new Ada.Containers.Hashed_Maps
 7     (Key_Type   => String, -- Key type is String
 8      Element_Type => Integer); -- Element type is Integer
10   My_Map : My_Dictionary.Map;
13   -- Insert key-value pairs into the map
14   My_Map.Insert ("Alice", 25);
15   My_Map.Insert ("Bob", 30);
16   My_Map.Insert ("Charlie", 35);
18   -- Access and print the values
19   Ada.Text_IO.Put_Line ("Alice's age: " & Integer'Image (My_Map.Element("Alice")));
20   Ada.Text_IO.Put_Line ("Bob's age: " & Integer'Image (My_Map.Element("Bob")));
21   Ada.Text_IO.Put_Line ("Charlie's age: " & Integer'Image (My_Map.Element("Charlie")));
23   -- Check if a key exists in the map
24   if My_Map.Exists("Alice") then
25      Ada.Text_IO.Put_Line ("Alice is in the map");
26   end if;
28   -- Remove a key-value pair from the map
29   My_Map.Delete("Bob");
31   -- Print the remaining key-value pairs
32   for Pair of My_Map.Iterate loop
33      Ada.Text_IO.Put_Line (Pair.Key & "'s age: " & Integer'Image (Pair.Item));
34   end loop;
35end Dictionary_Example;

In this example, we create a map called My_Map using Ada.Containers.Hashed_Maps. We insert key-value pairs into the map using the Insert procedure. We access the values using the Element function and print them using Ada.Text_IO.Put_Line. We can check if a key exists using the Exists function. We can remove a key-value pair using the Delete procedure. Finally, we iterate over the remaining key-value pairs using a loop and print them.

You can also use Ada.Containers.Ordered_Maps if you need to maintain the order of the key-value pairs based on the keys. The usage is similar, but the insertion order will be preserved.

Remember to add the Ada.Containers library to your project file or command-line arguments to compile and run the code successfully.