OSDN Git Service

2006-10-31 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-htable.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                          G N A T . H T A B L E                           --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                     Copyright (C) 1995-2005, AdaCore                     --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  Hash table searching routines
35
36 --  This package contains two separate packages. The Simple_HTable package
37 --  provides a very simple abstraction that associates one element to one
38 --  key value and takes care of all allocations automatically using the heap.
39 --  The Static_HTable package provides a more complex interface that allows
40 --  complete control over allocation.
41
42 --  Note: actual code is found in System.HTable (s-htable.ads/adb) since
43 --  this facility is accessed from run time routines, but clients should
44 --  always access the version supplied via GNAT.HTable.
45
46 with System.HTable;
47
48 package GNAT.HTable is
49    pragma Preelaborate;
50    pragma Elaborate_Body;
51    --  The elaborate body is because we have a dummy body to deal with
52    --  bootstrap path problems (we used to have a real body, and now we don't
53    --  need it any more, but the bootstrap requires that we have a dummy body,
54    --  since otherwise the old body gets picked up.
55
56    -------------------
57    -- Simple_HTable --
58    -------------------
59
60    --  A simple hash table abstraction, easy to instantiate, easy to use.
61    --  The table associates one element to one key with the procedure Set.
62    --  Get retrieves the Element stored for a given Key. The efficiency of
63    --  retrieval is function of the size of the Table parameterized by
64    --  Header_Num and the hashing function Hash.
65
66    generic package Simple_HTable renames System.HTable.Simple_HTable;
67
68    --  For convenience of reference here is what this package has in it:
69
70    --  generic
71    --     type Header_Num is range <>;
72    --     --  An integer type indicating the number and range of hash headers
73
74    --     type Element is private;
75    --     --  The type of element to be stored
76
77    --     No_Element : Element;
78    --     --  The object that is returned by Get when no element has been set
79    --     --  for a given key
80
81    --     type Key is private;
82    --     with function Hash  (F : Key)      return Header_Num;
83    --     with function Equal (F1, F2 : Key) return Boolean;
84
85    --  package Simple_HTable is
86
87    --     procedure Set (K : Key; E : Element);
88    --     --  Associates an element with a given key. Overrides any previously
89    --     --  associated element.
90
91    --     procedure Reset;
92    --     --  Removes and frees all elements in the table
93
94    --     function Get (K : Key) return Element;
95    --     --  Returns the Element associated with a key or No_Element if the
96    --     --  given key has not associated element
97
98    --     procedure Remove (K : Key);
99    --     --  Removes the latest inserted element pointer associated with the
100    --     --  given key if any, does nothing if none.
101
102    --     function Get_First return Element;
103    --     --  Returns No_Element if the HTable is empty, otherwise returns one
104    --     --  non specified element. There is no guarantee that 2 calls to
105    --     --  this function will return the same element.
106
107    --     function Get_Next return Element;
108    --     --  Returns a non-specified element that has not been returned by the
109    --     --  same function since the last call to Get_First or No_Element if
110    --     --  there is no such element. If there is no call to 'Set' in between
111    --     --  Get_Next calls, all the elements of the HTable will be traversed.
112    --  end Simple_HTable;
113
114    -------------------
115    -- Static_HTable --
116    -------------------
117
118    --  A low-level Hash-Table abstraction, not as easy to instantiate as
119    --  Simple_HTable but designed to allow complete control over the
120    --  allocation of necessary data structures. Particularly useful when
121    --  dynamic allocation is not desired. The model is that each Element
122    --  contains its own Key that can be retrieved by Get_Key. Furthermore,
123    --  Element provides a link that can be used by the HTable for linking
124    --  elements with same hash codes:
125
126    --       Element
127
128    --         +-------------------+
129    --         |       Key         |
130    --         +-------------------+
131    --         :    other data     :
132    --         +-------------------+
133    --         |     Next Elmt     |
134    --         +-------------------+
135
136    generic package Static_HTable renames System.HTable.Static_HTable;
137
138    --  For convenience of reference here is what this package has in it:
139
140    --  generic
141    --     type Header_Num is range <>;
142    --     --  An integer type indicating the number and range of hash headers.
143
144    --     type Element (<>) is limited private;
145    --     --  The type of element to be stored
146
147    --     type Elmt_Ptr is private;
148    --     --  The type used to reference an element (will usually be an
149    --     --  access type, but could be some other form of type such as
150    --     --  an integer type).
151
152    --     Null_Ptr : Elmt_Ptr;
153    --     --  The null value of the Elmt_Ptr type.
154
155    --     with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
156    --     with function  Next     (E : Elmt_Ptr) return Elmt_Ptr;
157    --     --  The type must provide an internal link for the sake of the
158    --     --  staticness of the HTable.
159
160    --     type Key is limited private;
161    --     with function Get_Key (E : Elmt_Ptr) return Key;
162    --     with function Hash    (F : Key)      return Header_Num;
163    --     with function Equal   (F1, F2 : Key) return Boolean;
164
165    --  package Static_HTable is
166
167    --     procedure Reset;
168    --     --  Resets the hash table by setting all its elements to Null_Ptr.
169    --     --  The effect is to clear the hash table so that it can be reused.
170    --     --  For the most common case where Elmt_Ptr is an access type, and
171    --     --  Null_Ptr is null, this is only needed if the same table is
172    --     --  reused in a new context. If Elmt_Ptr is other than an access
173    --     --  type, or Null_Ptr is other than null, then Reset must be called
174    --     --  before the first use of the hash table.
175
176    --     procedure Set (E : Elmt_Ptr);
177    --     --  Insert the element pointer in the HTable
178
179    --     function Get (K : Key) return Elmt_Ptr;
180    --     --  Returns the latest inserted element pointer with the given Key
181    --     --  or null if none.
182
183    --     procedure Remove (K : Key);
184    --     --  Removes the latest inserted element pointer associated with the
185    --     --  given key if any, does nothing if none.
186
187    --     function Get_First return Elmt_Ptr;
188    --     --  Returns Null_Ptr if the HTable is empty, otherwise returns one
189    --     --  non specified element. There is no guarantee that 2 calls to
190    --     --  this function will return the same element.
191
192    --     function Get_Next return Elmt_Ptr;
193    --     --  Returns a non-specified element that has not been returned by
194    --     --  the same function since the last call to Get_First or Null_Ptr
195    --     --  if there is no such element or Get_First has bever been called.
196    --     --  If there is no call to 'Set' in between Get_Next calls, all
197    --     --  the elements of the HTable will be traversed.
198
199    --  end Static_HTable;
200
201    ----------
202    -- Hash --
203    ----------
204
205    --  A generic hashing function working on String keys
206
207    generic function Hash renames System.HTable.Hash;
208
209    --  generic
210    --     type Header_Num is range <>;
211    --  function Hash (Key : String) return Header_Num;
212
213 end GNAT.HTable;