2010-12-20 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / ada / g-dynhta.ads
blobf679d10de99f6cd0dd7b8160e8929a341450f01d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME 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-2008, 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 ------------------------------------------------------------------------------
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 releasing all memory associated with
107 -- it. The hash table can safely be reused after this call. 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 of
112 -- 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 never 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 -- Releases all memory associated with the table. The table can be
181 -- reused after this call (it is automatically allocated on the first
182 -- access to the table).
184 function Get (T : Instance; K : Key) return Element;
185 -- Returns the Element associated with a key or No_Element if the
186 -- given key has not associated element
188 procedure Remove (T : Instance; K : Key);
189 -- Removes the latest inserted element pointer associated with the
190 -- given key if any, does nothing if none.
192 function Get_First (T : Instance) return Element;
193 -- Returns No_Element if the Htable is empty, otherwise returns one
194 -- non specified element. There is no guarantee that two calls to this
195 -- function will return the same element, if the Htable has been
196 -- modified between the two calls.
198 function Get_Next (T : Instance) return Element;
199 -- Returns a non-specified element that has not been returned by the
200 -- same function since the last call to Get_First or No_Element if
201 -- there is no such element. If there is no call to 'Set' in between
202 -- Get_Next calls, all the elements of the Htable will be traversed.
203 -- To guarantee that all the elements of the Htable will be traversed,
204 -- no modification of the Htable (Set, Reset, Remove) should occur
205 -- between a call to Get_First and subsequent consecutive calls to
206 -- Get_Next, until one of these calls returns No_Element.
208 private
210 type Element_Wrapper;
211 type Elmt_Ptr is access all Element_Wrapper;
212 type Element_Wrapper is record
213 K : Key;
214 E : Element;
215 Next : Elmt_Ptr;
216 end record;
218 procedure Free is new
219 Ada.Unchecked_Deallocation (Element_Wrapper, Elmt_Ptr);
221 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
222 function Next (E : Elmt_Ptr) return Elmt_Ptr;
223 function Get_Key (E : Elmt_Ptr) return Key;
225 package Tab is new Static_HTable
226 (Header_Num => Header_Num,
227 Element => Element_Wrapper,
228 Elmt_Ptr => Elmt_Ptr,
229 Null_Ptr => null,
230 Set_Next => Set_Next,
231 Next => Next,
232 Key => Key,
233 Get_Key => Get_Key,
234 Hash => Hash,
235 Equal => Equal);
237 type Instance is new Tab.Instance;
238 Nil : constant Instance := Instance (Tab.Nil);
240 end Simple_HTable;
242 end GNAT.Dynamic_HTables;