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
68 function Equivalent_Elements
(Left
, Right
: Element_Type
) return Boolean;
70 type Set
(Capacity
: Count_Type
) is private;
71 pragma Preelaborable_Initialization
(Set
);
73 type Cursor
is private;
74 pragma Preelaborable_Initialization
(Cursor
);
76 Empty_Set
: constant Set
;
78 No_Element
: constant Cursor
;
80 function "=" (Left
, Right
: Set
) return Boolean;
82 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean;
84 function To_Set
(New_Item
: Element_Type
) return Set
;
86 function Length
(Container
: Set
) return Count_Type
;
88 function Is_Empty
(Container
: Set
) return Boolean;
90 procedure Clear
(Container
: in out Set
);
92 procedure Assign
(Target
: in out Set
; Source
: Set
) with
93 Pre
=> Target
.Capacity
>= Length
(Source
);
95 function Copy
(Source
: Set
; Capacity
: Count_Type
:= 0) return Set
with
96 Pre
=> Capacity
>= Source
.Capacity
;
100 Position
: Cursor
) return Element_Type
102 Pre
=> Has_Element
(Container
, Position
);
104 procedure Replace_Element
105 (Container
: in out Set
;
107 New_Item
: Element_Type
)
109 Pre
=> Has_Element
(Container
, Position
);
111 procedure Move
(Target
: in out Set
; Source
: in out Set
) with
112 Pre
=> Target
.Capacity
>= Length
(Source
);
115 (Container
: in out Set
;
116 New_Item
: Element_Type
;
117 Position
: out Cursor
;
118 Inserted
: out Boolean)
120 Pre
=> Length
(Container
) < Container
.Capacity
;
123 (Container
: in out Set
;
124 New_Item
: Element_Type
)
126 Pre
=> Length
(Container
) < Container
.Capacity
127 and then (not Contains
(Container
, New_Item
));
130 (Container
: in out Set
;
131 New_Item
: Element_Type
)
133 Pre
=> Length
(Container
) < Container
.Capacity
;
136 (Container
: in out Set
;
137 New_Item
: Element_Type
)
139 Pre
=> Contains
(Container
, New_Item
);
142 (Container
: in out Set
;
143 Item
: Element_Type
);
146 (Container
: in out Set
;
149 Pre
=> Contains
(Container
, Item
);
152 (Container
: in out Set
;
153 Position
: in out Cursor
)
155 Pre
=> Has_Element
(Container
, Position
);
157 procedure Delete_First
(Container
: in out Set
);
159 procedure Delete_Last
(Container
: in out Set
);
161 procedure Union
(Target
: in out Set
; Source
: Set
) with
162 Pre
=> Length
(Target
) + Length
(Source
) -
163 Length
(Intersection
(Target
, Source
)) <= Target
.Capacity
;
165 function Union
(Left
, Right
: Set
) return Set
;
167 function "or" (Left
, Right
: Set
) return Set
renames Union
;
169 procedure Intersection
(Target
: in out Set
; Source
: Set
);
171 function Intersection
(Left
, Right
: Set
) return Set
;
173 function "and" (Left
, Right
: Set
) return Set
renames Intersection
;
175 procedure Difference
(Target
: in out Set
; Source
: Set
);
177 function Difference
(Left
, Right
: Set
) return Set
;
179 function "-" (Left
, Right
: Set
) return Set
renames Difference
;
181 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
);
183 function Symmetric_Difference
(Left
, Right
: Set
) return Set
;
185 function "xor" (Left
, Right
: Set
) return Set
renames Symmetric_Difference
;
187 function Overlap
(Left
, Right
: Set
) return Boolean;
189 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean;
191 function First
(Container
: Set
) return Cursor
;
193 function First_Element
(Container
: Set
) return Element_Type
with
194 Pre
=> not Is_Empty
(Container
);
196 function Last
(Container
: Set
) return Cursor
;
198 function Last_Element
(Container
: Set
) return Element_Type
with
199 Pre
=> not Is_Empty
(Container
);
201 function Next
(Container
: Set
; Position
: Cursor
) return Cursor
with
202 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
204 procedure Next
(Container
: Set
; Position
: in out Cursor
) with
205 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
207 function Previous
(Container
: Set
; Position
: Cursor
) return Cursor
with
208 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
210 procedure Previous
(Container
: Set
; Position
: in out Cursor
) with
211 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
213 function Find
(Container
: Set
; Item
: Element_Type
) return Cursor
;
215 function Floor
(Container
: Set
; Item
: Element_Type
) return Cursor
;
217 function Ceiling
(Container
: Set
; Item
: Element_Type
) return Cursor
;
219 function Contains
(Container
: Set
; Item
: Element_Type
) return Boolean;
221 function Has_Element
(Container
: Set
; Position
: Cursor
) return Boolean;
224 type Key_Type
(<>) is private;
226 with function Key
(Element
: Element_Type
) return Key_Type
;
228 with function "<" (Left
, Right
: Key_Type
) return Boolean is <>;
230 package Generic_Keys
is
232 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
234 function Key
(Container
: Set
; Position
: Cursor
) return Key_Type
;
236 function Element
(Container
: Set
; Key
: Key_Type
) return Element_Type
;
239 (Container
: in out Set
;
241 New_Item
: Element_Type
);
243 procedure Exclude
(Container
: in out Set
; Key
: Key_Type
);
245 procedure Delete
(Container
: in out Set
; Key
: Key_Type
);
247 function Find
(Container
: Set
; Key
: Key_Type
) return Cursor
;
249 function Floor
(Container
: Set
; Key
: Key_Type
) return Cursor
;
251 function Ceiling
(Container
: Set
; Key
: Key_Type
) return Cursor
;
253 function Contains
(Container
: Set
; Key
: Key_Type
) return Boolean;
257 function Strict_Equal
(Left
, Right
: Set
) return Boolean;
258 -- Strict_Equal returns True if the containers are physically equal, i.e.
259 -- they are structurally equal (function "=" returns True) and that they
260 -- have the same set of cursors.
262 function Left
(Container
: Set
; Position
: Cursor
) return Set
with
263 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
264 function Right
(Container
: Set
; Position
: Cursor
) return Set
with
265 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
266 -- Left returns a container containing all elements preceding Position
267 -- (excluded) in Container. Right returns a container containing all
268 -- elements following Position (included) in Container. These two new
269 -- functions can be used to express invariant properties in loops which
270 -- iterate over containers. Left returns the part of the container already
271 -- scanned and Right the part not scanned yet.
275 pragma Inline
(Next
);
276 pragma Inline
(Previous
);
278 type Node_Type
is record
279 Has_Element
: Boolean := False;
280 Parent
: Count_Type
:= 0;
281 Left
: Count_Type
:= 0;
282 Right
: Count_Type
:= 0;
283 Color
: Red_Black_Trees
.Color_Type
;
284 Element
: Element_Type
;
287 package Tree_Types
is
288 new Red_Black_Trees
.Generic_Bounded_Tree_Types
(Node_Type
);
290 type Set
(Capacity
: Count_Type
) is
291 new Tree_Types
.Tree_Type
(Capacity
) with null record;
295 type Cursor
is record
299 No_Element
: constant Cursor
:= (Node
=> 0);
301 Empty_Set
: constant Set
:= (Capacity
=> 0, others => <>);
303 end Ada
.Containers
.Formal_Ordered_Sets
;