1------------------------------------------------------------------------------ 2-- -- 3-- GNAT LIBRARY COMPONENTS -- 4-- -- 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 -- 6-- -- 7-- S p e c -- 8-- -- 9-- Copyright (C) 2004-2015, Free Software Foundation, Inc. -- 10-- -- 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. -- 14-- -- 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. -- 21-- -- 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. -- 25-- -- 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------------------------------------------------------------------------------ 31 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. 37 38-- The modifications are: 39 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 47-- have been removed. 48 49-- There are three new functions: 50 51-- function Strict_Equal (Left, Right : Set) return Boolean; 52-- function First_To_Previous (Container : Set; Current : Cursor) 53-- return Set; 54-- function Current_To_Last (Container : Set; Current : Cursor) 55-- return Set; 56 57-- See detailed specifications for these subprograms 58 59private with Ada.Containers.Red_Black_Trees; 60 61generic 62 type Element_Type is private; 63 64 with function "<" (Left, Right : Element_Type) return Boolean is <>; 65 with function "=" (Left, Right : Element_Type) return Boolean is <>; 66 67package Ada.Containers.Formal_Ordered_Sets with 68 Pure, 69 SPARK_Mode 70is 71 pragma Annotate (GNATprove, External_Axiomatization); 72 73 function Equivalent_Elements (Left, Right : Element_Type) return Boolean 74 with 75 Global => null; 76 77 type Set (Capacity : Count_Type) is private with 78 Iterable => (First => First, 79 Next => Next, 80 Has_Element => Has_Element, 81 Element => Element), 82 Default_Initial_Condition => Is_Empty (Set); 83 pragma Preelaborable_Initialization (Set); 84 85 type Cursor is private; 86 pragma Preelaborable_Initialization (Cursor); 87 88 Empty_Set : constant Set; 89 90 No_Element : constant Cursor; 91 92 function "=" (Left, Right : Set) return Boolean with 93 Global => null; 94 95 function Equivalent_Sets (Left, Right : Set) return Boolean with 96 Global => null; 97 98 function To_Set (New_Item : Element_Type) return Set with 99 Global => null; 100 101 function Length (Container : Set) return Count_Type with 102 Global => null; 103 104 function Is_Empty (Container : Set) return Boolean with 105 Global => null; 106 107 procedure Clear (Container : in out Set) with 108 Global => null; 109 110 procedure Assign (Target : in out Set; Source : Set) with 111 Pre => Target.Capacity >= Length (Source); 112 113 function Copy (Source : Set; Capacity : Count_Type := 0) return Set with 114 Global => null, 115 Pre => Capacity = 0 or else Capacity >= Source.Capacity; 116 117 function Element 118 (Container : Set; 119 Position : Cursor) return Element_Type 120 with 121 Global => null, 122 Pre => Has_Element (Container, Position); 123 124 procedure Replace_Element 125 (Container : in out Set; 126 Position : Cursor; 127 New_Item : Element_Type) 128 with 129 Global => null, 130 Pre => Has_Element (Container, Position); 131 132 procedure Move (Target : in out Set; Source : in out Set) with 133 Global => null, 134 Pre => Target.Capacity >= Length (Source); 135 136 procedure Insert 137 (Container : in out Set; 138 New_Item : Element_Type; 139 Position : out Cursor; 140 Inserted : out Boolean) 141 with 142 Global => null, 143 Pre => Length (Container) < Container.Capacity; 144 145 procedure Insert 146 (Container : in out Set; 147 New_Item : Element_Type) 148 with 149 Global => null, 150 Pre => Length (Container) < Container.Capacity 151 and then (not Contains (Container, New_Item)); 152 153 procedure Include 154 (Container : in out Set; 155 New_Item : Element_Type) 156 with 157 Global => null, 158 Pre => Length (Container) < Container.Capacity; 159 160 procedure Replace 161 (Container : in out Set; 162 New_Item : Element_Type) 163 with 164 Global => null, 165 Pre => Contains (Container, New_Item); 166 167 procedure Exclude 168 (Container : in out Set; 169 Item : Element_Type) 170 with 171 Global => null; 172 173 procedure Delete 174 (Container : in out Set; 175 Item : Element_Type) 176 with 177 Global => null, 178 Pre => Contains (Container, Item); 179 180 procedure Delete 181 (Container : in out Set; 182 Position : in out Cursor) 183 with 184 Global => null, 185 Pre => Has_Element (Container, Position); 186 187 procedure Delete_First (Container : in out Set) with 188 Global => null; 189 190 procedure Delete_Last (Container : in out Set) with 191 Global => null; 192 193 procedure Union (Target : in out Set; Source : Set) with 194 Global => null, 195 Pre => Length (Target) + Length (Source) - 196 Length (Intersection (Target, Source)) <= Target.Capacity; 197 198 function Union (Left, Right : Set) return Set with 199 Global => null, 200 Pre => Length (Left) + Length (Right) - 201 Length (Intersection (Left, Right)) <= Count_Type'Last; 202 203 function "or" (Left, Right : Set) return Set renames Union; 204 205 procedure Intersection (Target : in out Set; Source : Set) with 206 Global => null; 207 208 function Intersection (Left, Right : Set) return Set with 209 Global => null; 210 211 function "and" (Left, Right : Set) return Set renames Intersection; 212 213 procedure Difference (Target : in out Set; Source : Set) with 214 Global => null; 215 216 function Difference (Left, Right : Set) return Set with 217 Global => null; 218 219 function "-" (Left, Right : Set) return Set renames Difference; 220 221 procedure Symmetric_Difference (Target : in out Set; Source : Set) with 222 Global => null, 223 Pre => Length (Target) + Length (Source) - 224 2 * Length (Intersection (Target, Source)) <= Target.Capacity; 225 226 function Symmetric_Difference (Left, Right : Set) return Set with 227 Global => null, 228 Pre => Length (Left) + Length (Right) - 229 2 * Length (Intersection (Left, Right)) <= Count_Type'Last; 230 231 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference; 232 233 function Overlap (Left, Right : Set) return Boolean with 234 Global => null; 235 236 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean with 237 Global => null; 238 239 function First (Container : Set) return Cursor with 240 Global => null; 241 242 function First_Element (Container : Set) return Element_Type with 243 Global => null, 244 Pre => not Is_Empty (Container); 245 246 function Last (Container : Set) return Cursor; 247 248 function Last_Element (Container : Set) return Element_Type with 249 Global => null, 250 Pre => not Is_Empty (Container); 251 252 function Next (Container : Set; Position : Cursor) return Cursor with 253 Global => null, 254 Pre => Has_Element (Container, Position) or else Position = No_Element; 255 256 procedure Next (Container : Set; Position : in out Cursor) with 257 Global => null, 258 Pre => Has_Element (Container, Position) or else Position = No_Element; 259 260 function Previous (Container : Set; Position : Cursor) return Cursor with 261 Global => null, 262 Pre => Has_Element (Container, Position) or else Position = No_Element; 263 264 procedure Previous (Container : Set; Position : in out Cursor) with 265 Global => null, 266 Pre => Has_Element (Container, Position) or else Position = No_Element; 267 268 function Find (Container : Set; Item : Element_Type) return Cursor with 269 Global => null; 270 271 function Floor (Container : Set; Item : Element_Type) return Cursor with 272 Global => null; 273 274 function Ceiling (Container : Set; Item : Element_Type) return Cursor with 275 Global => null; 276 277 function Contains (Container : Set; Item : Element_Type) return Boolean with 278 Global => null; 279 280 function Has_Element (Container : Set; Position : Cursor) return Boolean 281 with 282 Global => null; 283 284 generic 285 type Key_Type (<>) is private; 286 287 with function Key (Element : Element_Type) return Key_Type; 288 289 with function "<" (Left, Right : Key_Type) return Boolean is <>; 290 291 package Generic_Keys is 292 293 function Equivalent_Keys (Left, Right : Key_Type) return Boolean with 294 Global => null; 295 296 function Key (Container : Set; Position : Cursor) return Key_Type with 297 Global => null; 298 299 function Element (Container : Set; Key : Key_Type) return Element_Type 300 with 301 Global => null; 302 303 procedure Replace 304 (Container : in out Set; 305 Key : Key_Type; 306 New_Item : Element_Type) 307 with 308 Global => null; 309 310 procedure Exclude (Container : in out Set; Key : Key_Type) with 311 Global => null; 312 313 procedure Delete (Container : in out Set; Key : Key_Type) with 314 Global => null; 315 316 function Find (Container : Set; Key : Key_Type) return Cursor with 317 Global => null; 318 319 function Floor (Container : Set; Key : Key_Type) return Cursor with 320 Global => null; 321 322 function Ceiling (Container : Set; Key : Key_Type) return Cursor with 323 Global => null; 324 325 function Contains (Container : Set; Key : Key_Type) return Boolean with 326 Global => null; 327 328 end Generic_Keys; 329 330 function Strict_Equal (Left, Right : Set) return Boolean with 331 Ghost, 332 Global => null; 333 -- Strict_Equal returns True if the containers are physically equal, i.e. 334 -- they are structurally equal (function "=" returns True) and that they 335 -- have the same set of cursors. 336 337 function First_To_Previous (Container : Set; Current : Cursor) return Set 338 with 339 Ghost, 340 Global => null, 341 Pre => Has_Element (Container, Current) or else Current = No_Element; 342 343 function Current_To_Last (Container : Set; Current : Cursor) return Set 344 with 345 Ghost, 346 Global => null, 347 Pre => Has_Element (Container, Current) or else Current = No_Element; 348 -- First_To_Previous returns a container containing all elements preceding 349 -- Current (excluded) in Container. Current_To_Last returns a container 350 -- containing all elements following Current (included) in Container. 351 -- These two new functions can be used to express invariant properties in 352 -- loops which iterate over containers. First_To_Previous returns the part 353 -- of the container already scanned and Current_To_Last the part not 354 -- scanned yet. 355 356private 357 pragma SPARK_Mode (Off); 358 359 pragma Inline (Next); 360 pragma Inline (Previous); 361 362 type Node_Type is record 363 Has_Element : Boolean := False; 364 Parent : Count_Type := 0; 365 Left : Count_Type := 0; 366 Right : Count_Type := 0; 367 Color : Red_Black_Trees.Color_Type; 368 Element : Element_Type; 369 end record; 370 371 package Tree_Types is 372 new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type); 373 374 type Set (Capacity : Count_Type) is 375 new Tree_Types.Tree_Type (Capacity) with null record; 376 377 use Red_Black_Trees; 378 379 type Cursor is record 380 Node : Count_Type; 381 end record; 382 383 No_Element : constant Cursor := (Node => 0); 384 385 Empty_Set : constant Set := (Capacity => 0, others => <>); 386 387end Ada.Containers.Formal_Ordered_Sets; 388