2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / g-dynhta.ads
blobcea431467ceb030e2be951a118b93b9bbdeb13c4
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME COMPONENTS --
4 -- --
5 -- G N A T . D Y N A M I C _ H T A B L E S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1995-2003 Ada Core Technologies, Inc. --
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 ------------------------------------------------------------------------------
34 -- Hash table searching routines
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.
42 -- This package provides a facility similar to that of GNAT.HTable, except
43 -- that this package declares types that can be used to define dynamic
44 -- instances of hash tables, while instantiations in GNAT.HTable creates a
45 -- single instance of the hash table.
47 -- Note that this interface should remain synchronized with those in
48 -- GNAT.HTable to keep as much coherency as possible between these two
49 -- related units.
51 with Ada.Unchecked_Deallocation;
52 package GNAT.Dynamic_HTables is
54 -------------------
55 -- Static_HTable --
56 -------------------
58 -- A low-level Hash-Table abstraction, not as easy to instantiate as
59 -- Simple_HTable but designed to allow complete control over the
60 -- allocation of necessary data structures. Particularly useful when
61 -- dynamic allocation is not desired. The model is that each Element
62 -- contains its own Key that can be retrieved by Get_Key. Furthermore,
63 -- Element provides a link that can be used by the HTable for linking
64 -- elements with same hash codes:
66 -- Element
68 -- +-------------------+
69 -- | Key |
70 -- +-------------------+
71 -- : other data :
72 -- +-------------------+
73 -- | Next Elmt |
74 -- +-------------------+
76 generic
77 type Header_Num is range <>;
78 -- An integer type indicating the number and range of hash headers.
80 type Element (<>) is limited private;
81 -- The type of element to be stored
83 type Elmt_Ptr is private;
84 -- The type used to reference an element (will usually be an access
85 -- type, but could be some other form of type such as an integer type).
87 Null_Ptr : Elmt_Ptr;
88 -- The null value of the Elmt_Ptr type.
90 with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
91 with function Next (E : Elmt_Ptr) return Elmt_Ptr;
92 -- The type must provide an internal link for the sake of the
93 -- staticness of the HTable.
95 type Key is limited private;
96 with function Get_Key (E : Elmt_Ptr) return Key;
97 with function Hash (F : Key) return Header_Num;
98 with function Equal (F1, F2 : Key) return Boolean;
100 package Static_HTable is
102 type Instance is private;
103 Nil : constant Instance;
105 procedure Reset (T : in out Instance);
106 -- Resets the hash table by setting all its elements to Null_Ptr. The
107 -- effect is to clear the hash table so that it can be reused. For the
108 -- most common case where Elmt_Ptr is an access type, and Null_Ptr is
109 -- null, this is only needed if the same table is reused in a new
110 -- context. If Elmt_Ptr is other than an access type, or Null_Ptr is
111 -- other than null, then Reset must be called before the first use
112 -- of the hash table.
114 procedure Set (T : in out Instance; E : Elmt_Ptr);
115 -- Insert the element pointer in the HTable
117 function Get (T : Instance; K : Key) return Elmt_Ptr;
118 -- Returns the latest inserted element pointer with the given Key
119 -- or null if none.
121 procedure Remove (T : Instance; K : Key);
122 -- Removes the latest inserted element pointer associated with the
123 -- given key if any, does nothing if none.
125 function Get_First (T : Instance) return Elmt_Ptr;
126 -- Returns Null_Ptr if the Htable is empty, otherwise returns one
127 -- non specified element. There is no guarantee that 2 calls to this
128 -- function will return the same element.
130 function Get_Next (T : Instance) return Elmt_Ptr;
131 -- Returns a non-specified element that has not been returned by the
132 -- same function since the last call to Get_First or Null_Ptr if
133 -- there is no such element or Get_First has bever been called. If
134 -- there is no call to 'Set' in between Get_Next calls, all the
135 -- elements of the Htable will be traversed.
137 private
139 type Instance_Data;
140 type Instance is access all Instance_Data;
141 Nil : constant Instance := null;
143 end Static_HTable;
145 -------------------
146 -- Simple_HTable --
147 -------------------
149 -- A simple hash table abstraction, easy to instantiate, easy to use.
150 -- The table associates one element to one key with the procedure Set.
151 -- Get retrieves the Element stored for a given Key. The efficiency of
152 -- retrieval is function of the size of the Table parameterized by
153 -- Header_Num and the hashing function Hash.
155 generic
156 type Header_Num is range <>;
157 -- An integer type indicating the number and range of hash headers.
159 type Element is private;
160 -- The type of element to be stored
162 No_Element : Element;
163 -- The object that is returned by Get when no element has been set for
164 -- a given key
166 type Key is private;
167 with function Hash (F : Key) return Header_Num;
168 with function Equal (F1, F2 : Key) return Boolean;
170 package Simple_HTable is
172 type Instance is private;
173 Nil : constant Instance;
175 procedure Set (T : in out Instance; K : Key; E : Element);
176 -- Associates an element with a given key. Overrides any previously
177 -- associated element.
179 procedure Reset (T : in out Instance);
180 -- Removes and frees all elements in the table
182 function Get (T : Instance; K : Key) return Element;
183 -- Returns the Element associated with a key or No_Element if the
184 -- given key has not associated element
186 procedure Remove (T : Instance; K : Key);
187 -- Removes the latest inserted element pointer associated with the
188 -- given key if any, does nothing if none.
190 function Get_First (T : Instance) return Element;
191 -- Returns No_Element if the Htable is empty, otherwise returns one
192 -- non specified element. There is no guarantee that two calls to this
193 -- function will return the same element, if the Htable has been
194 -- modified between the two calls.
196 function Get_Next (T : Instance) return Element;
197 -- Returns a non-specified element that has not been returned by the
198 -- same function since the last call to Get_First or No_Element if
199 -- there is no such element. If there is no call to 'Set' in between
200 -- Get_Next calls, all the elements of the Htable will be traversed.
201 -- To guarantee that all the elements of the Htable will be traversed,
202 -- no modification of the Htable (Set, Reset, Remove) should occur
203 -- between a call to Get_First and subsequent consecutive calls to
204 -- Get_Next, until one of these calls returns No_Element.
206 private
208 type Element_Wrapper;
209 type Elmt_Ptr is access all Element_Wrapper;
210 type Element_Wrapper is record
211 K : Key;
212 E : Element;
213 Next : Elmt_Ptr;
214 end record;
216 procedure Free is new
217 Ada.Unchecked_Deallocation (Element_Wrapper, Elmt_Ptr);
219 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
220 function Next (E : Elmt_Ptr) return Elmt_Ptr;
221 function Get_Key (E : Elmt_Ptr) return Key;
223 package Tab is new Static_HTable
224 (Header_Num => Header_Num,
225 Element => Element_Wrapper,
226 Elmt_Ptr => Elmt_Ptr,
227 Null_Ptr => null,
228 Set_Next => Set_Next,
229 Next => Next,
230 Key => Key,
231 Get_Key => Get_Key,
232 Hash => Hash,
233 Equal => Equal);
235 type Instance is new Tab.Instance;
236 Nil : constant Instance := Instance (Tab.Nil);
238 end Simple_HTable;
240 end GNAT.Dynamic_HTables;