-- GNAT.HTable.Simple_HTable Example
--
-- Example of how to create a String To Object map
-- Also see GNAT.Spitbol.Table
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with GNAT.IO; use GNAT.IO;
with GNAT.HTable; use GNAT.HTable;
procedure Hash is
subtype Inches is Positive range 1 .. 144;
type Object is
record
ID : Natural;
Height : Inches := 1;
end record;
Null_Object : constant Object := (ID => 0, Height => 1);
subtype Map_Range is Positive range 1..255;
function String_Hash is new Hash (Map_Range);
function Unbounded_String_Hash(F : Unbounded_String) return Map_Range is
begin
return String_Hash(To_String(F));
end Unbounded_String_Hash;
function String_Equal(F1, F2 : Unbounded_String) return Boolean is
begin
return F1=F2;
end String_Equal;
package String_To_Obj_Map is new Simple_HTable (Header_Num => Map_Range,
Element => Object,
No_Element => Null_Object,
Key => Unbounded_String,
Hash => Unbounded_String_Hash,
Equal => String_Equal);
Current_Object : Object;
begin
String_To_Obj_Map.Set(To_Unbounded_String("Bob"),
(ID => 401, Height=> 65));
String_To_Obj_Map.Set(To_Unbounded_String("Sue"),
(ID => 123, Height=> 62));
String_To_Obj_Map.Set(To_Unbounded_String("Marvin"),
(ID => 321, Height=> 76));
String_To_Obj_Map.Set(To_Unbounded_String("George"),
(ID => 555, Height=> 68));
Current_Object := String_To_Obj_Map.Get_First;
while Current_Object /= Null_Object loop
Put_Line("ID =" & Natural'Image(Current_Object.ID));
Put_Line("Height =" & Inches'Image(Current_Object.Height));
Current_Object := String_To_Obj_Map.Get_Next;
end loop;
Put_Line("Bob's height is" &
Inches'Image(String_To_Obj_Map.Get(To_Unbounded_String("Bob")).Height));
end Hash;
|