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-2005 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, 59 Temple Place - Suite 330, Boston, --
24 -- MA 02111-1307, 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;
42 type Element_Type is private;
44 with function "<" (Left, Right : Element_Type) return Boolean is <>;
45 with function "=" (Left, Right : Element_Type) return Boolean is <>;
47 package Ada.Containers.Ordered_Sets is
48 pragma Preelaborate (Ordered_Sets);
50 type Set is tagged private;
52 type Cursor is private;
54 Empty_Set : constant Set;
56 No_Element : constant Cursor;
58 function "=" (Left, Right : Set) return Boolean;
60 function Equivalent_Sets (Left, Right : Set) return Boolean;
62 function Length (Container : Set) return Count_Type;
64 function Is_Empty (Container : Set) return Boolean;
66 procedure Clear (Container : in out Set);
68 function Element (Position : Cursor) return Element_Type;
70 procedure Query_Element
72 Process : not null access procedure (Element : Element_Type));
74 procedure Replace_Element
75 (Container : Set; -- TODO: need ARG ruling
84 (Container : in out Set;
85 New_Item : Element_Type;
86 Position : out Cursor;
87 Inserted : out Boolean);
90 (Container : in out Set;
91 New_Item : Element_Type);
94 (Container : in out Set;
95 New_Item : Element_Type);
98 (Container : in out Set; -- TODO: need ARG ruling
99 New_Item : Element_Type);
102 (Container : in out Set;
103 Item : Element_Type);
106 (Container : in out Set;
107 Position : in out Cursor);
109 procedure Delete_First (Container : in out Set);
111 procedure Delete_Last (Container : in out Set);
114 (Container : in out Set;
115 Item : Element_Type);
117 procedure Union (Target : in out Set; Source : Set);
119 function Union (Left, Right : Set) return Set;
121 function "or" (Left, Right : Set) return Set renames Union;
123 procedure Intersection (Target : in out Set; Source : Set);
125 function Intersection (Left, Right : Set) return Set;
127 function "and" (Left, Right : Set) return Set renames Intersection;
129 procedure Difference (Target : in out Set;
132 function Difference (Left, Right : Set) return Set;
134 function "-" (Left, Right : Set) return Set renames Difference;
136 procedure Symmetric_Difference (Target : in out Set; Source : Set);
138 function Symmetric_Difference (Left, Right : Set) return Set;
140 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
142 function Overlap (Left, Right : Set) return Boolean;
144 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
146 function Contains (Container : Set; Item : Element_Type) return Boolean;
148 function Find (Container : Set; Item : Element_Type) return Cursor;
150 function Floor (Container : Set; Item : Element_Type) return Cursor;
152 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
154 function First (Container : Set) return Cursor;
156 function First_Element (Container : Set) return Element_Type;
158 function Last (Container : Set) return Cursor;
160 function Last_Element (Container : Set) return Element_Type;
162 function Next (Position : Cursor) return Cursor;
164 procedure Next (Position : in out Cursor);
166 function Previous (Position : Cursor) return Cursor;
168 procedure Previous (Position : in out Cursor);
170 function Has_Element (Position : Cursor) return Boolean;
172 function "<" (Left, Right : Cursor) return Boolean;
174 function ">" (Left, Right : Cursor) return Boolean;
176 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
178 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
180 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
182 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
186 Process : not null access procedure (Position : Cursor));
188 procedure Reverse_Iterate
190 Process : not null access procedure (Position : Cursor));
193 type Key_Type (<>) is limited private;
195 with function Key (Element : Element_Type) return Key_Type;
199 Right : Element_Type) return Boolean is <>;
203 Right : Element_Type) return Boolean is <>;
205 package Generic_Keys is
207 function Contains (Container : Set; Key : Key_Type) return Boolean;
209 function Find (Container : Set; Key : Key_Type) return Cursor;
211 function Floor (Container : Set; Key : Key_Type) return Cursor;
213 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
215 function Key (Position : Cursor) return Key_Type;
217 function Element (Container : Set; Key : Key_Type) return Element_Type;
220 (Container : in out Set; -- TODO: need ARG ruling
222 New_Item : Element_Type);
224 procedure Delete (Container : in out Set; Key : Key_Type);
226 procedure Exclude (Container : in out Set; Key : Key_Type);
228 function "<" (Left : Cursor; Right : Key_Type) return Boolean;
230 function ">" (Left : Cursor; Right : Key_Type) return Boolean;
232 function "<" (Left : Key_Type; Right : Cursor) return Boolean;
234 function ">" (Left : Key_Type; Right : Cursor) return Boolean;
236 procedure Update_Element_Preserving_Key
237 (Container : in out Set;
239 Process : not null access
240 procedure (Element : in out Element_Type));
247 type Node_Access is access Node_Type;
249 type Node_Type is limited record
250 Parent : Node_Access;
253 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
254 Element : Element_Type;
257 package Tree_Types is new Red_Black_Trees.Generic_Tree_Types
261 type Set is new Ada.Finalization.Controlled with record
262 Tree : Tree_Types.Tree_Type;
265 procedure Adjust (Container : in out Set);
267 procedure Finalize (Container : in out Set) renames Clear;
271 use Ada.Finalization;
273 type Set_Access is access all Set;
274 for Set_Access'Storage_Size use 0;
276 type Cursor is record
277 Container : Set_Access;
281 No_Element : constant Cursor := Cursor'(null, null);
286 (Stream : access Root_Stream_Type'Class;
289 for Set'Write use Write;
292 (Stream : access Root_Stream_Type'Class;
293 Container : out Set);
295 for Set'Read use Read;
297 Empty_Set : constant Set :=
298 (Controlled with Tree => (First => null,
305 end Ada.Containers.Ordered_Sets;