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 _ O R D E R E D _ M A P S  --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2014, 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.Red_Black_Trees;
37private with Ada.Streams;
38private with Ada.Finalization;
39
40generic
41   type Key_Type is private;
42   type Element_Type is private;
43
44   with function "<" (Left, Right : Key_Type) return Boolean is <>;
45   with function "=" (Left, Right : Element_Type) return Boolean is <>;
46
47package Ada.Containers.Bounded_Ordered_Maps is
48   pragma Pure;
49   pragma Remote_Types;
50
51   function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
52
53   type Map (Capacity : Count_Type) is tagged private with
54      Constant_Indexing => Constant_Reference,
55      Variable_Indexing => Reference,
56      Default_Iterator  => Iterate,
57      Iterator_Element  => Element_Type;
58
59   pragma Preelaborable_Initialization (Map);
60
61   type Cursor is private;
62   pragma Preelaborable_Initialization (Cursor);
63
64   Empty_Map : constant Map;
65
66   No_Element : constant Cursor;
67
68   function Has_Element (Position : Cursor) return Boolean;
69
70   package Map_Iterator_Interfaces is new
71     Ada.Iterator_Interfaces (Cursor, Has_Element);
72
73   function "=" (Left, Right : Map) return Boolean;
74
75   function Length (Container : Map) return Count_Type;
76
77   function Is_Empty (Container : Map) return Boolean;
78
79   procedure Clear (Container : in out Map);
80
81   function Key (Position : Cursor) return Key_Type;
82
83   function Element (Position : Cursor) return Element_Type;
84
85   procedure Replace_Element
86     (Container : in out Map;
87      Position  : Cursor;
88      New_Item  : Element_Type);
89
90   procedure Query_Element
91     (Position : Cursor;
92      Process  : not null access
93                   procedure (Key : Key_Type; Element : Element_Type));
94
95   procedure Update_Element
96     (Container : in out Map;
97      Position  : Cursor;
98      Process   : not null access
99                    procedure (Key : Key_Type; Element : in out Element_Type));
100
101   type Constant_Reference_Type
102      (Element : not null access constant Element_Type) is private
103   with
104      Implicit_Dereference => Element;
105
106   type Reference_Type (Element : not null access Element_Type) is private
107   with
108      Implicit_Dereference => Element;
109
110   function Constant_Reference
111     (Container : aliased Map;
112      Position  : Cursor) return Constant_Reference_Type;
113
114   function Reference
115     (Container : aliased in out Map;
116      Position  : Cursor) return Reference_Type;
117
118   function Constant_Reference
119     (Container : aliased Map;
120      Key       : Key_Type) return Constant_Reference_Type;
121
122   function Reference
123     (Container : aliased in out Map;
124      Key       : Key_Type) return Reference_Type;
125
126   procedure Assign (Target : in out Map; Source : Map);
127
128   function Copy (Source : Map; Capacity : Count_Type := 0) return Map;
129
130   procedure Move (Target : in out Map; Source : in out Map);
131
132   procedure Insert
133     (Container : in out Map;
134      Key       : Key_Type;
135      New_Item  : Element_Type;
136      Position  : out Cursor;
137      Inserted  : out Boolean);
138
139   procedure Insert
140     (Container : in out Map;
141      Key       : Key_Type;
142      Position  : out Cursor;
143      Inserted  : out Boolean);
144
145   procedure Insert
146     (Container : in out Map;
147      Key       : Key_Type;
148      New_Item  : Element_Type);
149
150   procedure Include
151     (Container : in out Map;
152      Key       : Key_Type;
153      New_Item  : Element_Type);
154
155   procedure Replace
156     (Container : in out Map;
157      Key       : Key_Type;
158      New_Item  : Element_Type);
159
160   procedure Exclude (Container : in out Map; Key : Key_Type);
161
162   procedure Delete (Container : in out Map; Key : Key_Type);
163
164   procedure Delete (Container : in out Map; Position : in out Cursor);
165
166   procedure Delete_First (Container : in out Map);
167
168   procedure Delete_Last (Container : in out Map);
169
170   function First (Container : Map) return Cursor;
171
172   function First_Element (Container : Map) return Element_Type;
173
174   function First_Key (Container : Map) return Key_Type;
175
176   function Last (Container : Map) return Cursor;
177
178   function Last_Element (Container : Map) return Element_Type;
179
180   function Last_Key (Container : Map) return Key_Type;
181
182   function Next (Position : Cursor) return Cursor;
183
184   procedure Next (Position : in out Cursor);
185
186   function Previous (Position : Cursor) return Cursor;
187
188   procedure Previous (Position : in out Cursor);
189
190   function Find (Container : Map; Key : Key_Type) return Cursor;
191
192   function Element (Container : Map; Key : Key_Type) return Element_Type;
193
194   function Floor (Container : Map; Key : Key_Type) return Cursor;
195
196   function Ceiling (Container : Map; Key : Key_Type) return Cursor;
197
198   function Contains (Container : Map; Key : Key_Type) return Boolean;
199
200   function "<" (Left, Right : Cursor) return Boolean;
201
202   function ">" (Left, Right : Cursor) return Boolean;
203
204   function "<" (Left : Cursor; Right : Key_Type) return Boolean;
205
206   function ">" (Left : Cursor; Right : Key_Type) return Boolean;
207
208   function "<" (Left : Key_Type; Right : Cursor) return Boolean;
209
210   function ">" (Left : Key_Type; Right : Cursor) return Boolean;
211
212   procedure Iterate
213     (Container : Map;
214      Process   : not null access procedure (Position : Cursor));
215
216   procedure Reverse_Iterate
217     (Container : Map;
218      Process   : not null access procedure (Position : Cursor));
219
220   function Iterate
221     (Container : Map)
222      return Map_Iterator_Interfaces.Reversible_Iterator'Class;
223
224   function Iterate
225     (Container : Map;
226      Start     : Cursor)
227      return Map_Iterator_Interfaces.Reversible_Iterator'Class;
228
229private
230
231   use Ada.Finalization;
232   pragma Inline (Next);
233   pragma Inline (Previous);
234
235   type Node_Type is record
236      Parent  : Count_Type;
237      Left    : Count_Type;
238      Right   : Count_Type;
239      Color   : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
240      Key     : Key_Type;
241      Element : aliased Element_Type;
242   end record;
243
244   package Tree_Types is
245     new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
246
247   type Map (Capacity : Count_Type) is
248     new Tree_Types.Tree_Type (Capacity) with null record;
249
250   use Red_Black_Trees;
251   use Tree_Types;
252   use Ada.Streams;
253
254   procedure Write
255     (Stream    : not null access Root_Stream_Type'Class;
256      Container : Map);
257
258   for Map'Write use Write;
259
260   procedure Read
261     (Stream    : not null access Root_Stream_Type'Class;
262      Container : out Map);
263
264   for Map'Read use Read;
265
266   type Map_Access is access all Map;
267   for Map_Access'Storage_Size use 0;
268
269   type Cursor is record
270      Container : Map_Access;
271      Node      : Count_Type := 0;
272   end record;
273
274   procedure Write
275     (Stream : not null access Root_Stream_Type'Class;
276      Item   : Cursor);
277
278   for Cursor'Write use Write;
279
280   procedure Read
281     (Stream : not null access Root_Stream_Type'Class;
282      Item   : out Cursor);
283
284   for Cursor'Read use Read;
285
286   type Reference_Control_Type is new Controlled with record
287      Container : Map_Access;
288   end record;
289
290   overriding procedure Adjust (Control : in out Reference_Control_Type);
291   pragma Inline (Adjust);
292
293   overriding procedure Finalize (Control : in out Reference_Control_Type);
294   pragma Inline (Finalize);
295
296   type Constant_Reference_Type
297     (Element : not null access constant Element_Type) is
298   record
299      Control : Reference_Control_Type;
300   end record;
301
302   procedure Read
303     (Stream : not null access Root_Stream_Type'Class;
304      Item   : out Constant_Reference_Type);
305
306   for Constant_Reference_Type'Read use Read;
307
308   procedure Write
309     (Stream : not null access Root_Stream_Type'Class;
310      Item   : Constant_Reference_Type);
311
312   for Constant_Reference_Type'Write use Write;
313
314   type Reference_Type (Element : not null access Element_Type) is record
315      Control : Reference_Control_Type;
316   end record;
317
318   procedure Read
319     (Stream : not null access Root_Stream_Type'Class;
320      Item   : out Reference_Type);
321
322   for Reference_Type'Read use Read;
323
324   procedure Write
325     (Stream : not null access Root_Stream_Type'Class;
326      Item   : Reference_Type);
327
328   for Reference_Type'Write use Write;
329
330   Empty_Map : constant Map := Map'(Tree_Type with Capacity => 0);
331
332   No_Element : constant Cursor := Cursor'(null, 0);
333
334   type Iterator is new Limited_Controlled and
335     Map_Iterator_Interfaces.Reversible_Iterator with
336   record
337      Container : Map_Access;
338      Node      : Count_Type;
339   end record;
340
341   overriding procedure Finalize (Object : in out Iterator);
342
343   overriding function First (Object : Iterator) return Cursor;
344   overriding function Last  (Object : Iterator) return Cursor;
345
346   overriding function Next
347     (Object   : Iterator;
348      Position : Cursor) return Cursor;
349
350   overriding function Previous
351     (Object   : Iterator;
352      Position : Cursor) return Cursor;
353
354end Ada.Containers.Bounded_Ordered_Maps;
355