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-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.Streams;
38
39generic
40   type Key_Type is private;
41   type Element_Type is private;
42
43   with function Hash (Key : Key_Type) return Hash_Type;
44   with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
45   with function "=" (Left, Right : Element_Type) return Boolean is <>;
46
47package Ada.Containers.Bounded_Hashed_Maps is
48   pragma Pure;
49   pragma Remote_Types;
50
51   type Map (Capacity : Count_Type; Modulus : Hash_Type) is tagged private with
52      Constant_Indexing => Constant_Reference,
53      Variable_Indexing => Reference,
54      Default_Iterator  => Iterate,
55      Iterator_Element  => Element_Type;
56
57   pragma Preelaborable_Initialization (Map);
58
59   type Cursor is private;
60   pragma Preelaborable_Initialization (Cursor);
61
62   Empty_Map : constant Map;
63   --  Map objects declared without an initialization expression are
64   --  initialized to the value Empty_Map.
65
66   No_Element : constant Cursor;
67   --  Cursor objects declared without an initialization expression are
68   --  initialized to the value No_Element.
69
70   function Has_Element (Position : Cursor) return Boolean;
71   --  Equivalent to Position /= No_Element
72
73   package Map_Iterator_Interfaces is new
74     Ada.Iterator_Interfaces (Cursor, Has_Element);
75
76   function "=" (Left, Right : Map) return Boolean;
77   --  For each key/element pair in Left, equality attempts to find the key in
78   --  Right; if a search fails the equality returns False. The search works by
79   --  calling Hash to find the bucket in the Right map that corresponds to the
80   --  Left key. If bucket is non-empty, then equality calls Equivalent_Keys
81   --  to compare the key (in Left) to the key of each node in the bucket (in
82   --  Right); if the keys are equivalent, then the equality test for this
83   --  key/element pair (in Left) completes by calling the element equality
84   --  operator to compare the element (in Left) to the element of the node
85   --  (in Right) whose key matched.
86
87   function Capacity (Container : Map) return Count_Type;
88   --  Returns the current capacity of the map. Capacity is the maximum length
89   --  before which rehashing in guaranteed not to occur.
90
91   procedure Reserve_Capacity (Container : in out Map; Capacity : Count_Type);
92   --  If the value of the Capacity actual parameter is less or equal to
93   --  Container.Capacity, then the operation has no effect.  Otherwise it
94   --  raises Capacity_Error (as no expansion of capacity is possible for a
95   --  bounded form).
96
97   function Default_Modulus (Capacity : Count_Type) return Hash_Type;
98   --  Returns a modulus value (hash table size) which is optimal for the
99   --  specified capacity (which corresponds to the maximum number of items).
100
101   function Length (Container : Map) return Count_Type;
102   --  Returns the number of items in the map
103
104   function Is_Empty (Container : Map) return Boolean;
105   --  Equivalent to Length (Container) = 0
106
107   procedure Clear (Container : in out Map);
108   --  Removes all of the items from the map
109
110   function Key (Position : Cursor) return Key_Type;
111   --  Returns the key of the node designated by the cursor
112
113   function Element (Position : Cursor) return Element_Type;
114   --  Returns the element of the node designated by the cursor
115
116   procedure Replace_Element
117     (Container : in out Map;
118      Position  : Cursor;
119      New_Item  : Element_Type);
120   --  Assigns the value New_Item to the element designated by the cursor
121
122   procedure Query_Element
123     (Position : Cursor;
124      Process  : not null access
125                   procedure (Key : Key_Type; Element : Element_Type));
126   --  Calls Process with the key and element (both having only a constant
127   --  view) of the node designed by the cursor.
128
129   procedure Update_Element
130     (Container : in out Map;
131      Position  : Cursor;
132      Process   : not null access
133                    procedure (Key : Key_Type; Element : in out Element_Type));
134   --  Calls Process with the key (with only a constant view) and element (with
135   --  a variable view) of the node designed by the cursor.
136
137   type Constant_Reference_Type
138      (Element : not null access constant Element_Type) is
139   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
151   function Reference
152     (Container : aliased in out Map;
153      Position  : Cursor) return Reference_Type;
154
155   function Constant_Reference
156     (Container : aliased Map;
157      Key       : Key_Type) return Constant_Reference_Type;
158
159   function Reference
160     (Container : aliased in out Map;
161      Key       : Key_Type) return Reference_Type;
162
163   procedure Assign (Target : in out Map; Source : Map);
164   --  If Target denotes the same object as Source, then the operation has no
165   --  effect. If the Target capacity is less than the Source length, then
166   --  Assign raises Capacity_Error.  Otherwise, Assign clears Target and then
167   --  copies the (active) elements from Source to Target.
168
169   function Copy
170     (Source   : Map;
171      Capacity : Count_Type := 0;
172      Modulus  : Hash_Type := 0) return Map;
173   --  Constructs a new set object whose elements correspond to Source.  If the
174   --  Capacity parameter is 0, then the capacity of the result is the same as
175   --  the length of Source. If the Capacity parameter is equal or greater than
176   --  the length of Source, then the capacity of the result is the specified
177   --  value. Otherwise, Copy raises Capacity_Error. If the Modulus parameter
178   --  is 0, then the modulus of the result is the value returned by a call to
179   --  Default_Modulus with the capacity parameter determined as above;
180   --  otherwise the modulus of the result is the specified value.
181
182   procedure Move (Target : in out Map; Source : in out Map);
183   --  Clears Target (if it's not empty), and then moves (not copies) the
184   --  buckets array and nodes from Source to Target.
185
186   procedure Insert
187     (Container : in out Map;
188      Key       : Key_Type;
189      New_Item  : Element_Type;
190      Position  : out Cursor;
191      Inserted  : out Boolean);
192   --  Conditionally inserts New_Item into the map. If Key is already in the
193   --  map, then Inserted returns False and Position designates the node
194   --  containing the existing key/element pair (neither of which is modified).
195   --  If Key is not already in the map, the Inserted returns True and Position
196   --  designates the newly-inserted node container Key and New_Item. The
197   --  search for the key works as follows. Hash is called to determine Key's
198   --  bucket; if the bucket is non-empty, then Equivalent_Keys is called to
199   --  compare Key to each node in that bucket. If the bucket is empty, or
200   --  there were no matching keys in the bucket, the search "fails" and the
201   --  key/item pair is inserted in the map (and Inserted returns True);
202   --  otherwise, the search "succeeds" (and Inserted returns False).
203
204   procedure Insert
205     (Container : in out Map;
206      Key       : Key_Type;
207      Position  : out Cursor;
208      Inserted  : out Boolean);
209   --  The same as the (conditional) Insert that accepts an element parameter,
210   --  with the difference that if Inserted returns True, then the element of
211   --  the newly-inserted node is initialized to its default value.
212
213   procedure Insert
214     (Container : in out Map;
215      Key       : Key_Type;
216      New_Item  : Element_Type);
217   --  Attempts to insert Key into the map, performing the usual search (which
218   --  involves calling both Hash and Equivalent_Keys); if the search succeeds
219   --  (because Key is already in the map), then it raises Constraint_Error.
220   --  (This version of Insert is similar to Replace, but having the opposite
221   --  exception behavior. It is intended for use when you want to assert that
222   --  Key is not already in the map.)
223
224   procedure Include
225     (Container : in out Map;
226      Key       : Key_Type;
227      New_Item  : Element_Type);
228   --  Attempts to insert Key into the map. If Key is already in the map, then
229   --  both the existing key and element are assigned the values of Key and
230   --  New_Item, respectively. (This version of Insert only raises an exception
231   --  if cursor tampering occurs. It is intended for use when you want to
232   --  insert the key/element pair in the map, and you don't care whether Key
233   --  is already present.)
234
235   procedure Replace
236     (Container : in out Map;
237      Key       : Key_Type;
238      New_Item  : Element_Type);
239   --  Searches for Key in the map; if the search fails (because Key was not in
240   --  the map), then it raises Constraint_Error. Otherwise, both the existing
241   --  key and element are assigned the values of Key and New_Item rsp. (This
242   --  is similar to Insert, but with the opposite exception behavior. It is to
243   --  be used when you want to assert that Key is already in the map.)
244
245   procedure Exclude (Container : in out Map; Key : Key_Type);
246   --  Searches for Key in the map, and if found, removes its node from the map
247   --  and then deallocates it. The search works as follows. The operation
248   --  calls Hash to determine the key's bucket; if the bucket is not empty, it
249   --  calls Equivalent_Keys to compare Key to each key in the bucket. (This is
250   --  the deletion analog of Include. It is intended for use when you want to
251   --  remove the item from the map, but don't care whether the key is already
252   --  in the map.)
253
254   procedure Delete (Container : in out Map; Key : Key_Type);
255   --  Searches for Key in the map (which involves calling both Hash and
256   --  Equivalent_Keys). If the search fails, then the operation raises
257   --  Constraint_Error. Otherwise it removes the node from the map and then
258   --  deallocates it. (This is the deletion analog of non-conditional
259   --  Insert. It is intended for use when you want to assert that the item is
260   --  already in the map.)
261
262   procedure Delete (Container : in out Map; Position : in out Cursor);
263   --  Removes the node designated by Position from the map, and then
264   --  deallocates the node. The operation calls Hash to determine the bucket,
265   --  and then compares Position to each node in the bucket until there's a
266   --  match (it does not call Equivalent_Keys).
267
268   function First (Container : Map) return Cursor;
269   --  Returns a cursor that designates the first non-empty bucket, by
270   --  searching from the beginning of the buckets array.
271
272   function Next (Position : Cursor) return Cursor;
273   --  Returns a cursor that designates the node that follows the current one
274   --  designated by Position. If Position designates the last node in its
275   --  bucket, the operation calls Hash to compute the index of this bucket,
276   --  and searches the buckets array for the first non-empty bucket, starting
277   --  from that index; otherwise, it simply follows the link to the next node
278   --  in the same bucket.
279
280   procedure Next (Position : in out Cursor);
281   --  Equivalent to Position := Next (Position)
282
283   function Find (Container : Map; Key : Key_Type) return Cursor;
284   --  Searches for Key in the map. Find calls Hash to determine the key's
285   --  bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
286   --  Key to each key in the bucket. If the search succeeds, Find returns a
287   --  cursor designating the matching node; otherwise, it returns No_Element.
288
289   function Contains (Container : Map; Key : Key_Type) return Boolean;
290   --  Equivalent to Find (Container, Key) /= No_Element
291
292   function Element (Container : Map; Key : Key_Type) return Element_Type;
293   --  Equivalent to Element (Find (Container, Key))
294
295   function Equivalent_Keys (Left, Right : Cursor) return Boolean;
296   --  Returns the result of calling Equivalent_Keys with the keys of the nodes
297   --  designated by cursors Left and Right.
298
299   function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
300   --  Returns the result of calling Equivalent_Keys with key of the node
301   --  designated by Left and key Right.
302
303   function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
304   --  Returns the result of calling Equivalent_Keys with key Left and the node
305   --  designated by Right.
306
307   procedure Iterate
308     (Container : Map;
309      Process   : not null access procedure (Position : Cursor));
310   --  Calls Process for each node in the map
311
312   function Iterate (Container : Map)
313      return Map_Iterator_Interfaces.Forward_Iterator'class;
314
315private
316   pragma Inline (Length);
317   pragma Inline (Is_Empty);
318   pragma Inline (Clear);
319   pragma Inline (Key);
320   pragma Inline (Element);
321   pragma Inline (Move);
322   pragma Inline (Contains);
323   pragma Inline (Capacity);
324   pragma Inline (Reserve_Capacity);
325   pragma Inline (Has_Element);
326   pragma Inline (Next);
327
328   type Node_Type is record
329      Key     : Key_Type;
330      Element : aliased Element_Type;
331      Next    : Count_Type;
332   end record;
333
334   package HT_Types is
335     new Hash_Tables.Generic_Bounded_Hash_Table_Types (Node_Type);
336
337   type Map (Capacity : Count_Type; Modulus : Hash_Type) is
338      new HT_Types.Hash_Table_Type (Capacity, Modulus) with null record;
339
340   use HT_Types;
341   use Ada.Streams;
342
343   procedure Write
344     (Stream    : not null access Root_Stream_Type'Class;
345      Container : Map);
346
347   for Map'Write use Write;
348
349   procedure Read
350     (Stream    : not null access Root_Stream_Type'Class;
351      Container : out Map);
352
353   for Map'Read use Read;
354
355   type Map_Access is access all Map;
356   for Map_Access'Storage_Size use 0;
357
358   --  Note: If a Cursor object has no explicit initialization expression,
359   --  it must default initialize to the same value as constant No_Element.
360   --  The Node component of type Cursor has scalar type Count_Type, so it
361   --  requires an explicit initialization expression of its own declaration,
362   --  in order for objects of record type Cursor to properly initialize.
363
364   type Cursor is record
365      Container : Map_Access;
366      Node      : Count_Type := 0;
367   end record;
368
369   procedure Read
370     (Stream : not null access Root_Stream_Type'Class;
371      Item   : out Cursor);
372
373   for Cursor'Read use Read;
374
375   procedure Write
376     (Stream : not null access Root_Stream_Type'Class;
377      Item   : Cursor);
378
379   for Cursor'Write use Write;
380
381   type Constant_Reference_Type
382      (Element : not null access constant Element_Type) is null record;
383
384   procedure Write
385     (Stream : not null access Root_Stream_Type'Class;
386      Item   : Constant_Reference_Type);
387
388   for Constant_Reference_Type'Write use Write;
389
390   procedure Read
391     (Stream : not null access Root_Stream_Type'Class;
392      Item   : out Constant_Reference_Type);
393
394   for Constant_Reference_Type'Read use Read;
395
396   type Reference_Type
397      (Element : not null access Element_Type) is null record;
398
399   procedure Write
400     (Stream : not null access Root_Stream_Type'Class;
401      Item   : Reference_Type);
402
403   for Reference_Type'Write use Write;
404
405   procedure Read
406     (Stream : not null access Root_Stream_Type'Class;
407      Item   : out Reference_Type);
408
409   for Reference_Type'Read use Read;
410
411   Empty_Map : constant Map :=
412     (Hash_Table_Type with Capacity => 0, Modulus => 0);
413
414   No_Element : constant Cursor := (Container => null, Node => 0);
415
416end Ada.Containers.Bounded_Hashed_Maps;
417