How to resolve the algorithm Associative array/Merging step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Associative array/Merging step by step in the Ada programming language

Table of Contents

Problem Statement

Define two associative arrays, where one represents the following "base" data: And the other represents "update" data: Merge these into a new associative array that contains every key found in either of the source ones. Each key should map to the value in the second (update) table if that exists, or else to the value in the first (base) table. If possible, do this in a way that does not mutate the original two associative arrays. Obviously this should be done in a way that would work for any data, not just the specific data given here, but in this example the result should be:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Associative array/Merging step by step in the Ada programming language

Source code in the ada programming language

with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Merge_Maps is
   use Ada.Text_Io;

   type Key_Type   is new String;
   type Value_Type is new String;

   package Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => Key_Type,
                                                 Element_Type => Value_Type);
   use Maps;

   function Merge (Original : Map; Update : Map) return Map is
      Result : Map    := Original;
      Cur    : Cursor := Update.First;
   begin
      while Has_Element (Cur) loop
         if Original.Contains (Key (Cur)) then
            Result.Replace_Element (Result.Find (Key (Cur)),
                                    Element (Cur));
         else
            Result.Insert (Key (Cur), Element (Cur));
         end if;
         Next (Cur);
      end loop;
      return Result;
   end Merge;

   procedure Put_Map (M : Map) is
      Cur : Cursor := M.First;
   begin
      while Has_Element (Cur) loop
         Put (String (Key (Cur)));
         Set_Col (12);
         Put (String (Element (Cur)));
         New_Line;
         Next (Cur);
      end loop;
   end Put_Map;

   Original : Map;
   Update   : Map;
   Result   : Map;
begin
   Original.Insert ("name", "Rocket Skates");
   Original.Insert ("price", "12.75");
   Original.Insert ("color", "yellow");

   Update.Insert ("price", "15.25");
   Update.Insert ("color", "red");
   Update.Insert ("year", "1974");

   Result := Merge (Original, Update);

   Put_Line ("Original:");
   Put_Map (Original);
   New_Line;

   Put_Line ("Update:");
   Put_Map (Update);
   New_Line;

   Put_Line ("Result of merge:");
   Put_Map (Result);
   New_Line;

end Merge_Maps;


  

You may also check:How to resolve the algorithm Flipping bits game step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Stack step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the R programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Maple programming language
You may also check:How to resolve the algorithm Animation step by step in the F# programming language