1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
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 --
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
.Streams
;
38 private with Ada
.Finalization
;
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
.Bounded_Ordered_Maps
is
48 pragma Annotate
(CodePeer
, Skip_Analysis
);
52 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
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
;
60 pragma Preelaborable_Initialization
(Map
);
62 type Cursor
is private;
63 pragma Preelaborable_Initialization
(Cursor
);
65 Empty_Map
: constant Map
;
67 No_Element
: constant Cursor
;
69 function Has_Element
(Position
: Cursor
) return Boolean;
71 package Map_Iterator_Interfaces
is new
72 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
74 function "=" (Left
, Right
: Map
) return Boolean;
76 function Length
(Container
: Map
) return Count_Type
;
78 function Is_Empty
(Container
: Map
) return Boolean;
80 procedure Clear
(Container
: in out Map
);
82 function Key
(Position
: Cursor
) return Key_Type
;
84 function Element
(Position
: Cursor
) return Element_Type
;
86 procedure Replace_Element
87 (Container
: in out Map
;
89 New_Item
: Element_Type
);
91 procedure Query_Element
93 Process
: not null access
94 procedure (Key
: Key_Type
; Element
: Element_Type
));
96 procedure Update_Element
97 (Container
: in out Map
;
99 Process
: not null access
100 procedure (Key
: Key_Type
; Element
: in out Element_Type
));
102 type Constant_Reference_Type
103 (Element
: not null access constant Element_Type
) is private
105 Implicit_Dereference
=> Element
;
107 type Reference_Type
(Element
: not null access Element_Type
) is private
109 Implicit_Dereference
=> Element
;
111 function Constant_Reference
112 (Container
: aliased Map
;
113 Position
: Cursor
) return Constant_Reference_Type
;
116 (Container
: aliased in out Map
;
117 Position
: Cursor
) return Reference_Type
;
119 function Constant_Reference
120 (Container
: aliased Map
;
121 Key
: Key_Type
) return Constant_Reference_Type
;
124 (Container
: aliased in out Map
;
125 Key
: Key_Type
) return Reference_Type
;
127 procedure Assign
(Target
: in out Map
; Source
: Map
);
129 function Copy
(Source
: Map
; Capacity
: Count_Type
:= 0) return Map
;
131 procedure Move
(Target
: in out Map
; Source
: in out Map
);
134 (Container
: in out Map
;
136 New_Item
: Element_Type
;
137 Position
: out Cursor
;
138 Inserted
: out Boolean);
141 (Container
: in out Map
;
143 Position
: out Cursor
;
144 Inserted
: out Boolean);
147 (Container
: in out Map
;
149 New_Item
: Element_Type
);
152 (Container
: in out Map
;
154 New_Item
: Element_Type
);
157 (Container
: in out Map
;
159 New_Item
: Element_Type
);
161 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
);
163 procedure Delete
(Container
: in out Map
; Key
: Key_Type
);
165 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
);
167 procedure Delete_First
(Container
: in out Map
);
169 procedure Delete_Last
(Container
: in out Map
);
171 function First
(Container
: Map
) return Cursor
;
173 function First_Element
(Container
: Map
) return Element_Type
;
175 function First_Key
(Container
: Map
) return Key_Type
;
177 function Last
(Container
: Map
) return Cursor
;
179 function Last_Element
(Container
: Map
) return Element_Type
;
181 function Last_Key
(Container
: Map
) return Key_Type
;
183 function Next
(Position
: Cursor
) return Cursor
;
185 procedure Next
(Position
: in out Cursor
);
187 function Previous
(Position
: Cursor
) return Cursor
;
189 procedure Previous
(Position
: in out Cursor
);
191 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
;
193 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
;
195 function Floor
(Container
: Map
; Key
: Key_Type
) return Cursor
;
197 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
;
199 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean;
201 function "<" (Left
, Right
: Cursor
) return Boolean;
203 function ">" (Left
, Right
: Cursor
) return Boolean;
205 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
207 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
209 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
211 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
215 Process
: not null access procedure (Position
: Cursor
));
217 procedure Reverse_Iterate
219 Process
: not null access procedure (Position
: Cursor
));
223 return Map_Iterator_Interfaces
.Reversible_Iterator
'Class;
228 return Map_Iterator_Interfaces
.Reversible_Iterator
'Class;
232 use Ada
.Finalization
;
233 pragma Inline
(Next
);
234 pragma Inline
(Previous
);
236 type Node_Type
is record
240 Color
: Red_Black_Trees
.Color_Type
:= Red_Black_Trees
.Red
;
242 Element
: aliased Element_Type
;
245 package Tree_Types
is
246 new Red_Black_Trees
.Generic_Bounded_Tree_Types
(Node_Type
);
248 type Map
(Capacity
: Count_Type
) is
249 new Tree_Types
.Tree_Type
(Capacity
) with null record;
252 use Tree_Types
, Tree_Types
.Implementation
;
256 (Stream
: not null access Root_Stream_Type
'Class;
259 for Map
'Write use Write
;
262 (Stream
: not null access Root_Stream_Type
'Class;
263 Container
: out Map
);
265 for Map
'Read use Read
;
267 type Map_Access
is access all Map
;
268 for Map_Access
'Storage_Size use 0;
270 type Cursor
is record
271 Container
: Map_Access
;
272 Node
: Count_Type
:= 0;
276 (Stream
: not null access Root_Stream_Type
'Class;
279 for Cursor
'Write use Write
;
282 (Stream
: not null access Root_Stream_Type
'Class;
285 for Cursor
'Read use Read
;
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
290 type Constant_Reference_Type
291 (Element
: not null access constant Element_Type
) is
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
301 (Stream
: not null access Root_Stream_Type
'Class;
302 Item
: out Constant_Reference_Type
);
304 for Constant_Reference_Type
'Read use Read
;
307 (Stream
: not null access Root_Stream_Type
'Class;
308 Item
: Constant_Reference_Type
);
310 for Constant_Reference_Type
'Write use Write
;
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
321 (Stream
: not null access Root_Stream_Type
'Class;
322 Item
: out Reference_Type
);
324 for Reference_Type
'Read use Read
;
327 (Stream
: not null access Root_Stream_Type
'Class;
328 Item
: Reference_Type
);
330 for Reference_Type
'Write use Write
;
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
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.
344 type Element_Access
is access all Element_Type
with
347 function Get_Element_Access
348 (Position
: Cursor
) return not null Element_Access
;
349 -- Returns a pointer to the element designated by Position.
351 Empty_Map
: constant Map
:= Map
'(Tree_Type with Capacity => 0);
353 No_Element : constant Cursor := Cursor'(null, 0);
355 type Iterator
is new Limited_Controlled
and
356 Map_Iterator_Interfaces
.Reversible_Iterator
with
358 Container
: Map_Access
;
361 with Disable_Controlled
=> not T_Check
;
363 overriding
procedure Finalize
(Object
: in out Iterator
);
365 overriding
function First
(Object
: Iterator
) return Cursor
;
366 overriding
function Last
(Object
: Iterator
) return Cursor
;
368 overriding
function Next
370 Position
: Cursor
) return Cursor
;
372 overriding
function Previous
374 Position
: Cursor
) return Cursor
;
376 end Ada
.Containers
.Bounded_Ordered_Maps
;