--  Ada's bounded containers at run time: each family's operations, in Ada's names.
with Ada.Containers.Bounded_Doubly_Linked_Lists;
with Ada.Containers.Bounded_Hashed_Maps;
with Ada.Containers.Bounded_Hashed_Sets;
with Ada.Containers.Bounded_Ordered_Maps;
with Ada.Containers.Bounded_Ordered_Sets;
with Ada.Containers.Bounded_Vectors;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   use type Ada.Containers.Count_Type;
   type Index is range 0 .. 9;
   function Hash (C : Character) return Ada.Containers.Hash_Type is (Character'Pos (C));

   package Vectors is new Ada.Containers.Bounded_Vectors (Index, Character);
   package Lists is new Ada.Containers.Bounded_Doubly_Linked_Lists (Integer);
   package Hashed_Maps is new Ada.Containers.Bounded_Hashed_Maps (Character, Integer, Hash, "=");
   package Ordered_Maps is new Ada.Containers.Bounded_Ordered_Maps (Character, Integer);
   package Hashed_Sets is new Ada.Containers.Bounded_Hashed_Sets (Character, Hash, "=");
   package Ordered_Sets is new Ada.Containers.Bounded_Ordered_Sets (Character);
   use type Vectors.Vector;

   V : Vectors.Vector (6);
   L : Lists.List (4);
   C : Lists.Cursor;
   HM : Hashed_Maps.Map (4, Hashed_Maps.Default_Modulus (4));
   OM : Ordered_Maps.Map (4);
   HS : Hashed_Sets.Set (4, Hashed_Sets.Default_Modulus (4));
   OS : Ordered_Sets.Set (4);
begin
   --  vectors indexed from 0
   Vectors.Append (V, 'c');
   Vectors.Append (V, 'a');
   Vectors.Prepend (V, 'r');
   Vectors.Insert (V, 1, 'o');
   for E of V loop
      Put (E);
   end loop;
   Put_Line (Index'Image (Vectors.Last_Index (V)) & Index'Image (Vectors.Find_Index (V, 'c')));
   Vectors.Reverse_Elements (V);
   Vectors.Swap (V, 0, 3);
   Vectors.Delete (V, 1);
   for E of V loop
      Put (E);
   end loop;
   New_Line;
   if Vectors.Contains (V, 'r') and then Vectors.Copy (V) = V then
      Put_Line ("r is there; a copy is equal");
   end if;

   --  lists and their cursors
   Lists.Append (L, 2);
   Lists.Append (L, 3);
   Lists.Prepend (L, 1);
   C := Lists.Find (L, 2);
   if Lists.Has_Element (L, C) then
      Lists.Insert (L, C, 9);
      Lists.Delete (L, C);
   end if;
   C := Lists.First (L);
   while Lists.Has_Element (L, C) loop
      pragma Loop_Invariant (Lists.Has_Element (L, C));
      Put (Integer'Image (Lists.Element (L, C)));
      C := Lists.Next (L, C);
   end loop;
   Put_Line (" (first" & Integer'Image (Lists.First_Element (L)) & ", last" & Integer'Image (Lists.Last_Element (L)) & ")");

   --  maps: in insertion order, and in key order
   Hashed_Maps.Insert (HM, 'z', 26);
   Ordered_Maps.Insert (OM, 'z', 26);
   if not Hashed_Maps.Contains (HM, 'b') and then not Ordered_Maps.Contains (OM, 'b') then
      Hashed_Maps.Insert (HM, 'b', 2);
      Ordered_Maps.Insert (OM, 'b', 2);
   end if;
   Hashed_Maps.Include (HM, 'z', 25);
   Ordered_Maps.Include (OM, 'm', 13);
   for I in 1 .. Hashed_Maps.Length (HM) loop
      Put (Hashed_Maps.Key_At (HM, I) & Integer'Image (Hashed_Maps.Element_At (HM, I)) & " ");
   end loop;
   New_Line;
   for I in 1 .. Ordered_Maps.Length (OM) loop
      Put (Ordered_Maps.Key_At (OM, I) & Integer'Image (Ordered_Maps.Element_At (OM, I)) & " ");
   end loop;
   New_Line;
   Ordered_Maps.Exclude (OM, 'm');
   Put_Line (Ordered_Maps.First_Key (OM) & Ordered_Maps.Last_Key (OM) & Ada.Containers.Count_Type'Image (Ordered_Maps.Length (OM)));

   --  sets
   for Ch of String'("set") loop
      if Hashed_Sets.Length (HS) < 4 and then Ordered_Sets.Length (OS) < 4 then
         Hashed_Sets.Include (HS, Ch);
         Ordered_Sets.Include (OS, Ch);
      end if;
   end loop;
   for E of HS loop
      Put (E);
   end loop;
   Put (' ');
   for E of OS loop
      Put (E);
   end loop;
   New_Line;
end Main;
