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