-- $Date: 2003/12/23 12:57:33 $
-- $Revision: 1.5 $
-- $Author: jcrocholl $

with Interfaces;
use Interfaces;

package body String_Hash_Tables is

   -- Calculate a hash code from an Unbounded_String.
   function Hash
     (Key : in Unbounded_String-- Calculate hash of this key.
      Max : in Positive)         -- Maximum result value.
     return Positive             -- Number in the range from 1 to Max.
   is
      use type Unsigned_32;
      Key_String : String := To_String(Key);
      Collect    : Unsigned_32 := 0;
   begin
      for Index in Key_String'Range loop
         Collect := Collect + Character'Pos(Key_String(Index));
         Collect := Rotate_Right(Collect, 13);
      end loop;
      return Positive(Collect mod Unsigned_32(Max) + 1);
   end Hash;

   -- Check for available space in the table, grow if necessary, then
   -- insert the key item pair into the table.
   --
   -- Raises Key_Exists if the key is already in use.
   procedure Put
     (Table : in out Hash_Table-- Add pair to this hash table.
      Key   : in String;         -- The new key to be added.
      Item  : in Item_Type)      -- The new item to be added.
   is
   begin
      US_Hash_Tables.Put(Table, To_Unbounded_String(Key), Item);
   end Put;

   -- Retrieve an item from the table.
   --
   -- Output parameter Found tells you whether the item was found in
   -- the table.
   procedure Get
     (Table : in Hash_Table-- Get value from this hash table.
      Key   : in String;     -- Look up this key.
      Item  : out Item_Type-- The item associated with the key.
      Found : out Boolean)   -- Was the item actually found?
   is
   begin
      US_Hash_Tables.Get(Table, To_Unbounded_String(Key), Item, Found);
   end Get;

   -- Retrieve an item from the table.
   --
   -- Raises Key_Not_Found if the key is not in the table.
   function Get
     (Table : in Hash_Table-- Get value from this hash table.
      Key   : in String)     -- Look up this key.
     return Item_Type        -- The item associated with the key.
   is
   begin
      return US_Hash_Tables.Get(Table, To_Unbounded_String(Key));
   end Get;

end String_Hash_Tables;