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-2013, 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 meant to facilitate formal proofs by
34 -- making it easier to express properties, and by making the specification of
35 -- this unit compatible with SPARK 2014. Note that the API of this unit may be
36 -- subject to incompatible changes as SPARK 2014 evolves.
38 -- The modifications are:
40 -- A parameter for the container is added to every function reading the
41 -- content of a container: Key, Element, Next, Query_Element, Previous,
42 -- Has_Element, Iterate, Reverse_Iterate. This change is motivated by the
43 -- need to have cursors which are valid on different containers (typically
44 -- a container C and its previous version C'Old) for expressing properties,
45 -- which is not possible if cursors encapsulate an access to the underlying
46 -- container. The operators "<" and ">" that could not be modified that way
49 -- There are three new functions:
51 -- function Strict_Equal (Left, Right : Set) return Boolean;
52 -- function Left (Container : Set; Position : Cursor) return Set;
53 -- function Right (Container : Set; Position : Cursor) return Set;
55 -- See detailed specifications for these subprograms
57 private with Ada
.Containers
.Red_Black_Trees
;
60 type Element_Type
is private;
62 with function "<" (Left
, Right
: Element_Type
) return Boolean is <>;
63 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
65 package Ada
.Containers
.Formal_Ordered_Sets
is
66 pragma Annotate
(GNATprove
, External_Axiomatization
);
69 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean;
71 type Set
(Capacity
: Count_Type
) is private;
72 pragma Preelaborable_Initialization
(Set
);
74 type Cursor
is private;
75 pragma Preelaborable_Initialization
(Cursor
);
77 Empty_Set
: constant Set
;
79 No_Element
: constant Cursor
;
81 function "=" (Left
, Right
: Set
) return Boolean;
83 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean;
85 function To_Set
(New_Item
: Element_Type
) return Set
;
87 function Length
(Container
: Set
) return Count_Type
;
89 function Is_Empty
(Container
: Set
) return Boolean;
91 procedure Clear
(Container
: in out Set
);
93 procedure Assign
(Target
: in out Set
; Source
: Set
) with
94 Pre
=> Target
.Capacity
>= Length
(Source
);
96 function Copy
(Source
: Set
; Capacity
: Count_Type
:= 0) return Set
with
97 Pre
=> Capacity
= 0 or else Capacity
>= Source
.Capacity
;
101 Position
: Cursor
) return Element_Type
103 Pre
=> Has_Element
(Container
, Position
);
105 procedure Replace_Element
106 (Container
: in out Set
;
108 New_Item
: Element_Type
)
110 Pre
=> Has_Element
(Container
, Position
);
112 procedure Move
(Target
: in out Set
; Source
: in out Set
) with
113 Pre
=> Target
.Capacity
>= Length
(Source
);
116 (Container
: in out Set
;
117 New_Item
: Element_Type
;
118 Position
: out Cursor
;
119 Inserted
: out Boolean)
121 Pre
=> Length
(Container
) < Container
.Capacity
;
124 (Container
: in out Set
;
125 New_Item
: Element_Type
)
127 Pre
=> Length
(Container
) < Container
.Capacity
128 and then (not Contains
(Container
, New_Item
));
131 (Container
: in out Set
;
132 New_Item
: Element_Type
)
134 Pre
=> Length
(Container
) < Container
.Capacity
;
137 (Container
: in out Set
;
138 New_Item
: Element_Type
)
140 Pre
=> Contains
(Container
, New_Item
);
143 (Container
: in out Set
;
144 Item
: Element_Type
);
147 (Container
: in out Set
;
150 Pre
=> Contains
(Container
, Item
);
153 (Container
: in out Set
;
154 Position
: in out Cursor
)
156 Pre
=> Has_Element
(Container
, Position
);
158 procedure Delete_First
(Container
: in out Set
);
160 procedure Delete_Last
(Container
: in out Set
);
162 procedure Union
(Target
: in out Set
; Source
: Set
) with
163 Pre
=> Length
(Target
) + Length
(Source
) -
164 Length
(Intersection
(Target
, Source
)) <= Target
.Capacity
;
166 function Union
(Left
, Right
: Set
) return Set
;
168 function "or" (Left
, Right
: Set
) return Set
renames Union
;
170 procedure Intersection
(Target
: in out Set
; Source
: Set
);
172 function Intersection
(Left
, Right
: Set
) return Set
;
174 function "and" (Left
, Right
: Set
) return Set
renames Intersection
;
176 procedure Difference
(Target
: in out Set
; Source
: Set
);
178 function Difference
(Left
, Right
: Set
) return Set
;
180 function "-" (Left
, Right
: Set
) return Set
renames Difference
;
182 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
);
184 function Symmetric_Difference
(Left
, Right
: Set
) return Set
;
186 function "xor" (Left
, Right
: Set
) return Set
renames Symmetric_Difference
;
188 function Overlap
(Left
, Right
: Set
) return Boolean;
190 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean;
192 function First
(Container
: Set
) return Cursor
;
194 function First_Element
(Container
: Set
) return Element_Type
with
195 Pre
=> not Is_Empty
(Container
);
197 function Last
(Container
: Set
) return Cursor
;
199 function Last_Element
(Container
: Set
) return Element_Type
with
200 Pre
=> not Is_Empty
(Container
);
202 function Next
(Container
: Set
; Position
: Cursor
) return Cursor
with
203 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
205 procedure Next
(Container
: Set
; Position
: in out Cursor
) with
206 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
208 function Previous
(Container
: Set
; Position
: Cursor
) return Cursor
with
209 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
211 procedure Previous
(Container
: Set
; Position
: in out Cursor
) with
212 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
214 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
;
216 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
;
218 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
;
220 function Contains
(Container
: Set
; Item
: Element_Type
) return Boolean;
222 function Has_Element
(Container
: Set
; Position
: Cursor
) return Boolean;
225 type Key_Type
(<>) is private;
227 with function Key
(Element
: Element_Type
) return Key_Type
;
229 with function "<" (Left
, Right
: Key_Type
) return Boolean is <>;
231 package Generic_Keys
is
233 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
235 function Key
(Container
: Set
; Position
: Cursor
) return Key_Type
;
237 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
;
240 (Container
: in out Set
;
242 New_Item
: Element_Type
);
244 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
);
246 procedure Delete
(Container
: in out Set
; Key
: Key_Type
);
248 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
;
250 function Floor
(Container
: Set
; Key
: Key_Type
) return Cursor
;
252 function Ceiling
(Container
: Set
; Key
: Key_Type
) return Cursor
;
254 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean;
258 function Strict_Equal
(Left
, Right
: Set
) return Boolean;
259 -- Strict_Equal returns True if the containers are physically equal, i.e.
260 -- they are structurally equal (function "=" returns True) and that they
261 -- have the same set of cursors.
263 function Left
(Container
: Set
; Position
: Cursor
) return Set
with
264 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
265 function Right
(Container
: Set
; Position
: Cursor
) return Set
with
266 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
267 -- Left returns a container containing all elements preceding Position
268 -- (excluded) in Container. Right returns a container containing all
269 -- elements following Position (included) in Container. These two new
270 -- functions can be used to express invariant properties in loops which
271 -- iterate over containers. Left returns the part of the container already
272 -- scanned and Right the part not scanned yet.
276 pragma Inline
(Next
);
277 pragma Inline
(Previous
);
279 type Node_Type
is record
280 Has_Element
: Boolean := False;
281 Parent
: Count_Type
:= 0;
282 Left
: Count_Type
:= 0;
283 Right
: Count_Type
:= 0;
284 Color
: Red_Black_Trees
.Color_Type
;
285 Element
: Element_Type
;
288 package Tree_Types
is
289 new Red_Black_Trees
.Generic_Bounded_Tree_Types
(Node_Type
);
291 type Set
(Capacity
: Count_Type
) is
292 new Tree_Types
.Tree_Type
(Capacity
) with null record;
296 type Cursor
is record
300 No_Element
: constant Cursor
:= (Node
=> 0);
302 Empty_Set
: constant Set
:= (Capacity
=> 0, others => <>);
304 end Ada
.Containers
.Formal_Ordered_Sets
;