Maps in Ada

Our example showcases the use of maps, associative data types for storing key/value pairs. Here is how you would implement similar functionality in Ada.

Ada Code Example:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Indefinite_Hashed_Maps;

procedure Main is

   package String_To_Integer_Map is new Ada.Containers.Indefinite_Hashed_Maps
     (Key_Type        => String,
      Element_Type    => Integer,
      Hash            => Ada.Strings.Hash,
      "="             => Ada.Strings.Equal);

   M : String_To_Integer_Map.Map := String_To_Integer_Map.Empty_Map;

begin
   -- Set key/value pairs
   String_To_Integer_Map.Insert (M, Key => "k1", New_Item => 7);
   String_To_Integer_Map.Insert (M, Key => "k2", New_Item => 13);
   
   -- Print the map size
   Put_Line ("len: " & Integer'Image (String_To_Integer_Map.Length (M)));

   -- Get value for a key and print it
   declare
      V1 : Integer := String_To_Integer_Map.Element (M, "k1");
   begin
      Put_Line ("v1: " & Integer'Image (V1));
   end;
   
   -- Getting a value for a non-existent key returns the default value
   declare
      V3 : Integer := String_To_Integer_Map.Element (M, "k3");
   begin
      Put_Line ("v3: " & Integer'Image (V3));
   end;

   -- Deletes key/value pair
   String_To_Integer_Map.Delete (M, "k2");
   
   -- Print the map size after deletion
   Put_Line ("len after delete: " & Integer'Image (String_To_Integer_Map.Length (M)));

   -- Clear all items from the map
   String_To_Integer_Map.Clear (M);

   -- Print the map size after clearing
   Put_Line ("len after clear: " & Integer'Image (String_To_Integer_Map.Length (M)));

   -- Check if a key exists in the map
   declare
      Found : Boolean := String_To_Integer_Map.Contains (M, "k2");
   begin
      Put_Line ("prs: " & Boolean'Image (Found));
   end;
   
end Main;

Explanation:

To create an empty map, we instantiate a map type using Ada’s container library:

package String_To_Integer_Map is new Ada.Containers.Indefinite_Hashed_Maps
  (Key_Type        => String,
   Element_Type    => Integer,
   Hash            => Ada.Strings.Hash,
   "="             => Ada.Strings.Equal);

M : String_To_Integer_Map.Map := String_To_Integer_Map.Empty_Map;

Set key/value pairs with Insert method:

String_To_Integer_Map.Insert (M, Key => "k1", New_Item => 7);
String_To_Integer_Map.Insert (M, Key => "k2", New_Item => 13);

Retrieve and print values:

declare
   V1 : Integer := String_To_Integer_Map.Element (M, "k1");
begin
   Put_Line ("v1: " & Integer'Image (V1));
end;

If the key doesn’t exist, the map will raise a Key_Error, usually caught and handled:

declare
   V3 : Integer := String_To_Integer_Map.Element (M, "k3");
begin
   Put_Line ("v3: " & Integer'Image (V3));
exception
   when String_To_Integer_Map.Key_Error =>
      Put_Line ("v3: 0"); -- Assuming 0 is the default value
end;

The Length function returns the number of key/value pairs:

Put_Line ("len: " & Integer'Image (String_To_Integer_Map.Length (M)));

Use Delete to remove a key/value pair:

String_To_Integer_Map.Delete (M, "k2");

The Clear method removes all key/value pairs:

String_To_Integer_Map.Clear (M);

Check if a key is present:

declare
   Found : Boolean := String_To_Integer_Map.Contains (M, "k2");
begin
   Put_Line ("prs: " & Boolean'Image (Found));
end;

This demonstrates how you can perform similar operations on maps in Ada, including insertion, retrieval, deletion, checking the existence of keys, and clearing all entries, using Ada’s standard library for associative containers.