a-cohase.ads, [...]: Document which generic formal operations are called for each operation.
2007-12-06 Matthew Heaney <heaney@adacore.com> * a-cohase.ads, a-cihama.ads, a-cihase.ads, a-cohama.ads: Document which generic formal operations are called for each operation. From-SVN: r130873
This commit is contained in:
parent
a2ad1f7960
commit
8236f02792
|
@ -34,9 +34,9 @@
|
|||
-- This unit was originally developed by Matthew J Heaney. --
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
with Ada.Containers.Hash_Tables;
|
||||
with Ada.Streams;
|
||||
with Ada.Finalization;
|
||||
private with Ada.Containers.Hash_Tables;
|
||||
private with Ada.Streams;
|
||||
private with Ada.Finalization;
|
||||
|
||||
generic
|
||||
type Key_Type (<>) is private;
|
||||
|
@ -57,43 +57,77 @@ package Ada.Containers.Indefinite_Hashed_Maps is
|
|||
pragma Preelaborable_Initialization (Cursor);
|
||||
|
||||
Empty_Map : constant Map;
|
||||
-- Map objects declared without an initialization expression are
|
||||
-- initialized to the value Empty_Map.
|
||||
|
||||
No_Element : constant Cursor;
|
||||
-- Cursor objects declared without an initialization expression are
|
||||
-- initialized to the value No_Element.
|
||||
|
||||
function "=" (Left, Right : Map) return Boolean;
|
||||
-- For each key/element pair in Left, equality attempts to find the key in
|
||||
-- Right; if a search fails the equality returns False. The search works by
|
||||
-- calling Hash to find the bucket in the Right map that corresponds to the
|
||||
-- Left key. If bucket is non-empty, then equality calls Equivalent_Keys
|
||||
-- to compare the key (in Left) to the key of each node in the bucket (in
|
||||
-- Right); if the keys are equivalent, then the equality test for this
|
||||
-- key/element pair (in Left) completes by calling the element equality
|
||||
-- operator to compare the element (in Left) to the element of the node
|
||||
-- (in Right) whose key matched.
|
||||
|
||||
function Capacity (Container : Map) return Count_Type;
|
||||
-- Returns the current capacity of the map. Capacity is the maximum length
|
||||
-- before which rehashing in guaranteed not to occur.
|
||||
|
||||
procedure Reserve_Capacity
|
||||
(Container : in out Map;
|
||||
Capacity : Count_Type);
|
||||
procedure Reserve_Capacity (Container : in out Map; Capacity : Count_Type);
|
||||
-- Adjusts the current capacity, by allocating a new buckets array. If the
|
||||
-- requested capacity is less than the current capacity, then the capacity
|
||||
-- is contracted (to a value not less than the curent length). If the
|
||||
-- requested capacity is greater than the current capacity, then the
|
||||
-- capacity is expanded (to a value not less than what is requested). In
|
||||
-- either case, the nodes are rehashed from the old buckets array onto the
|
||||
-- new buckets array (Hash is called once for each existing key in order to
|
||||
-- compute the new index), and then the old buckets array is deallocated.
|
||||
|
||||
function Length (Container : Map) return Count_Type;
|
||||
-- Returns the number of items in the map
|
||||
|
||||
function Is_Empty (Container : Map) return Boolean;
|
||||
-- Equivalent to Length (Container) = 0
|
||||
|
||||
procedure Clear (Container : in out Map);
|
||||
-- Removes all of the items from the map
|
||||
|
||||
function Key (Position : Cursor) return Key_Type;
|
||||
-- Returns the key of the node designated by the cursor
|
||||
|
||||
function Element (Position : Cursor) return Element_Type;
|
||||
-- Returns the element of the node designated by the cursor
|
||||
|
||||
procedure Replace_Element
|
||||
(Container : in out Map;
|
||||
Position : Cursor;
|
||||
New_Item : Element_Type);
|
||||
-- Assigns the value New_Item to the element designated by the cursor
|
||||
|
||||
procedure Query_Element
|
||||
(Position : Cursor;
|
||||
Process : not null access procedure (Key : Key_Type;
|
||||
Element : Element_Type));
|
||||
-- Calls Process with the key and element (both having only a constant
|
||||
-- view) of the node designed by the cursor.
|
||||
|
||||
procedure Update_Element
|
||||
(Container : in out Map;
|
||||
Position : Cursor;
|
||||
Process : not null access procedure (Key : Key_Type;
|
||||
Element : in out Element_Type));
|
||||
-- Calls Process with the key (with only a constant view) and element (with
|
||||
-- a variable view) of the node designed by the cursor.
|
||||
|
||||
procedure Move (Target : in out Map; Source : in out Map);
|
||||
-- Clears Target (if it's not empty), and then moves (not copies) the
|
||||
-- buckets array and nodes from Source to Target.
|
||||
|
||||
procedure Insert
|
||||
(Container : in out Map;
|
||||
|
@ -101,51 +135,120 @@ package Ada.Containers.Indefinite_Hashed_Maps is
|
|||
New_Item : Element_Type;
|
||||
Position : out Cursor;
|
||||
Inserted : out Boolean);
|
||||
-- Conditionally inserts New_Item into the map. If Key is already in the
|
||||
-- map, then Inserted returns False and Position designates the node
|
||||
-- containing the existing key/element pair (neither of which is modified).
|
||||
-- If Key is not already in the map, the Inserted returns True and Position
|
||||
-- designates the newly-inserted node container Key and New_Item. The
|
||||
-- search for the key works as follows. Hash is called to determine Key's
|
||||
-- bucket; if the bucket is non-empty, then Equivalent_Keys is called to
|
||||
-- compare Key to each node in that bucket. If the bucket is empty, or
|
||||
-- there were no matching keys in the bucket, the search "fails" and the
|
||||
-- key/item pair is inserted in the map (and Inserted returns True);
|
||||
-- otherwise, the search "succeeds" (and Inserted returns False).
|
||||
|
||||
procedure Insert
|
||||
(Container : in out Map;
|
||||
Key : Key_Type;
|
||||
New_Item : Element_Type);
|
||||
-- Attempts to insert Key into the map, performing the usual search (which
|
||||
-- involves calling both Hash and Equivalent_Keys); if the search succeeds
|
||||
-- (because Key is already in the map), then it raises Constraint_Error.
|
||||
-- (This version of Insert is similar to Replace, but having the opposite
|
||||
-- exception behavior. It is intended for use when you want to assert that
|
||||
-- Key is not already in the map.)
|
||||
|
||||
procedure Include
|
||||
(Container : in out Map;
|
||||
Key : Key_Type;
|
||||
New_Item : Element_Type);
|
||||
-- Attempts to insert Key into the map. If Key is already in the map, then
|
||||
-- both the existing key and element are assigned the values of Key and
|
||||
-- New_Item, respectively. (This version of Insert only raises an exception
|
||||
-- if cursor tampering occurs. It is intended for use when you want to
|
||||
-- insert the key/element pair in the map, and you don't care whether Key
|
||||
-- is already present.)
|
||||
|
||||
procedure Replace
|
||||
(Container : in out Map;
|
||||
Key : Key_Type;
|
||||
New_Item : Element_Type);
|
||||
-- Searches for Key in the map; if the search fails (because Key was not in
|
||||
-- the map), then it raises Constraint_Error. Otherwise, both the existing
|
||||
-- key and element are assigned the values of Key and New_Item rsp. (This
|
||||
-- is similar to Insert, but with the opposite exception behavior. It is
|
||||
-- intended for use when you want to assert that Key is already in the
|
||||
-- map.)
|
||||
|
||||
procedure Exclude (Container : in out Map; Key : Key_Type);
|
||||
-- Searches for Key in the map, and if found, removes its node from the map
|
||||
-- and then deallocates it. The search works as follows. The operation
|
||||
-- calls Hash to determine the key's bucket; if the bucket is not empty, it
|
||||
-- calls Equivalent_Keys to compare Key to each key in the bucket. (This is
|
||||
-- the deletion analog of Include. It is intended for use when you want to
|
||||
-- remove the item from the map, but don't care whether the key is already
|
||||
-- in the map.)
|
||||
|
||||
procedure Delete (Container : in out Map; Key : Key_Type);
|
||||
-- Searches for Key in the map (which involves calling both Hash and
|
||||
-- Equivalent_Keys). If the search fails, then the operation raises
|
||||
-- Constraint_Eror. Otherwise it removes the node from the map and then
|
||||
-- deallocates it. (This is the deletion analog of non-conditional
|
||||
-- Insert. It is intended for use when you want to assert that the item is
|
||||
-- already in the map.)
|
||||
|
||||
procedure Delete (Container : in out Map; Position : in out Cursor);
|
||||
-- Removes the node designated by Position from the map, and then
|
||||
-- deallocates the node. The operation calls Hash to determine the bucket,
|
||||
-- and then compares Position to each node in the bucket until there's a
|
||||
-- match (it does not call Equivalent_Keys).
|
||||
|
||||
function First (Container : Map) return Cursor;
|
||||
-- Returns a cursor that designates the first non-empty bucket, by
|
||||
-- searching from the beginning of the buckets array.
|
||||
|
||||
function Next (Position : Cursor) return Cursor;
|
||||
-- Returns a cursor that designates the node that follows the current one
|
||||
-- designated by Position. If Position designates the last node in its
|
||||
-- bucket, the operation calls Hash to compute the index of this bucket,
|
||||
-- and searches the buckets array for the first non-empty bucket, starting
|
||||
-- from that index; otherwise, it simply follows the link to the next node
|
||||
-- in the same bucket.
|
||||
|
||||
procedure Next (Position : in out Cursor);
|
||||
-- Equivalent to Position := Next (Position)
|
||||
|
||||
function Find (Container : Map; Key : Key_Type) return Cursor;
|
||||
-- Searches for Key in the map. Find calls Hash to determine the key's
|
||||
-- bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
|
||||
-- Key to each key in the bucket. If the search succeeds, Find returns a
|
||||
-- cursor designating the matching node; otherwise, it returns No_Element.
|
||||
|
||||
function Contains (Container : Map; Key : Key_Type) return Boolean;
|
||||
-- Equivalent to Find (Container, Key) /= No_Element
|
||||
|
||||
function Element (Container : Map; Key : Key_Type) return Element_Type;
|
||||
-- Equivalent to Element (Find (Container, Key))
|
||||
|
||||
function Has_Element (Position : Cursor) return Boolean;
|
||||
-- Equivalent to Position /= No_Element
|
||||
|
||||
function Equivalent_Keys (Left, Right : Cursor) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Keys with the keys of the nodes
|
||||
-- designated by cursors Left and Right.
|
||||
|
||||
function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Keys with key of the node
|
||||
-- designated by Left and key Right.
|
||||
|
||||
function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Keys with key Left and the node
|
||||
-- designated by Right.
|
||||
|
||||
procedure Iterate
|
||||
(Container : Map;
|
||||
Process : not null access procedure (Position : Cursor));
|
||||
-- Calls Process for each node in the map
|
||||
|
||||
private
|
||||
pragma Inline ("=");
|
||||
|
@ -186,8 +289,10 @@ private
|
|||
use Ada.Finalization;
|
||||
use Ada.Streams;
|
||||
|
||||
overriding
|
||||
procedure Adjust (Container : in out Map);
|
||||
|
||||
overriding
|
||||
procedure Finalize (Container : in out Map);
|
||||
|
||||
type Map_Access is access constant Map;
|
||||
|
|
|
@ -59,114 +59,270 @@ package Ada.Containers.Indefinite_Hashed_Sets is
|
|||
pragma Preelaborable_Initialization (Cursor);
|
||||
|
||||
Empty_Set : constant Set;
|
||||
-- Set objects declared without an initialization expression are
|
||||
-- initialized to the value Empty_Set.
|
||||
|
||||
No_Element : constant Cursor;
|
||||
-- Cursor objects declared without an initialization expression are
|
||||
-- initialized to the value No_Element.
|
||||
|
||||
function "=" (Left, Right : Set) return Boolean;
|
||||
-- For each element in Left, set equality attempts to find the equal
|
||||
-- element in Right; if a search fails, then set equality immediately
|
||||
-- returns False. The search works by calling Hash to find the bucket in
|
||||
-- the Right set that corresponds to the Left element. If the bucket is
|
||||
-- non-empty, the search calls the generic formal element equality operator
|
||||
-- to compare the element (in Left) to the element of each node in the
|
||||
-- bucket (in Right); the search terminates when a matching node in the
|
||||
-- bucket is found, or the nodes in the bucket are exhausted. (Note that
|
||||
-- element equality is called here, not Equivalent_Elements. Set equality
|
||||
-- is the only operation in which element equality is used. Compare set
|
||||
-- equality to Equivalent_Sets, which does call Equivalent_Elements.)
|
||||
|
||||
function Equivalent_Sets (Left, Right : Set) return Boolean;
|
||||
-- Similar to set equality, with the difference that the element in Left is
|
||||
-- compared to the elements in Right using the generic formal
|
||||
-- Equivalent_Elements operation instead of element equality.
|
||||
|
||||
function To_Set (New_Item : Element_Type) return Set;
|
||||
-- Constructs a singleton set comprising New_Element. To_Set calls Hash to
|
||||
-- determine the bucket for New_Item.
|
||||
|
||||
function Capacity (Container : Set) return Count_Type;
|
||||
-- Returns the current capacity of the set. Capacity is the maximum length
|
||||
-- before which rehashing in guaranteed not to occur.
|
||||
|
||||
procedure Reserve_Capacity
|
||||
(Container : in out Set;
|
||||
Capacity : Count_Type);
|
||||
procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
|
||||
-- Adjusts the current capacity, by allocating a new buckets array. If the
|
||||
-- requested capacity is less than the current capacity, then the capacity
|
||||
-- is contracted (to a value not less than the current length). If the
|
||||
-- requested capacity is greater than the current capacity, then the
|
||||
-- capacity is expanded (to a value not less than what is requested). In
|
||||
-- either case, the nodes are rehashed from the old buckets array onto the
|
||||
-- new buckets array (Hash is called once for each existing element in
|
||||
-- order to compute the new index), and then the old buckets array is
|
||||
-- deallocated.
|
||||
|
||||
function Length (Container : Set) return Count_Type;
|
||||
-- Returns the number of items in the set
|
||||
|
||||
function Is_Empty (Container : Set) return Boolean;
|
||||
-- Equivalent to Length (Container) = 0
|
||||
|
||||
procedure Clear (Container : in out Set);
|
||||
-- Removes all of the items from the set
|
||||
|
||||
function Element (Position : Cursor) return Element_Type;
|
||||
-- Returns the element of the node designated by the cursor
|
||||
|
||||
procedure Replace_Element
|
||||
(Container : in out Set;
|
||||
Position : Cursor;
|
||||
New_Item : Element_Type);
|
||||
-- If New_Item is equivalent (as determined by calling Equivalent_Elements)
|
||||
-- to the element of the node designated by Position, then New_Element is
|
||||
-- assigned to that element. Otherwise, it calls Hash to determine the
|
||||
-- bucket for New_Item. If the bucket is not empty, then it calls
|
||||
-- Equivalent_Elements for each node in that bucket to determine whether
|
||||
-- New_Item is equivalent to an element in that bucket. If
|
||||
-- Equivalent_Elements returns True then Program_Error is raised (because
|
||||
-- an element may appear only once in the set); otherwise, New_Item is
|
||||
-- assigned to the node designated by Position, and the node is moved to
|
||||
-- its new bucket.
|
||||
|
||||
procedure Query_Element
|
||||
(Position : Cursor;
|
||||
Process : not null access procedure (Element : Element_Type));
|
||||
-- Calls Process with the element (having only a constant view) of the node
|
||||
-- designed by the cursor.
|
||||
|
||||
procedure Move
|
||||
(Target : in out Set;
|
||||
Source : in out Set);
|
||||
procedure Move (Target : in out Set; Source : in out Set);
|
||||
-- Clears Target (if it's not empty), and then moves (not copies) the
|
||||
-- buckets array and nodes from Source to Target.
|
||||
|
||||
procedure Insert
|
||||
(Container : in out Set;
|
||||
New_Item : Element_Type;
|
||||
Position : out Cursor;
|
||||
Inserted : out Boolean);
|
||||
-- Conditionally inserts New_Item into the set. If New_Item is already in
|
||||
-- the set, then Inserted returns False and Position designates the node
|
||||
-- containing the existing element (which is not modified). If New_Item is
|
||||
-- not already in the set, then Inserted returns True and Position
|
||||
-- designates the newly-inserted node containing New_Item. The search for
|
||||
-- an existing element works as follows. Hash is called to determine
|
||||
-- New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
|
||||
-- is called to compare New_Item to the element of each node in that
|
||||
-- bucket. If the bucket is empty, or there were no equivalent elements in
|
||||
-- the bucket, the search "fails" and the New_Item is inserted in the set
|
||||
-- (and Inserted returns True); otherwise, the search "succeeds" (and
|
||||
-- Inserted returns False).
|
||||
|
||||
procedure Insert (Container : in out Set; New_Item : Element_Type);
|
||||
-- Attempts to insert New_Item into the set, performing the usual insertion
|
||||
-- search (which involves calling both Hash and Equivalent_Elements); if
|
||||
-- the search succeeds (New_Item is equivalent to an element already in the
|
||||
-- set, and so was not inserted), then this operation raises
|
||||
-- Constraint_Error. (This version of Insert is similar to Replace, but
|
||||
-- having the opposite exception behavior. It is intended for use when you
|
||||
-- want to assert that the item is not already in the set.)
|
||||
|
||||
procedure Include (Container : in out Set; New_Item : Element_Type);
|
||||
-- Attempts to insert New_Item into the set. If an element equivalent to
|
||||
-- New_Item is already in the set (the insertion search succeeded, and
|
||||
-- hence New_Item was not inserted), then the value of New_Item is assigned
|
||||
-- to the existing element. (This insertion operation only raises an
|
||||
-- exception if cursor tampering occurs. It is intended for use when you
|
||||
-- want to insert the item in the set, and you don't care whether an
|
||||
-- equivalent element is already present.)
|
||||
|
||||
procedure Replace (Container : in out Set; New_Item : Element_Type);
|
||||
-- Searches for New_Item in the set; if the search fails (because an
|
||||
-- equivalent element was not in the set), then it raises
|
||||
-- Constraint_Error. Otherwise, the existing element is assigned the value
|
||||
-- New_Item. (This is similar to Insert, but with the opposite exception
|
||||
-- behavior. It is intended for use when you want to assert that the item
|
||||
-- is already in the set.)
|
||||
|
||||
procedure Exclude (Container : in out Set; Item : Element_Type);
|
||||
-- Searches for Item in the set, and if found, removes its node from the
|
||||
-- set and then deallocates it. The search works as follows. The operation
|
||||
-- calls Hash to determine the item's bucket; if the bucket is not empty,
|
||||
-- it calls Equivalent_Elements to compare Item to the element of each node
|
||||
-- in the bucket. (This is the deletion analog of Include. It is intended
|
||||
-- for use when you want to remove the item from the set, but don't care
|
||||
-- whether the item is already in the set.)
|
||||
|
||||
procedure Delete (Container : in out Set; Item : Element_Type);
|
||||
-- Searches for Item in the set (which involves calling both Hash and
|
||||
-- Equivalent_Elements). If the search fails, then the operation raises
|
||||
-- Constraint_Error. Otherwise it removes the node from the set and then
|
||||
-- deallocates it. (This is the deletion analog of non-conditional
|
||||
-- Insert. It is intended for use when you want to assert that the item is
|
||||
-- already in the set.)
|
||||
|
||||
procedure Delete (Container : in out Set; Position : in out Cursor);
|
||||
-- Removes the node designated by Position from the set, and then
|
||||
-- deallocates the node. The operation calls Hash to determine the bucket,
|
||||
-- and then compares Position to each node in the bucket until there's a
|
||||
-- match (it does not call Equivalent_Elements).
|
||||
|
||||
procedure Union (Target : in out Set; Source : Set);
|
||||
-- The operation first calls Reserve_Capacity if the current capacity is
|
||||
-- less than the sum of the lengths of Source and Target. It then iterates
|
||||
-- over the Source set, and conditionally inserts each element into Target.
|
||||
|
||||
function Union (Left, Right : Set) return Set;
|
||||
-- The operation first copies the Left set to the result, and then iterates
|
||||
-- over the Right set to conditionally insert each element into the result.
|
||||
|
||||
function "or" (Left, Right : Set) return Set renames Union;
|
||||
|
||||
procedure Intersection (Target : in out Set; Source : Set);
|
||||
-- Iterates over the Target set (calling First and Next), calling Find to
|
||||
-- determine whether the element is in Source. If an equivalent element is
|
||||
-- not found in Source, the element is deleted from Target.
|
||||
|
||||
function Intersection (Left, Right : Set) return Set;
|
||||
-- Iterates over the Left set, calling Find to determine whether the
|
||||
-- element is in Right. If an equivalent element is found, it is inserted
|
||||
-- into the result set.
|
||||
|
||||
function "and" (Left, Right : Set) return Set renames Intersection;
|
||||
|
||||
procedure Difference (Target : in out Set; Source : Set);
|
||||
-- Iterates over the Source (calling First and Next), calling Find to
|
||||
-- determine whether the element is in Target. If an equivalent element is
|
||||
-- found, it is deleted from Target.
|
||||
|
||||
function Difference (Left, Right : Set) return Set;
|
||||
-- Iterates over the Left set, calling Find to determine whether the
|
||||
-- element is in the Right set. If an equivalent element is not found, the
|
||||
-- element is inserted into the result set.
|
||||
|
||||
function "-" (Left, Right : Set) return Set renames Difference;
|
||||
|
||||
procedure Symmetric_Difference (Target : in out Set; Source : Set);
|
||||
-- The operation first calls Reserve_Capacity if the current capacity is
|
||||
-- less than the sum of the lengths of Source and Target. It then iterates
|
||||
-- over the Source set, searching for the element in Target (calling Hash
|
||||
-- and Equivalent_Elements). If an equivalent element is found, it is
|
||||
-- removed from Target; otherwise it is inserted into Target.
|
||||
|
||||
function Symmetric_Difference (Left, Right : Set) return Set;
|
||||
-- The operation first iterates over the Left set. It calls Find to
|
||||
-- determine whether the element is in the Right set. If no equivalent
|
||||
-- element is found, the element from Left is inserted into the result. The
|
||||
-- operation then iterates over the Right set, to determine whether the
|
||||
-- element is in the Left set. If no equivalent element is found, the Right
|
||||
-- element is inserted into the result.
|
||||
|
||||
function "xor" (Left, Right : Set) return Set
|
||||
renames Symmetric_Difference;
|
||||
|
||||
function Overlap (Left, Right : Set) return Boolean;
|
||||
-- Iterates over the Left set (calling First and Next), calling Find to
|
||||
-- determine whether the element is in the Right set. If an equivalent
|
||||
-- element is found, the operation immediately returns True. The operation
|
||||
-- returns False if the iteration over Left terminates without finding any
|
||||
-- equivalent element in Right.
|
||||
|
||||
function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
|
||||
-- Iterates over Subset (calling First and Next), calling Find to determine
|
||||
-- whether the element is in Of_Set. If no equivalent element is found in
|
||||
-- Of_Set, the operation immediately returns False. The operation returns
|
||||
-- True if the iteration over Subset terminates without finding an element
|
||||
-- not in Of_Set (that is, every element in Subset is equivalent to an
|
||||
-- element in Of_Set).
|
||||
|
||||
function First (Container : Set) return Cursor;
|
||||
-- Returns a cursor that designates the first non-empty bucket, by
|
||||
-- searching from the beginning of the buckets array.
|
||||
|
||||
function Next (Position : Cursor) return Cursor;
|
||||
-- Returns a cursor that designates the node that follows the current one
|
||||
-- designated by Position. If Position designates the last node in its
|
||||
-- bucket, the operation calls Hash to compute the index of this bucket,
|
||||
-- and searches the buckets array for the first non-empty bucket, starting
|
||||
-- from that index; otherwise, it simply follows the link to the next node
|
||||
-- in the same bucket.
|
||||
|
||||
procedure Next (Position : in out Cursor);
|
||||
-- Equivalent to Position := Next (Position)
|
||||
|
||||
function Find (Container : Set; Item : Element_Type) return Cursor;
|
||||
-- Searches for Item in the set. Find calls Hash to determine the item's
|
||||
-- bucket; if the bucket is not empty, it calls Equivalent_Elements to
|
||||
-- compare Item to each element in the bucket. If the search succeeds, Find
|
||||
-- returns a cursor designating the node containing the equivalent element;
|
||||
-- otherwise, it returns No_Element.
|
||||
|
||||
function Contains (Container : Set; Item : Element_Type) return Boolean;
|
||||
-- Equivalent to Find (Container, Item) /= No_Element
|
||||
|
||||
function Has_Element (Position : Cursor) return Boolean;
|
||||
-- Equivalent to Position /= No_Element
|
||||
|
||||
function Equivalent_Elements (Left, Right : Cursor) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Elements with the elements of
|
||||
-- the nodes designated by cursors Left and Right.
|
||||
|
||||
function Equivalent_Elements
|
||||
(Left : Cursor;
|
||||
Right : Element_Type) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Elements with element of the
|
||||
-- node designated by Left and element Right.
|
||||
|
||||
function Equivalent_Elements
|
||||
(Left : Element_Type;
|
||||
Right : Cursor) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Elements with element Left and
|
||||
-- the element of the node designated by Right.
|
||||
|
||||
procedure Iterate
|
||||
(Container : Set;
|
||||
Process : not null access procedure (Position : Cursor));
|
||||
-- Calls Process for each node in the set
|
||||
|
||||
generic
|
||||
type Key_Type (<>) is private;
|
||||
|
@ -180,27 +336,61 @@ package Ada.Containers.Indefinite_Hashed_Sets is
|
|||
package Generic_Keys is
|
||||
|
||||
function Key (Position : Cursor) return Key_Type;
|
||||
-- Applies generic formal operation Key to the element of the node
|
||||
-- designated by Position.
|
||||
|
||||
function Element (Container : Set; Key : Key_Type) return Element_Type;
|
||||
-- Searches (as per the key-based Find) for the node containing Key, and
|
||||
-- returns the associated element.
|
||||
|
||||
procedure Replace
|
||||
(Container : in out Set;
|
||||
Key : Key_Type;
|
||||
New_Item : Element_Type);
|
||||
-- Searches (as per the key-based Find) for the node containing Key, and
|
||||
-- then replaces the element of that node (as per the element-based
|
||||
-- Replace_Element).
|
||||
|
||||
procedure Exclude (Container : in out Set; Key : Key_Type);
|
||||
-- Searches for Key in the set, and if found, removes its node from the
|
||||
-- set and then deallocates it. The search works by first calling Hash
|
||||
-- (on Key) to determine the bucket; if the bucket is not empty, it
|
||||
-- calls Equivalent_Keys to compare parameter Key to the value of
|
||||
-- generic formal operation Key applied to element of each node in the
|
||||
-- bucket.
|
||||
|
||||
procedure Delete (Container : in out Set; Key : Key_Type);
|
||||
-- Deletes the node containing Key as per Exclude, with the difference
|
||||
-- that Constraint_Error is raised if Key is not found.
|
||||
|
||||
function Find (Container : Set; Key : Key_Type) return Cursor;
|
||||
-- Searches for the node containing Key, and returns a cursor
|
||||
-- designating the node. The search works by first calling Hash (on Key)
|
||||
-- to determine the bucket. If the bucket is not empty, the search
|
||||
-- compares Key to the element of each node in the bucket, and returns
|
||||
-- the matching node. The comparison itself works by applying the
|
||||
-- generic formal Key operation to the element of the node, and then
|
||||
-- calling generic formal operation Equivalent_Keys.
|
||||
|
||||
function Contains (Container : Set; Key : Key_Type) return Boolean;
|
||||
-- Equivalent to Find (Container, Key) /= No_Element
|
||||
|
||||
procedure Update_Element_Preserving_Key
|
||||
(Container : in out Set;
|
||||
Position : Cursor;
|
||||
Process : not null access
|
||||
procedure (Element : in out Element_Type));
|
||||
-- Calls Process with the element of the node designated by Position,
|
||||
-- but with the restriction that the key-value of the element is not
|
||||
-- modified. The operation first makes a copy of the value returned by
|
||||
-- applying generic formal operation Key on the element of the node, and
|
||||
-- then calls Process with the element. The operation verifies that the
|
||||
-- key-part has not been modified by calling generic formal operation
|
||||
-- Equivalent_Keys to compare the saved key-value to the value returned
|
||||
-- by applying generic formal operation Key to the post-Process value of
|
||||
-- element. If the key values compare equal then the operation
|
||||
-- completes. Otherwise, the node is removed from the map and
|
||||
-- Program_Error is raised.
|
||||
|
||||
end Generic_Keys;
|
||||
|
||||
|
|
|
@ -33,9 +33,9 @@
|
|||
-- This unit was originally developed by Matthew J Heaney. --
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
with Ada.Containers.Hash_Tables;
|
||||
with Ada.Streams;
|
||||
with Ada.Finalization;
|
||||
private with Ada.Containers.Hash_Tables;
|
||||
private with Ada.Streams;
|
||||
private with Ada.Finalization;
|
||||
|
||||
generic
|
||||
type Key_Type is private;
|
||||
|
@ -56,43 +56,77 @@ package Ada.Containers.Hashed_Maps is
|
|||
pragma Preelaborable_Initialization (Cursor);
|
||||
|
||||
Empty_Map : constant Map;
|
||||
-- Map objects declared without an initialization expression are
|
||||
-- initialized to the value Empty_Map.
|
||||
|
||||
No_Element : constant Cursor;
|
||||
-- Cursor objects declared without an initialization expression are
|
||||
-- initialized to the value No_Element.
|
||||
|
||||
function "=" (Left, Right : Map) return Boolean;
|
||||
-- For each key/element pair in Left, equality attempts to find the key in
|
||||
-- Right; if a search fails the equality returns False. The search works by
|
||||
-- calling Hash to find the bucket in the Right map that corresponds to the
|
||||
-- Left key. If bucket is non-empty, then equality calls Equivalent_Keys
|
||||
-- to compare the key (in Left) to the key of each node in the bucket (in
|
||||
-- Right); if the keys are equivalent, then the equality test for this
|
||||
-- key/element pair (in Left) completes by calling the element equality
|
||||
-- operator to compare the element (in Left) to the element of the node
|
||||
-- (in Right) whose key matched.
|
||||
|
||||
function Capacity (Container : Map) return Count_Type;
|
||||
-- Returns the current capacity of the map. Capacity is the maximum length
|
||||
-- before which rehashing in guaranteed not to occur.
|
||||
|
||||
procedure Reserve_Capacity (Container : in out Map;
|
||||
Capacity : Count_Type);
|
||||
procedure Reserve_Capacity (Container : in out Map; Capacity : Count_Type);
|
||||
-- Adjusts the current capacity, by allocating a new buckets array. If the
|
||||
-- requested capacity is less than the current capacity, then the capacity
|
||||
-- is contracted (to a value not less than the curent length). If the
|
||||
-- requested capacity is greater than the current capacity, then the
|
||||
-- capacity is expanded (to a value not less than what is requested). In
|
||||
-- either case, the nodes are rehashed from the old buckets array onto the
|
||||
-- new buckets array (Hash is called once for each existing key in order to
|
||||
-- compute the new index), and then the old buckets array is deallocated.
|
||||
|
||||
function Length (Container : Map) return Count_Type;
|
||||
-- Returns the number of items in the map
|
||||
|
||||
function Is_Empty (Container : Map) return Boolean;
|
||||
-- Equivalent to Length (Container) = 0
|
||||
|
||||
procedure Clear (Container : in out Map);
|
||||
-- Removes all of the items from the map
|
||||
|
||||
function Key (Position : Cursor) return Key_Type;
|
||||
-- Returns the key of the node designated by the cursor
|
||||
|
||||
function Element (Position : Cursor) return Element_Type;
|
||||
-- Returns the element of the node designated by the cursor
|
||||
|
||||
procedure Replace_Element
|
||||
(Container : in out Map;
|
||||
Position : Cursor;
|
||||
New_Item : Element_Type);
|
||||
-- Assigns the value New_Item to the element designated by the cursor
|
||||
|
||||
procedure Query_Element
|
||||
(Position : Cursor;
|
||||
Process : not null access
|
||||
procedure (Key : Key_Type; Element : Element_Type));
|
||||
-- Calls Process with the key and element (both having only a constant
|
||||
-- view) of the node designed by the cursor.
|
||||
|
||||
procedure Update_Element
|
||||
(Container : in out Map;
|
||||
Position : Cursor;
|
||||
Process : not null access
|
||||
procedure (Key : Key_Type; Element : in out Element_Type));
|
||||
-- Calls Process with the key (with only a constant view) and element (with
|
||||
-- a variable view) of the node designed by the cursor.
|
||||
|
||||
procedure Move (Target : in out Map; Source : in out Map);
|
||||
-- Clears Target (if it's not empty), and then moves (not copies) the
|
||||
-- buckets array and nodes from Source to Target.
|
||||
|
||||
procedure Insert
|
||||
(Container : in out Map;
|
||||
|
@ -100,57 +134,128 @@ package Ada.Containers.Hashed_Maps is
|
|||
New_Item : Element_Type;
|
||||
Position : out Cursor;
|
||||
Inserted : out Boolean);
|
||||
-- Conditionally inserts New_Item into the map. If Key is already in the
|
||||
-- map, then Inserted returns False and Position designates the node
|
||||
-- containing the existing key/element pair (neither of which is modified).
|
||||
-- If Key is not already in the map, the Inserted returns True and Position
|
||||
-- designates the newly-inserted node container Key and New_Item. The
|
||||
-- search for the key works as follows. Hash is called to determine Key's
|
||||
-- bucket; if the bucket is non-empty, then Equivalent_Keys is called to
|
||||
-- compare Key to each node in that bucket. If the bucket is empty, or
|
||||
-- there were no matching keys in the bucket, the search "fails" and the
|
||||
-- key/item pair is inserted in the map (and Inserted returns True);
|
||||
-- otherwise, the search "succeeds" (and Inserted returns False).
|
||||
|
||||
procedure Insert
|
||||
(Container : in out Map;
|
||||
Key : Key_Type;
|
||||
Position : out Cursor;
|
||||
Inserted : out Boolean);
|
||||
-- The same as the (conditional) Insert that accepts an element parameter,
|
||||
-- with the difference that if Inserted returns True, then the element of
|
||||
-- the newly-inserted node is initialized to its default value.
|
||||
|
||||
procedure Insert
|
||||
(Container : in out Map;
|
||||
Key : Key_Type;
|
||||
New_Item : Element_Type);
|
||||
-- Attempts to insert Key into the map, performing the usual search (which
|
||||
-- involves calling both Hash and Equivalent_Keys); if the search succeeds
|
||||
-- (because Key is already in the map), then it raises Constraint_Error.
|
||||
-- (This version of Insert is similar to Replace, but having the opposite
|
||||
-- exception behavior. It is intended for use when you want to assert that
|
||||
-- Key is not already in the map.)
|
||||
|
||||
procedure Include
|
||||
(Container : in out Map;
|
||||
Key : Key_Type;
|
||||
New_Item : Element_Type);
|
||||
-- Attempts to insert Key into the map. If Key is already in the map, then
|
||||
-- both the existing key and element are assigned the values of Key and
|
||||
-- New_Item, respectively. (This version of Insert only raises an exception
|
||||
-- if cursor tampering occurs. It is intended for use when you want to
|
||||
-- insert the key/element pair in the map, and you don't care whether Key
|
||||
-- is already present.)
|
||||
|
||||
procedure Replace
|
||||
(Container : in out Map;
|
||||
Key : Key_Type;
|
||||
New_Item : Element_Type);
|
||||
-- Searches for Key in the map; if the search fails (because Key was not in
|
||||
-- the map), then it raises Constraint_Error. Otherwise, both the existing
|
||||
-- key and element are assigned the values of Key and New_Item rsp. (This
|
||||
-- is similar to Insert, but with the opposite exception behavior. It is to
|
||||
-- be used when you want to assert that Key is already in the map.)
|
||||
|
||||
procedure Exclude (Container : in out Map; Key : Key_Type);
|
||||
-- Searches for Key in the map, and if found, removes its node from the map
|
||||
-- and then deallocates it. The search works as follows. The operation
|
||||
-- calls Hash to determine the key's bucket; if the bucket is not empty, it
|
||||
-- calls Equivalent_Keys to compare Key to each key in the bucket. (This is
|
||||
-- the deletion analog of Include. It is intended for use when you want to
|
||||
-- remove the item from the map, but don't care whether the key is already
|
||||
-- in the map.)
|
||||
|
||||
procedure Delete (Container : in out Map; Key : Key_Type);
|
||||
-- Searches for Key in the map (which involves calling both Hash and
|
||||
-- Equivalent_Keys). If the search fails, then the operation raises
|
||||
-- Constraint_Eror. Otherwise it removes the node from the map and then
|
||||
-- deallocates it. (This is the deletion analog of non-conditional
|
||||
-- Insert. It is intended for use when you want to assert that the item is
|
||||
-- already in the map.)
|
||||
|
||||
procedure Delete (Container : in out Map; Position : in out Cursor);
|
||||
-- Removes the node designated by Position from the map, and then
|
||||
-- deallocates the node. The operation calls Hash to determine the bucket,
|
||||
-- and then compares Position to each node in the bucket until there's a
|
||||
-- match (it does not call Equivalent_Keys).
|
||||
|
||||
function First (Container : Map) return Cursor;
|
||||
-- Returns a cursor that designates the first non-empty bucket, by
|
||||
-- searching from the beginning of the buckets array.
|
||||
|
||||
function Next (Position : Cursor) return Cursor;
|
||||
-- Returns a cursor that designates the node that follows the current one
|
||||
-- designated by Position. If Position designates the last node in its
|
||||
-- bucket, the operation calls Hash to compute the index of this bucket,
|
||||
-- and searches the buckets array for the first non-empty bucket, starting
|
||||
-- from that index; otherwise, it simply follows the link to the next node
|
||||
-- in the same bucket.
|
||||
|
||||
procedure Next (Position : in out Cursor);
|
||||
-- Equivalent to Position := Next (Position)
|
||||
|
||||
function Find (Container : Map; Key : Key_Type) return Cursor;
|
||||
-- Searches for Key in the map. Find calls Hash to determine the key's
|
||||
-- bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
|
||||
-- Key to each key in the bucket. If the search succeeds, Find returns a
|
||||
-- cursor designating the matching node; otherwise, it returns No_Element.
|
||||
|
||||
function Contains (Container : Map; Key : Key_Type) return Boolean;
|
||||
-- Equivalent to Find (Container, Key) /= No_Element
|
||||
|
||||
function Element (Container : Map; Key : Key_Type) return Element_Type;
|
||||
-- Equivalent to Element (Find (Container, Key))
|
||||
|
||||
function Has_Element (Position : Cursor) return Boolean;
|
||||
-- Equivalent to Position /= No_Element
|
||||
|
||||
function Equivalent_Keys (Left, Right : Cursor) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Keys with the keys of the nodes
|
||||
-- designated by cursors Left and Right.
|
||||
|
||||
function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Keys with key of the node
|
||||
-- designated by Left and key Right.
|
||||
|
||||
function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Keys with key Left and the node
|
||||
-- designated by Right.
|
||||
|
||||
procedure Iterate
|
||||
(Container : Map;
|
||||
Process : not null access procedure (Position : Cursor));
|
||||
-- Calls Process for each node in the map
|
||||
|
||||
private
|
||||
pragma Inline ("=");
|
||||
|
@ -187,8 +292,10 @@ private
|
|||
use HT_Types;
|
||||
use Ada.Finalization;
|
||||
|
||||
overriding
|
||||
procedure Adjust (Container : in out Map);
|
||||
|
||||
overriding
|
||||
procedure Finalize (Container : in out Map);
|
||||
|
||||
use Ada.Streams;
|
||||
|
|
|
@ -58,114 +58,272 @@ package Ada.Containers.Hashed_Sets is
|
|||
pragma Preelaborable_Initialization (Cursor);
|
||||
|
||||
Empty_Set : constant Set;
|
||||
-- Set objects declared without an initialization expression are
|
||||
-- initialized to the value Empty_Set.
|
||||
|
||||
No_Element : constant Cursor;
|
||||
-- Cursor objects declared without an initialization expression are
|
||||
-- initialized to the value No_Element.
|
||||
|
||||
function "=" (Left, Right : Set) return Boolean;
|
||||
-- For each element in Left, set equality attempts to find the equal
|
||||
-- element in Right; if a search fails, then set equality immediately
|
||||
-- returns False. The search works by calling Hash to find the bucket in
|
||||
-- the Right set that corresponds to the Left element. If the bucket is
|
||||
-- non-empty, the search calls the generic formal element equality operator
|
||||
-- to compare the element (in Left) to the element of each node in the
|
||||
-- bucket (in Right); the search terminates when a matching node in the
|
||||
-- bucket is found, or the nodes in the bucket are exhausted. (Note that
|
||||
-- element equality is called here, not Equivalent_Elements. Set equality
|
||||
-- is the only operation in which element equality is used. Compare set
|
||||
-- equality to Equivalent_Sets, which does call Equivalent_Elements.)
|
||||
|
||||
function Equivalent_Sets (Left, Right : Set) return Boolean;
|
||||
-- Similar to set equality, with the difference that the element in Left is
|
||||
-- compared to the elements in Right using the generic formal
|
||||
-- Equivalent_Elements operation instead of element equality.
|
||||
|
||||
function To_Set (New_Item : Element_Type) return Set;
|
||||
-- Constructs a singleton set comprising New_Element. To_Set calls Hash to
|
||||
-- determine the bucket for New_Item.
|
||||
|
||||
function Capacity (Container : Set) return Count_Type;
|
||||
-- Returns the current capacity of the set. Capacity is the maximum length
|
||||
-- before which rehashing in guaranteed not to occur.
|
||||
|
||||
procedure Reserve_Capacity
|
||||
(Container : in out Set;
|
||||
Capacity : Count_Type);
|
||||
procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
|
||||
-- Adjusts the current capacity, by allocating a new buckets array. If the
|
||||
-- requested capacity is less than the current capacity, then the capacity
|
||||
-- is contracted (to a value not less than the current length). If the
|
||||
-- requested capacity is greater than the current capacity, then the
|
||||
-- capacity is expanded (to a value not less than what is requested). In
|
||||
-- either case, the nodes are rehashed from the old buckets array onto the
|
||||
-- new buckets array (Hash is called once for each existing element in
|
||||
-- order to compute the new index), and then the old buckets array is
|
||||
-- deallocated.
|
||||
|
||||
function Length (Container : Set) return Count_Type;
|
||||
-- Returns the number of items in the set
|
||||
|
||||
function Is_Empty (Container : Set) return Boolean;
|
||||
-- Equivalent to Length (Container) = 0
|
||||
|
||||
procedure Clear (Container : in out Set);
|
||||
-- Removes all of the items from the set
|
||||
|
||||
function Element (Position : Cursor) return Element_Type;
|
||||
-- Returns the element of the node designated by the cursor
|
||||
|
||||
procedure Replace_Element
|
||||
(Container : in out Set;
|
||||
Position : Cursor;
|
||||
New_Item : Element_Type);
|
||||
-- If New_Item is equivalent (as determined by calling Equivalent_Elements)
|
||||
-- to the element of the node designated by Position, then New_Element is
|
||||
-- assigned to that element. Otherwise, it calls Hash to determine the
|
||||
-- bucket for New_Item. If the bucket is not empty, then it calls
|
||||
-- Equivalent_Elements for each node in that bucket to determine whether
|
||||
-- New_Item is equivalent to an element in that bucket. If
|
||||
-- Equivalent_Elements returns True then Program_Error is raised (because
|
||||
-- an element may appear only once in the set); otherwise, New_Item is
|
||||
-- assigned to the node designated by Position, and the node is moved to
|
||||
-- its new bucket.
|
||||
|
||||
procedure Query_Element
|
||||
(Position : Cursor;
|
||||
Process : not null access procedure (Element : Element_Type));
|
||||
-- Calls Process with the element (having only a constant view) of the node
|
||||
-- designed by the cursor.
|
||||
|
||||
procedure Move (Target : in out Set; Source : in out Set);
|
||||
-- Clears Target (if it's not empty), and then moves (not copies) the
|
||||
-- buckets array and nodes from Source to Target.
|
||||
|
||||
procedure Insert
|
||||
(Container : in out Set;
|
||||
New_Item : Element_Type;
|
||||
Position : out Cursor;
|
||||
Inserted : out Boolean);
|
||||
-- Conditionally inserts New_Item into the set. If New_Item is already in
|
||||
-- the set, then Inserted returns False and Position designates the node
|
||||
-- containing the existing element (which is not modified). If New_Item is
|
||||
-- not already in the set, then Inserted returns True and Position
|
||||
-- designates the newly-inserted node containing New_Item. The search for
|
||||
-- an existing element works as follows. Hash is called to determine
|
||||
-- New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
|
||||
-- is called to compare New_Item to the element of each node in that
|
||||
-- bucket. If the bucket is empty, or there were no equivalent elements in
|
||||
-- the bucket, the search "fails" and the New_Item is inserted in the set
|
||||
-- (and Inserted returns True); otherwise, the search "succeeds" (and
|
||||
-- Inserted returns False).
|
||||
|
||||
procedure Insert (Container : in out Set; New_Item : Element_Type);
|
||||
-- Attempts to insert New_Item into the set, performing the usual insertion
|
||||
-- search (which involves calling both Hash and Equivalent_Elements); if
|
||||
-- the search succeeds (New_Item is equivalent to an element already in the
|
||||
-- set, and so was not inserted), then this operation raises
|
||||
-- Constraint_Error. (This version of Insert is similar to Replace, but
|
||||
-- having the opposite exception behavior. It is intended for use when you
|
||||
-- want to assert that the item is not already in the set.)
|
||||
|
||||
procedure Include (Container : in out Set; New_Item : Element_Type);
|
||||
-- Attempts to insert New_Item into the set. If an element equivalent to
|
||||
-- New_Item is already in the set (the insertion search succeeded, and
|
||||
-- hence New_Item was not inserted), then the value of New_Item is assigned
|
||||
-- to the existing element. (This insertion operation only raises an
|
||||
-- exception if cursor tampering occurs. It is intended for use when you
|
||||
-- want to insert the item in the set, and you don't care whether an
|
||||
-- equivalent element is already present.)
|
||||
|
||||
procedure Replace (Container : in out Set; New_Item : Element_Type);
|
||||
-- Searches for New_Item in the set; if the search fails (because an
|
||||
-- equivalent element was not in the set), then it raises
|
||||
-- Constraint_Error. Otherwise, the existing element is assigned the value
|
||||
-- New_Item. (This is similar to Insert, but with the opposite exception
|
||||
-- behavior. It is intended for use when you want to assert that the item
|
||||
-- is already in the set.)
|
||||
|
||||
procedure Exclude (Container : in out Set; Item : Element_Type);
|
||||
-- Searches for Item in the set, and if found, removes its node from the
|
||||
-- set and then deallocates it. The search works as follows. The operation
|
||||
-- calls Hash to determine the item's bucket; if the bucket is not empty,
|
||||
-- it calls Equivalent_Elements to compare Item to the element of each node
|
||||
-- in the bucket. (This is the deletion analog of Include. It is intended
|
||||
-- for use when you want to remove the item from the set, but don't care
|
||||
-- whether the item is already in the set.)
|
||||
|
||||
procedure Delete (Container : in out Set; Item : Element_Type);
|
||||
-- Searches for Item in the set (which involves calling both Hash and
|
||||
-- Equivalent_Elements). If the search fails, then the operation raises
|
||||
-- Constraint_Error. Otherwise it removes the node from the set and then
|
||||
-- deallocates it. (This is the deletion analog of non-conditional
|
||||
-- Insert. It is intended for use when you want to assert that the item is
|
||||
-- already in the set.)
|
||||
|
||||
procedure Delete (Container : in out Set; Position : in out Cursor);
|
||||
-- Removes the node designated by Position from the set, and then
|
||||
-- deallocates the node. The operation calls Hash to determine the bucket,
|
||||
-- and then compares Position to each node in the bucket until there's a
|
||||
-- match (it does not call Equivalent_Elements).
|
||||
|
||||
procedure Union (Target : in out Set; Source : Set);
|
||||
-- The operation first calls Reserve_Capacity if the current capacity is
|
||||
-- less than the sum of the lengths of Source and Target. It then iterates
|
||||
-- over the Source set, and conditionally inserts each element into Target.
|
||||
|
||||
function Union (Left, Right : Set) return Set;
|
||||
-- The operation first copies the Left set to the result, and then iterates
|
||||
-- over the Right set to conditionally insert each element into the result.
|
||||
|
||||
function "or" (Left, Right : Set) return Set renames Union;
|
||||
|
||||
procedure Intersection (Target : in out Set; Source : Set);
|
||||
-- Iterates over the Target set (calling First and Next), calling Find to
|
||||
-- determine whether the element is in Source. If an equivalent element is
|
||||
-- not found in Source, the element is deleted from Target.
|
||||
|
||||
function Intersection (Left, Right : Set) return Set;
|
||||
-- Iterates over the Left set, calling Find to determine whether the
|
||||
-- element is in Right. If an equivalent element is found, it is inserted
|
||||
-- into the result set.
|
||||
|
||||
function "and" (Left, Right : Set) return Set renames Intersection;
|
||||
|
||||
procedure Difference (Target : in out Set; Source : Set);
|
||||
-- Iterates over the Source (calling First and Next), calling Find to
|
||||
-- determine whether the element is in Target. If an equivalent element is
|
||||
-- found, it is deleted from Target.
|
||||
|
||||
function Difference (Left, Right : Set) return Set;
|
||||
-- Iterates over the Left set, calling Find to determine whether the
|
||||
-- element is in the Right set. If an equivalent element is not found, the
|
||||
-- element is inserted into the result set.
|
||||
|
||||
function "-" (Left, Right : Set) return Set renames Difference;
|
||||
|
||||
procedure Symmetric_Difference (Target : in out Set; Source : Set);
|
||||
-- The operation first calls Reserve_Capacity if the current capacity is
|
||||
-- less than the sum of the lengths of Source and Target. It then iterates
|
||||
-- over the Source set, searching for the element in Target (calling Hash
|
||||
-- and Equivalent_Elements). If an equivalent element is found, it is
|
||||
-- removed from Target; otherwise it is inserted into Target.
|
||||
|
||||
function Symmetric_Difference (Left, Right : Set) return Set;
|
||||
-- The operation first iterates over the Left set. It calls Find to
|
||||
-- determine whether the element is in the Right set. If no equivalent
|
||||
-- element is found, the element from Left is inserted into the result. The
|
||||
-- operation then iterates over the Right set, to determine whether the
|
||||
-- element is in the Left set. If no equivalent element is found, the Right
|
||||
-- element is inserted into the result.
|
||||
|
||||
function "xor" (Left, Right : Set) return Set
|
||||
renames Symmetric_Difference;
|
||||
|
||||
function Overlap (Left, Right : Set) return Boolean;
|
||||
-- Iterates over the Left set (calling First and Next), calling Find to
|
||||
-- determine whether the element is in the Right set. If an equivalent
|
||||
-- element is found, the operation immediately returns True. The operation
|
||||
-- returns False if the iteration over Left terminates without finding any
|
||||
-- equivalent element in Right.
|
||||
|
||||
function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
|
||||
-- Iterates over Subset (calling First and Next), calling Find to determine
|
||||
-- whether the element is in Of_Set. If no equivalent element is found in
|
||||
-- Of_Set, the operation immediately returns False. The operation returns
|
||||
-- True if the iteration over Subset terminates without finding an element
|
||||
-- not in Of_Set (that is, every element in Subset is equivalent to an
|
||||
-- element in Of_Set).
|
||||
|
||||
function First (Container : Set) return Cursor;
|
||||
-- Returns a cursor that designates the first non-empty bucket, by
|
||||
-- searching from the beginning of the buckets array.
|
||||
|
||||
function Next (Position : Cursor) return Cursor;
|
||||
-- Returns a cursor that designates the node that follows the current one
|
||||
-- designated by Position. If Position designates the last node in its
|
||||
-- bucket, the operation calls Hash to compute the index of this bucket,
|
||||
-- and searches the buckets array for the first non-empty bucket, starting
|
||||
-- from that index; otherwise, it simply follows the link to the next node
|
||||
-- in the same bucket.
|
||||
|
||||
procedure Next (Position : in out Cursor);
|
||||
-- Equivalent to Position := Next (Position)
|
||||
|
||||
function Find
|
||||
(Container : Set;
|
||||
Item : Element_Type) return Cursor;
|
||||
-- Searches for Item in the set. Find calls Hash to determine the item's
|
||||
-- bucket; if the bucket is not empty, it calls Equivalent_Elements to
|
||||
-- compare Item to each element in the bucket. If the search succeeds, Find
|
||||
-- returns a cursor designating the node containing the equivalent element;
|
||||
-- otherwise, it returns No_Element.
|
||||
|
||||
function Contains (Container : Set; Item : Element_Type) return Boolean;
|
||||
-- Equivalent to Find (Container, Item) /= No_Element
|
||||
|
||||
function Has_Element (Position : Cursor) return Boolean;
|
||||
-- Equivalent to Position /= No_Element
|
||||
|
||||
function Equivalent_Elements (Left, Right : Cursor) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Elements with the elements of
|
||||
-- the nodes designated by cursors Left and Right.
|
||||
|
||||
function Equivalent_Elements
|
||||
(Left : Cursor;
|
||||
Right : Element_Type) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Elements with element of the
|
||||
-- node designated by Left and element Right.
|
||||
|
||||
function Equivalent_Elements
|
||||
(Left : Element_Type;
|
||||
Right : Cursor) return Boolean;
|
||||
-- Returns the result of calling Equivalent_Elements with element Left and
|
||||
-- the element of the node designated by Right.
|
||||
|
||||
procedure Iterate
|
||||
(Container : Set;
|
||||
Process : not null access procedure (Position : Cursor));
|
||||
-- Calls Process for each node in the set
|
||||
|
||||
generic
|
||||
type Key_Type (<>) is private;
|
||||
|
@ -179,27 +337,61 @@ package Ada.Containers.Hashed_Sets is
|
|||
package Generic_Keys is
|
||||
|
||||
function Key (Position : Cursor) return Key_Type;
|
||||
-- Applies generic formal operation Key to the element of the node
|
||||
-- designated by Position.
|
||||
|
||||
function Element (Container : Set; Key : Key_Type) return Element_Type;
|
||||
-- Searches (as per the key-based Find) for the node containing Key, and
|
||||
-- returns the associated element.
|
||||
|
||||
procedure Replace
|
||||
(Container : in out Set;
|
||||
Key : Key_Type;
|
||||
New_Item : Element_Type);
|
||||
-- Searches (as per the key-based Find) for the node containing Key, and
|
||||
-- then replaces the element of that node (as per the element-based
|
||||
-- Replace_Element).
|
||||
|
||||
procedure Exclude (Container : in out Set; Key : Key_Type);
|
||||
-- Searches for Key in the set, and if found, removes its node from the
|
||||
-- set and then deallocates it. The search works by first calling Hash
|
||||
-- (on Key) to determine the bucket; if the bucket is not empty, it
|
||||
-- calls Equivalent_Keys to compare parameter Key to the value of
|
||||
-- generic formal operation Key applied to element of each node in the
|
||||
-- bucket.
|
||||
|
||||
procedure Delete (Container : in out Set; Key : Key_Type);
|
||||
-- Deletes the node containing Key as per Exclude, with the difference
|
||||
-- that Constraint_Error is raised if Key is not found.
|
||||
|
||||
function Find (Container : Set; Key : Key_Type) return Cursor;
|
||||
-- Searches for the node containing Key, and returns a cursor
|
||||
-- designating the node. The search works by first calling Hash (on Key)
|
||||
-- to determine the bucket. If the bucket is not empty, the search
|
||||
-- compares Key to the element of each node in the bucket, and returns
|
||||
-- the matching node. The comparison itself works by applying the
|
||||
-- generic formal Key operation to the element of the node, and then
|
||||
-- calling generic formal operation Equivalent_Keys.
|
||||
|
||||
function Contains (Container : Set; Key : Key_Type) return Boolean;
|
||||
-- Equivalent to Find (Container, Key) /= No_Element
|
||||
|
||||
procedure Update_Element_Preserving_Key
|
||||
(Container : in out Set;
|
||||
Position : Cursor;
|
||||
Process : not null access
|
||||
procedure (Element : in out Element_Type));
|
||||
-- Calls Process with the element of the node designated by Position,
|
||||
-- but with the restriction that the key-value of the element is not
|
||||
-- modified. The operation first makes a copy of the value returned by
|
||||
-- applying generic formal operation Key on the element of the node, and
|
||||
-- then calls Process with the element. The operation verifies that the
|
||||
-- key-part has not been modified by calling generic formal operation
|
||||
-- Equivalent_Keys to compare the saved key-value to the value returned
|
||||
-- by applying generic formal operation Key to the post-Process value of
|
||||
-- element. If the key values compare equal then the operation
|
||||
-- completes. Otherwise, the node is removed from the map and
|
||||
-- Program_Error is raised.
|
||||
|
||||
end Generic_Keys;
|
||||
|
||||
|
|
Loading…
Reference in New Issue