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-2013, 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
51 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
53 type Map
is tagged private with
54 Constant_Indexing
=> Constant_Reference
,
55 Variable_Indexing
=> Reference
,
56 Default_Iterator
=> Iterate
,
57 Iterator_Element
=> Element_Type
;
59 type Cursor
is private;
60 pragma Preelaborable_Initialization
(Cursor
);
62 Empty_Map
: constant Map
;
64 No_Element
: constant Cursor
;
66 function Has_Element
(Position
: Cursor
) return Boolean;
68 package Map_Iterator_Interfaces
is new
69 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
71 function "=" (Left
, Right
: Map
) return Boolean;
73 function Length
(Container
: Map
) return Count_Type
;
75 function Is_Empty
(Container
: Map
) return Boolean;
77 procedure Clear
(Container
: in out Map
);
79 function Key
(Position
: Cursor
) return Key_Type
;
81 function Element
(Position
: Cursor
) return Element_Type
;
83 procedure Replace_Element
84 (Container
: in out Map
;
86 New_Item
: Element_Type
);
88 procedure Query_Element
90 Process
: not null access
91 procedure (Key
: Key_Type
; Element
: Element_Type
));
93 procedure Update_Element
94 (Container
: in out Map
;
96 Process
: not null access
97 procedure (Key
: Key_Type
; Element
: in out Element_Type
));
99 type Constant_Reference_Type
100 (Element
: not null access constant Element_Type
) is private
102 Implicit_Dereference
=> Element
;
104 type Reference_Type
(Element
: not null access Element_Type
) is private
106 Implicit_Dereference
=> Element
;
108 function Constant_Reference
109 (Container
: aliased Map
;
110 Position
: Cursor
) return Constant_Reference_Type
;
111 pragma Inline
(Constant_Reference
);
114 (Container
: aliased in out Map
;
115 Position
: Cursor
) return Reference_Type
;
116 pragma Inline
(Reference
);
118 function Constant_Reference
119 (Container
: aliased Map
;
120 Key
: Key_Type
) return Constant_Reference_Type
;
121 pragma Inline
(Constant_Reference
);
124 (Container
: aliased in out Map
;
125 Key
: Key_Type
) return Reference_Type
;
126 pragma Inline
(Reference
);
128 procedure Assign
(Target
: in out Map
; Source
: Map
);
130 function Copy
(Source
: Map
) return Map
;
132 procedure Move
(Target
: in out Map
; Source
: in out Map
);
135 (Container
: in out Map
;
137 New_Item
: Element_Type
;
138 Position
: out Cursor
;
139 Inserted
: out Boolean);
142 (Container
: in out Map
;
144 Position
: out Cursor
;
145 Inserted
: out Boolean);
148 (Container
: in out Map
;
150 New_Item
: Element_Type
);
153 (Container
: in out Map
;
155 New_Item
: Element_Type
);
158 (Container
: in out Map
;
160 New_Item
: Element_Type
);
162 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
);
164 procedure Delete
(Container
: in out Map
; Key
: Key_Type
);
166 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
);
168 procedure Delete_First
(Container
: in out Map
);
170 procedure Delete_Last
(Container
: in out Map
);
172 function First
(Container
: Map
) return Cursor
;
174 function First_Element
(Container
: Map
) return Element_Type
;
176 function First_Key
(Container
: Map
) return Key_Type
;
178 function Last
(Container
: Map
) return Cursor
;
180 function Last_Element
(Container
: Map
) return Element_Type
;
182 function Last_Key
(Container
: Map
) return Key_Type
;
184 function Next
(Position
: Cursor
) return Cursor
;
186 procedure Next
(Position
: in out Cursor
);
188 function Previous
(Position
: Cursor
) return Cursor
;
190 procedure Previous
(Position
: in out Cursor
);
192 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
;
194 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
;
196 function Floor
(Container
: Map
; Key
: Key_Type
) return Cursor
;
198 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
;
200 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean;
202 function "<" (Left
, Right
: Cursor
) return Boolean;
204 function ">" (Left
, Right
: Cursor
) return Boolean;
206 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
208 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
210 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
212 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
216 Process
: not null access procedure (Position
: Cursor
));
218 procedure Reverse_Iterate
220 Process
: not null access procedure (Position
: Cursor
));
222 -- The map container supports iteration in both the forward and reverse
223 -- directions, hence these constructor functions return an object that
224 -- supports the Reversible_Iterator interface.
228 return Map_Iterator_Interfaces
.Reversible_Iterator
'class;
233 return Map_Iterator_Interfaces
.Reversible_Iterator
'class;
237 pragma Inline
(Next
);
238 pragma Inline
(Previous
);
241 type Node_Access
is access Node_Type
;
243 type Node_Type
is limited record
244 Parent
: Node_Access
;
247 Color
: Red_Black_Trees
.Color_Type
:= Red_Black_Trees
.Red
;
249 Element
: aliased Element_Type
;
252 package Tree_Types
is
253 new Red_Black_Trees
.Generic_Tree_Types
(Node_Type
, Node_Access
);
255 type Map
is new Ada
.Finalization
.Controlled
with record
256 Tree
: Tree_Types
.Tree_Type
;
259 overriding
procedure Adjust
(Container
: in out Map
);
261 overriding
procedure Finalize
(Container
: in out Map
) renames Clear
;
265 use Ada
.Finalization
;
269 (Stream
: not null access Root_Stream_Type
'Class;
272 for Map
'Write use Write
;
275 (Stream
: not null access Root_Stream_Type
'Class;
276 Container
: out Map
);
278 for Map
'Read use Read
;
280 type Map_Access
is access all Map
;
281 for Map_Access
'Storage_Size use 0;
283 type Cursor
is record
284 Container
: Map_Access
;
289 (Stream
: not null access Root_Stream_Type
'Class;
292 for Cursor
'Write use Write
;
295 (Stream
: not null access Root_Stream_Type
'Class;
298 for Cursor
'Read use Read
;
300 type Reference_Control_Type
is
301 new Controlled
with record
302 Container
: Map_Access
;
305 overriding
procedure Adjust
(Control
: in out Reference_Control_Type
);
306 pragma Inline
(Adjust
);
308 overriding
procedure Finalize
(Control
: in out Reference_Control_Type
);
309 pragma Inline
(Finalize
);
311 type Constant_Reference_Type
312 (Element
: not null access constant Element_Type
) is
314 Control
: Reference_Control_Type
;
318 (Stream
: not null access Root_Stream_Type
'Class;
319 Item
: out Constant_Reference_Type
);
321 for Constant_Reference_Type
'Read use Read
;
324 (Stream
: not null access Root_Stream_Type
'Class;
325 Item
: Constant_Reference_Type
);
327 for Constant_Reference_Type
'Write use Write
;
330 (Element
: not null access Element_Type
) is
332 Control
: Reference_Control_Type
;
336 (Stream
: not null access Root_Stream_Type
'Class;
337 Item
: out Reference_Type
);
339 for Reference_Type
'Read use Read
;
342 (Stream
: not null access Root_Stream_Type
'Class;
343 Item
: Reference_Type
);
345 for Reference_Type
'Write use Write
;
347 Empty_Map
: constant Map
:=
348 (Controlled
with Tree
=> (First
=> null,
355 No_Element
: constant Cursor
:= Cursor
'(null, null);
357 type Iterator is new Limited_Controlled and
358 Map_Iterator_Interfaces.Reversible_Iterator with
360 Container : Map_Access;
364 overriding procedure Finalize (Object : in out Iterator);
366 overriding function First (Object : Iterator) return Cursor;
367 overriding function Last (Object : Iterator) return Cursor;
369 overriding function Next
371 Position : Cursor) return Cursor;
373 overriding function Previous
375 Position : Cursor) return Cursor;
377 end Ada.Containers.Ordered_Maps;