OSDN Git Service

2010-10-18 Vincent Celier <celier@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-2010, 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 pragma Compiler_Unit;
47
48 with System.HTable;
49
50 package GNAT.HTable is
51    pragma Preelaborate;
52    pragma Elaborate_Body;
53    --  The elaborate body is because we have a dummy body to deal with
54    --  bootstrap path problems (we used to have a real body, and now we don't
55    --  need it any more, but the bootstrap requires that we have a dummy body,
56    --  since otherwise the old body gets picked up.
57
58    -------------------
59    -- Simple_HTable --
60    -------------------
61
62    --  A simple hash table abstraction, easy to instantiate, easy to use.
63    --  The table associates one element to one key with the procedure Set.
64    --  Get retrieves the Element stored for a given Key. The efficiency of
65    --  retrieval is function of the size of the Table parameterized by
66    --  Header_Num and the hashing function Hash.
67
68    generic package Simple_HTable renames System.HTable.Simple_HTable;
69
70    --  For convenience of reference here is what this package has in it:
71
72    --  generic
73    --     type Header_Num is range <>;
74    --     --  An integer type indicating the number and range of hash headers
75
76    --     type Element is private;
77    --     --  The type of element to be stored
78
79    --     No_Element : Element;
80    --     --  The object that is returned by Get when no element has been set
81    --     --  for a given key
82
83    --     type Key is private;
84    --     with function Hash  (F : Key)      return Header_Num;
85    --     with function Equal (F1, F2 : Key) return Boolean;
86
87    --  package Simple_HTable is
88
89    --     procedure Set (K : Key; E : Element);
90    --     --  Associates an element with a given key. Overrides any previously
91    --     --  associated element.
92
93    --     procedure Reset;
94    --     --  Removes and frees all elements in the table
95
96    --     function Get (K : Key) return Element;
97    --     --  Returns the Element associated with a key or No_Element if the
98    --     --  given key has not associated element
99
100    --     procedure Remove (K : Key);
101    --     --  Removes the latest inserted element pointer associated with the
102    --     --  given key if any, does nothing if none.
103
104    --     function Get_First return Element;
105    --     --  Returns No_Element if the HTable is empty, otherwise returns one
106    --     --  non specified element. There is no guarantee that two calls to
107    --     --  this function will return the same element.
108
109    --     function Get_Next return Element;
110    --     --  Returns a non-specified element that has not been returned by the
111    --     --  same function since the last call to Get_First or No_Element if
112    --     --  there is no such element. If there is no call to 'Set' in between
113    --     --  Get_Next calls, all the elements of the HTable will be traversed.
114
115    --     procedure Get_First (K : out Key; E : out Element);
116    --     --  This version of the iterator returns a key/element pair. A non-
117    --     --  specified entry is returned, and there is no guarantee that two
118    --     --  calls to this procedure will return the same element.
119
120    --     procedure Get_Next (K : out Key; E : out Element);
121    --     --  This version of the iterator returns a key/element pair. It
122    --     --  returns a non-specified element that has not been returned since
123    --     --  the last call to Get_First. If there is no remaining element,
124    --     --  then E is set to No_Element, and the value in K is undefined.
125    --     --  If there is no call to Set in between Get_Next calls, all the
126    --     --  elements of the HTable will be traversed.
127
128    --  end Simple_HTable;
129
130    -------------------
131    -- Static_HTable --
132    -------------------
133
134    --  A low-level Hash-Table abstraction, not as easy to instantiate as
135    --  Simple_HTable but designed to allow complete control over the
136    --  allocation of necessary data structures. Particularly useful when
137    --  dynamic allocation is not desired. The model is that each Element
138    --  contains its own Key that can be retrieved by Get_Key. Furthermore,
139    --  Element provides a link that can be used by the HTable for linking
140    --  elements with same hash codes:
141
142    --       Element
143
144    --         +-------------------+
145    --         |       Key         |
146    --         +-------------------+
147    --         :    other data     :
148    --         +-------------------+
149    --         |     Next Elmt     |
150    --         +-------------------+
151
152    generic package Static_HTable renames System.HTable.Static_HTable;
153
154    --  For convenience of reference here is what this package has in it:
155
156    --  generic
157    --     type Header_Num is range <>;
158    --     --  An integer type indicating the number and range of hash headers.
159
160    --     type Element (<>) is limited private;
161    --     --  The type of element to be stored
162
163    --     type Elmt_Ptr is private;
164    --     --  The type used to reference an element (will usually be an
165    --     --  access type, but could be some other form of type such as
166    --     --  an integer type).
167
168    --     Null_Ptr : Elmt_Ptr;
169    --     --  The null value of the Elmt_Ptr type.
170
171    --     with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
172    --     with function  Next     (E : Elmt_Ptr) return Elmt_Ptr;
173    --     --  The type must provide an internal link for the sake of the
174    --     --  staticness of the HTable.
175
176    --     type Key is limited private;
177    --     with function Get_Key (E : Elmt_Ptr) return Key;
178    --     with function Hash    (F : Key)      return Header_Num;
179    --     with function Equal   (F1, F2 : Key) return Boolean;
180
181    --  package Static_HTable is
182
183    --     procedure Reset;
184    --     --  Resets the hash table by setting all its elements to Null_Ptr.
185    --     --  The effect is to clear the hash table so that it can be reused.
186    --     --  For the most common case where Elmt_Ptr is an access type, and
187    --     --  Null_Ptr is null, this is only needed if the same table is
188    --     --  reused in a new context. If Elmt_Ptr is other than an access
189    --     --  type, or Null_Ptr is other than null, then Reset must be called
190    --     --  before the first use of the hash table.
191
192    --     procedure Set (E : Elmt_Ptr);
193    --     --  Insert the element pointer in the HTable
194
195    --     function Get (K : Key) return Elmt_Ptr;
196    --     --  Returns the latest inserted element pointer with the given Key
197    --     --  or null if none.
198
199    --     procedure Remove (K : Key);
200    --     --  Removes the latest inserted element pointer associated with the
201    --     --  given key if any, does nothing if none.
202
203    --     function Get_First return Elmt_Ptr;
204    --     --  Returns Null_Ptr if the HTable is empty, otherwise returns one
205    --     --  non specified element. There is no guarantee that two calls to
206    --     --  this function will return the same element.
207
208    --     function Get_Next return Elmt_Ptr;
209    --     --  Returns a non-specified element that has not been returned by
210    --     --  the same function since the last call to Get_First or Null_Ptr
211    --     --  if there is no such element or Get_First has never been called.
212    --     --  If there is no call to 'Set' in between Get_Next calls, all
213    --     --  the elements of the HTable will be traversed.
214
215    --  end Static_HTable;
216
217    ----------
218    -- Hash --
219    ----------
220
221    --  A generic hashing function working on String keys
222
223    generic function Hash renames System.HTable.Hash;
224
225    --  generic
226    --     type Header_Num is range <>;
227    --  function Hash (Key : String) return Header_Num;
228
229 end GNAT.HTable;