| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T . S E T S -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2018-2025, AdaCore -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- Note: this unit is used during bootstrap, see ADA_GENERATED_FILES in |
| 33 |
-- gcc-interface/Make-lang.in for details on the constraints. |
| 34 |
|
| 35 |
with GNAT.Dynamic_HTables; use GNAT.Dynamic_HTables; |
| 36 |
|
| 37 |
package GNAT.Sets is |
| 38 |
|
| 39 |
-------------------- |
| 40 |
-- Membership_Set -- |
| 41 |
-------------------- |
| 42 |
|
| 43 |
-- The following package offers a membership set abstraction with the |
| 44 |
-- following characteristics: |
| 45 |
-- |
| 46 |
-- * Creation of multiple instances, of different sizes |
| 47 |
-- * Iterable elements |
| 48 |
-- |
| 49 |
-- The following use pattern must be employed with this set: |
| 50 |
-- |
| 51 |
-- Set : Membership_Set := Create (<some size>); |
| 52 |
-- |
| 53 |
-- <various operations> |
| 54 |
-- |
| 55 |
-- Destroy (Set); |
| 56 |
-- |
| 57 |
-- The destruction of the set reclaims all storage occupied by it. |
| 58 |
|
| 59 |
generic |
| 60 |
type Element_Type is private; |
| 61 |
|
| 62 |
with function "=" |
| 63 |
(Left : Element_Type; |
| 64 |
Right : Element_Type) return Boolean; |
| 65 |
|
| 66 |
with function Hash (Key : Element_Type) return Bucket_Range_Type; |
| 67 |
-- Map an arbitrary key into the range of buckets |
| 68 |
|
| 69 |
package Membership_Sets is |
| 70 |
|
| 71 |
-------------------- |
| 72 |
-- Set operations -- |
| 73 |
-------------------- |
| 74 |
|
| 75 |
-- The following type denotes a membership set handle. Each instance |
| 76 |
-- must be created using routine Create. |
| 77 |
|
| 78 |
type Membership_Set is private; |
| 79 |
Nil : constant Membership_Set; |
| 80 |
|
| 81 |
function Contains |
| 82 |
(S : Membership_Set; |
| 83 |
Elem : Element_Type) return Boolean; |
| 84 |
-- Determine whether membership set S contains element Elem |
| 85 |
|
| 86 |
function Create (Initial_Size : Positive) return Membership_Set; |
| 87 |
-- Create a new membership set with bucket capacity Initial_Size. This |
| 88 |
-- routine must be called at the start of the membership set's lifetime. |
| 89 |
|
| 90 |
procedure Delete |
| 91 |
(S : Membership_Set; |
| 92 |
Elem : Element_Type); |
| 93 |
-- Delete element Elem from membership set S. The routine has no effect |
| 94 |
-- if the element is not present in the membership set. This action will |
| 95 |
-- raise Iterated if the membership set has outstanding iterators. |
| 96 |
|
| 97 |
procedure Destroy (S : in out Membership_Set); |
| 98 |
-- Destroy the contents of membership set S, rendering it unusable. This |
| 99 |
-- routine must be called at the end of the membership set's lifetime. |
| 100 |
-- This action will raise Iterated if the hash table has outstanding |
| 101 |
-- iterators. |
| 102 |
|
| 103 |
procedure Insert |
| 104 |
(S : Membership_Set; |
| 105 |
Elem : Element_Type); |
| 106 |
-- Insert element Elem in membership set S. The routine has no effect |
| 107 |
-- if the element is already present in the membership set. This action |
| 108 |
-- will raise Iterated if the membership set has outstanding iterators. |
| 109 |
|
| 110 |
function Is_Empty (S : Membership_Set) return Boolean; |
| 111 |
-- Determine whether set S is empty |
| 112 |
|
| 113 |
function Present (S : Membership_Set) return Boolean; |
| 114 |
-- Determine whether set S exists |
| 115 |
|
| 116 |
procedure Reset (S : Membership_Set); |
| 117 |
-- Destroy the contents of membership set S, and reset it to its initial |
| 118 |
-- created state. This action will raise Iterated if the membership set |
| 119 |
-- has outstanding iterators. |
| 120 |
|
| 121 |
function Size (S : Membership_Set) return Natural; |
| 122 |
-- Obtain the number of elements in membership set S |
| 123 |
|
| 124 |
------------------------- |
| 125 |
-- Iterator operations -- |
| 126 |
------------------------- |
| 127 |
|
| 128 |
-- The following type represents an element iterator. An iterator locks |
| 129 |
-- all mutation operations, and unlocks them once it is exhausted. The |
| 130 |
-- iterator must be used with the following pattern: |
| 131 |
-- |
| 132 |
-- Iter := Iterate (My_Set); |
| 133 |
-- while Has_Next (Iter) loop |
| 134 |
-- Next (Iter, Element); |
| 135 |
-- end loop; |
| 136 |
-- |
| 137 |
-- It is possible to advance the iterator by using Next only, however |
| 138 |
-- this risks raising Iterator_Exhausted. |
| 139 |
|
| 140 |
type Iterator is private; |
| 141 |
|
| 142 |
function Iterate (S : Membership_Set) return Iterator; |
| 143 |
-- Obtain an iterator over the elements of membership set S. This action |
| 144 |
-- locks all mutation functionality of the associated membership set. |
| 145 |
|
| 146 |
function Has_Next (Iter : Iterator) return Boolean; |
| 147 |
-- Determine whether iterator Iter has more keys to examine. If the |
| 148 |
-- iterator has been exhausted, restore all mutation functionality of |
| 149 |
-- the associated membership set. |
| 150 |
|
| 151 |
procedure Next (Iter : in out Iterator; Elem : out Element_Type); |
| 152 |
-- Return the current element referenced by iterator Iter and advance |
| 153 |
-- to the next available element. If the iterator has been exhausted |
| 154 |
-- and further attempts are made to advance it, this routine restores |
| 155 |
-- mutation functionality of the associated membership set, and then |
| 156 |
-- raises Iterator_Exhausted. |
| 157 |
|
| 158 |
private |
| 159 |
procedure Destroy (B : in out Boolean); |
| 160 |
-- Destroy boolean B |
| 161 |
|
| 162 |
package Hashed_Set is new Dynamic_Hash_Tables |
| 163 |
(Key_Type => Element_Type, |
| 164 |
Value_Type => Boolean, |
| 165 |
No_Value => False, |
| 166 |
Expansion_Threshold => 1.5, |
| 167 |
Expansion_Factor => 2, |
| 168 |
Compression_Threshold => 0.3, |
| 169 |
Compression_Factor => 2, |
| 170 |
"=" => "=", |
| 171 |
Destroy_Value => Destroy, |
| 172 |
Hash => Hash); |
| 173 |
|
| 174 |
type Membership_Set is new Hashed_Set.Dynamic_Hash_Table; |
| 175 |
Nil : constant Membership_Set := Membership_Set (Hashed_Set.Nil); |
| 176 |
|
| 177 |
type Iterator is new Hashed_Set.Iterator; |
| 178 |
end Membership_Sets; |
| 179 |
|
| 180 |
end GNAT.Sets; |