1------------------------------------------------------------------------------ 2-- -- 3-- GNAT LIBRARY COMPONENTS -- 4-- -- 5-- A D A . C O N T A I N E R S . H A S H E D _ S E T S -- 6-- -- 7-- S p e c -- 8-- -- 9-- Copyright (C) 2004-2020, 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-- This unit was originally developed by Matthew J Heaney. -- 32------------------------------------------------------------------------------ 33 34with Ada.Iterator_Interfaces; 35 36private with Ada.Containers.Hash_Tables; 37with Ada.Containers.Helpers; 38private with Ada.Finalization; 39private with Ada.Streams; 40private with Ada.Strings.Text_Output; 41 42generic 43 type Element_Type is private; 44 45 with function Hash (Element : Element_Type) return Hash_Type; 46 47 with function Equivalent_Elements 48 (Left, Right : Element_Type) return Boolean; 49 50 with function "=" (Left, Right : Element_Type) return Boolean is <>; 51 52package Ada.Containers.Hashed_Sets with 53 SPARK_Mode => Off 54is 55 pragma Annotate (CodePeer, Skip_Analysis); 56 pragma Preelaborate; 57 pragma Remote_Types; 58 59 type Set is tagged private 60 with 61 Constant_Indexing => Constant_Reference, 62 Default_Iterator => Iterate, 63 Iterator_Element => Element_Type, 64 Aggregate => (Empty => Empty, 65 Add_Unnamed => Include); 66 67 pragma Preelaborable_Initialization (Set); 68 69 type Cursor is private; 70 pragma Preelaborable_Initialization (Cursor); 71 72 Empty_Set : constant Set; 73 -- Set objects declared without an initialization expression are 74 -- initialized to the value Empty_Set. 75 76 No_Element : constant Cursor; 77 -- Cursor objects declared without an initialization expression are 78 -- initialized to the value No_Element. 79 80 function Has_Element (Position : Cursor) return Boolean; 81 -- Equivalent to Position /= No_Element 82 83 package Set_Iterator_Interfaces is new 84 Ada.Iterator_Interfaces (Cursor, Has_Element); 85 86 function Empty (Capacity : Count_Type := 1000) return Set; 87 88 function "=" (Left, Right : Set) return Boolean; 89 -- For each element in Left, set equality attempts to find the equal 90 -- element in Right; if a search fails, then set equality immediately 91 -- returns False. The search works by calling Hash to find the bucket in 92 -- the Right set that corresponds to the Left element. If the bucket is 93 -- non-empty, the search calls the generic formal element equality operator 94 -- to compare the element (in Left) to the element of each node in the 95 -- bucket (in Right); the search terminates when a matching node in the 96 -- bucket is found, or the nodes in the bucket are exhausted. (Note that 97 -- element equality is called here, not Equivalent_Elements. Set equality 98 -- is the only operation in which element equality is used. Compare set 99 -- equality to Equivalent_Sets, which does call Equivalent_Elements.) 100 101 function Equivalent_Sets (Left, Right : Set) return Boolean; 102 -- Similar to set equality, with the difference that the element in Left is 103 -- compared to the elements in Right using the generic formal 104 -- Equivalent_Elements operation instead of element equality. 105 106 function To_Set (New_Item : Element_Type) return Set; 107 -- Constructs a singleton set comprising New_Element. To_Set calls Hash to 108 -- determine the bucket for New_Item. 109 110 function Capacity (Container : Set) return Count_Type; 111 -- Returns the current capacity of the set. Capacity is the maximum length 112 -- before which rehashing in guaranteed not to occur. 113 114 procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type); 115 -- Adjusts the current capacity, by allocating a new buckets array. If the 116 -- requested capacity is less than the current capacity, then the capacity 117 -- is contracted (to a value not less than the current length). If the 118 -- requested capacity is greater than the current capacity, then the 119 -- capacity is expanded (to a value not less than what is requested). In 120 -- either case, the nodes are rehashed from the old buckets array onto the 121 -- new buckets array (Hash is called once for each existing element in 122 -- order to compute the new index), and then the old buckets array is 123 -- deallocated. 124 125 function Length (Container : Set) return Count_Type; 126 -- Returns the number of items in the set 127 128 function Is_Empty (Container : Set) return Boolean; 129 -- Equivalent to Length (Container) = 0 130 131 procedure Clear (Container : in out Set); 132 -- Removes all of the items from the set 133 134 function Element (Position : Cursor) return Element_Type; 135 -- Returns the element of the node designated by the cursor 136 137 procedure Replace_Element 138 (Container : in out Set; 139 Position : Cursor; 140 New_Item : Element_Type); 141 -- If New_Item is equivalent (as determined by calling Equivalent_Elements) 142 -- to the element of the node designated by Position, then New_Element is 143 -- assigned to that element. Otherwise, it calls Hash to determine the 144 -- bucket for New_Item. If the bucket is not empty, then it calls 145 -- Equivalent_Elements for each node in that bucket to determine whether 146 -- New_Item is equivalent to an element in that bucket. If 147 -- Equivalent_Elements returns True then Program_Error is raised (because 148 -- an element may appear only once in the set); otherwise, New_Item is 149 -- assigned to the node designated by Position, and the node is moved to 150 -- its new bucket. 151 152 procedure Query_Element 153 (Position : Cursor; 154 Process : not null access procedure (Element : Element_Type)); 155 -- Calls Process with the element (having only a constant view) of the node 156 -- designed by the cursor. 157 158 type Constant_Reference_Type 159 (Element : not null access constant Element_Type) is private 160 with Implicit_Dereference => Element; 161 162 function Constant_Reference 163 (Container : aliased Set; 164 Position : Cursor) return Constant_Reference_Type; 165 pragma Inline (Constant_Reference); 166 167 procedure Assign (Target : in out Set; Source : Set); 168 169 function Copy (Source : Set; Capacity : Count_Type := 0) return Set; 170 171 procedure Move (Target : in out Set; Source : in out Set); 172 -- Clears Target (if it's not empty), and then moves (not copies) the 173 -- buckets array and nodes from Source to Target. 174 175 procedure Insert 176 (Container : in out Set; 177 New_Item : Element_Type; 178 Position : out Cursor; 179 Inserted : out Boolean); 180 -- Conditionally inserts New_Item into the set. If New_Item is already in 181 -- the set, then Inserted returns False and Position designates the node 182 -- containing the existing element (which is not modified). If New_Item is 183 -- not already in the set, then Inserted returns True and Position 184 -- designates the newly-inserted node containing New_Item. The search for 185 -- an existing element works as follows. Hash is called to determine 186 -- New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements 187 -- is called to compare New_Item to the element of each node in that 188 -- bucket. If the bucket is empty, or there were no equivalent elements in 189 -- the bucket, the search "fails" and the New_Item is inserted in the set 190 -- (and Inserted returns True); otherwise, the search "succeeds" (and 191 -- Inserted returns False). 192 193 procedure Insert (Container : in out Set; New_Item : Element_Type); 194 -- Attempts to insert New_Item into the set, performing the usual insertion 195 -- search (which involves calling both Hash and Equivalent_Elements); if 196 -- the search succeeds (New_Item is equivalent to an element already in the 197 -- set, and so was not inserted), then this operation raises 198 -- Constraint_Error. (This version of Insert is similar to Replace, but 199 -- having the opposite exception behavior. It is intended for use when you 200 -- want to assert that the item is not already in the set.) 201 202 procedure Include (Container : in out Set; New_Item : Element_Type); 203 -- Attempts to insert New_Item into the set. If an element equivalent to 204 -- New_Item is already in the set (the insertion search succeeded, and 205 -- hence New_Item was not inserted), then the value of New_Item is assigned 206 -- to the existing element. (This insertion operation only raises an 207 -- exception if cursor tampering occurs. It is intended for use when you 208 -- want to insert the item in the set, and you don't care whether an 209 -- equivalent element is already present.) 210 211 procedure Replace (Container : in out Set; New_Item : Element_Type); 212 -- Searches for New_Item in the set; if the search fails (because an 213 -- equivalent element was not in the set), then it raises 214 -- Constraint_Error. Otherwise, the existing element is assigned the value 215 -- New_Item. (This is similar to Insert, but with the opposite exception 216 -- behavior. It is intended for use when you want to assert that the item 217 -- is already in the set.) 218 219 procedure Exclude (Container : in out Set; Item : Element_Type); 220 -- Searches for Item in the set, and if found, removes its node from the 221 -- set and then deallocates it. The search works as follows. The operation 222 -- calls Hash to determine the item's bucket; if the bucket is not empty, 223 -- it calls Equivalent_Elements to compare Item to the element of each node 224 -- in the bucket. (This is the deletion analog of Include. It is intended 225 -- for use when you want to remove the item from the set, but don't care 226 -- whether the item is already in the set.) 227 228 procedure Delete (Container : in out Set; Item : Element_Type); 229 -- Searches for Item in the set (which involves calling both Hash and 230 -- Equivalent_Elements). If the search fails, then the operation raises 231 -- Constraint_Error. Otherwise it removes the node from the set and then 232 -- deallocates it. (This is the deletion analog of non-conditional 233 -- Insert. It is intended for use when you want to assert that the item is 234 -- already in the set.) 235 236 procedure Delete (Container : in out Set; Position : in out Cursor); 237 -- Removes the node designated by Position from the set, and then 238 -- deallocates the node. The operation calls Hash to determine the bucket, 239 -- and then compares Position to each node in the bucket until there's a 240 -- match (it does not call Equivalent_Elements). 241 242 procedure Union (Target : in out Set; Source : Set); 243 -- The operation first calls Reserve_Capacity if the current capacity is 244 -- less than the sum of the lengths of Source and Target. It then iterates 245 -- over the Source set, and conditionally inserts each element into Target. 246 247 function Union (Left, Right : Set) return Set; 248 -- The operation first copies the Left set to the result, and then iterates 249 -- over the Right set to conditionally insert each element into the result. 250 251 function "or" (Left, Right : Set) return Set renames Union; 252 253 procedure Intersection (Target : in out Set; Source : Set); 254 -- Iterates over the Target set (calling First and Next), calling Find to 255 -- determine whether the element is in Source. If an equivalent element is 256 -- not found in Source, the element is deleted from Target. 257 258 function Intersection (Left, Right : Set) return Set; 259 -- Iterates over the Left set, calling Find to determine whether the 260 -- element is in Right. If an equivalent element is found, it is inserted 261 -- into the result set. 262 263 function "and" (Left, Right : Set) return Set renames Intersection; 264 265 procedure Difference (Target : in out Set; Source : Set); 266 -- Iterates over the Source (calling First and Next), calling Find to 267 -- determine whether the element is in Target. If an equivalent element is 268 -- found, it is deleted from Target. 269 270 function Difference (Left, Right : Set) return Set; 271 -- Iterates over the Left set, calling Find to determine whether the 272 -- element is in the Right set. If an equivalent element is not found, the 273 -- element is inserted into the result set. 274 275 function "-" (Left, Right : Set) return Set renames Difference; 276 277 procedure Symmetric_Difference (Target : in out Set; Source : Set); 278 -- The operation first calls Reserve_Capacity if the current capacity is 279 -- less than the sum of the lengths of Source and Target. It then iterates 280 -- over the Source set, searching for the element in Target (calling Hash 281 -- and Equivalent_Elements). If an equivalent element is found, it is 282 -- removed from Target; otherwise it is inserted into Target. 283 284 function Symmetric_Difference (Left, Right : Set) return Set; 285 -- The operation first iterates over the Left set. It calls Find to 286 -- determine whether the element is in the Right set. If no equivalent 287 -- element is found, the element from Left is inserted into the result. The 288 -- operation then iterates over the Right set, to determine whether the 289 -- element is in the Left set. If no equivalent element is found, the Right 290 -- element is inserted into the result. 291 292 function "xor" (Left, Right : Set) return Set 293 renames Symmetric_Difference; 294 295 function Overlap (Left, Right : Set) return Boolean; 296 -- Iterates over the Left set (calling First and Next), calling Find to 297 -- determine whether the element is in the Right set. If an equivalent 298 -- element is found, the operation immediately returns True. The operation 299 -- returns False if the iteration over Left terminates without finding any 300 -- equivalent element in Right. 301 302 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean; 303 -- Iterates over Subset (calling First and Next), calling Find to determine 304 -- whether the element is in Of_Set. If no equivalent element is found in 305 -- Of_Set, the operation immediately returns False. The operation returns 306 -- True if the iteration over Subset terminates without finding an element 307 -- not in Of_Set (that is, every element in Subset is equivalent to an 308 -- element in Of_Set). 309 310 function First (Container : Set) return Cursor; 311 -- Returns a cursor that designates the first non-empty bucket, by 312 -- searching from the beginning of the buckets array. 313 314 function Next (Position : Cursor) return Cursor; 315 -- Returns a cursor that designates the node that follows the current one 316 -- designated by Position. If Position designates the last node in its 317 -- bucket, the operation calls Hash to compute the index of this bucket, 318 -- and searches the buckets array for the first non-empty bucket, starting 319 -- from that index; otherwise, it simply follows the link to the next node 320 -- in the same bucket. 321 322 procedure Next (Position : in out Cursor); 323 -- Equivalent to Position := Next (Position) 324 325 function Find 326 (Container : Set; 327 Item : Element_Type) return Cursor; 328 -- Searches for Item in the set. Find calls Hash to determine the item's 329 -- bucket; if the bucket is not empty, it calls Equivalent_Elements to 330 -- compare Item to each element in the bucket. If the search succeeds, Find 331 -- returns a cursor designating the node containing the equivalent element; 332 -- otherwise, it returns No_Element. 333 334 function Contains (Container : Set; Item : Element_Type) return Boolean; 335 -- Equivalent to Find (Container, Item) /= No_Element 336 337 function Equivalent_Elements (Left, Right : Cursor) return Boolean; 338 -- Returns the result of calling Equivalent_Elements with the elements of 339 -- the nodes designated by cursors Left and Right. 340 341 function Equivalent_Elements 342 (Left : Cursor; 343 Right : Element_Type) return Boolean; 344 -- Returns the result of calling Equivalent_Elements with element of the 345 -- node designated by Left and element Right. 346 347 function Equivalent_Elements 348 (Left : Element_Type; 349 Right : Cursor) return Boolean; 350 -- Returns the result of calling Equivalent_Elements with element Left and 351 -- the element of the node designated by Right. 352 353 procedure Iterate 354 (Container : Set; 355 Process : not null access procedure (Position : Cursor)); 356 -- Calls Process for each node in the set 357 358 function Iterate 359 (Container : Set) return Set_Iterator_Interfaces.Forward_Iterator'Class; 360 361 generic 362 type Key_Type (<>) is private; 363 364 with function Key (Element : Element_Type) return Key_Type; 365 366 with function Hash (Key : Key_Type) return Hash_Type; 367 368 with function Equivalent_Keys (Left, Right : Key_Type) return Boolean; 369 370 package Generic_Keys is 371 372 function Key (Position : Cursor) return Key_Type; 373 -- Applies generic formal operation Key to the element of the node 374 -- designated by Position. 375 376 function Element (Container : Set; Key : Key_Type) return Element_Type; 377 -- Searches (as per the key-based Find) for the node containing Key, and 378 -- returns the associated element. 379 380 procedure Replace 381 (Container : in out Set; 382 Key : Key_Type; 383 New_Item : Element_Type); 384 -- Searches (as per the key-based Find) for the node containing Key, and 385 -- then replaces the element of that node (as per the element-based 386 -- Replace_Element). 387 388 procedure Exclude (Container : in out Set; Key : Key_Type); 389 -- Searches for Key in the set, and if found, removes its node from the 390 -- set and then deallocates it. The search works by first calling Hash 391 -- (on Key) to determine the bucket; if the bucket is not empty, it 392 -- calls Equivalent_Keys to compare parameter Key to the value of 393 -- generic formal operation Key applied to element of each node in the 394 -- bucket. 395 396 procedure Delete (Container : in out Set; Key : Key_Type); 397 -- Deletes the node containing Key as per Exclude, with the difference 398 -- that Constraint_Error is raised if Key is not found. 399 400 function Find (Container : Set; Key : Key_Type) return Cursor; 401 -- Searches for the node containing Key, and returns a cursor 402 -- designating the node. The search works by first calling Hash (on Key) 403 -- to determine the bucket. If the bucket is not empty, the search 404 -- compares Key to the element of each node in the bucket, and returns 405 -- the matching node. The comparison itself works by applying the 406 -- generic formal Key operation to the element of the node, and then 407 -- calling generic formal operation Equivalent_Keys. 408 409 function Contains (Container : Set; Key : Key_Type) return Boolean; 410 -- Equivalent to Find (Container, Key) /= No_Element 411 412 procedure Update_Element_Preserving_Key 413 (Container : in out Set; 414 Position : Cursor; 415 Process : not null access 416 procedure (Element : in out Element_Type)); 417 -- Calls Process with the element of the node designated by Position, 418 -- but with the restriction that the key-value of the element is not 419 -- modified. The operation first makes a copy of the value returned by 420 -- applying generic formal operation Key on the element of the node, and 421 -- then calls Process with the element. The operation verifies that the 422 -- key-part has not been modified by calling generic formal operation 423 -- Equivalent_Keys to compare the saved key-value to the value returned 424 -- by applying generic formal operation Key to the post-Process value of 425 -- element. If the key values compare equal then the operation 426 -- completes. Otherwise, the node is removed from the set and 427 -- Program_Error is raised. 428 429 type Reference_Type (Element : not null access Element_Type) is private 430 with Implicit_Dereference => Element; 431 432 function Reference_Preserving_Key 433 (Container : aliased in out Set; 434 Position : Cursor) return Reference_Type; 435 436 function Constant_Reference 437 (Container : aliased Set; 438 Key : Key_Type) return Constant_Reference_Type; 439 440 function Reference_Preserving_Key 441 (Container : aliased in out Set; 442 Key : Key_Type) return Reference_Type; 443 444 private 445 use Ada.Streams; 446 type Set_Access is access all Set; 447 for Set_Access'Storage_Size use 0; 448 449 -- Key_Preserving references must carry information to allow removal 450 -- of elements whose value may have been altered improperly, i.e. have 451 -- been given values incompatible with the hash-code of the previous 452 -- value, and are thus in the wrong bucket. (RM 18.7 (96.6/3)) 453 454 -- We cannot store the key directly because it is an unconstrained type. 455 -- To avoid using additional dynamic allocation we store the old cursor 456 -- which simplifies possible removal. This is not possible for some 457 -- other set types. 458 459 -- The mechanism is different for Update_Element_Preserving_Key, as 460 -- in that case the check that buckets have not changed is performed 461 -- at the time of the update, not when the reference is finalized. 462 463 package Impl is new Helpers.Generic_Implementation; 464 465 type Reference_Control_Type is 466 new Impl.Reference_Control_Type with 467 record 468 Container : Set_Access; 469 Index : Hash_Type; 470 Old_Pos : Cursor; 471 Old_Hash : Hash_Type; 472 end record; 473 474 overriding procedure Finalize (Control : in out Reference_Control_Type); 475 pragma Inline (Finalize); 476 477 type Reference_Type (Element : not null access Element_Type) is record 478 Control : Reference_Control_Type; 479 end record; 480 481 procedure Read 482 (Stream : not null access Root_Stream_Type'Class; 483 Item : out Reference_Type); 484 485 for Reference_Type'Read use Read; 486 487 procedure Write 488 (Stream : not null access Root_Stream_Type'Class; 489 Item : Reference_Type); 490 491 for Reference_Type'Write use Write; 492 end Generic_Keys; 493 494private 495 pragma Inline (Next); 496 497 type Node_Type; 498 type Node_Access is access Node_Type; 499 500 type Node_Type is limited record 501 Element : aliased Element_Type; 502 Next : Node_Access; 503 end record; 504 505 package HT_Types is 506 new Hash_Tables.Generic_Hash_Table_Types (Node_Type, Node_Access); 507 508 type Set is new Ada.Finalization.Controlled with record 509 HT : HT_Types.Hash_Table_Type; 510 end record with Put_Image => Put_Image; 511 512 procedure Put_Image 513 (S : in out Ada.Strings.Text_Output.Sink'Class; V : Set); 514 515 overriding procedure Adjust (Container : in out Set); 516 517 overriding procedure Finalize (Container : in out Set); 518 519 use HT_Types, HT_Types.Implementation; 520 use Ada.Finalization; 521 use Ada.Streams; 522 523 procedure Write 524 (Stream : not null access Root_Stream_Type'Class; 525 Container : Set); 526 527 for Set'Write use Write; 528 529 procedure Read 530 (Stream : not null access Root_Stream_Type'Class; 531 Container : out Set); 532 533 for Set'Read use Read; 534 535 type Set_Access is access all Set; 536 for Set_Access'Storage_Size use 0; 537 538 type Cursor is record 539 Container : Set_Access; 540 Node : Node_Access; 541 Position : Hash_Type := Hash_Type'Last; 542 end record; 543 544 procedure Write 545 (Stream : not null access Root_Stream_Type'Class; 546 Item : Cursor); 547 548 for Cursor'Write use Write; 549 550 procedure Read 551 (Stream : not null access Root_Stream_Type'Class; 552 Item : out Cursor); 553 554 for Cursor'Read use Read; 555 556 subtype Reference_Control_Type is Implementation.Reference_Control_Type; 557 -- It is necessary to rename this here, so that the compiler can find it 558 559 type Constant_Reference_Type 560 (Element : not null access constant Element_Type) is 561 record 562 Control : Reference_Control_Type := 563 raise Program_Error with "uninitialized reference"; 564 -- The RM says, "The default initialization of an object of 565 -- type Constant_Reference_Type or Reference_Type propagates 566 -- Program_Error." 567 end record; 568 569 procedure Read 570 (Stream : not null access Root_Stream_Type'Class; 571 Item : out Constant_Reference_Type); 572 573 for Constant_Reference_Type'Read use Read; 574 575 procedure Write 576 (Stream : not null access Root_Stream_Type'Class; 577 Item : Constant_Reference_Type); 578 579 for Constant_Reference_Type'Write use Write; 580 581 -- Three operations are used to optimize in the expansion of "for ... of" 582 -- loops: the Next(Cursor) procedure in the visible part, and the following 583 -- Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for 584 -- details. 585 586 function Pseudo_Reference 587 (Container : aliased Set'Class) return Reference_Control_Type; 588 pragma Inline (Pseudo_Reference); 589 -- Creates an object of type Reference_Control_Type pointing to the 590 -- container, and increments the Lock. Finalization of this object will 591 -- decrement the Lock. 592 593 type Element_Access is access all Element_Type with 594 Storage_Size => 0; 595 596 function Get_Element_Access 597 (Position : Cursor) return not null Element_Access; 598 -- Returns a pointer to the element designated by Position. 599 600 Empty_Set : constant Set := (Controlled with others => <>); 601 602 No_Element : constant Cursor := 603 (Container => null, Node => null, Position => Hash_Type'Last); 604 605 type Iterator is new Limited_Controlled and 606 Set_Iterator_Interfaces.Forward_Iterator with 607 record 608 Container : Set_Access; 609 end record 610 with Disable_Controlled => not T_Check; 611 612 overriding function First (Object : Iterator) return Cursor; 613 614 overriding function Next 615 (Object : Iterator; 616 Position : Cursor) return Cursor; 617 overriding procedure Finalize (Object : in out Iterator); 618 619end Ada.Containers.Hashed_Sets; 620