111
|
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 -- --
|
131
|
9 -- Copyright (C) 2004-2018, Free Software Foundation, Inc. --
|
111
|
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
|
|
34 with Ada.Iterator_Interfaces;
|
|
35
|
|
36 private with Ada.Containers.Red_Black_Trees;
|
|
37 private with Ada.Streams;
|
|
38 private with Ada.Finalization;
|
|
39
|
|
40 generic
|
|
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
|
|
47 package Ada.Containers.Bounded_Ordered_Maps is
|
|
48 pragma Annotate (CodePeer, Skip_Analysis);
|
|
49 pragma Pure;
|
|
50 pragma Remote_Types;
|
|
51
|
|
52 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
|
|
53
|
|
54 type Map (Capacity : Count_Type) is tagged private with
|
|
55 Constant_Indexing => Constant_Reference,
|
|
56 Variable_Indexing => Reference,
|
|
57 Default_Iterator => Iterate,
|
|
58 Iterator_Element => Element_Type;
|
|
59
|
|
60 pragma Preelaborable_Initialization (Map);
|
|
61
|
|
62 type Cursor is private;
|
|
63 pragma Preelaborable_Initialization (Cursor);
|
|
64
|
|
65 Empty_Map : constant Map;
|
|
66
|
|
67 No_Element : constant Cursor;
|
|
68
|
|
69 function Has_Element (Position : Cursor) return Boolean;
|
|
70
|
|
71 package Map_Iterator_Interfaces is new
|
|
72 Ada.Iterator_Interfaces (Cursor, Has_Element);
|
|
73
|
|
74 function "=" (Left, Right : Map) return Boolean;
|
|
75
|
|
76 function Length (Container : Map) return Count_Type;
|
|
77
|
|
78 function Is_Empty (Container : Map) return Boolean;
|
|
79
|
|
80 procedure Clear (Container : in out Map);
|
|
81
|
|
82 function Key (Position : Cursor) return Key_Type;
|
|
83
|
|
84 function Element (Position : Cursor) return Element_Type;
|
|
85
|
|
86 procedure Replace_Element
|
|
87 (Container : in out Map;
|
|
88 Position : Cursor;
|
|
89 New_Item : Element_Type);
|
|
90
|
|
91 procedure Query_Element
|
|
92 (Position : Cursor;
|
|
93 Process : not null access
|
|
94 procedure (Key : Key_Type; Element : Element_Type));
|
|
95
|
|
96 procedure Update_Element
|
|
97 (Container : in out Map;
|
|
98 Position : Cursor;
|
|
99 Process : not null access
|
|
100 procedure (Key : Key_Type; Element : in out Element_Type));
|
|
101
|
|
102 type Constant_Reference_Type
|
|
103 (Element : not null access constant Element_Type) is private
|
|
104 with
|
|
105 Implicit_Dereference => Element;
|
|
106
|
|
107 type Reference_Type (Element : not null access Element_Type) is private
|
|
108 with
|
|
109 Implicit_Dereference => Element;
|
|
110
|
|
111 function Constant_Reference
|
|
112 (Container : aliased Map;
|
|
113 Position : Cursor) return Constant_Reference_Type;
|
|
114
|
|
115 function Reference
|
|
116 (Container : aliased in out Map;
|
|
117 Position : Cursor) return Reference_Type;
|
|
118
|
|
119 function Constant_Reference
|
|
120 (Container : aliased Map;
|
|
121 Key : Key_Type) return Constant_Reference_Type;
|
|
122
|
|
123 function Reference
|
|
124 (Container : aliased in out Map;
|
|
125 Key : Key_Type) return Reference_Type;
|
|
126
|
|
127 procedure Assign (Target : in out Map; Source : Map);
|
|
128
|
|
129 function Copy (Source : Map; Capacity : Count_Type := 0) return Map;
|
|
130
|
|
131 procedure Move (Target : in out Map; Source : in out Map);
|
|
132
|
|
133 procedure Insert
|
|
134 (Container : in out Map;
|
|
135 Key : Key_Type;
|
|
136 New_Item : Element_Type;
|
|
137 Position : out Cursor;
|
|
138 Inserted : out Boolean);
|
|
139
|
|
140 procedure Insert
|
|
141 (Container : in out Map;
|
|
142 Key : Key_Type;
|
|
143 Position : out Cursor;
|
|
144 Inserted : out Boolean);
|
|
145
|
|
146 procedure Insert
|
|
147 (Container : in out Map;
|
|
148 Key : Key_Type;
|
|
149 New_Item : Element_Type);
|
|
150
|
|
151 procedure Include
|
|
152 (Container : in out Map;
|
|
153 Key : Key_Type;
|
|
154 New_Item : Element_Type);
|
|
155
|
|
156 procedure Replace
|
|
157 (Container : in out Map;
|
|
158 Key : Key_Type;
|
|
159 New_Item : Element_Type);
|
|
160
|
|
161 procedure Exclude (Container : in out Map; Key : Key_Type);
|
|
162
|
|
163 procedure Delete (Container : in out Map; Key : Key_Type);
|
|
164
|
|
165 procedure Delete (Container : in out Map; Position : in out Cursor);
|
|
166
|
|
167 procedure Delete_First (Container : in out Map);
|
|
168
|
|
169 procedure Delete_Last (Container : in out Map);
|
|
170
|
|
171 function First (Container : Map) return Cursor;
|
|
172
|
|
173 function First_Element (Container : Map) return Element_Type;
|
|
174
|
|
175 function First_Key (Container : Map) return Key_Type;
|
|
176
|
|
177 function Last (Container : Map) return Cursor;
|
|
178
|
|
179 function Last_Element (Container : Map) return Element_Type;
|
|
180
|
|
181 function Last_Key (Container : Map) return Key_Type;
|
|
182
|
|
183 function Next (Position : Cursor) return Cursor;
|
|
184
|
|
185 procedure Next (Position : in out Cursor);
|
|
186
|
|
187 function Previous (Position : Cursor) return Cursor;
|
|
188
|
|
189 procedure Previous (Position : in out Cursor);
|
|
190
|
|
191 function Find (Container : Map; Key : Key_Type) return Cursor;
|
|
192
|
|
193 function Element (Container : Map; Key : Key_Type) return Element_Type;
|
|
194
|
|
195 function Floor (Container : Map; Key : Key_Type) return Cursor;
|
|
196
|
|
197 function Ceiling (Container : Map; Key : Key_Type) return Cursor;
|
|
198
|
|
199 function Contains (Container : Map; Key : Key_Type) return Boolean;
|
|
200
|
|
201 function "<" (Left, Right : Cursor) return Boolean;
|
|
202
|
|
203 function ">" (Left, Right : Cursor) return Boolean;
|
|
204
|
|
205 function "<" (Left : Cursor; Right : Key_Type) return Boolean;
|
|
206
|
|
207 function ">" (Left : Cursor; Right : Key_Type) return Boolean;
|
|
208
|
|
209 function "<" (Left : Key_Type; Right : Cursor) return Boolean;
|
|
210
|
|
211 function ">" (Left : Key_Type; Right : Cursor) return Boolean;
|
|
212
|
|
213 procedure Iterate
|
|
214 (Container : Map;
|
|
215 Process : not null access procedure (Position : Cursor));
|
|
216
|
|
217 procedure Reverse_Iterate
|
|
218 (Container : Map;
|
|
219 Process : not null access procedure (Position : Cursor));
|
|
220
|
|
221 function Iterate
|
|
222 (Container : Map)
|
|
223 return Map_Iterator_Interfaces.Reversible_Iterator'Class;
|
|
224
|
|
225 function Iterate
|
|
226 (Container : Map;
|
|
227 Start : Cursor)
|
|
228 return Map_Iterator_Interfaces.Reversible_Iterator'Class;
|
|
229
|
|
230 private
|
|
231
|
|
232 use Ada.Finalization;
|
|
233 pragma Inline (Next);
|
|
234 pragma Inline (Previous);
|
|
235
|
|
236 type Node_Type is record
|
|
237 Parent : Count_Type;
|
|
238 Left : Count_Type;
|
|
239 Right : Count_Type;
|
|
240 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
|
|
241 Key : Key_Type;
|
|
242 Element : aliased Element_Type;
|
|
243 end record;
|
|
244
|
|
245 package Tree_Types is
|
|
246 new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
|
|
247
|
|
248 type Map (Capacity : Count_Type) is
|
|
249 new Tree_Types.Tree_Type (Capacity) with null record;
|
|
250
|
|
251 use Red_Black_Trees;
|
|
252 use Tree_Types, Tree_Types.Implementation;
|
|
253 use Ada.Streams;
|
|
254
|
|
255 procedure Write
|
|
256 (Stream : not null access Root_Stream_Type'Class;
|
|
257 Container : Map);
|
|
258
|
|
259 for Map'Write use Write;
|
|
260
|
|
261 procedure Read
|
|
262 (Stream : not null access Root_Stream_Type'Class;
|
|
263 Container : out Map);
|
|
264
|
|
265 for Map'Read use Read;
|
|
266
|
|
267 type Map_Access is access all Map;
|
|
268 for Map_Access'Storage_Size use 0;
|
|
269
|
|
270 type Cursor is record
|
|
271 Container : Map_Access;
|
|
272 Node : Count_Type := 0;
|
|
273 end record;
|
|
274
|
|
275 procedure Write
|
|
276 (Stream : not null access Root_Stream_Type'Class;
|
|
277 Item : Cursor);
|
|
278
|
|
279 for Cursor'Write use Write;
|
|
280
|
|
281 procedure Read
|
|
282 (Stream : not null access Root_Stream_Type'Class;
|
|
283 Item : out Cursor);
|
|
284
|
|
285 for Cursor'Read use Read;
|
|
286
|
|
287 subtype Reference_Control_Type is Implementation.Reference_Control_Type;
|
|
288 -- It is necessary to rename this here, so that the compiler can find it
|
|
289
|
|
290 type Constant_Reference_Type
|
|
291 (Element : not null access constant Element_Type) is
|
|
292 record
|
|
293 Control : Reference_Control_Type :=
|
|
294 raise Program_Error with "uninitialized reference";
|
|
295 -- The RM says, "The default initialization of an object of
|
|
296 -- type Constant_Reference_Type or Reference_Type propagates
|
|
297 -- Program_Error."
|
|
298 end record;
|
|
299
|
|
300 procedure Read
|
|
301 (Stream : not null access Root_Stream_Type'Class;
|
|
302 Item : out Constant_Reference_Type);
|
|
303
|
|
304 for Constant_Reference_Type'Read use Read;
|
|
305
|
|
306 procedure Write
|
|
307 (Stream : not null access Root_Stream_Type'Class;
|
|
308 Item : Constant_Reference_Type);
|
|
309
|
|
310 for Constant_Reference_Type'Write use Write;
|
|
311
|
|
312 type Reference_Type (Element : not null access Element_Type) is record
|
|
313 Control : Reference_Control_Type :=
|
|
314 raise Program_Error with "uninitialized reference";
|
|
315 -- The RM says, "The default initialization of an object of
|
|
316 -- type Constant_Reference_Type or Reference_Type propagates
|
|
317 -- Program_Error."
|
|
318 end record;
|
|
319
|
|
320 procedure Read
|
|
321 (Stream : not null access Root_Stream_Type'Class;
|
|
322 Item : out Reference_Type);
|
|
323
|
|
324 for Reference_Type'Read use Read;
|
|
325
|
|
326 procedure Write
|
|
327 (Stream : not null access Root_Stream_Type'Class;
|
|
328 Item : Reference_Type);
|
|
329
|
|
330 for Reference_Type'Write use Write;
|
|
331
|
|
332 -- Three operations are used to optimize in the expansion of "for ... of"
|
|
333 -- loops: the Next(Cursor) procedure in the visible part, and the following
|
|
334 -- Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for
|
|
335 -- details.
|
|
336
|
|
337 function Pseudo_Reference
|
|
338 (Container : aliased Map'Class) return Reference_Control_Type;
|
|
339 pragma Inline (Pseudo_Reference);
|
|
340 -- Creates an object of type Reference_Control_Type pointing to the
|
|
341 -- container, and increments the Lock. Finalization of this object will
|
|
342 -- decrement the Lock.
|
|
343
|
|
344 type Element_Access is access all Element_Type with
|
|
345 Storage_Size => 0;
|
|
346
|
|
347 function Get_Element_Access
|
|
348 (Position : Cursor) return not null Element_Access;
|
|
349 -- Returns a pointer to the element designated by Position.
|
|
350
|
|
351 Empty_Map : constant Map := Map'(Tree_Type with Capacity => 0);
|
|
352
|
|
353 No_Element : constant Cursor := Cursor'(null, 0);
|
|
354
|
|
355 type Iterator is new Limited_Controlled and
|
|
356 Map_Iterator_Interfaces.Reversible_Iterator with
|
|
357 record
|
|
358 Container : Map_Access;
|
|
359 Node : Count_Type;
|
|
360 end record
|
|
361 with Disable_Controlled => not T_Check;
|
|
362
|
|
363 overriding procedure Finalize (Object : in out Iterator);
|
|
364
|
|
365 overriding function First (Object : Iterator) return Cursor;
|
|
366 overriding function Last (Object : Iterator) return Cursor;
|
|
367
|
|
368 overriding function Next
|
|
369 (Object : Iterator;
|
|
370 Position : Cursor) return Cursor;
|
|
371
|
|
372 overriding function Previous
|
|
373 (Object : Iterator;
|
|
374 Position : Cursor) return Cursor;
|
|
375
|
|
376 end Ada.Containers.Bounded_Ordered_Maps;
|