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