1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . O R D E R E D _ M A P S --
9 -- Copyright (C) 2004-2015, Free Software Foundation, Inc. --
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. --
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. --
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. --
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/>. --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
34 with Ada
.Iterator_Interfaces
;
36 private with Ada
.Containers
.Red_Black_Trees
;
37 private with Ada
.Finalization
;
38 private with Ada
.Streams
;
41 type Key_Type
is private;
42 type Element_Type
is private;
44 with function "<" (Left
, Right
: Key_Type
) return Boolean is <>;
45 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
47 package Ada
.Containers
.Ordered_Maps
is
48 pragma Annotate
(CodePeer
, Skip_Analysis
);
52 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
54 type Map
is tagged private with
55 Constant_Indexing
=> Constant_Reference
,
56 Variable_Indexing
=> Reference
,
57 Default_Iterator
=> Iterate
,
58 Iterator_Element
=> Element_Type
;
60 type Cursor
is private;
61 pragma Preelaborable_Initialization
(Cursor
);
63 Empty_Map
: constant Map
;
65 No_Element
: constant Cursor
;
67 function Has_Element
(Position
: Cursor
) return Boolean;
69 package Map_Iterator_Interfaces
is new
70 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
72 function "=" (Left
, Right
: Map
) return Boolean;
74 function Length
(Container
: Map
) return Count_Type
;
76 function Is_Empty
(Container
: Map
) return Boolean;
78 procedure Clear
(Container
: in out Map
);
80 function Key
(Position
: Cursor
) return Key_Type
;
82 function Element
(Position
: Cursor
) return Element_Type
;
84 procedure Replace_Element
85 (Container
: in out Map
;
87 New_Item
: Element_Type
);
89 procedure Query_Element
91 Process
: not null access
92 procedure (Key
: Key_Type
; Element
: Element_Type
));
94 procedure Update_Element
95 (Container
: in out Map
;
97 Process
: not null access
98 procedure (Key
: Key_Type
; Element
: in out Element_Type
));
100 type Constant_Reference_Type
101 (Element
: not null access constant Element_Type
) is private
103 Implicit_Dereference
=> Element
;
105 type Reference_Type
(Element
: not null access Element_Type
) is private
107 Implicit_Dereference
=> Element
;
109 function Constant_Reference
110 (Container
: aliased Map
;
111 Position
: Cursor
) return Constant_Reference_Type
;
112 pragma Inline
(Constant_Reference
);
115 (Container
: aliased in out Map
;
116 Position
: Cursor
) return Reference_Type
;
117 pragma Inline
(Reference
);
119 function Constant_Reference
120 (Container
: aliased Map
;
121 Key
: Key_Type
) return Constant_Reference_Type
;
122 pragma Inline
(Constant_Reference
);
125 (Container
: aliased in out Map
;
126 Key
: Key_Type
) return Reference_Type
;
127 pragma Inline
(Reference
);
129 procedure Assign
(Target
: in out Map
; Source
: Map
);
131 function Copy
(Source
: Map
) return Map
;
133 procedure Move
(Target
: in out Map
; Source
: in out Map
);
136 (Container
: in out Map
;
138 New_Item
: Element_Type
;
139 Position
: out Cursor
;
140 Inserted
: out Boolean);
143 (Container
: in out Map
;
145 Position
: out Cursor
;
146 Inserted
: out Boolean);
149 (Container
: in out Map
;
151 New_Item
: Element_Type
);
154 (Container
: in out Map
;
156 New_Item
: Element_Type
);
159 (Container
: in out Map
;
161 New_Item
: Element_Type
);
163 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
);
165 procedure Delete
(Container
: in out Map
; Key
: Key_Type
);
167 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
);
169 procedure Delete_First
(Container
: in out Map
);
171 procedure Delete_Last
(Container
: in out Map
);
173 function First
(Container
: Map
) return Cursor
;
175 function First_Element
(Container
: Map
) return Element_Type
;
177 function First_Key
(Container
: Map
) return Key_Type
;
179 function Last
(Container
: Map
) return Cursor
;
181 function Last_Element
(Container
: Map
) return Element_Type
;
183 function Last_Key
(Container
: Map
) return Key_Type
;
185 function Next
(Position
: Cursor
) return Cursor
;
187 procedure Next
(Position
: in out Cursor
);
189 function Previous
(Position
: Cursor
) return Cursor
;
191 procedure Previous
(Position
: in out Cursor
);
193 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
;
195 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
;
197 function Floor
(Container
: Map
; Key
: Key_Type
) return Cursor
;
199 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
;
201 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean;
203 function "<" (Left
, Right
: Cursor
) return Boolean;
205 function ">" (Left
, Right
: Cursor
) return Boolean;
207 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
209 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
211 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
213 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
217 Process
: not null access procedure (Position
: Cursor
));
219 procedure Reverse_Iterate
221 Process
: not null access procedure (Position
: Cursor
));
223 -- The map container supports iteration in both the forward and reverse
224 -- directions, hence these constructor functions return an object that
225 -- supports the Reversible_Iterator interface.
229 return Map_Iterator_Interfaces
.Reversible_Iterator
'class;
234 return Map_Iterator_Interfaces
.Reversible_Iterator
'class;
238 pragma Inline
(Next
);
239 pragma Inline
(Previous
);
242 type Node_Access
is access Node_Type
;
244 type Node_Type
is limited record
245 Parent
: Node_Access
;
248 Color
: Red_Black_Trees
.Color_Type
:= Red_Black_Trees
.Red
;
250 Element
: aliased Element_Type
;
253 package Tree_Types
is
254 new Red_Black_Trees
.Generic_Tree_Types
(Node_Type
, Node_Access
);
256 type Map
is new Ada
.Finalization
.Controlled
with record
257 Tree
: Tree_Types
.Tree_Type
;
260 overriding
procedure Adjust
(Container
: in out Map
);
262 overriding
procedure Finalize
(Container
: in out Map
) renames Clear
;
265 use Tree_Types
, Tree_Types
.Implementation
;
266 use Ada
.Finalization
;
270 (Stream
: not null access Root_Stream_Type
'Class;
273 for Map
'Write use Write
;
276 (Stream
: not null access Root_Stream_Type
'Class;
277 Container
: out Map
);
279 for Map
'Read use Read
;
281 type Map_Access
is access all Map
;
282 for Map_Access
'Storage_Size use 0;
284 type Cursor
is record
285 Container
: Map_Access
;
290 (Stream
: not null access Root_Stream_Type
'Class;
293 for Cursor
'Write use Write
;
296 (Stream
: not null access Root_Stream_Type
'Class;
299 for Cursor
'Read use Read
;
301 subtype Reference_Control_Type
is Implementation
.Reference_Control_Type
;
302 -- It is necessary to rename this here, so that the compiler can find it
304 type Constant_Reference_Type
305 (Element
: not null access constant Element_Type
) is
307 Control
: Reference_Control_Type
:=
308 raise Program_Error
with "uninitialized reference";
309 -- The RM says, "The default initialization of an object of
310 -- type Constant_Reference_Type or Reference_Type propagates
315 (Stream
: not null access Root_Stream_Type
'Class;
316 Item
: out Constant_Reference_Type
);
318 for Constant_Reference_Type
'Read use Read
;
321 (Stream
: not null access Root_Stream_Type
'Class;
322 Item
: Constant_Reference_Type
);
324 for Constant_Reference_Type
'Write use Write
;
327 (Element
: not null access Element_Type
) is
329 Control
: Reference_Control_Type
:=
330 raise Program_Error
with "uninitialized reference";
331 -- The RM says, "The default initialization of an object of
332 -- type Constant_Reference_Type or Reference_Type propagates
337 (Stream
: not null access Root_Stream_Type
'Class;
338 Item
: out Reference_Type
);
340 for Reference_Type
'Read use Read
;
343 (Stream
: not null access Root_Stream_Type
'Class;
344 Item
: Reference_Type
);
346 for Reference_Type
'Write use Write
;
348 -- Three operations are used to optimize in the expansion of "for ... of"
349 -- loops: the Next(Cursor) procedure in the visible part, and the following
350 -- Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for
353 function Pseudo_Reference
354 (Container
: aliased Map
'Class) return Reference_Control_Type
;
355 pragma Inline
(Pseudo_Reference
);
356 -- Creates an object of type Reference_Control_Type pointing to the
357 -- container, and increments the Lock. Finalization of this object will
358 -- decrement the Lock.
360 type Element_Access
is access all Element_Type
with
363 function Get_Element_Access
364 (Position
: Cursor
) return not null Element_Access
;
365 -- Returns a pointer to the element designated by Position.
367 Empty_Map
: constant Map
:= (Controlled
with others => <>);
369 No_Element
: constant Cursor
:= Cursor
'(null, null);
371 type Iterator is new Limited_Controlled and
372 Map_Iterator_Interfaces.Reversible_Iterator with
374 Container : Map_Access;
377 with Disable_Controlled => not T_Check;
379 overriding procedure Finalize (Object : in out Iterator);
381 overriding function First (Object : Iterator) return Cursor;
382 overriding function Last (Object : Iterator) return Cursor;
384 overriding function Next
386 Position : Cursor) return Cursor;
388 overriding function Previous
390 Position : Cursor) return Cursor;
392 end Ada.Containers.Ordered_Maps;