with Verada.Containers.Unbounded.Maps;
with Verada.Containers.Unbounded.Ordered_Sets;
with Ada.Text_IO; use Ada.Text_IO;

--  Unbounded ordered sets and maps (D102): reclaimed, so freed where they die
procedure Main is
   package Int_Sets is new Verada.Containers.Unbounded.Ordered_Sets (Integer);
   package Counts is new Verada.Containers.Unbounded.Maps (Character, Natural);

   procedure Sets is
      S : Int_Sets.Set;
   begin
      for I in 1 .. 50 loop
         pragma Loop_Invariant (Int_Sets.Length (S) <= I - 1);
         if not Int_Sets.Contains (S, (I * 37) mod 101) then
            Int_Sets.Insert (S, (I * 37) mod 101);
         end if;
      end loop;
      Put (Integer'Image (Int_Sets.Length (S)) & ":");
      for I in 1 .. Int_Sets.Length (S) loop
         if I <= 5 then
            Put (Integer'Image (Int_Sets.Element (S, I)));
         end if;
      end loop;
      New_Line;
   end Sets;

   procedure Maps is
      Text : constant String := "the quick brown fox jumps over the lazy dog";
      M : Counts.Map;
      C : Character;
   begin
      for I in Text'Range loop
         pragma Loop_Invariant (Counts.Length (M) <= I - Text'First);
         C := Text (I);
         if C /= ' ' then
            if Counts.Contains (M, C) then
               if Counts.Value (M, C) < Natural'Last then
                  Counts.Replace (M, C, Counts.Value (M, C) + 1);
               end if;
            else
               Counts.Insert (M, C, 1);
            end if;
         end if;
      end loop;
      for I in 1 .. Counts.Length (M) loop
         if Counts.Element (M, I) > 1 then
            Put (Counts.Key (M, I) & Integer'Image (Counts.Element (M, I)) & " ");
         end if;
      end loop;
      New_Line;
   end Maps;
begin
   Sets;
   Maps;
end Main;
