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-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
.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
51 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
53 type Map
(Capacity
: Count_Type
) is tagged private with
54 Constant_Indexing
=> Constant_Reference
,
55 Variable_Indexing
=> Reference
,
56 Default_Iterator
=> Iterate
,
57 Iterator_Element
=> Element_Type
;
59 pragma Preelaborable_Initialization
(Map
);
61 type Cursor
is private;
62 pragma Preelaborable_Initialization
(Cursor
);
64 Empty_Map
: constant Map
;
66 No_Element
: constant Cursor
;
68 function Has_Element
(Position
: Cursor
) return Boolean;
70 package Map_Iterator_Interfaces
is new
71 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
73 function "=" (Left
, Right
: Map
) return Boolean;
75 function Length
(Container
: Map
) return Count_Type
;
77 function Is_Empty
(Container
: Map
) return Boolean;
79 procedure Clear
(Container
: in out Map
);
81 function Key
(Position
: Cursor
) return Key_Type
;
83 function Element
(Position
: Cursor
) return Element_Type
;
85 procedure Replace_Element
86 (Container
: in out Map
;
88 New_Item
: Element_Type
);
90 procedure Query_Element
92 Process
: not null access
93 procedure (Key
: Key_Type
; Element
: Element_Type
));
95 procedure Update_Element
96 (Container
: in out Map
;
98 Process
: not null access
99 procedure (Key
: Key_Type
; Element
: in out Element_Type
));
101 type Constant_Reference_Type
102 (Element
: not null access constant Element_Type
) is private
104 Implicit_Dereference
=> Element
;
106 type Reference_Type
(Element
: not null access Element_Type
) is private
108 Implicit_Dereference
=> Element
;
110 function Constant_Reference
111 (Container
: aliased Map
;
112 Position
: Cursor
) return Constant_Reference_Type
;
115 (Container
: aliased in out Map
;
116 Position
: Cursor
) return Reference_Type
;
118 function Constant_Reference
119 (Container
: aliased Map
;
120 Key
: Key_Type
) return Constant_Reference_Type
;
123 (Container
: aliased in out Map
;
124 Key
: Key_Type
) return Reference_Type
;
126 procedure Assign
(Target
: in out Map
; Source
: Map
);
128 function Copy
(Source
: Map
; Capacity
: Count_Type
:= 0) return Map
;
130 procedure Move
(Target
: in out Map
; Source
: in out Map
);
133 (Container
: in out Map
;
135 New_Item
: Element_Type
;
136 Position
: out Cursor
;
137 Inserted
: out Boolean);
140 (Container
: in out Map
;
142 Position
: out Cursor
;
143 Inserted
: out Boolean);
146 (Container
: in out Map
;
148 New_Item
: Element_Type
);
151 (Container
: in out Map
;
153 New_Item
: Element_Type
);
156 (Container
: in out Map
;
158 New_Item
: Element_Type
);
160 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
);
162 procedure Delete
(Container
: in out Map
; Key
: Key_Type
);
164 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
);
166 procedure Delete_First
(Container
: in out Map
);
168 procedure Delete_Last
(Container
: in out Map
);
170 function First
(Container
: Map
) return Cursor
;
172 function First_Element
(Container
: Map
) return Element_Type
;
174 function First_Key
(Container
: Map
) return Key_Type
;
176 function Last
(Container
: Map
) return Cursor
;
178 function Last_Element
(Container
: Map
) return Element_Type
;
180 function Last_Key
(Container
: Map
) return Key_Type
;
182 function Next
(Position
: Cursor
) return Cursor
;
184 procedure Next
(Position
: in out Cursor
);
186 function Previous
(Position
: Cursor
) return Cursor
;
188 procedure Previous
(Position
: in out Cursor
);
190 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
;
192 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
;
194 function Floor
(Container
: Map
; Key
: Key_Type
) return Cursor
;
196 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
;
198 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean;
200 function "<" (Left
, Right
: Cursor
) return Boolean;
202 function ">" (Left
, Right
: Cursor
) return Boolean;
204 function "<" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
206 function ">" (Left
: Cursor
; Right
: Key_Type
) return Boolean;
208 function "<" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
210 function ">" (Left
: Key_Type
; Right
: Cursor
) return Boolean;
214 Process
: not null access procedure (Position
: Cursor
));
216 procedure Reverse_Iterate
218 Process
: not null access procedure (Position
: Cursor
));
222 return Map_Iterator_Interfaces
.Reversible_Iterator
'Class;
227 return Map_Iterator_Interfaces
.Reversible_Iterator
'Class;
231 pragma Inline
(Next
);
232 pragma Inline
(Previous
);
234 type Node_Type
is record
238 Color
: Red_Black_Trees
.Color_Type
:= Red_Black_Trees
.Red
;
240 Element
: aliased Element_Type
;
243 package Tree_Types
is
244 new Red_Black_Trees
.Generic_Bounded_Tree_Types
(Node_Type
);
246 type Map
(Capacity
: Count_Type
) is
247 new Tree_Types
.Tree_Type
(Capacity
) with null record;
254 (Stream
: not null access Root_Stream_Type
'Class;
257 for Map
'Write use Write
;
260 (Stream
: not null access Root_Stream_Type
'Class;
261 Container
: out Map
);
263 for Map
'Read use Read
;
265 type Map_Access
is access all Map
;
266 for Map_Access
'Storage_Size use 0;
268 type Cursor
is record
269 Container
: Map_Access
;
270 Node
: Count_Type
:= 0;
274 (Stream
: not null access Root_Stream_Type
'Class;
277 for Cursor
'Write use Write
;
280 (Stream
: not null access Root_Stream_Type
'Class;
283 for Cursor
'Read use Read
;
285 type Constant_Reference_Type
286 (Element
: not null access constant Element_Type
) is null record;
289 (Stream
: not null access Root_Stream_Type
'Class;
290 Item
: out Constant_Reference_Type
);
292 for Constant_Reference_Type
'Read use Read
;
295 (Stream
: not null access Root_Stream_Type
'Class;
296 Item
: Constant_Reference_Type
);
298 for Constant_Reference_Type
'Write use Write
;
301 (Element
: not null access Element_Type
) is null record;
304 (Stream
: not null access Root_Stream_Type
'Class;
305 Item
: out Reference_Type
);
307 for Reference_Type
'Read use Read
;
310 (Stream
: not null access Root_Stream_Type
'Class;
311 Item
: Reference_Type
);
313 for Reference_Type
'Write use Write
;
315 Empty_Map
: constant Map
:= Map
'(Tree_Type with Capacity => 0);
317 No_Element : constant Cursor := Cursor'(null, 0);
319 use Ada
.Finalization
;
321 type Iterator
is new Limited_Controlled
and
322 Map_Iterator_Interfaces
.Reversible_Iterator
with
324 Container
: Map_Access
;
328 overriding
procedure Finalize
(Object
: in out Iterator
);
330 overriding
function First
(Object
: Iterator
) return Cursor
;
331 overriding
function Last
(Object
: Iterator
) return Cursor
;
333 overriding
function Next
335 Position
: Cursor
) return Cursor
;
337 overriding
function Previous
339 Position
: Cursor
) return Cursor
;
341 end Ada
.Containers
.Bounded_Ordered_Maps
;