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 _ H A S H 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_Hashed_Sets in the
33 -- 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: Element, Next, Query_Element, Has_Element, Key,
42 -- Iterate, Equivalent_Elements. This change is motivated by the need to
43 -- have cursors which are valid on different containers (typically a
44 -- container C and its previous version C'Old) for expressing properties,
45 -- which is not possible if cursors encapsulate an access to the underlying
48 -- There are three new functions:
50 -- function Strict_Equal (Left, Right : Set) return Boolean;
51 -- function Left (Container : Set; Position : Cursor) return Set;
52 -- function Right (Container : Set; Position : Cursor) return Set;
54 -- See detailed specifications for these subprograms
56 private with Ada
.Containers
.Hash_Tables
;
59 type Element_Type
is private;
61 with function Hash
(Element
: Element_Type
) return Hash_Type
;
63 with function Equivalent_Elements
(Left
, Right
: Element_Type
)
66 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
68 package Ada
.Containers
.Formal_Hashed_Sets
is
69 pragma Annotate
(GNATprove
, External_Axiomatization
);
72 type Set
(Capacity
: Count_Type
; Modulus
: Hash_Type
) is private;
73 pragma Preelaborable_Initialization
(Set
);
75 type Cursor
is private;
76 pragma Preelaborable_Initialization
(Cursor
);
78 Empty_Set
: constant Set
;
80 No_Element
: constant Cursor
;
82 function "=" (Left
, Right
: Set
) return Boolean;
84 function Equivalent_Sets
(Left
, Right
: Set
) return Boolean;
86 function To_Set
(New_Item
: Element_Type
) return Set
;
88 function Capacity
(Container
: Set
) return Count_Type
;
90 procedure Reserve_Capacity
91 (Container
: in out Set
;
92 Capacity
: Count_Type
)
94 Pre
=> Capacity
<= Container
.Capacity
;
96 function Length
(Container
: Set
) return Count_Type
;
98 function Is_Empty
(Container
: Set
) return Boolean;
100 procedure Clear
(Container
: in out Set
);
102 procedure Assign
(Target
: in out Set
; Source
: Set
) with
103 Pre
=> Target
.Capacity
>= Length
(Source
);
107 Capacity
: Count_Type
:= 0) return Set
109 Pre
=> Capacity
= 0 or else Capacity
>= Source
.Capacity
;
113 Position
: Cursor
) return Element_Type
115 Pre
=> Has_Element
(Container
, Position
);
117 procedure Replace_Element
118 (Container
: in out Set
;
120 New_Item
: Element_Type
)
122 Pre
=> Has_Element
(Container
, Position
);
124 procedure Move
(Target
: in out Set
; Source
: in out Set
) with
125 Pre
=> Target
.Capacity
>= Length
(Source
);
128 (Container
: in out Set
;
129 New_Item
: Element_Type
;
130 Position
: out Cursor
;
131 Inserted
: out Boolean)
133 Pre
=> Length
(Container
) < Container
.Capacity
;
135 procedure Insert
(Container
: in out Set
; New_Item
: Element_Type
) with
136 Pre
=> Length
(Container
) < Container
.Capacity
137 and then (not Contains
(Container
, New_Item
));
139 procedure Include
(Container
: in out Set
; New_Item
: Element_Type
) with
140 Pre
=> Length
(Container
) < Container
.Capacity
;
142 procedure Replace
(Container
: in out Set
; New_Item
: Element_Type
) with
143 Pre
=> Contains
(Container
, New_Item
);
145 procedure Exclude
(Container
: in out Set
; Item
: Element_Type
);
147 procedure Delete
(Container
: in out Set
; Item
: Element_Type
) with
148 Pre
=> Contains
(Container
, Item
);
150 procedure Delete
(Container
: in out Set
; Position
: in out Cursor
) with
151 Pre
=> Has_Element
(Container
, Position
);
153 procedure Union
(Target
: in out Set
; Source
: Set
) with
154 Pre
=> Length
(Target
) + Length
(Source
) -
155 Length
(Intersection
(Target
, Source
)) <= Target
.Capacity
;
157 function Union
(Left
, Right
: Set
) return Set
;
159 function "or" (Left
, Right
: Set
) return Set
renames Union
;
161 procedure Intersection
(Target
: in out Set
; Source
: Set
);
163 function Intersection
(Left
, Right
: Set
) return Set
;
165 function "and" (Left
, Right
: Set
) return Set
renames Intersection
;
167 procedure Difference
(Target
: in out Set
; Source
: Set
);
169 function Difference
(Left
, Right
: Set
) return Set
;
171 function "-" (Left
, Right
: Set
) return Set
renames Difference
;
173 procedure Symmetric_Difference
(Target
: in out Set
; Source
: Set
);
175 function Symmetric_Difference
(Left
, Right
: Set
) return Set
;
177 function "xor" (Left
, Right
: Set
) return Set
178 renames Symmetric_Difference
;
180 function Overlap
(Left
, Right
: Set
) return Boolean;
182 function Is_Subset
(Subset
: Set
; Of_Set
: Set
) return Boolean;
184 function First
(Container
: Set
) return Cursor
;
186 function Next
(Container
: Set
; Position
: Cursor
) return Cursor
with
187 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
189 procedure Next
(Container
: Set
; Position
: in out Cursor
) with
190 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
194 Item
: Element_Type
) return Cursor
;
196 function Contains
(Container
: Set
; Item
: Element_Type
) return Boolean;
198 function Has_Element
(Container
: Set
; Position
: Cursor
) return Boolean;
200 function Equivalent_Elements
(Left
: Set
; CLeft
: Cursor
;
201 Right
: Set
; CRight
: Cursor
) return Boolean;
203 function Equivalent_Elements
204 (Left
: Set
; CLeft
: Cursor
;
205 Right
: Element_Type
) return Boolean;
207 function Equivalent_Elements
208 (Left
: Element_Type
;
209 Right
: Set
; CRight
: Cursor
) return Boolean;
211 function Default_Modulus
(Capacity
: Count_Type
) return Hash_Type
;
214 type Key_Type
(<>) is private;
216 with function Key
(Element
: Element_Type
) return Key_Type
;
218 with function Hash
(Key
: Key_Type
) return Hash_Type
;
220 with function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
222 package Generic_Keys
is
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 Contains
(Container
: Set
; Key
: Key_Type
) return Boolean;
243 function Strict_Equal
(Left
, Right
: Set
) return Boolean;
244 -- Strict_Equal returns True if the containers are physically equal, i.e.
245 -- they are structurally equal (function "=" returns True) and that they
246 -- have the same set of cursors.
248 function Left
(Container
: Set
; Position
: Cursor
) return Set
with
249 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
250 function Right
(Container
: Set
; Position
: Cursor
) return Set
with
251 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
252 -- Left returns a container containing all elements preceding Position
253 -- (excluded) in Container. Right returns a container containing all
254 -- elements following Position (included) in Container. These two new
255 -- functions can be used to express invariant properties in loops which
256 -- iterate over containers. Left returns the part of the container already
257 -- scanned and Right the part not scanned yet.
261 pragma Inline
(Next
);
265 Element
: Element_Type
;
267 Has_Element
: Boolean := False;
270 package HT_Types
is new
271 Ada
.Containers
.Hash_Tables
.Generic_Bounded_Hash_Table_Types
(Node_Type
);
273 type Set
(Capacity
: Count_Type
; Modulus
: Hash_Type
) is
274 new HT_Types
.Hash_Table_Type
(Capacity
, Modulus
) with null record;
278 type Cursor
is record
282 No_Element
: constant Cursor
:= (Node
=> 0);
284 Empty_Set
: constant Set
:= (Capacity
=> 0, Modulus
=> 0, others => <>);
286 end Ada
.Containers
.Formal_Hashed_Sets
;