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-2007, 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 2, 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. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, USA. --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
33 -- This unit was originally developed by Matthew J Heaney. --
34 ------------------------------------------------------------------------------
36 with Ada.Containers.Red_Black_Trees;
37 with Ada.Finalization;
41 type Element_Type is private;
43 with function "<" (Left, Right : Element_Type) return Boolean is <>;
44 with function "=" (Left, Right : Element_Type) return Boolean is <>;
46 package Ada.Containers.Ordered_Sets is
50 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
52 type Set is tagged private;
53 pragma Preelaborable_Initialization (Set);
55 type Cursor is private;
56 pragma Preelaborable_Initialization (Cursor);
58 Empty_Set : constant Set;
60 No_Element : constant Cursor;
62 function "=" (Left, Right : Set) return Boolean;
64 function Equivalent_Sets (Left, Right : Set) return Boolean;
66 function To_Set (New_Item : Element_Type) return Set;
68 function Length (Container : Set) return Count_Type;
70 function Is_Empty (Container : Set) return Boolean;
72 procedure Clear (Container : in out Set);
74 function Element (Position : Cursor) return Element_Type;
76 procedure Replace_Element
77 (Container : in out Set;
79 New_Item : Element_Type);
81 procedure Query_Element
83 Process : not null access procedure (Element : Element_Type));
85 procedure Move (Target : in out Set; Source : in out Set);
88 (Container : in out Set;
89 New_Item : Element_Type;
90 Position : out Cursor;
91 Inserted : out Boolean);
94 (Container : in out Set;
95 New_Item : Element_Type);
98 (Container : in out Set;
99 New_Item : Element_Type);
102 (Container : in out Set;
103 New_Item : Element_Type);
106 (Container : in out Set;
107 Item : Element_Type);
110 (Container : in out Set;
111 Item : Element_Type);
114 (Container : in out Set;
115 Position : in out Cursor);
117 procedure Delete_First (Container : in out Set);
119 procedure Delete_Last (Container : in out Set);
121 procedure Union (Target : in out Set; Source : Set);
123 function Union (Left, Right : Set) return Set;
125 function "or" (Left, Right : Set) return Set renames Union;
127 procedure Intersection (Target : in out Set; Source : Set);
129 function Intersection (Left, Right : Set) return Set;
131 function "and" (Left, Right : Set) return Set renames Intersection;
133 procedure Difference (Target : in out Set; Source : Set);
135 function Difference (Left, Right : Set) return Set;
137 function "-" (Left, Right : Set) return Set renames Difference;
139 procedure Symmetric_Difference (Target : in out Set; Source : Set);
141 function Symmetric_Difference (Left, Right : Set) return Set;
143 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
145 function Overlap (Left, Right : Set) return Boolean;
147 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
149 function First (Container : Set) return Cursor;
151 function First_Element (Container : Set) return Element_Type;
153 function Last (Container : Set) return Cursor;
155 function Last_Element (Container : Set) return Element_Type;
157 function Next (Position : Cursor) return Cursor;
159 procedure Next (Position : in out Cursor);
161 function Previous (Position : Cursor) return Cursor;
163 procedure Previous (Position : in out Cursor);
165 function Find (Container : Set; Item : Element_Type) return Cursor;
167 function Floor (Container : Set; Item : Element_Type) return Cursor;
169 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
171 function Contains (Container : Set; Item : Element_Type) return Boolean;
173 function Has_Element (Position : Cursor) return Boolean;
175 function "<" (Left, Right : Cursor) return Boolean;
177 function ">" (Left, Right : Cursor) return Boolean;
179 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
181 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
183 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
185 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
189 Process : not null access procedure (Position : Cursor));
191 procedure Reverse_Iterate
193 Process : not null access procedure (Position : Cursor));
196 type Key_Type (<>) is private;
198 with function Key (Element : Element_Type) return Key_Type;
200 with function "<" (Left, Right : Key_Type) return Boolean is <>;
202 package Generic_Keys is
204 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
206 function Key (Position : Cursor) return Key_Type;
208 function Element (Container : Set; Key : Key_Type) return Element_Type;
211 (Container : in out Set;
213 New_Item : Element_Type);
215 procedure Exclude (Container : in out Set; Key : Key_Type);
217 procedure Delete (Container : in out Set; Key : Key_Type);
219 function Find (Container : Set; Key : Key_Type) return Cursor;
221 function Floor (Container : Set; Key : Key_Type) return Cursor;
223 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
225 function Contains (Container : Set; Key : Key_Type) return Boolean;
227 procedure Update_Element_Preserving_Key
228 (Container : in out Set;
230 Process : not null access
231 procedure (Element : in out Element_Type));
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;
248 Element : Element_Type;
251 package Tree_Types is new Red_Black_Trees.Generic_Tree_Types
255 type Set is new Ada.Finalization.Controlled with record
256 Tree : Tree_Types.Tree_Type;
259 procedure Adjust (Container : in out Set);
261 procedure Finalize (Container : in out Set) renames Clear;
265 use Ada.Finalization;
268 type Set_Access is access all Set;
269 for Set_Access'Storage_Size use 0;
271 type Cursor is record
272 Container : Set_Access;
277 (Stream : not null access Root_Stream_Type'Class;
280 for Cursor'Write use Write;
283 (Stream : not null access Root_Stream_Type'Class;
286 for Cursor'Read use Read;
288 No_Element : constant Cursor := Cursor'(null, null);
291 (Stream : not null access Root_Stream_Type'Class;
294 for Set'Write use Write;
297 (Stream : not null access Root_Stream_Type'Class;
298 Container : out Set);
300 for Set'Read use Read;
302 Empty_Set : constant Set :=
303 (Controlled with Tree => (First => null,
310 end Ada.Containers.Ordered_Sets;