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 _ S E T S --
9 -- Copyright (C) 2004-2023, 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 with Ada
.Containers
.Helpers
;
37 private with Ada
.Containers
.Red_Black_Trees
;
38 private with Ada
.Finalization
;
39 private with Ada
.Streams
;
40 private with Ada
.Strings
.Text_Buffers
;
43 type Element_Type
is private;
45 with function "<" (Left
, Right
: Element_Type
) return Boolean is <>;
46 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
48 package Ada
.Containers
.Ordered_Sets
with
51 pragma Annotate
(CodePeer
, Skip_Analysis
);
55 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean;
57 type Set
is tagged private
58 with Constant_Indexing
=> Constant_Reference
,
59 Default_Iterator
=> Iterate
,
60 Iterator_Element
=> Element_Type
,
61 Aggregate
=> (Empty
=> Empty
,
62 Add_Unnamed
=> Include
);
64 pragma Preelaborable_Initialization
(Set
);
66 type Cursor
is private;
67 pragma Preelaborable_Initialization
(Cursor
);
69 function Has_Element
(Position
: Cursor
) return Boolean;
71 Empty_Set
: constant Set
;
72 function Empty
return Set
;
74 No_Element
: constant Cursor
;
76 package Set_Iterator_Interfaces
is new
77 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
79 function "=" (Left
, Right
: Set
) return Boolean;
81 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean;
83 function To_Set
(New_Item
: Element_Type
) return Set
;
85 function Length
(Container
: Set
) return Count_Type
;
87 function Is_Empty
(Container
: Set
) return Boolean;
89 procedure Clear
(Container
: in out Set
);
91 function Element
(Position
: Cursor
) return Element_Type
;
93 procedure Replace_Element
94 (Container
: in out Set
;
96 New_Item
: Element_Type
);
98 procedure Query_Element
100 Process
: not null access procedure (Element
: Element_Type
));
102 type Constant_Reference_Type
103 (Element
: not null access constant Element_Type
) is
106 Implicit_Dereference
=> Element
;
108 function Constant_Reference
109 (Container
: aliased Set
;
110 Position
: Cursor
) return Constant_Reference_Type
;
111 pragma Inline
(Constant_Reference
);
113 procedure Assign
(Target
: in out Set
; Source
: Set
);
115 function Copy
(Source
: Set
) return Set
;
117 procedure Move
(Target
: in out Set
; Source
: in out Set
);
120 (Container
: in out Set
;
121 New_Item
: Element_Type
;
122 Position
: out Cursor
;
123 Inserted
: out Boolean);
126 (Container
: in out Set
;
127 New_Item
: Element_Type
);
130 (Container
: in out Set
;
131 New_Item
: Element_Type
);
134 (Container
: in out Set
;
135 New_Item
: Element_Type
);
138 (Container
: in out Set
;
139 Item
: Element_Type
);
142 (Container
: in out Set
;
143 Item
: Element_Type
);
146 (Container
: in out Set
;
147 Position
: in out Cursor
);
149 procedure Delete_First
(Container
: in out Set
);
151 procedure Delete_Last
(Container
: in out Set
);
153 procedure Union
(Target
: in out Set
; Source
: Set
);
155 function Union
(Left
, Right
: Set
) return Set
;
157 function "or" (Left
, Right
: Set
) return Set
renames Union
;
159 procedure Intersection
(Target
: in out Set
; Source
: Set
);
161 function Intersection
(Left
, Right
: Set
) return Set
;
163 function "and" (Left
, Right
: Set
) return Set
renames Intersection
;
165 procedure Difference
(Target
: in out Set
; Source
: Set
);
167 function Difference
(Left
, Right
: Set
) return Set
;
169 function "-" (Left
, Right
: Set
) return Set
renames Difference
;
171 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
);
173 function Symmetric_Difference
(Left
, Right
: Set
) return Set
;
175 function "xor" (Left
, Right
: Set
) return Set
renames Symmetric_Difference
;
177 function Overlap
(Left
, Right
: Set
) return Boolean;
179 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean;
181 function First
(Container
: Set
) return Cursor
;
183 function First_Element
(Container
: Set
) return Element_Type
;
185 function Last
(Container
: Set
) return Cursor
;
187 function Last_Element
(Container
: Set
) return Element_Type
;
189 function Next
(Position
: Cursor
) return Cursor
;
191 procedure Next
(Position
: in out Cursor
);
193 function Previous
(Position
: Cursor
) return Cursor
;
195 procedure Previous
(Position
: in out Cursor
);
197 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
;
199 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
;
201 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
;
203 function Contains
(Container
: Set
; Item
: Element_Type
) return Boolean;
205 function "<" (Left
, Right
: Cursor
) return Boolean;
207 function ">" (Left
, Right
: Cursor
) return Boolean;
209 function "<" (Left
: Cursor
; Right
: Element_Type
) return Boolean;
211 function ">" (Left
: Cursor
; Right
: Element_Type
) return Boolean;
213 function "<" (Left
: Element_Type
; Right
: Cursor
) return Boolean;
215 function ">" (Left
: Element_Type
; Right
: Cursor
) return Boolean;
219 Process
: not null access procedure (Position
: Cursor
));
221 procedure Reverse_Iterate
223 Process
: not null access procedure (Position
: Cursor
));
227 return Set_Iterator_Interfaces
.Reversible_Iterator
'class;
232 return Set_Iterator_Interfaces
.Reversible_Iterator
'class;
234 -- Ada 2022 features:
236 function Has_Element
(Container
: Set
; Position
: Cursor
) return Boolean;
238 function Tampering_With_Cursors_Prohibited
(Container
: Set
) return Boolean;
240 function Element
(Container
: Set
; Position
: Cursor
) return Element_Type
;
242 procedure Query_Element
245 Process
: not null access procedure (Element
: Element_Type
));
247 function Next
(Container
: Set
; Position
: Cursor
) return Cursor
;
249 procedure Next
(Container
: Set
; Position
: in out Cursor
);
254 type Key_Type
(<>) is private;
256 with function Key
(Element
: Element_Type
) return Key_Type
;
258 with function "<" (Left
, Right
: Key_Type
) return Boolean is <>;
260 package Generic_Keys
is
262 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
264 function Key
(Position
: Cursor
) return Key_Type
;
266 function Key
(Container
: Set
; Position
: Cursor
) return Key_Type
is
267 (Key
(Element
(Container
, Position
)));
269 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
;
272 (Container
: in out Set
;
274 New_Item
: Element_Type
);
276 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
);
278 procedure Delete
(Container
: in out Set
; Key
: Key_Type
);
280 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
;
282 function Floor
(Container
: Set
; Key
: Key_Type
) return Cursor
;
284 function Ceiling
(Container
: Set
; Key
: Key_Type
) return Cursor
;
286 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean;
288 procedure Update_Element_Preserving_Key
289 (Container
: in out Set
;
291 Process
: not null access
292 procedure (Element
: in out Element_Type
));
294 type Reference_Type
(Element
: not null access Element_Type
) is private
296 Implicit_Dereference
=> Element
;
298 function Reference_Preserving_Key
299 (Container
: aliased in out Set
;
300 Position
: Cursor
) return Reference_Type
;
302 function Constant_Reference
303 (Container
: aliased Set
;
304 Key
: Key_Type
) return Constant_Reference_Type
;
306 function Reference_Preserving_Key
307 (Container
: aliased in out Set
;
308 Key
: Key_Type
) return Reference_Type
;
311 type Set_Access
is access all Set
;
312 for Set_Access
'Storage_Size use 0;
314 type Key_Access
is access all Key_Type
;
316 package Impl
is new Helpers
.Generic_Implementation
;
318 type Reference_Control_Type
is
319 new Impl
.Reference_Control_Type
with
321 Container
: Set_Access
;
323 Old_Key
: Key_Access
;
326 overriding
procedure Finalize
(Control
: in out Reference_Control_Type
);
327 pragma Inline
(Finalize
);
329 type Reference_Type
(Element
: not null access Element_Type
) is record
330 Control
: Reference_Control_Type
;
336 (Stream
: not null access Root_Stream_Type
'Class;
337 Item
: Reference_Type
);
339 for Reference_Type
'Write use Write
;
342 (Stream
: not null access Root_Stream_Type
'Class;
343 Item
: out Reference_Type
);
345 for Reference_Type
'Read use Read
;
350 pragma Inline
(Next
);
351 pragma Inline
(Previous
);
354 type Node_Access
is access Node_Type
;
356 type Node_Type
is limited record
357 Parent
: Node_Access
;
360 Color
: Red_Black_Trees
.Color_Type
:= Red_Black_Trees
.Red
;
361 Element
: aliased Element_Type
;
364 package Tree_Types
is
365 new Red_Black_Trees
.Generic_Tree_Types
(Node_Type
, Node_Access
);
367 type Set
is new Ada
.Finalization
.Controlled
with record
368 Tree
: Tree_Types
.Tree_Type
;
369 end record with Put_Image
=> Put_Image
;
372 (S
: in out Ada
.Strings
.Text_Buffers
.Root_Buffer_Type
'Class; V
: Set
);
374 overriding
procedure Adjust
(Container
: in out Set
);
376 overriding
procedure Finalize
(Container
: in out Set
) renames Clear
;
379 use Tree_Types
, Tree_Types
.Implementation
;
380 use Ada
.Finalization
;
384 (Stream
: not null access Root_Stream_Type
'Class;
387 for Set
'Write use Write
;
390 (Stream
: not null access Root_Stream_Type
'Class;
391 Container
: out Set
);
393 for Set
'Read use Read
;
395 type Set_Access
is access all Set
;
396 for Set_Access
'Storage_Size use 0;
398 type Cursor
is record
399 Container
: Set_Access
;
404 (Stream
: not null access Root_Stream_Type
'Class;
407 for Cursor
'Write use Write
;
410 (Stream
: not null access Root_Stream_Type
'Class;
413 for Cursor
'Read use Read
;
415 subtype Reference_Control_Type
is Implementation
.Reference_Control_Type
;
416 -- It is necessary to rename this here, so that the compiler can find it
418 type Constant_Reference_Type
419 (Element
: not null access constant Element_Type
) is
421 Control
: Reference_Control_Type
:=
422 raise Program_Error
with "uninitialized reference";
423 -- The RM says, "The default initialization of an object of
424 -- type Constant_Reference_Type or Reference_Type propagates
429 (Stream
: not null access Root_Stream_Type
'Class;
430 Item
: Constant_Reference_Type
);
432 for Constant_Reference_Type
'Write use Write
;
435 (Stream
: not null access Root_Stream_Type
'Class;
436 Item
: out Constant_Reference_Type
);
438 for Constant_Reference_Type
'Read use Read
;
440 -- See Ada.Containers.Vectors for documentation on the following
442 procedure _Next
(Position
: in out Cursor
) renames Next
;
443 procedure _Previous
(Position
: in out Cursor
) renames Previous
;
445 function Pseudo_Reference
446 (Container
: aliased Set
'Class) return Reference_Control_Type
;
447 pragma Inline
(Pseudo_Reference
);
448 -- Creates an object of type Reference_Control_Type pointing to the
449 -- container, and increments the Lock. Finalization of this object will
450 -- decrement the Lock.
452 type Element_Access
is access all Element_Type
with
455 function Get_Element_Access
456 (Position
: Cursor
) return not null Element_Access
;
457 -- Returns a pointer to the element designated by Position.
459 Empty_Set
: constant Set
:= (Controlled
with others => <>);
460 function Empty
return Set
is (Empty_Set
);
462 No_Element
: constant Cursor
:= Cursor
'(null, null);
464 type Iterator is new Limited_Controlled and
465 Set_Iterator_Interfaces.Reversible_Iterator with
467 Container : Set_Access;
470 with Disable_Controlled => not T_Check;
472 overriding procedure Finalize (Object : in out Iterator);
474 overriding function First (Object : Iterator) return Cursor;
475 overriding function Last (Object : Iterator) return Cursor;
477 overriding function Next
479 Position : Cursor) return Cursor;
481 overriding function Previous
483 Position : Cursor) return Cursor;
485 end Ada.Containers.Ordered_Sets;