ada: Update copyright notice
[official-gcc.git] / gcc / ada / libgnat / a-chtgke.adb
blob81b2eb82ab614fdf8df63063a3c76762e2322b4a
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.HASH_TABLES.GENERIC_KEYS --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2023, Free Software Foundation, Inc. --
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 -- 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;
42 Key : Key_Type;
43 Node : Node_Access) return Boolean
45 Lock : With_Lock (HT.TC'Unrestricted_Access);
46 begin
47 return Equivalent_Keys (Key, Node);
48 end Checked_Equivalent_Keys;
50 -------------------
51 -- Checked_Index --
52 -------------------
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);
59 begin
60 return Hash (Key) mod HT.Buckets'Length;
61 end Checked_Index;
63 --------------------------
64 -- Delete_Key_Sans_Free --
65 --------------------------
67 procedure Delete_Key_Sans_Free
68 (HT : in out Hash_Table_Type;
69 Key : Key_Type;
70 X : out Node_Access)
72 Indx : Hash_Type;
73 Prev : Node_Access;
75 begin
76 if HT.Length = 0 then
77 X := null;
78 return;
79 end if;
81 -- Per AI05-0022, the container implementation is required to detect
82 -- element tampering by a generic actual subprogram.
84 TC_Check (HT.TC);
86 Indx := Checked_Index (HT, Key);
87 X := HT.Buckets (Indx);
89 if X = null then
90 return;
91 end if;
93 if Checked_Equivalent_Keys (HT, Key, X) then
94 HT.Buckets (Indx) := Next (X);
95 HT.Length := HT.Length - 1;
96 return;
97 end if;
99 loop
100 Prev := X;
101 X := Next (Prev);
103 if X = null then
104 return;
105 end if;
107 if Checked_Equivalent_Keys (HT, Key, X) then
108 Set_Next (Node => Prev, Next => Next (X));
109 HT.Length := HT.Length - 1;
110 return;
111 end if;
112 end loop;
113 end Delete_Key_Sans_Free;
115 ----------
116 -- Find --
117 ----------
119 function Find
120 (HT : aliased in out Hash_Table_Type;
121 Key : Key_Type) return Node_Access
123 Indx : Hash_Type;
124 Node : Node_Access;
126 begin
127 if HT.Length = 0 then
128 return null;
129 end if;
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
136 return Node;
137 end if;
138 Node := Next (Node);
139 end loop;
141 return null;
142 end Find;
144 --------------------------------
145 -- Generic_Conditional_Insert --
146 --------------------------------
148 procedure Generic_Conditional_Insert
149 (HT : in out Hash_Table_Type;
150 Key : Key_Type;
151 Node : out Node_Access;
152 Inserted : out Boolean)
154 Indx : Hash_Type;
156 begin
157 -- Per AI05-0022, the container implementation is required to detect
158 -- element tampering by a generic actual subprogram.
160 TC_Check (HT.TC);
162 Indx := Checked_Index (HT, Key);
163 Node := HT.Buckets (Indx);
165 if Node = null then
166 if Checks and then HT.Length = Count_Type'Last then
167 raise Constraint_Error;
168 end if;
170 Node := New_Node (Next => null);
171 Inserted := True;
173 HT.Buckets (Indx) := Node;
174 HT.Length := HT.Length + 1;
176 return;
177 end if;
179 loop
180 if Checked_Equivalent_Keys (HT, Key, Node) then
181 Inserted := False;
182 return;
183 end if;
185 Node := Next (Node);
187 exit when Node = null;
188 end loop;
190 if Checks and then HT.Length = Count_Type'Last then
191 raise Constraint_Error;
192 end if;
194 Node := New_Node (Next => HT.Buckets (Indx));
195 Inserted := True;
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;
207 Node : Node_Access;
208 Key : Key_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);
217 N, M : Node_Access;
219 begin
220 -- Per AI05-0022, the container implementation is required to detect
221 -- element tampering by a generic actual subprogram.
223 declare
224 Lock : With_Lock (HT.TC'Unrestricted_Access);
225 begin
226 Old_Indx := Hash (Node) mod HT.Buckets'Length;
227 end;
229 if Checked_Equivalent_Keys (HT, Key, Node) then
230 TE_Check (HT.TC);
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.
238 Assign (Node, Key);
239 return;
240 end if;
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
245 -- Node.
247 N := New_Bucket;
248 while N /= null loop
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";
253 end if;
255 N := Next (N);
256 end loop;
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.
269 TE_Check (HT.TC);
271 Assign (Node, Key);
272 return;
273 end if;
275 -- The node is a bucket different from the bucket implied by Key
277 TC_Check (HT.TC);
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).
283 Assign (Node, Key);
285 -- Now we can safely remove the node from its current bucket
287 N := HT.Buckets (Old_Indx);
288 pragma Assert (N /= null);
290 if N = Node then
291 HT.Buckets (Old_Indx) := Next (Node);
293 else
294 pragma Assert (HT.Length > 1);
296 loop
297 M := Next (N);
298 pragma Assert (M /= null);
300 if M = Node then
301 Set_Next (Node => N, Next => Next (Node));
302 exit;
303 end if;
305 N := M;
306 end loop;
307 end if;
309 -- Now we link the node into its new bucket (corresponding to Key)
311 Set_Next (Node => Node, Next => New_Bucket);
312 New_Bucket := Node;
313 end Generic_Replace_Element;
315 -----------
316 -- Index --
317 -----------
319 function Index
320 (HT : Hash_Table_Type;
321 Key : Key_Type) return Hash_Type
323 begin
324 return Hash (Key) mod HT.Buckets'Length;
325 end Index;
327 end Ada.Containers.Hash_Tables.Generic_Keys;