1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . F O R M A L _ O R D E R E D _ S E T S --
9 -- Copyright (C) 2004-2010, 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/>. --
30 ------------------------------------------------------------------------------
32 -- This spec is derived from package Ada.Containers.Bounded_Ordered_Sets in
33 -- the Ada 2012 RM. The modifications are to facilitate formal proofs by
34 -- making it easier to express properties.
36 -- The modifications are:
38 -- A parameter for the container is added to every function reading the
39 -- content of a container: Key, Element, Next, Query_Element, Previous,
40 -- Has_Element, Iterate, Reverse_Iterate. This change is motivated by the
41 -- need to have cursors which are valid on different containers (typically
42 -- a container C and its previous version C'Old) for expressing properties,
43 -- which is not possible if cursors encapsulate an access to the underlying
44 -- container. The operators "<" and ">" that could not be modified that way
47 -- There are two new functions:
49 -- function Left (Container : Set; Position : Cursor) return Set;
50 -- function Right (Container : Set; Position : Cursor) return Set;
52 -- Left returns a container containing all elements preceding Position
53 -- (excluded) in Container. Right returns a container containing all
54 -- elements following Position (included) in Container. These two new
55 -- functions are useful to express invariant properties in loops which
56 -- iterate over containers. Left returns the part of the container already
57 -- scanned and Right the part not scanned yet.
59 private with Ada.Containers.Red_Black_Trees;
60 private with Ada.Streams;
65 type Element_Type is private;
67 with function "<" (Left, Right : Element_Type) return Boolean is <>;
68 with function "=" (Left, Right : Element_Type) return Boolean is <>;
70 package Ada.Containers.Formal_Ordered_Sets is
73 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
75 type Set (Capacity : Count_Type) is tagged private;
76 -- pragma Preelaborable_Initialization (Set);
78 type Cursor is private;
79 pragma Preelaborable_Initialization (Cursor);
81 Empty_Set : constant Set;
83 No_Element : constant Cursor;
85 function "=" (Left, Right : Set) return Boolean;
87 function Equivalent_Sets (Left, Right : Set) return Boolean;
89 function To_Set (New_Item : Element_Type) return Set;
91 function Length (Container : Set) return Count_Type;
93 function Is_Empty (Container : Set) return Boolean;
95 procedure Clear (Container : in out Set);
97 procedure Assign (Target : in out Set; Source : Set);
99 function Copy (Source : Set; Capacity : Count_Type := 0) return Set;
101 function Element (Container : Set; Position : Cursor) return Element_Type;
103 procedure Replace_Element
104 (Container : in out Set;
106 New_Item : Element_Type);
108 procedure Query_Element
109 (Container : in out Set;
111 Process : not null access procedure (Element : Element_Type));
113 procedure Move (Target : in out Set; Source : in out Set);
116 (Container : in out Set;
117 New_Item : Element_Type;
118 Position : out Cursor;
119 Inserted : out Boolean);
122 (Container : in out Set;
123 New_Item : Element_Type);
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 Item : Element_Type);
138 (Container : in out Set;
139 Item : Element_Type);
142 (Container : in out Set;
143 Position : in out Cursor);
145 procedure Delete_First (Container : in out Set);
147 procedure Delete_Last (Container : in out Set);
149 procedure Union (Target : in out Set; Source : Set);
151 function Union (Left, Right : Set) return Set;
153 function "or" (Left, Right : Set) return Set renames Union;
155 procedure Intersection (Target : in out Set; Source : Set);
157 function Intersection (Left, Right : Set) return Set;
159 function "and" (Left, Right : Set) return Set renames Intersection;
161 procedure Difference (Target : in out Set; Source : Set);
163 function Difference (Left, Right : Set) return Set;
165 function "-" (Left, Right : Set) return Set renames Difference;
167 procedure Symmetric_Difference (Target : in out Set; Source : Set);
169 function Symmetric_Difference (Left, Right : Set) return Set;
171 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
173 function Overlap (Left, Right : Set) return Boolean;
175 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
177 function First (Container : Set) return Cursor;
179 function First_Element (Container : Set) return Element_Type;
181 function Last (Container : Set) return Cursor;
183 function Last_Element (Container : Set) return Element_Type;
185 function Next (Container : Set; Position : Cursor) return Cursor;
187 procedure Next (Container : Set; Position : in out Cursor);
189 function Previous (Container : Set; Position : Cursor) return Cursor;
191 procedure Previous (Container : Set; Position : in out Cursor);
193 function Find (Container : Set; Item : Element_Type) return Cursor;
195 function Floor (Container : Set; Item : Element_Type) return Cursor;
197 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
199 function Contains (Container : Set; Item : Element_Type) return Boolean;
201 function Has_Element (Container : Set; Position : Cursor) return Boolean;
206 not null access procedure (Container : Set; Position : Cursor));
208 procedure Reverse_Iterate
210 Process : not null access
211 procedure (Container : Set; Position : Cursor));
214 type Key_Type (<>) is private;
216 with function Key (Element : Element_Type) return Key_Type;
218 with function "<" (Left, Right : Key_Type) return Boolean is <>;
220 package Generic_Keys is
222 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
224 function Key (Container : Set; Position : Cursor) return Key_Type;
226 function Element (Container : Set; Key : Key_Type) return Element_Type;
229 (Container : in out Set;
231 New_Item : Element_Type);
233 procedure Exclude (Container : in out Set; Key : Key_Type);
235 procedure Delete (Container : in out Set; Key : Key_Type);
237 function Find (Container : Set; Key : Key_Type) return Cursor;
239 function Floor (Container : Set; Key : Key_Type) return Cursor;
241 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
243 function Contains (Container : Set; Key : Key_Type) return Boolean;
245 procedure Update_Element_Preserving_Key
246 (Container : in out Set;
248 Process : not null access
249 procedure (Element : in out Element_Type));
253 function Strict_Equal (Left, Right : Set) return Boolean;
255 function Left (Container : Set; Position : Cursor) return Set;
257 function Right (Container : Set; Position : Cursor) return Set;
261 pragma Inline (Next);
262 pragma Inline (Previous);
264 type Node_Type is record
265 Has_Element : Boolean := False;
269 Color : Red_Black_Trees.Color_Type;
270 Element : Element_Type;
273 type Kind is (Plain, Part);
275 package Tree_Types is
276 new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
278 type Tree_Type_Access is access all Tree_Types.Tree_Type;
280 type Set (Capacity : Count_Type) is tagged record
281 Tree : Tree_Type_Access := new Tree_Types.Tree_Type (Capacity);
283 Length : Count_Type := 0;
284 First : Count_Type := 0;
285 Last : Count_Type := 0;
291 type Set_Access is access all Set;
292 for Set_Access'Storage_Size use 0;
294 type Cursor is record
299 (Stream : not null access Root_Stream_Type'Class;
302 for Cursor'Write use Write;
305 (Stream : not null access Root_Stream_Type'Class;
308 for Cursor'Read use Read;
310 No_Element : constant Cursor := (Node => 0);
313 (Stream : not null access Root_Stream_Type'Class;
316 for Set'Write use Write;
319 (Stream : not null access Root_Stream_Type'Class;
320 Container : out Set);
322 for Set'Read use Read;
324 Empty_Set : constant Set := (Capacity => 0, others => <>);
326 end Ada.Containers.Formal_Ordered_Sets;