1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.HASH_TABLES.GENERIC_KEYS --
9 -- Copyright (C) 2004-2023, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 package body Ada
.Containers
.Hash_Tables
.Generic_Keys
is
32 pragma Warnings
(Off
, "variable ""Busy*"" is not referenced");
33 pragma Warnings
(Off
, "variable ""Lock*"" is not referenced");
34 -- See comment in Ada.Containers.Helpers
36 -----------------------------
37 -- Checked_Equivalent_Keys --
38 -----------------------------
40 function Checked_Equivalent_Keys
41 (HT
: aliased in out Hash_Table_Type
;
43 Node
: Node_Access
) return Boolean
45 Lock
: With_Lock
(HT
.TC
'Unrestricted_Access);
47 return Equivalent_Keys
(Key
, Node
);
48 end Checked_Equivalent_Keys
;
54 function Checked_Index
55 (HT
: aliased in out Hash_Table_Type
;
56 Key
: Key_Type
) return Hash_Type
58 Lock
: With_Lock
(HT
.TC
'Unrestricted_Access);
60 return Hash
(Key
) mod HT
.Buckets
'Length;
63 --------------------------
64 -- Delete_Key_Sans_Free --
65 --------------------------
67 procedure Delete_Key_Sans_Free
68 (HT
: in out Hash_Table_Type
;
81 -- Per AI05-0022, the container implementation is required to detect
82 -- element tampering by a generic actual subprogram.
86 Indx
:= Checked_Index
(HT
, Key
);
87 X
:= HT
.Buckets
(Indx
);
93 if Checked_Equivalent_Keys
(HT
, Key
, X
) then
94 HT
.Buckets
(Indx
) := Next
(X
);
95 HT
.Length
:= HT
.Length
- 1;
107 if Checked_Equivalent_Keys
(HT
, Key
, X
) then
108 Set_Next
(Node
=> Prev
, Next
=> Next
(X
));
109 HT
.Length
:= HT
.Length
- 1;
113 end Delete_Key_Sans_Free
;
120 (HT
: aliased in out Hash_Table_Type
;
121 Key
: Key_Type
) return Node_Access
127 if HT
.Length
= 0 then
131 Indx
:= Checked_Index
(HT
, Key
);
133 Node
:= HT
.Buckets
(Indx
);
134 while Node
/= null loop
135 if Checked_Equivalent_Keys
(HT
, Key
, Node
) then
144 --------------------------------
145 -- Generic_Conditional_Insert --
146 --------------------------------
148 procedure Generic_Conditional_Insert
149 (HT
: in out Hash_Table_Type
;
151 Node
: out Node_Access
;
152 Inserted
: out Boolean)
157 -- Per AI05-0022, the container implementation is required to detect
158 -- element tampering by a generic actual subprogram.
162 Indx
:= Checked_Index
(HT
, Key
);
163 Node
:= HT
.Buckets
(Indx
);
166 if Checks
and then HT
.Length
= Count_Type
'Last then
167 raise Constraint_Error
;
170 Node
:= New_Node
(Next
=> null);
173 HT
.Buckets
(Indx
) := Node
;
174 HT
.Length
:= HT
.Length
+ 1;
180 if Checked_Equivalent_Keys
(HT
, Key
, Node
) then
187 exit when Node
= null;
190 if Checks
and then HT
.Length
= Count_Type
'Last then
191 raise Constraint_Error
;
194 Node
:= New_Node
(Next
=> HT
.Buckets
(Indx
));
197 HT
.Buckets
(Indx
) := Node
;
198 HT
.Length
:= HT
.Length
+ 1;
199 end Generic_Conditional_Insert
;
201 -----------------------------
202 -- Generic_Replace_Element --
203 -----------------------------
205 procedure Generic_Replace_Element
206 (HT
: in out Hash_Table_Type
;
210 pragma Assert
(HT
.Length
> 0);
211 pragma Assert
(Node
/= null);
213 Old_Indx
: Hash_Type
;
214 New_Indx
: constant Hash_Type
:= Checked_Index
(HT
, Key
);
216 New_Bucket
: Node_Access
renames HT
.Buckets
(New_Indx
);
220 -- Per AI05-0022, the container implementation is required to detect
221 -- element tampering by a generic actual subprogram.
224 Lock
: With_Lock
(HT
.TC
'Unrestricted_Access);
226 Old_Indx
:= Hash
(Node
) mod HT
.Buckets
'Length;
229 if Checked_Equivalent_Keys
(HT
, Key
, Node
) then
232 -- We can change a node's key to Key (that's what Assign is for), but
233 -- only if Key is not already in the hash table. (In a unique-key
234 -- hash table as this one a key is mapped to exactly one node only.)
235 -- The exception is when Key is mapped to Node, in which case the
236 -- change is allowed.
242 -- Key is not equivalent to Node, so we now have to determine if it's
243 -- equivalent to some other node in the hash table. This is the case
244 -- irrespective of whether Key is in the same or a different bucket from
249 if Checks
and then Checked_Equivalent_Keys
(HT
, Key
, N
) then
250 pragma Assert
(N
/= Node
);
251 raise Program_Error
with
252 "attempt to replace existing element";
258 -- We have determined that Key is not already in the hash table, so
259 -- the change is tentatively allowed. We now perform the standard
260 -- checks to determine whether the hash table is locked (because you
261 -- cannot change an element while it's in use by Query_Element or
262 -- Update_Element), or if the container is busy (because moving a
263 -- node to a different bucket would interfere with iteration).
265 if Old_Indx
= New_Indx
then
266 -- The node is already in the bucket implied by Key. In this case
267 -- we merely change its value without moving it.
275 -- The node is a bucket different from the bucket implied by Key
279 -- Do the assignment first, before moving the node, so that if Assign
280 -- propagates an exception, then the hash table will not have been
281 -- modified (except for any possible side-effect Assign had on Node).
285 -- Now we can safely remove the node from its current bucket
287 N
:= HT
.Buckets
(Old_Indx
);
288 pragma Assert
(N
/= null);
291 HT
.Buckets
(Old_Indx
) := Next
(Node
);
294 pragma Assert
(HT
.Length
> 1);
298 pragma Assert
(M
/= null);
301 Set_Next
(Node
=> N
, Next
=> Next
(Node
));
309 -- Now we link the node into its new bucket (corresponding to Key)
311 Set_Next
(Node
=> Node
, Next
=> New_Bucket
);
313 end Generic_Replace_Element
;
320 (HT
: Hash_Table_Type
;
321 Key
: Key_Type
) return Hash_Type
324 return Hash
(Key
) mod HT
.Buckets
'Length;
327 end Ada
.Containers
.Hash_Tables
.Generic_Keys
;