------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
-- --
|
-- --
|
-- GNAT RUN-TIME COMPONENTS --
|
-- GNAT RUN-TIME COMPONENTS --
|
-- --
|
-- --
|
-- G N A T . D Y N A M I C _ H T A B L E S --
|
-- G N A T . D Y N A M I C _ H T A B L E S --
|
-- --
|
-- --
|
-- S p e c --
|
-- S p e c --
|
-- --
|
-- --
|
-- Copyright (C) 1995-2008, AdaCore --
|
-- Copyright (C) 1995-2008, AdaCore --
|
-- --
|
-- --
|
-- GNAT is free software; you can redistribute it and/or modify it under --
|
-- GNAT is free software; you can redistribute it and/or modify it under --
|
-- terms of the GNU General Public License as published by the Free Soft- --
|
-- terms of the GNU General Public License as published by the Free Soft- --
|
-- ware Foundation; either version 2, or (at your option) any later ver- --
|
-- ware Foundation; either version 2, or (at your option) any later ver- --
|
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
|
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
|
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
|
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
|
-- for more details. You should have received a copy of the GNU General --
|
-- for more details. You should have received a copy of the GNU General --
|
-- Public License distributed with GNAT; see file COPYING. If not, write --
|
-- Public License distributed with GNAT; see file COPYING. If not, write --
|
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
|
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
|
-- Boston, MA 02110-1301, USA. --
|
-- Boston, MA 02110-1301, USA. --
|
-- --
|
-- --
|
-- As a special exception, if other files instantiate generics from this --
|
-- As a special exception, if other files instantiate generics from this --
|
-- unit, or you link this unit with other files to produce an executable, --
|
-- unit, or you link this unit with other files to produce an executable, --
|
-- this unit does not by itself cause the resulting executable to be --
|
-- this unit does not by itself cause the resulting executable to be --
|
-- covered by the GNU General Public License. This exception does not --
|
-- covered by the GNU General Public License. This exception does not --
|
-- however invalidate any other reasons why the executable file might be --
|
-- however invalidate any other reasons why the executable file might be --
|
-- covered by the GNU Public License. --
|
-- covered by the GNU Public License. --
|
-- --
|
-- --
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
-- --
|
-- --
|
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
|
|
-- Hash table searching routines
|
-- Hash table searching routines
|
|
|
-- This package contains two separate packages. The Simple_HTable package
|
-- This package contains two separate packages. The Simple_HTable package
|
-- provides a very simple abstraction that associates one element to one
|
-- provides a very simple abstraction that associates one element to one
|
-- key value and takes care of all allocations automatically using the heap.
|
-- key value and takes care of all allocations automatically using the heap.
|
-- The Static_HTable package provides a more complex interface that allows
|
-- The Static_HTable package provides a more complex interface that allows
|
-- complete control over allocation.
|
-- complete control over allocation.
|
|
|
-- This package provides a facility similar to that of GNAT.HTable, except
|
-- This package provides a facility similar to that of GNAT.HTable, except
|
-- that this package declares types that can be used to define dynamic
|
-- that this package declares types that can be used to define dynamic
|
-- instances of hash tables, while instantiations in GNAT.HTable creates a
|
-- instances of hash tables, while instantiations in GNAT.HTable creates a
|
-- single instance of the hash table.
|
-- single instance of the hash table.
|
|
|
-- Note that this interface should remain synchronized with those in
|
-- Note that this interface should remain synchronized with those in
|
-- GNAT.HTable to keep as much coherency as possible between these two
|
-- GNAT.HTable to keep as much coherency as possible between these two
|
-- related units.
|
-- related units.
|
|
|
with Ada.Unchecked_Deallocation;
|
with Ada.Unchecked_Deallocation;
|
package GNAT.Dynamic_HTables is
|
package GNAT.Dynamic_HTables is
|
|
|
-------------------
|
-------------------
|
-- Static_HTable --
|
-- Static_HTable --
|
-------------------
|
-------------------
|
|
|
-- A low-level Hash-Table abstraction, not as easy to instantiate as
|
-- A low-level Hash-Table abstraction, not as easy to instantiate as
|
-- Simple_HTable but designed to allow complete control over the
|
-- Simple_HTable but designed to allow complete control over the
|
-- allocation of necessary data structures. Particularly useful when
|
-- allocation of necessary data structures. Particularly useful when
|
-- dynamic allocation is not desired. The model is that each Element
|
-- dynamic allocation is not desired. The model is that each Element
|
-- contains its own Key that can be retrieved by Get_Key. Furthermore,
|
-- contains its own Key that can be retrieved by Get_Key. Furthermore,
|
-- Element provides a link that can be used by the HTable for linking
|
-- Element provides a link that can be used by the HTable for linking
|
-- elements with same hash codes:
|
-- elements with same hash codes:
|
|
|
-- Element
|
-- Element
|
|
|
-- +-------------------+
|
-- +-------------------+
|
-- | Key |
|
-- | Key |
|
-- +-------------------+
|
-- +-------------------+
|
-- : other data :
|
-- : other data :
|
-- +-------------------+
|
-- +-------------------+
|
-- | Next Elmt |
|
-- | Next Elmt |
|
-- +-------------------+
|
-- +-------------------+
|
|
|
generic
|
generic
|
type Header_Num is range <>;
|
type Header_Num is range <>;
|
-- An integer type indicating the number and range of hash headers
|
-- An integer type indicating the number and range of hash headers
|
|
|
type Element (<>) is limited private;
|
type Element (<>) is limited private;
|
-- The type of element to be stored
|
-- The type of element to be stored
|
|
|
type Elmt_Ptr is private;
|
type Elmt_Ptr is private;
|
-- The type used to reference an element (will usually be an access
|
-- The type used to reference an element (will usually be an access
|
-- type, but could be some other form of type such as an integer type).
|
-- type, but could be some other form of type such as an integer type).
|
|
|
Null_Ptr : Elmt_Ptr;
|
Null_Ptr : Elmt_Ptr;
|
-- The null value of the Elmt_Ptr type
|
-- The null value of the Elmt_Ptr type
|
|
|
with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
|
with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
|
with function Next (E : Elmt_Ptr) return Elmt_Ptr;
|
with function Next (E : Elmt_Ptr) return Elmt_Ptr;
|
-- The type must provide an internal link for the sake of the
|
-- The type must provide an internal link for the sake of the
|
-- staticness of the HTable.
|
-- staticness of the HTable.
|
|
|
type Key is limited private;
|
type Key is limited private;
|
with function Get_Key (E : Elmt_Ptr) return Key;
|
with function Get_Key (E : Elmt_Ptr) return Key;
|
with function Hash (F : Key) return Header_Num;
|
with function Hash (F : Key) return Header_Num;
|
with function Equal (F1, F2 : Key) return Boolean;
|
with function Equal (F1, F2 : Key) return Boolean;
|
|
|
package Static_HTable is
|
package Static_HTable is
|
|
|
type Instance is private;
|
type Instance is private;
|
Nil : constant Instance;
|
Nil : constant Instance;
|
|
|
procedure Reset (T : in out Instance);
|
procedure Reset (T : in out Instance);
|
-- Resets the hash table by releasing all memory associated with
|
-- Resets the hash table by releasing all memory associated with
|
-- it. The hash table can safely be reused after this call. For the
|
-- it. The hash table can safely be reused after this call. For the
|
-- most common case where Elmt_Ptr is an access type, and Null_Ptr is
|
-- most common case where Elmt_Ptr is an access type, and Null_Ptr is
|
-- null, this is only needed if the same table is reused in a new
|
-- null, this is only needed if the same table is reused in a new
|
-- context. If Elmt_Ptr is other than an access type, or Null_Ptr is
|
-- context. If Elmt_Ptr is other than an access type, or Null_Ptr is
|
-- other than null, then Reset must be called before the first use of
|
-- other than null, then Reset must be called before the first use of
|
-- the hash table.
|
-- the hash table.
|
|
|
procedure Set (T : in out Instance; E : Elmt_Ptr);
|
procedure Set (T : in out Instance; E : Elmt_Ptr);
|
-- Insert the element pointer in the HTable
|
-- Insert the element pointer in the HTable
|
|
|
function Get (T : Instance; K : Key) return Elmt_Ptr;
|
function Get (T : Instance; K : Key) return Elmt_Ptr;
|
-- Returns the latest inserted element pointer with the given Key
|
-- Returns the latest inserted element pointer with the given Key
|
-- or null if none.
|
-- or null if none.
|
|
|
procedure Remove (T : Instance; K : Key);
|
procedure Remove (T : Instance; K : Key);
|
-- Removes the latest inserted element pointer associated with the
|
-- Removes the latest inserted element pointer associated with the
|
-- given key if any, does nothing if none.
|
-- given key if any, does nothing if none.
|
|
|
function Get_First (T : Instance) return Elmt_Ptr;
|
function Get_First (T : Instance) return Elmt_Ptr;
|
-- Returns Null_Ptr if the Htable is empty, otherwise returns one
|
-- Returns Null_Ptr if the Htable is empty, otherwise returns one
|
-- non specified element. There is no guarantee that 2 calls to this
|
-- non specified element. There is no guarantee that 2 calls to this
|
-- function will return the same element.
|
-- function will return the same element.
|
|
|
function Get_Next (T : Instance) return Elmt_Ptr;
|
function Get_Next (T : Instance) return Elmt_Ptr;
|
-- Returns a non-specified element that has not been returned by the
|
-- Returns a non-specified element that has not been returned by the
|
-- same function since the last call to Get_First or Null_Ptr if
|
-- same function since the last call to Get_First or Null_Ptr if
|
-- there is no such element or Get_First has never been called. If
|
-- there is no such element or Get_First has never been called. If
|
-- there is no call to 'Set' in between Get_Next calls, all the
|
-- there is no call to 'Set' in between Get_Next calls, all the
|
-- elements of the Htable will be traversed.
|
-- elements of the Htable will be traversed.
|
|
|
private
|
private
|
|
|
type Instance_Data;
|
type Instance_Data;
|
type Instance is access all Instance_Data;
|
type Instance is access all Instance_Data;
|
Nil : constant Instance := null;
|
Nil : constant Instance := null;
|
|
|
end Static_HTable;
|
end Static_HTable;
|
|
|
-------------------
|
-------------------
|
-- Simple_HTable --
|
-- Simple_HTable --
|
-------------------
|
-------------------
|
|
|
-- A simple hash table abstraction, easy to instantiate, easy to use.
|
-- A simple hash table abstraction, easy to instantiate, easy to use.
|
-- The table associates one element to one key with the procedure Set.
|
-- The table associates one element to one key with the procedure Set.
|
-- Get retrieves the Element stored for a given Key. The efficiency of
|
-- Get retrieves the Element stored for a given Key. The efficiency of
|
-- retrieval is function of the size of the Table parameterized by
|
-- retrieval is function of the size of the Table parameterized by
|
-- Header_Num and the hashing function Hash.
|
-- Header_Num and the hashing function Hash.
|
|
|
generic
|
generic
|
type Header_Num is range <>;
|
type Header_Num is range <>;
|
-- An integer type indicating the number and range of hash headers
|
-- An integer type indicating the number and range of hash headers
|
|
|
type Element is private;
|
type Element is private;
|
-- The type of element to be stored
|
-- The type of element to be stored
|
|
|
No_Element : Element;
|
No_Element : Element;
|
-- The object that is returned by Get when no element has been set for
|
-- The object that is returned by Get when no element has been set for
|
-- a given key
|
-- a given key
|
|
|
type Key is private;
|
type Key is private;
|
with function Hash (F : Key) return Header_Num;
|
with function Hash (F : Key) return Header_Num;
|
with function Equal (F1, F2 : Key) return Boolean;
|
with function Equal (F1, F2 : Key) return Boolean;
|
|
|
package Simple_HTable is
|
package Simple_HTable is
|
|
|
type Instance is private;
|
type Instance is private;
|
Nil : constant Instance;
|
Nil : constant Instance;
|
|
|
procedure Set (T : in out Instance; K : Key; E : Element);
|
procedure Set (T : in out Instance; K : Key; E : Element);
|
-- Associates an element with a given key. Overrides any previously
|
-- Associates an element with a given key. Overrides any previously
|
-- associated element.
|
-- associated element.
|
|
|
procedure Reset (T : in out Instance);
|
procedure Reset (T : in out Instance);
|
-- Releases all memory associated with the table. The table can be
|
-- Releases all memory associated with the table. The table can be
|
-- reused after this call (it is automatically allocated on the first
|
-- reused after this call (it is automatically allocated on the first
|
-- access to the table).
|
-- access to the table).
|
|
|
function Get (T : Instance; K : Key) return Element;
|
function Get (T : Instance; K : Key) return Element;
|
-- Returns the Element associated with a key or No_Element if the
|
-- Returns the Element associated with a key or No_Element if the
|
-- given key has not associated element
|
-- given key has not associated element
|
|
|
procedure Remove (T : Instance; K : Key);
|
procedure Remove (T : Instance; K : Key);
|
-- Removes the latest inserted element pointer associated with the
|
-- Removes the latest inserted element pointer associated with the
|
-- given key if any, does nothing if none.
|
-- given key if any, does nothing if none.
|
|
|
function Get_First (T : Instance) return Element;
|
function Get_First (T : Instance) return Element;
|
-- Returns No_Element if the Htable is empty, otherwise returns one
|
-- Returns No_Element if the Htable is empty, otherwise returns one
|
-- non specified element. There is no guarantee that two calls to this
|
-- non specified element. There is no guarantee that two calls to this
|
-- function will return the same element, if the Htable has been
|
-- function will return the same element, if the Htable has been
|
-- modified between the two calls.
|
-- modified between the two calls.
|
|
|
function Get_Next (T : Instance) return Element;
|
function Get_Next (T : Instance) return Element;
|
-- Returns a non-specified element that has not been returned by the
|
-- Returns a non-specified element that has not been returned by the
|
-- same function since the last call to Get_First or No_Element if
|
-- same function since the last call to Get_First or No_Element if
|
-- there is no such element. If there is no call to 'Set' in between
|
-- there is no such element. If there is no call to 'Set' in between
|
-- Get_Next calls, all the elements of the Htable will be traversed.
|
-- Get_Next calls, all the elements of the Htable will be traversed.
|
-- To guarantee that all the elements of the Htable will be traversed,
|
-- To guarantee that all the elements of the Htable will be traversed,
|
-- no modification of the Htable (Set, Reset, Remove) should occur
|
-- no modification of the Htable (Set, Reset, Remove) should occur
|
-- between a call to Get_First and subsequent consecutive calls to
|
-- between a call to Get_First and subsequent consecutive calls to
|
-- Get_Next, until one of these calls returns No_Element.
|
-- Get_Next, until one of these calls returns No_Element.
|
|
|
private
|
private
|
|
|
type Element_Wrapper;
|
type Element_Wrapper;
|
type Elmt_Ptr is access all Element_Wrapper;
|
type Elmt_Ptr is access all Element_Wrapper;
|
type Element_Wrapper is record
|
type Element_Wrapper is record
|
K : Key;
|
K : Key;
|
E : Element;
|
E : Element;
|
Next : Elmt_Ptr;
|
Next : Elmt_Ptr;
|
end record;
|
end record;
|
|
|
procedure Free is new
|
procedure Free is new
|
Ada.Unchecked_Deallocation (Element_Wrapper, Elmt_Ptr);
|
Ada.Unchecked_Deallocation (Element_Wrapper, Elmt_Ptr);
|
|
|
procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
|
procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
|
function Next (E : Elmt_Ptr) return Elmt_Ptr;
|
function Next (E : Elmt_Ptr) return Elmt_Ptr;
|
function Get_Key (E : Elmt_Ptr) return Key;
|
function Get_Key (E : Elmt_Ptr) return Key;
|
|
|
package Tab is new Static_HTable
|
package Tab is new Static_HTable
|
(Header_Num => Header_Num,
|
(Header_Num => Header_Num,
|
Element => Element_Wrapper,
|
Element => Element_Wrapper,
|
Elmt_Ptr => Elmt_Ptr,
|
Elmt_Ptr => Elmt_Ptr,
|
Null_Ptr => null,
|
Null_Ptr => null,
|
Set_Next => Set_Next,
|
Set_Next => Set_Next,
|
Next => Next,
|
Next => Next,
|
Key => Key,
|
Key => Key,
|
Get_Key => Get_Key,
|
Get_Key => Get_Key,
|
Hash => Hash,
|
Hash => Hash,
|
Equal => Equal);
|
Equal => Equal);
|
|
|
type Instance is new Tab.Instance;
|
type Instance is new Tab.Instance;
|
Nil : constant Instance := Instance (Tab.Nil);
|
Nil : constant Instance := Instance (Tab.Nil);
|
|
|
end Simple_HTable;
|
end Simple_HTable;
|
|
|
end GNAT.Dynamic_HTables;
|
end GNAT.Dynamic_HTables;
|
|
|