1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.HASH_TABLES.GENERIC_KEYS --
9 -- Copyright (C) 2004-2013, 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 -----------------------------
33 -- Checked_Equivalent_Keys --
34 -----------------------------
36 function Checked_Equivalent_Keys
37 (HT
: aliased in out Hash_Table_Type
;
39 Node
: Node_Access
) return Boolean
43 B
: Natural renames HT
.Busy
;
44 L
: Natural renames HT
.Lock
;
50 Result
:= Equivalent_Keys
(Key
, Node
);
63 end Checked_Equivalent_Keys
;
69 function Checked_Index
70 (HT
: aliased in out Hash_Table_Type
;
71 Key
: Key_Type
) return Hash_Type
75 B
: Natural renames HT
.Busy
;
76 L
: Natural renames HT
.Lock
;
82 Result
:= Hash
(Key
) mod HT
.Buckets
'Length;
97 --------------------------
98 -- Delete_Key_Sans_Free --
99 --------------------------
101 procedure Delete_Key_Sans_Free
102 (HT
: in out Hash_Table_Type
;
110 if HT
.Length
= 0 then
115 -- Per AI05-0022, the container implementation is required to detect
116 -- element tampering by a generic actual subprogram.
119 raise Program_Error
with
120 "attempt to tamper with cursors (container is busy)";
123 Indx
:= Checked_Index
(HT
, Key
);
124 X
:= HT
.Buckets
(Indx
);
130 if Checked_Equivalent_Keys
(HT
, Key
, X
) then
132 raise Program_Error
with
133 "attempt to tamper with cursors (container is busy)";
135 HT
.Buckets
(Indx
) := Next
(X
);
136 HT
.Length
:= HT
.Length
- 1;
148 if Checked_Equivalent_Keys
(HT
, Key
, X
) then
150 raise Program_Error
with
151 "attempt to tamper with cursors (container is busy)";
153 Set_Next
(Node
=> Prev
, Next
=> Next
(X
));
154 HT
.Length
:= HT
.Length
- 1;
158 end Delete_Key_Sans_Free
;
165 (HT
: aliased in out Hash_Table_Type
;
166 Key
: Key_Type
) return Node_Access
172 if HT
.Length
= 0 then
176 Indx
:= Checked_Index
(HT
, Key
);
178 Node
:= HT
.Buckets
(Indx
);
179 while Node
/= null loop
180 if Checked_Equivalent_Keys
(HT
, Key
, Node
) then
189 --------------------------------
190 -- Generic_Conditional_Insert --
191 --------------------------------
193 procedure Generic_Conditional_Insert
194 (HT
: in out Hash_Table_Type
;
196 Node
: out Node_Access
;
197 Inserted
: out Boolean)
202 -- Per AI05-0022, the container implementation is required to detect
203 -- element tampering by a generic actual subprogram.
206 raise Program_Error
with
207 "attempt to tamper with cursors (container is busy)";
210 Indx
:= Checked_Index
(HT
, Key
);
211 Node
:= HT
.Buckets
(Indx
);
214 if HT
.Length
= Count_Type
'Last then
215 raise Constraint_Error
;
218 Node
:= New_Node
(Next
=> null);
221 HT
.Buckets
(Indx
) := Node
;
222 HT
.Length
:= HT
.Length
+ 1;
228 if Checked_Equivalent_Keys
(HT
, Key
, Node
) then
235 exit when Node
= null;
238 if HT
.Length
= Count_Type
'Last then
239 raise Constraint_Error
;
242 Node
:= New_Node
(Next
=> HT
.Buckets
(Indx
));
245 HT
.Buckets
(Indx
) := Node
;
246 HT
.Length
:= HT
.Length
+ 1;
247 end Generic_Conditional_Insert
;
249 -----------------------------
250 -- Generic_Replace_Element --
251 -----------------------------
253 procedure Generic_Replace_Element
254 (HT
: in out Hash_Table_Type
;
258 pragma Assert
(HT
.Length
> 0);
259 pragma Assert
(Node
/= null);
261 Old_Indx
: Hash_Type
;
262 New_Indx
: constant Hash_Type
:= Checked_Index
(HT
, Key
);
264 New_Bucket
: Node_Access
renames HT
.Buckets
(New_Indx
);
268 -- Per AI05-0022, the container implementation is required to detect
269 -- element tampering by a generic actual subprogram.
272 B
: Natural renames HT
.Busy
;
273 L
: Natural renames HT
.Lock
;
279 Old_Indx
:= Hash
(Node
) mod HT
.Buckets
'Length;
292 if Checked_Equivalent_Keys
(HT
, Key
, Node
) then
294 raise Program_Error
with
295 "attempt to tamper with elements (container is locked)";
298 -- We can change a node's key to Key (that's what Assign is for), but
299 -- only if Key is not already in the hash table. (In a unique-key
300 -- hash table as this one a key is mapped to exactly one node only.)
301 -- The exception is when Key is mapped to Node, in which case the
302 -- change is allowed.
308 -- Key is not equivalent to Node, so we now have to determine if it's
309 -- equivalent to some other node in the hash table. This is the case
310 -- irrespective of whether Key is in the same or a different bucket from
315 if Checked_Equivalent_Keys
(HT
, Key
, N
) then
316 pragma Assert
(N
/= Node
);
317 raise Program_Error
with
318 "attempt to replace existing element";
324 -- We have determined that Key is not already in the hash table, so
325 -- the change is tentatively allowed. We now perform the standard
326 -- checks to determine whether the hash table is locked (because you
327 -- cannot change an element while it's in use by Query_Element or
328 -- Update_Element), or if the container is busy (because moving a
329 -- node to a different bucket would interfere with iteration).
331 if Old_Indx
= New_Indx
then
332 -- The node is already in the bucket implied by Key. In this case
333 -- we merely change its value without moving it.
336 raise Program_Error
with
337 "attempt to tamper with elements (container is locked)";
344 -- The node is a bucket different from the bucket implied by Key
347 raise Program_Error
with
348 "attempt to tamper with cursors (container is busy)";
351 -- Do the assignment first, before moving the node, so that if Assign
352 -- propagates an exception, then the hash table will not have been
353 -- modified (except for any possible side-effect Assign had on Node).
357 -- Now we can safely remove the node from its current bucket
359 N
:= HT
.Buckets
(Old_Indx
);
360 pragma Assert
(N
/= null);
363 HT
.Buckets
(Old_Indx
) := Next
(Node
);
366 pragma Assert
(HT
.Length
> 1);
370 pragma Assert
(M
/= null);
373 Set_Next
(Node
=> N
, Next
=> Next
(Node
));
381 -- Now we link the node into its new bucket (corresponding to Key)
383 Set_Next
(Node
=> Node
, Next
=> New_Bucket
);
385 end Generic_Replace_Element
;
392 (HT
: Hash_Table_Type
;
393 Key
: Key_Type
) return Hash_Type
396 return Hash
(Key
) mod HT
.Buckets
'Length;
399 end Ada
.Containers
.Hash_Tables
.Generic_Keys
;