1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT LIBRARY COMPONENTS                          --
4--                                                                          --
5--                  ADA.CONTAINERS.INDEFINITE_HASHED_MAPS                   --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2012, 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------------------------------------------------------------------------------
33
34with Ada.Iterator_Interfaces;
35
36private with Ada.Containers.Hash_Tables;
37private with Ada.Finalization;
38private with Ada.Streams;
39
40generic
41   type Key_Type (<>) is private;
42   type Element_Type (<>) is private;
43
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 <>;
47
48package Ada.Containers.Indefinite_Hashed_Maps is
49   pragma Preelaborate;
50   pragma Remote_Types;
51
52   type Map is tagged private with
53      Constant_Indexing => Constant_Reference,
54      Variable_Indexing => Reference,
55      Default_Iterator  => Iterate,
56      Iterator_Element  => Element_Type;
57
58   pragma Preelaborable_Initialization (Map);
59
60   type Cursor is private;
61   pragma Preelaborable_Initialization (Cursor);
62
63   Empty_Map : constant Map;
64   --  Map objects declared without an initialization expression are
65   --  initialized to the value Empty_Map.
66
67   No_Element : constant Cursor;
68   --  Cursor objects declared without an initialization expression are
69   --  initialized to the value No_Element.
70
71   function Has_Element (Position : Cursor) return Boolean;
72   --  Equivalent to Position /= No_Element
73
74   package Map_Iterator_Interfaces is new
75     Ada.Iterator_Interfaces (Cursor, Has_Element);
76
77   overriding 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.
87
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.
91
92   procedure Reserve_Capacity (Container : in out Map; Capacity : Count_Type);
93   --  Adjusts the current capacity, by allocating a new buckets array. If the
94   --  requested capacity is less than the current capacity, then the capacity
95   --  is contracted (to a value not less than the current length). If the
96   --  requested capacity is greater than the current capacity, then the
97   --  capacity is expanded (to a value not less than what is requested). In
98   --  either case, the nodes are rehashed from the old buckets array onto the
99   --  new buckets array (Hash is called once for each existing key in order to
100   --  compute the new index), and then the old buckets array is deallocated.
101
102   function Length (Container : Map) return Count_Type;
103   --  Returns the number of items in the map
104
105   function Is_Empty (Container : Map) return Boolean;
106   --  Equivalent to Length (Container) = 0
107
108   procedure Clear (Container : in out Map);
109   --  Removes all of the items from the map
110
111   function Key (Position : Cursor) return Key_Type;
112   --  Returns the key of the node designated by the cursor
113
114   function Element (Position : Cursor) return Element_Type;
115   --  Returns the element of the node designated by the cursor
116
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
122
123   procedure Query_Element
124     (Position : Cursor;
125      Process  : not null access procedure (Key     : Key_Type;
126                                            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.
129
130   procedure Update_Element
131     (Container : in out Map;
132      Position  : Cursor;
133      Process   : not null access procedure (Key     : Key_Type;
134                                             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.
137
138   type Constant_Reference_Type
139      (Element : not null access constant Element_Type) is private
140   with
141      Implicit_Dereference => Element;
142
143   type Reference_Type (Element : not null access Element_Type) is private
144   with
145      Implicit_Dereference => Element;
146
147   function Constant_Reference
148     (Container : aliased Map;
149      Position  : Cursor) return Constant_Reference_Type;
150   pragma Inline (Constant_Reference);
151
152   function Reference
153     (Container : aliased in out Map;
154      Position  : Cursor) return Reference_Type;
155   pragma Inline (Reference);
156
157   function Constant_Reference
158     (Container : aliased Map;
159      Key       : Key_Type) return Constant_Reference_Type;
160   pragma Inline (Constant_Reference);
161
162   function Reference
163     (Container : aliased in out Map;
164      Key       : Key_Type) return Reference_Type;
165   pragma Inline (Reference);
166
167   procedure Assign (Target : in out Map; Source : Map);
168
169   function Copy (Source : Map; Capacity : Count_Type := 0) return Map;
170
171   procedure Move (Target : in out Map; Source : in out Map);
172   --  Clears Target (if it's not empty), and then moves (not copies) the
173   --  buckets array and nodes from Source to Target.
174
175   procedure Insert
176     (Container : in out Map;
177      Key       : Key_Type;
178      New_Item  : Element_Type;
179      Position  : out Cursor;
180      Inserted  : out Boolean);
181   --  Conditionally inserts New_Item into the map. If Key is already in the
182   --  map, then Inserted returns False and Position designates the node
183   --  containing the existing key/element pair (neither of which is modified).
184   --  If Key is not already in the map, the Inserted returns True and Position
185   --  designates the newly-inserted node container Key and New_Item. The
186   --  search for the key works as follows. Hash is called to determine Key's
187   --  bucket; if the bucket is non-empty, then Equivalent_Keys is called to
188   --  compare Key to each node in that bucket. If the bucket is empty, or
189   --  there were no matching keys in the bucket, the search "fails" and the
190   --  key/item pair is inserted in the map (and Inserted returns True);
191   --  otherwise, the search "succeeds" (and Inserted returns False).
192
193   procedure Insert
194     (Container : in out Map;
195      Key       : Key_Type;
196      New_Item  : Element_Type);
197   --  Attempts to insert Key into the map, performing the usual search (which
198   --  involves calling both Hash and Equivalent_Keys); if the search succeeds
199   --  (because Key is already in the map), then it raises Constraint_Error.
200   --  (This version of Insert is similar to Replace, but having the opposite
201   --  exception behavior. It is intended for use when you want to assert that
202   --  Key is not already in the map.)
203
204   procedure Include
205     (Container : in out Map;
206      Key       : Key_Type;
207      New_Item  : Element_Type);
208   --  Attempts to insert Key into the map. If Key is already in the map, then
209   --  both the existing key and element are assigned the values of Key and
210   --  New_Item, respectively. (This version of Insert only raises an exception
211   --  if cursor tampering occurs. It is intended for use when you want to
212   --  insert the key/element pair in the map, and you don't care whether Key
213   --  is already present.)
214
215   procedure Replace
216     (Container : in out Map;
217      Key       : Key_Type;
218      New_Item  : Element_Type);
219   --  Searches for Key in the map; if the search fails (because Key was not in
220   --  the map), then it raises Constraint_Error. Otherwise, both the existing
221   --  key and element are assigned the values of Key and New_Item rsp. (This
222   --  is similar to Insert, but with the opposite exception behavior. It is
223   --  intended for use when you want to assert that Key is already in the
224   --  map.)
225
226   procedure Exclude (Container : in out Map; Key : Key_Type);
227   --  Searches for Key in the map, and if found, removes its node from the map
228   --  and then deallocates it. The search works as follows. The operation
229   --  calls Hash to determine the key's bucket; if the bucket is not empty, it
230   --  calls Equivalent_Keys to compare Key to each key in the bucket. (This is
231   --  the deletion analog of Include. It is intended for use when you want to
232   --  remove the item from the map, but don't care whether the key is already
233   --  in the map.)
234
235   procedure Delete (Container : in out Map; Key : Key_Type);
236   --  Searches for Key in the map (which involves calling both Hash and
237   --  Equivalent_Keys). If the search fails, then the operation raises
238   --  Constraint_Error. Otherwise it removes the node from the map and then
239   --  deallocates it. (This is the deletion analog of non-conditional
240   --  Insert. It is intended for use when you want to assert that the item is
241   --  already in the map.)
242
243   procedure Delete (Container : in out Map; Position : in out Cursor);
244   --  Removes the node designated by Position from the map, and then
245   --  deallocates the node. The operation calls Hash to determine the bucket,
246   --  and then compares Position to each node in the bucket until there's a
247   --  match (it does not call Equivalent_Keys).
248
249   function First (Container : Map) return Cursor;
250   --  Returns a cursor that designates the first non-empty bucket, by
251   --  searching from the beginning of the buckets array.
252
253   function Next (Position : Cursor) return Cursor;
254   --  Returns a cursor that designates the node that follows the current one
255   --  designated by Position. If Position designates the last node in its
256   --  bucket, the operation calls Hash to compute the index of this bucket,
257   --  and searches the buckets array for the first non-empty bucket, starting
258   --  from that index; otherwise, it simply follows the link to the next node
259   --  in the same bucket.
260
261   procedure Next (Position : in out Cursor);
262   --  Equivalent to Position := Next (Position)
263
264   function Find (Container : Map; Key : Key_Type) return Cursor;
265   --  Searches for Key in the map. Find calls Hash to determine the key's
266   --  bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
267   --  Key to each key in the bucket. If the search succeeds, Find returns a
268   --  cursor designating the matching node; otherwise, it returns No_Element.
269
270   function Contains (Container : Map; Key : Key_Type) return Boolean;
271   --  Equivalent to Find (Container, Key) /= No_Element
272
273   function Element (Container : Map; Key : Key_Type) return Element_Type;
274   --  Equivalent to Element (Find (Container, Key))
275
276   function Equivalent_Keys (Left, Right : Cursor) return Boolean;
277   --  Returns the result of calling Equivalent_Keys with the keys of the nodes
278   --  designated by cursors Left and Right.
279
280   function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
281   --  Returns the result of calling Equivalent_Keys with key of the node
282   --  designated by Left and key Right.
283
284   function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
285   --  Returns the result of calling Equivalent_Keys with key Left and the node
286   --  designated by Right.
287
288   procedure Iterate
289     (Container : Map;
290      Process   : not null access procedure (Position : Cursor));
291   --  Calls Process for each node in the map
292
293   function Iterate (Container : Map)
294      return Map_Iterator_Interfaces.Forward_Iterator'class;
295
296private
297   pragma Inline ("=");
298   pragma Inline (Length);
299   pragma Inline (Is_Empty);
300   pragma Inline (Clear);
301   pragma Inline (Key);
302   pragma Inline (Element);
303   pragma Inline (Move);
304   pragma Inline (Contains);
305   pragma Inline (Capacity);
306   pragma Inline (Reserve_Capacity);
307   pragma Inline (Has_Element);
308   pragma Inline (Equivalent_Keys);
309   pragma Inline (Next);
310
311   type Node_Type;
312   type Node_Access is access Node_Type;
313
314   type Key_Access is access Key_Type;
315   type Element_Access is access Element_Type;
316
317   type Node_Type is limited record
318      Key     : Key_Access;
319      Element : Element_Access;
320      Next    : Node_Access;
321   end record;
322
323   package HT_Types is
324      new Hash_Tables.Generic_Hash_Table_Types (Node_Type, Node_Access);
325
326   type Map is new Ada.Finalization.Controlled with record
327      HT : HT_Types.Hash_Table_Type;
328   end record;
329
330   overriding procedure Adjust   (Container : in out Map);
331
332   overriding procedure Finalize (Container : in out Map);
333
334   use HT_Types;
335   use Ada.Finalization;
336   use Ada.Streams;
337
338   procedure Write
339     (Stream    : not null access Root_Stream_Type'Class;
340      Container : Map);
341
342   for Map'Write use Write;
343
344   procedure Read
345     (Stream    : not null access Root_Stream_Type'Class;
346      Container : out Map);
347
348   for Map'Read use Read;
349
350   type Map_Access is access all Map;
351   for Map_Access'Storage_Size use 0;
352
353   type Cursor is record
354      Container : Map_Access;
355      Node      : Node_Access;
356   end record;
357
358   procedure Write
359     (Stream : not null access Root_Stream_Type'Class;
360      Item   : Cursor);
361
362   for Cursor'Write use Write;
363
364   procedure Read
365     (Stream : not null access Root_Stream_Type'Class;
366      Item   : out Cursor);
367
368   for Cursor'Read use Read;
369
370   type Reference_Control_Type is
371      new Controlled with record
372         Container : Map_Access;
373      end record;
374
375   overriding procedure Adjust (Control : in out Reference_Control_Type);
376   pragma Inline (Adjust);
377
378   overriding procedure Finalize (Control : in out Reference_Control_Type);
379   pragma Inline (Finalize);
380
381   type Constant_Reference_Type
382      (Element : not null access constant Element_Type) is
383      record
384         Control : Reference_Control_Type;
385      end record;
386
387   procedure Write
388     (Stream : not null access Root_Stream_Type'Class;
389      Item   : Constant_Reference_Type);
390
391   for Constant_Reference_Type'Write use Write;
392
393   procedure Read
394     (Stream : not null access Root_Stream_Type'Class;
395      Item   : out Constant_Reference_Type);
396
397   for Constant_Reference_Type'Read use Read;
398
399   type Reference_Type
400      (Element : not null access Element_Type) is
401      record
402         Control : Reference_Control_Type;
403      end record;
404
405   procedure Write
406     (Stream : not null access Root_Stream_Type'Class;
407      Item   : Reference_Type);
408
409   for Reference_Type'Write use Write;
410
411   procedure Read
412     (Stream : not null access Root_Stream_Type'Class;
413      Item   : out Reference_Type);
414
415   for Reference_Type'Read use Read;
416
417   Empty_Map : constant Map := (Controlled with HT => (null, 0, 0, 0));
418
419   No_Element : constant Cursor := (Container => null, Node => null);
420
421end Ada.Containers.Indefinite_Hashed_Maps;
422