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-2024, 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
;
39 private with Ada
.Strings
.Text_Buffers
;
42 type Key_Type
is private;
43 type Element_Type
is private;
45 with function "<" (Left
, Right
: Key_Type
) return Boolean is <>;
46 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
48 package Ada
.Containers
.Bounded_Ordered_Maps
with
51 pragma Annotate
(CodePeer
, Skip_Analysis
);
55 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
57 type Map
(Capacity
: Count_Type
) is tagged private with
58 Constant_Indexing
=> Constant_Reference
,
59 Variable_Indexing
=> Reference
,
60 Default_Iterator
=> Iterate
,
61 Iterator_Element
=> Element_Type
,
62 Aggregate
=> (Empty
=> Empty
,
64 Preelaborable_Initialization
65 => Element_Type
'Preelaborable_Initialization
67 Key_Type
'Preelaborable_Initialization;
69 type Cursor
is private with Preelaborable_Initialization
;
71 Empty_Map
: constant Map
;
73 function Empty
(Capacity
: Count_Type
:= 10) return Map
;
75 No_Element
: constant Cursor
;
77 function Has_Element
(Position
: Cursor
) return Boolean;
79 package Map_Iterator_Interfaces
is new
80 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
82 function "=" (Left
, Right
: Map
) return Boolean;
84 function Length
(Container
: Map
) return Count_Type
;
86 function Is_Empty
(Container
: Map
) return Boolean;
88 procedure Clear
(Container
: in out Map
);
90 function Key
(Position
: Cursor
) return Key_Type
;
92 function Element
(Position
: Cursor
) return Element_Type
;
94 procedure Replace_Element
95 (Container
: in out Map
;
97 New_Item
: Element_Type
);
99 procedure Query_Element
101 Process
: not null access
102 procedure (Key
: Key_Type
; Element
: Element_Type
));
104 procedure Update_Element
105 (Container
: in out Map
;
107 Process
: not null access
108 procedure (Key
: Key_Type
; Element
: in out Element_Type
));
110 type Constant_Reference_Type
111 (Element
: not null access constant Element_Type
) is private
113 Implicit_Dereference
=> Element
;
115 type Reference_Type
(Element
: not null access Element_Type
) is private
117 Implicit_Dereference
=> Element
;
119 function Constant_Reference
120 (Container
: aliased Map
;
121 Position
: Cursor
) return Constant_Reference_Type
;
124 (Container
: aliased in out Map
;
125 Position
: Cursor
) return Reference_Type
;
127 function Constant_Reference
128 (Container
: aliased Map
;
129 Key
: Key_Type
) return Constant_Reference_Type
;
132 (Container
: aliased in out Map
;
133 Key
: Key_Type
) return Reference_Type
;
135 procedure Assign
(Target
: in out Map
; Source
: Map
);
137 function Copy
(Source
: Map
; Capacity
: Count_Type
:= 0) return Map
;
139 procedure Move
(Target
: in out Map
; Source
: in out Map
);
142 (Container
: in out Map
;
144 New_Item
: Element_Type
;
145 Position
: out Cursor
;
146 Inserted
: out Boolean);
149 (Container
: in out Map
;
151 Position
: out Cursor
;
152 Inserted
: out Boolean);
155 (Container
: in out Map
;
157 New_Item
: Element_Type
);
160 (Container
: in out Map
;
162 New_Item
: Element_Type
);
165 (Container
: in out Map
;
167 New_Item
: Element_Type
);
169 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
);
171 procedure Delete
(Container
: in out Map
; Key
: Key_Type
);
173 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
);
175 procedure Delete_First
(Container
: in out Map
);
177 procedure Delete_Last
(Container
: in out Map
);
179 function First
(Container
: Map
) return Cursor
;
181 function First_Element
(Container
: Map
) return Element_Type
;
183 function First_Key
(Container
: Map
) return Key_Type
;
185 function Last
(Container
: Map
) return Cursor
;
187 function Last_Element
(Container
: Map
) return Element_Type
;
189 function Last_Key
(Container
: Map
) return Key_Type
;
191 function Next
(Position
: Cursor
) return Cursor
;
193 procedure Next
(Position
: in out Cursor
);
195 function Previous
(Position
: Cursor
) return Cursor
;
197 procedure Previous
(Position
: in out Cursor
);
199 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
;
201 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
;
203 function Floor
(Container
: Map
; Key
: Key_Type
) return Cursor
;
205 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
;
207 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean;
209 function "<" (Left
, Right
: Cursor
) return Boolean;
211 function ">" (Left
, Right
: Cursor
) return Boolean;
213 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
215 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
217 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
219 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
223 Process
: not null access procedure (Position
: Cursor
));
225 procedure Reverse_Iterate
227 Process
: not null access procedure (Position
: Cursor
));
231 return Map_Iterator_Interfaces
.Reversible_Iterator
'Class;
236 return Map_Iterator_Interfaces
.Reversible_Iterator
'Class;
240 use Ada
.Finalization
;
241 pragma Inline
(Next
);
242 pragma Inline
(Previous
);
244 type Node_Type
is record
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_Bounded_Tree_Types
(Node_Type
);
256 type Map
(Capacity
: Count_Type
) is
257 new Tree_Types
.Tree_Type
(Capacity
)
258 with null record with Put_Image
=> Put_Image
;
261 (S
: in out Ada
.Strings
.Text_Buffers
.Root_Buffer_Type
'Class; V
: Map
);
264 use Tree_Types
, Tree_Types
.Implementation
;
268 (Stream
: not null access Root_Stream_Type
'Class;
271 for Map
'Write use Write
;
274 (Stream
: not null access Root_Stream_Type
'Class;
275 Container
: out Map
);
277 for Map
'Read use Read
;
279 type Map_Access
is access all Map
;
280 for Map_Access
'Storage_Size use 0;
282 type Cursor
is record
283 Container
: Map_Access
;
284 Node
: Count_Type
:= 0;
288 (Stream
: not null access Root_Stream_Type
'Class;
291 for Cursor
'Write use Write
;
294 (Stream
: not null access Root_Stream_Type
'Class;
297 for Cursor
'Read use Read
;
299 subtype Reference_Control_Type
is Implementation
.Reference_Control_Type
;
300 -- It is necessary to rename this here, so that the compiler can find it
302 type Constant_Reference_Type
303 (Element
: not null access constant Element_Type
) is
305 Control
: Reference_Control_Type
:=
306 raise Program_Error
with "uninitialized reference";
307 -- The RM says, "The default initialization of an object of
308 -- type Constant_Reference_Type or Reference_Type propagates
313 (Stream
: not null access Root_Stream_Type
'Class;
314 Item
: out Constant_Reference_Type
);
316 for Constant_Reference_Type
'Read use Read
;
319 (Stream
: not null access Root_Stream_Type
'Class;
320 Item
: Constant_Reference_Type
);
322 for Constant_Reference_Type
'Write use Write
;
324 type Reference_Type
(Element
: not null access Element_Type
) is record
325 Control
: Reference_Control_Type
:=
326 raise Program_Error
with "uninitialized reference";
327 -- The RM says, "The default initialization of an object of
328 -- type Constant_Reference_Type or Reference_Type propagates
333 (Stream
: not null access Root_Stream_Type
'Class;
334 Item
: out Reference_Type
);
336 for Reference_Type
'Read use Read
;
339 (Stream
: not null access Root_Stream_Type
'Class;
340 Item
: Reference_Type
);
342 for Reference_Type
'Write use Write
;
344 -- See Ada.Containers.Vectors for documentation on the following
346 procedure _Next
(Position
: in out Cursor
) renames Next
;
347 procedure _Previous
(Position
: in out Cursor
) renames Previous
;
349 function Pseudo_Reference
350 (Container
: aliased Map
'Class) return Reference_Control_Type
;
351 pragma Inline
(Pseudo_Reference
);
352 -- Creates an object of type Reference_Control_Type pointing to the
353 -- container, and increments the Lock. Finalization of this object will
354 -- decrement the Lock.
356 type Element_Access
is access all Element_Type
with
359 function Get_Element_Access
360 (Position
: Cursor
) return not null Element_Access
;
361 -- Returns a pointer to the element designated by Position.
363 Empty_Map
: constant Map
:= Map
'(Tree_Type with Capacity => 0);
365 No_Element : constant Cursor := Cursor'(null, 0);
367 type Iterator
is new Limited_Controlled
and
368 Map_Iterator_Interfaces
.Reversible_Iterator
with
370 Container
: Map_Access
;
373 with Disable_Controlled
=> not T_Check
;
375 overriding
procedure Finalize
(Object
: in out Iterator
);
377 overriding
function First
(Object
: Iterator
) return Cursor
;
378 overriding
function Last
(Object
: Iterator
) return Cursor
;
380 overriding
function Next
382 Position
: Cursor
) return Cursor
;
384 overriding
function Previous
386 Position
: Cursor
) return Cursor
;
388 end Ada
.Containers
.Bounded_Ordered_Maps
;