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 _ S E T S --
9 -- Copyright (C) 2004-2011, 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 private with Ada.Containers.Red_Black_Trees;
35 with Ada.Streams; use Ada.Streams;
36 with Ada.Iterator_Interfaces;
39 type Element_Type is private;
41 with function "<" (Left, Right : Element_Type) return Boolean is <>;
42 with function "=" (Left, Right : Element_Type) return Boolean is <>;
44 package Ada.Containers.Bounded_Ordered_Sets is
48 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
50 type Set (Capacity : Count_Type) is tagged private
51 with Constant_Indexing => Constant_Reference,
52 Default_Iterator => Iterate,
53 Iterator_Element => Element_Type;
55 pragma Preelaborable_Initialization (Set);
57 type Cursor is private;
58 pragma Preelaborable_Initialization (Cursor);
60 Empty_Set : constant Set;
62 No_Element : constant Cursor;
63 function Has_Element (Position : Cursor) return Boolean;
65 package Set_Iterator_Interfaces is new
66 Ada.Iterator_Interfaces (Cursor, Has_Element);
68 type Constant_Reference_Type
69 (Element : not null access constant Element_Type) is
72 Implicit_Dereference => Element;
74 function Constant_Reference
75 (Container : Set; Position : Cursor)
76 return Constant_Reference_Type;
78 function "=" (Left, Right : Set) return Boolean;
80 function Equivalent_Sets (Left, Right : Set) return Boolean;
82 function To_Set (New_Item : Element_Type) return Set;
84 function Length (Container : Set) return Count_Type;
86 function Is_Empty (Container : Set) return Boolean;
88 procedure Clear (Container : in out Set);
90 function Element (Position : Cursor) return Element_Type;
92 procedure Replace_Element
93 (Container : in out Set;
95 New_Item : Element_Type);
97 procedure Query_Element
99 Process : not null access procedure (Element : Element_Type));
101 procedure Assign (Target : in out Set; Source : Set);
103 function Copy (Source : Set; Capacity : Count_Type := 0) return Set;
105 procedure Move (Target : in out Set; Source : in out Set);
108 (Container : in out Set;
109 New_Item : Element_Type;
110 Position : out Cursor;
111 Inserted : out Boolean);
114 (Container : in out Set;
115 New_Item : Element_Type);
118 (Container : in out Set;
119 New_Item : Element_Type);
122 (Container : in out Set;
123 New_Item : Element_Type);
126 (Container : in out Set;
127 Item : Element_Type);
130 (Container : in out Set;
131 Item : Element_Type);
134 (Container : in out Set;
135 Position : in out Cursor);
137 procedure Delete_First (Container : in out Set);
139 procedure Delete_Last (Container : in out Set);
141 procedure Union (Target : in out Set; Source : Set);
143 function Union (Left, Right : Set) return Set;
145 function "or" (Left, Right : Set) return Set renames Union;
147 procedure Intersection (Target : in out Set; Source : Set);
149 function Intersection (Left, Right : Set) return Set;
151 function "and" (Left, Right : Set) return Set renames Intersection;
153 procedure Difference (Target : in out Set; Source : Set);
155 function Difference (Left, Right : Set) return Set;
157 function "-" (Left, Right : Set) return Set renames Difference;
159 procedure Symmetric_Difference (Target : in out Set; Source : Set);
161 function Symmetric_Difference (Left, Right : Set) return Set;
163 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
165 function Overlap (Left, Right : Set) return Boolean;
167 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
169 function First (Container : Set) return Cursor;
171 function First_Element (Container : Set) return Element_Type;
173 function Last (Container : Set) return Cursor;
175 function Last_Element (Container : Set) return Element_Type;
177 function Next (Position : Cursor) return Cursor;
179 procedure Next (Position : in out Cursor);
181 function Previous (Position : Cursor) return Cursor;
183 procedure Previous (Position : in out Cursor);
185 function Find (Container : Set; Item : Element_Type) return Cursor;
187 function Floor (Container : Set; Item : Element_Type) return Cursor;
189 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
191 function Contains (Container : Set; Item : Element_Type) return Boolean;
193 function "<" (Left, Right : Cursor) return Boolean;
195 function ">" (Left, Right : Cursor) return Boolean;
197 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
199 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
201 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
203 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
207 Process : not null access procedure (Position : Cursor));
209 procedure Reverse_Iterate
211 Process : not null access procedure (Position : Cursor));
215 return Set_Iterator_Interfaces.Reversible_Iterator'class;
220 return Set_Iterator_Interfaces.Reversible_Iterator'class;
223 type Key_Type (<>) is private;
225 with function Key (Element : Element_Type) return Key_Type;
227 with function "<" (Left, Right : Key_Type) return Boolean is <>;
229 package Generic_Keys is
231 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
233 function Key (Position : Cursor) return Key_Type;
235 function Element (Container : Set; Key : Key_Type) return Element_Type;
238 (Container : in out Set;
240 New_Item : Element_Type);
242 procedure Exclude (Container : in out Set; Key : Key_Type);
244 procedure Delete (Container : in out Set; Key : Key_Type);
246 function Find (Container : Set; Key : Key_Type) return Cursor;
248 function Floor (Container : Set; Key : Key_Type) return Cursor;
250 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
252 function Contains (Container : Set; Key : Key_Type) return Boolean;
254 procedure Update_Element_Preserving_Key
255 (Container : in out Set;
257 Process : not null access
258 procedure (Element : in out Element_Type));
260 type Reference_Type (Element : not null access Element_Type) is private
262 Implicit_Dereference => Element;
264 function Reference_Preserving_Key
265 (Container : aliased in out Set;
266 Key : Key_Type) return Constant_Reference_Type;
268 function Reference_Preserving_Key
269 (Container : aliased in out Set;
270 Key : Key_Type) return Reference_Type;
274 (Element : not null access Element_Type) is null record;
277 (Stream : not null access Root_Stream_Type'Class;
278 Item : out Reference_Type);
280 for Reference_Type'Read use Read;
283 (Stream : not null access Root_Stream_Type'Class;
284 Item : Reference_Type);
286 for Reference_Type'Write use Write;
292 pragma Inline (Next);
293 pragma Inline (Previous);
295 type Node_Type is record
299 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
300 Element : Element_Type;
303 package Tree_Types is
304 new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
306 type Set (Capacity : Count_Type) is
307 new Tree_Types.Tree_Type (Capacity) with null record;
309 type Set_Access is access all Set;
310 for Set_Access'Storage_Size use 0;
312 -- Note: If a Cursor object has no explicit initialization expression,
313 -- it must default initialize to the same value as constant No_Element.
314 -- The Node component of type Cursor has scalar type Count_Type, so it
315 -- requires an explicit initialization expression of its own declaration,
316 -- in order for objects of record type Cursor to properly initialize.
318 type Cursor is record
319 Container : Set_Access;
320 Node : Count_Type := 0;
326 (Stream : not null access Root_Stream_Type'Class;
329 for Cursor'Write use Write;
332 (Stream : not null access Root_Stream_Type'Class;
335 for Cursor'Read use Read;
337 No_Element : constant Cursor := Cursor'(null, 0);
340 (Stream : not null access Root_Stream_Type'Class;
343 for Set'Write use Write;
346 (Stream : not null access Root_Stream_Type'Class;
347 Container : out Set);
349 for Set'Read use Read;
351 type Constant_Reference_Type
352 (Element : not null access constant Element_Type) is null record;
355 (Stream : not null access Root_Stream_Type'Class;
356 Item : out Constant_Reference_Type);
358 for Constant_Reference_Type'Read use Read;
361 (Stream : not null access Root_Stream_Type'Class;
362 Item : Constant_Reference_Type);
364 for Constant_Reference_Type'Write use Write;
366 Empty_Set : constant Set := Set'(Tree_Type with Capacity => 0);
368 end Ada.Containers.Bounded_Ordered_Sets;