2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / a-cbhama.ads
bloba03bfe6ab07555e1b05dd15b8df39add4fcd0916
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . B O U N D E D _ H A S H E D _ M A P S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2015, Free Software Foundation, Inc. --
10 -- --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
14 -- --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
21 -- --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
25 -- --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
30 -- --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
34 with Ada.Iterator_Interfaces;
36 private with Ada.Containers.Hash_Tables;
37 private with Ada.Streams;
38 private with Ada.Finalization;
40 generic
41 type Key_Type is private;
42 type Element_Type is private;
44 with function Hash (Key : Key_Type) return Hash_Type;
45 with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
46 with function "=" (Left, Right : Element_Type) return Boolean is <>;
48 package Ada.Containers.Bounded_Hashed_Maps is
49 pragma Pure;
50 pragma Remote_Types;
52 type Map (Capacity : Count_Type; Modulus : Hash_Type) is tagged private with
53 Constant_Indexing => Constant_Reference,
54 Variable_Indexing => Reference,
55 Default_Iterator => Iterate,
56 Iterator_Element => Element_Type;
58 pragma Preelaborable_Initialization (Map);
60 type Cursor is private;
61 pragma Preelaborable_Initialization (Cursor);
63 Empty_Map : constant Map;
64 -- Map objects declared without an initialization expression are
65 -- initialized to the value Empty_Map.
67 No_Element : constant Cursor;
68 -- Cursor objects declared without an initialization expression are
69 -- initialized to the value No_Element.
71 function Has_Element (Position : Cursor) return Boolean;
72 -- Equivalent to Position /= No_Element
74 package Map_Iterator_Interfaces is new
75 Ada.Iterator_Interfaces (Cursor, Has_Element);
77 function "=" (Left, Right : Map) return Boolean;
78 -- For each key/element pair in Left, equality attempts to find the key in
79 -- Right; if a search fails the equality returns False. The search works by
80 -- calling Hash to find the bucket in the Right map that corresponds to the
81 -- Left key. If bucket is non-empty, then equality calls Equivalent_Keys
82 -- to compare the key (in Left) to the key of each node in the bucket (in
83 -- Right); if the keys are equivalent, then the equality test for this
84 -- key/element pair (in Left) completes by calling the element equality
85 -- operator to compare the element (in Left) to the element of the node
86 -- (in Right) whose key matched.
88 function Capacity (Container : Map) return Count_Type;
89 -- Returns the current capacity of the map. Capacity is the maximum length
90 -- before which rehashing in guaranteed not to occur.
92 procedure Reserve_Capacity (Container : in out Map; Capacity : Count_Type);
93 -- If the value of the Capacity actual parameter is less or equal to
94 -- Container.Capacity, then the operation has no effect. Otherwise it
95 -- raises Capacity_Error (as no expansion of capacity is possible for a
96 -- bounded form).
98 function Default_Modulus (Capacity : Count_Type) return Hash_Type;
99 -- Returns a modulus value (hash table size) which is optimal for the
100 -- specified capacity (which corresponds to the maximum number of items).
102 function Length (Container : Map) return Count_Type;
103 -- Returns the number of items in the map
105 function Is_Empty (Container : Map) return Boolean;
106 -- Equivalent to Length (Container) = 0
108 procedure Clear (Container : in out Map);
109 -- Removes all of the items from the map
111 function Key (Position : Cursor) return Key_Type;
112 -- Returns the key of the node designated by the cursor
114 function Element (Position : Cursor) return Element_Type;
115 -- Returns the element of the node designated by the cursor
117 procedure Replace_Element
118 (Container : in out Map;
119 Position : Cursor;
120 New_Item : Element_Type);
121 -- Assigns the value New_Item to the element designated by the cursor
123 procedure Query_Element
124 (Position : Cursor;
125 Process : not null access
126 procedure (Key : Key_Type; Element : Element_Type));
127 -- Calls Process with the key and element (both having only a constant
128 -- view) of the node designed by the cursor.
130 procedure Update_Element
131 (Container : in out Map;
132 Position : Cursor;
133 Process : not null access
134 procedure (Key : Key_Type; Element : in out Element_Type));
135 -- Calls Process with the key (with only a constant view) and element (with
136 -- a variable view) of the node designed by the cursor.
138 type Constant_Reference_Type
139 (Element : not null access constant Element_Type) is
140 private
141 with
142 Implicit_Dereference => Element;
144 type Reference_Type (Element : not null access Element_Type) is private
145 with
146 Implicit_Dereference => Element;
148 function Constant_Reference
149 (Container : aliased Map;
150 Position : Cursor) return Constant_Reference_Type;
152 function Reference
153 (Container : aliased in out Map;
154 Position : Cursor) return Reference_Type;
156 function Constant_Reference
157 (Container : aliased Map;
158 Key : Key_Type) return Constant_Reference_Type;
160 function Reference
161 (Container : aliased in out Map;
162 Key : Key_Type) return Reference_Type;
164 procedure Assign (Target : in out Map; Source : Map);
165 -- If Target denotes the same object as Source, then the operation has no
166 -- effect. If the Target capacity is less than the Source length, then
167 -- Assign raises Capacity_Error. Otherwise, Assign clears Target and then
168 -- copies the (active) elements from Source to Target.
170 function Copy
171 (Source : Map;
172 Capacity : Count_Type := 0;
173 Modulus : Hash_Type := 0) return Map;
174 -- Constructs a new set object whose elements correspond to Source. If the
175 -- Capacity parameter is 0, then the capacity of the result is the same as
176 -- the length of Source. If the Capacity parameter is equal or greater than
177 -- the length of Source, then the capacity of the result is the specified
178 -- value. Otherwise, Copy raises Capacity_Error. If the Modulus parameter
179 -- is 0, then the modulus of the result is the value returned by a call to
180 -- Default_Modulus with the capacity parameter determined as above;
181 -- otherwise the modulus of the result is the specified value.
183 procedure Move (Target : in out Map; Source : in out Map);
184 -- Clears Target (if it's not empty), and then moves (not copies) the
185 -- buckets array and nodes from Source to Target.
187 procedure Insert
188 (Container : in out Map;
189 Key : Key_Type;
190 New_Item : Element_Type;
191 Position : out Cursor;
192 Inserted : out Boolean);
193 -- Conditionally inserts New_Item into the map. If Key is already in the
194 -- map, then Inserted returns False and Position designates the node
195 -- containing the existing key/element pair (neither of which is modified).
196 -- If Key is not already in the map, the Inserted returns True and Position
197 -- designates the newly-inserted node container Key and New_Item. The
198 -- search for the key works as follows. Hash is called to determine Key's
199 -- bucket; if the bucket is non-empty, then Equivalent_Keys is called to
200 -- compare Key to each node in that bucket. If the bucket is empty, or
201 -- there were no matching keys in the bucket, the search "fails" and the
202 -- key/item pair is inserted in the map (and Inserted returns True);
203 -- otherwise, the search "succeeds" (and Inserted returns False).
205 procedure Insert
206 (Container : in out Map;
207 Key : Key_Type;
208 Position : out Cursor;
209 Inserted : out Boolean);
210 -- The same as the (conditional) Insert that accepts an element parameter,
211 -- with the difference that if Inserted returns True, then the element of
212 -- the newly-inserted node is initialized to its default value.
214 procedure Insert
215 (Container : in out Map;
216 Key : Key_Type;
217 New_Item : Element_Type);
218 -- Attempts to insert Key into the map, performing the usual search (which
219 -- involves calling both Hash and Equivalent_Keys); if the search succeeds
220 -- (because Key is already in the map), then it raises Constraint_Error.
221 -- (This version of Insert is similar to Replace, but having the opposite
222 -- exception behavior. It is intended for use when you want to assert that
223 -- Key is not already in the map.)
225 procedure Include
226 (Container : in out Map;
227 Key : Key_Type;
228 New_Item : Element_Type);
229 -- Attempts to insert Key into the map. If Key is already in the map, then
230 -- both the existing key and element are assigned the values of Key and
231 -- New_Item, respectively. (This version of Insert only raises an exception
232 -- if cursor tampering occurs. It is intended for use when you want to
233 -- insert the key/element pair in the map, and you don't care whether Key
234 -- is already present.)
236 procedure Replace
237 (Container : in out Map;
238 Key : Key_Type;
239 New_Item : Element_Type);
240 -- Searches for Key in the map; if the search fails (because Key was not in
241 -- the map), then it raises Constraint_Error. Otherwise, both the existing
242 -- key and element are assigned the values of Key and New_Item rsp. (This
243 -- is similar to Insert, but with the opposite exception behavior. It is to
244 -- be used when you want to assert that Key is already in the map.)
246 procedure Exclude (Container : in out Map; Key : Key_Type);
247 -- Searches for Key in the map, and if found, removes its node from the map
248 -- and then deallocates it. The search works as follows. The operation
249 -- calls Hash to determine the key's bucket; if the bucket is not empty, it
250 -- calls Equivalent_Keys to compare Key to each key in the bucket. (This is
251 -- the deletion analog of Include. It is intended for use when you want to
252 -- remove the item from the map, but don't care whether the key is already
253 -- in the map.)
255 procedure Delete (Container : in out Map; Key : Key_Type);
256 -- Searches for Key in the map (which involves calling both Hash and
257 -- Equivalent_Keys). If the search fails, then the operation raises
258 -- Constraint_Error. Otherwise it removes the node from the map and then
259 -- deallocates it. (This is the deletion analog of non-conditional
260 -- Insert. It is intended for use when you want to assert that the item is
261 -- already in the map.)
263 procedure Delete (Container : in out Map; Position : in out Cursor);
264 -- Removes the node designated by Position from the map, and then
265 -- deallocates the node. The operation calls Hash to determine the bucket,
266 -- and then compares Position to each node in the bucket until there's a
267 -- match (it does not call Equivalent_Keys).
269 function First (Container : Map) return Cursor;
270 -- Returns a cursor that designates the first non-empty bucket, by
271 -- searching from the beginning of the buckets array.
273 function Next (Position : Cursor) return Cursor;
274 -- Returns a cursor that designates the node that follows the current one
275 -- designated by Position. If Position designates the last node in its
276 -- bucket, the operation calls Hash to compute the index of this bucket,
277 -- and searches the buckets array for the first non-empty bucket, starting
278 -- from that index; otherwise, it simply follows the link to the next node
279 -- in the same bucket.
281 procedure Next (Position : in out Cursor);
282 -- Equivalent to Position := Next (Position)
284 function Find (Container : Map; Key : Key_Type) return Cursor;
285 -- Searches for Key in the map. Find calls Hash to determine the key's
286 -- bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
287 -- Key to each key in the bucket. If the search succeeds, Find returns a
288 -- cursor designating the matching node; otherwise, it returns No_Element.
290 function Contains (Container : Map; Key : Key_Type) return Boolean;
291 -- Equivalent to Find (Container, Key) /= No_Element
293 function Element (Container : Map; Key : Key_Type) return Element_Type;
294 -- Equivalent to Element (Find (Container, Key))
296 function Equivalent_Keys (Left, Right : Cursor) return Boolean;
297 -- Returns the result of calling Equivalent_Keys with the keys of the nodes
298 -- designated by cursors Left and Right.
300 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
301 -- Returns the result of calling Equivalent_Keys with key of the node
302 -- designated by Left and key Right.
304 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
305 -- Returns the result of calling Equivalent_Keys with key Left and the node
306 -- designated by Right.
308 procedure Iterate
309 (Container : Map;
310 Process : not null access procedure (Position : Cursor));
311 -- Calls Process for each node in the map
313 function Iterate (Container : Map)
314 return Map_Iterator_Interfaces.Forward_Iterator'class;
316 private
317 pragma Inline (Length);
318 pragma Inline (Is_Empty);
319 pragma Inline (Clear);
320 pragma Inline (Key);
321 pragma Inline (Element);
322 pragma Inline (Move);
323 pragma Inline (Contains);
324 pragma Inline (Capacity);
325 pragma Inline (Reserve_Capacity);
326 pragma Inline (Has_Element);
327 pragma Inline (Next);
329 type Node_Type is record
330 Key : Key_Type;
331 Element : aliased Element_Type;
332 Next : Count_Type;
333 end record;
335 package HT_Types is
336 new Hash_Tables.Generic_Bounded_Hash_Table_Types (Node_Type);
338 type Map (Capacity : Count_Type; Modulus : Hash_Type) is
339 new HT_Types.Hash_Table_Type (Capacity, Modulus) with null record;
341 use HT_Types;
342 use Ada.Streams;
343 use Ada.Finalization;
345 procedure Write
346 (Stream : not null access Root_Stream_Type'Class;
347 Container : Map);
349 for Map'Write use Write;
351 procedure Read
352 (Stream : not null access Root_Stream_Type'Class;
353 Container : out Map);
355 for Map'Read use Read;
357 type Map_Access is access all Map;
358 for Map_Access'Storage_Size use 0;
360 -- Note: If a Cursor object has no explicit initialization expression,
361 -- it must default initialize to the same value as constant No_Element.
362 -- The Node component of type Cursor has scalar type Count_Type, so it
363 -- requires an explicit initialization expression of its own declaration,
364 -- in order for objects of record type Cursor to properly initialize.
366 type Cursor is record
367 Container : Map_Access;
368 Node : Count_Type := 0;
369 end record;
371 procedure Read
372 (Stream : not null access Root_Stream_Type'Class;
373 Item : out Cursor);
375 for Cursor'Read use Read;
377 procedure Write
378 (Stream : not null access Root_Stream_Type'Class;
379 Item : Cursor);
381 for Cursor'Write use Write;
383 type Reference_Control_Type is new Controlled with record
384 Container : Map_Access;
385 end record;
387 overriding procedure Adjust (Control : in out Reference_Control_Type);
388 pragma Inline (Adjust);
390 overriding procedure Finalize (Control : in out Reference_Control_Type);
391 pragma Inline (Finalize);
393 type Constant_Reference_Type
394 (Element : not null access constant Element_Type) is
395 record
396 Control : Reference_Control_Type :=
397 raise Program_Error with "uninitialized reference";
398 -- The RM says, "The default initialization of an object of
399 -- type Constant_Reference_Type or Reference_Type propagates
400 -- Program_Error."
401 end record;
403 procedure Write
404 (Stream : not null access Root_Stream_Type'Class;
405 Item : Constant_Reference_Type);
407 for Constant_Reference_Type'Write use Write;
409 procedure Read
410 (Stream : not null access Root_Stream_Type'Class;
411 Item : out Constant_Reference_Type);
413 for Constant_Reference_Type'Read use Read;
415 type Reference_Type (Element : not null access Element_Type) is record
416 Control : Reference_Control_Type :=
417 raise Program_Error with "uninitialized reference";
418 -- The RM says, "The default initialization of an object of
419 -- type Constant_Reference_Type or Reference_Type propagates
420 -- Program_Error."
421 end record;
423 procedure Write
424 (Stream : not null access Root_Stream_Type'Class;
425 Item : Reference_Type);
427 for Reference_Type'Write use Write;
429 procedure Read
430 (Stream : not null access Root_Stream_Type'Class;
431 Item : out Reference_Type);
433 for Reference_Type'Read use Read;
435 Empty_Map : constant Map :=
436 (Hash_Table_Type with Capacity => 0, Modulus => 0);
438 No_Element : constant Cursor := (Container => null, Node => 0);
440 type Iterator is new Limited_Controlled and
441 Map_Iterator_Interfaces.Forward_Iterator with
442 record
443 Container : Map_Access;
444 end record;
446 overriding procedure Finalize (Object : in out Iterator);
448 overriding function First (Object : Iterator) return Cursor;
450 overriding function Next
451 (Object : Iterator;
452 Position : Cursor) return Cursor;
454 end Ada.Containers.Bounded_Hashed_Maps;