annotate gcc/ada/libgnat/a-chtgbk.adb @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT LIBRARY COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- ADA.CONTAINERS.HASH_TABLES.GENERIC_BOUNDED_KEYS --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 2004-2017, Free Software Foundation, Inc. --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- This unit was originally developed by Matthew J Heaney. --
kono
parents:
diff changeset
28 ------------------------------------------------------------------------------
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 package body Ada.Containers.Hash_Tables.Generic_Bounded_Keys is
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 pragma Warnings (Off, "variable ""Busy*"" is not referenced");
kono
parents:
diff changeset
33 pragma Warnings (Off, "variable ""Lock*"" is not referenced");
kono
parents:
diff changeset
34 -- See comment in Ada.Containers.Helpers
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 -----------------------------
kono
parents:
diff changeset
37 -- Checked_Equivalent_Keys --
kono
parents:
diff changeset
38 -----------------------------
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 function Checked_Equivalent_Keys
kono
parents:
diff changeset
41 (HT : aliased in out Hash_Table_Type'Class;
kono
parents:
diff changeset
42 Key : Key_Type;
kono
parents:
diff changeset
43 Node : Count_Type) return Boolean
kono
parents:
diff changeset
44 is
kono
parents:
diff changeset
45 Lock : With_Lock (HT.TC'Unrestricted_Access);
kono
parents:
diff changeset
46 begin
kono
parents:
diff changeset
47 return Equivalent_Keys (Key, HT.Nodes (Node));
kono
parents:
diff changeset
48 end Checked_Equivalent_Keys;
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 -------------------
kono
parents:
diff changeset
51 -- Checked_Index --
kono
parents:
diff changeset
52 -------------------
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 function Checked_Index
kono
parents:
diff changeset
55 (HT : aliased in out Hash_Table_Type'Class;
kono
parents:
diff changeset
56 Key : Key_Type) return Hash_Type
kono
parents:
diff changeset
57 is
kono
parents:
diff changeset
58 Lock : With_Lock (HT.TC'Unrestricted_Access);
kono
parents:
diff changeset
59 begin
kono
parents:
diff changeset
60 return HT.Buckets'First + Hash (Key) mod HT.Buckets'Length;
kono
parents:
diff changeset
61 end Checked_Index;
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 --------------------------
kono
parents:
diff changeset
64 -- Delete_Key_Sans_Free --
kono
parents:
diff changeset
65 --------------------------
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 procedure Delete_Key_Sans_Free
kono
parents:
diff changeset
68 (HT : in out Hash_Table_Type'Class;
kono
parents:
diff changeset
69 Key : Key_Type;
kono
parents:
diff changeset
70 X : out Count_Type)
kono
parents:
diff changeset
71 is
kono
parents:
diff changeset
72 Indx : Hash_Type;
kono
parents:
diff changeset
73 Prev : Count_Type;
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 begin
kono
parents:
diff changeset
76 if HT.Length = 0 then
kono
parents:
diff changeset
77 X := 0;
kono
parents:
diff changeset
78 return;
kono
parents:
diff changeset
79 end if;
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 -- Per AI05-0022, the container implementation is required to detect
kono
parents:
diff changeset
82 -- element tampering by a generic actual subprogram.
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 TC_Check (HT.TC);
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 Indx := Checked_Index (HT, Key);
kono
parents:
diff changeset
87 X := HT.Buckets (Indx);
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 if X = 0 then
kono
parents:
diff changeset
90 return;
kono
parents:
diff changeset
91 end if;
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 if Checked_Equivalent_Keys (HT, Key, X) then
kono
parents:
diff changeset
94 TC_Check (HT.TC);
kono
parents:
diff changeset
95 HT.Buckets (Indx) := Next (HT.Nodes (X));
kono
parents:
diff changeset
96 HT.Length := HT.Length - 1;
kono
parents:
diff changeset
97 return;
kono
parents:
diff changeset
98 end if;
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 loop
kono
parents:
diff changeset
101 Prev := X;
kono
parents:
diff changeset
102 X := Next (HT.Nodes (Prev));
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 if X = 0 then
kono
parents:
diff changeset
105 return;
kono
parents:
diff changeset
106 end if;
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 if Checked_Equivalent_Keys (HT, Key, X) then
kono
parents:
diff changeset
109 TC_Check (HT.TC);
kono
parents:
diff changeset
110 Set_Next (HT.Nodes (Prev), Next => Next (HT.Nodes (X)));
kono
parents:
diff changeset
111 HT.Length := HT.Length - 1;
kono
parents:
diff changeset
112 return;
kono
parents:
diff changeset
113 end if;
kono
parents:
diff changeset
114 end loop;
kono
parents:
diff changeset
115 end Delete_Key_Sans_Free;
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 ----------
kono
parents:
diff changeset
118 -- Find --
kono
parents:
diff changeset
119 ----------
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 function Find
kono
parents:
diff changeset
122 (HT : Hash_Table_Type'Class;
kono
parents:
diff changeset
123 Key : Key_Type) return Count_Type
kono
parents:
diff changeset
124 is
kono
parents:
diff changeset
125 Indx : Hash_Type;
kono
parents:
diff changeset
126 Node : Count_Type;
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 begin
kono
parents:
diff changeset
129 if HT.Length = 0 then
kono
parents:
diff changeset
130 return 0;
kono
parents:
diff changeset
131 end if;
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 Indx := Checked_Index (HT'Unrestricted_Access.all, Key);
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 Node := HT.Buckets (Indx);
kono
parents:
diff changeset
136 while Node /= 0 loop
kono
parents:
diff changeset
137 if Checked_Equivalent_Keys
kono
parents:
diff changeset
138 (HT'Unrestricted_Access.all, Key, Node)
kono
parents:
diff changeset
139 then
kono
parents:
diff changeset
140 return Node;
kono
parents:
diff changeset
141 end if;
kono
parents:
diff changeset
142 Node := Next (HT.Nodes (Node));
kono
parents:
diff changeset
143 end loop;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 return 0;
kono
parents:
diff changeset
146 end Find;
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 --------------------------------
kono
parents:
diff changeset
149 -- Generic_Conditional_Insert --
kono
parents:
diff changeset
150 --------------------------------
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 procedure Generic_Conditional_Insert
kono
parents:
diff changeset
153 (HT : in out Hash_Table_Type'Class;
kono
parents:
diff changeset
154 Key : Key_Type;
kono
parents:
diff changeset
155 Node : out Count_Type;
kono
parents:
diff changeset
156 Inserted : out Boolean)
kono
parents:
diff changeset
157 is
kono
parents:
diff changeset
158 Indx : Hash_Type;
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 begin
kono
parents:
diff changeset
161 -- Per AI05-0022, the container implementation is required to detect
kono
parents:
diff changeset
162 -- element tampering by a generic actual subprogram.
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 TC_Check (HT.TC);
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 Indx := Checked_Index (HT, Key);
kono
parents:
diff changeset
167 Node := HT.Buckets (Indx);
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 if Node = 0 then
kono
parents:
diff changeset
170 if Checks and then HT.Length = HT.Capacity then
kono
parents:
diff changeset
171 raise Capacity_Error with "no more capacity for insertion";
kono
parents:
diff changeset
172 end if;
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 Node := New_Node;
kono
parents:
diff changeset
175 Set_Next (HT.Nodes (Node), Next => 0);
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 Inserted := True;
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 HT.Buckets (Indx) := Node;
kono
parents:
diff changeset
180 HT.Length := HT.Length + 1;
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 return;
kono
parents:
diff changeset
183 end if;
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 loop
kono
parents:
diff changeset
186 if Checked_Equivalent_Keys (HT, Key, Node) then
kono
parents:
diff changeset
187 Inserted := False;
kono
parents:
diff changeset
188 return;
kono
parents:
diff changeset
189 end if;
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 Node := Next (HT.Nodes (Node));
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 exit when Node = 0;
kono
parents:
diff changeset
194 end loop;
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 if Checks and then HT.Length = HT.Capacity then
kono
parents:
diff changeset
197 raise Capacity_Error with "no more capacity for insertion";
kono
parents:
diff changeset
198 end if;
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 Node := New_Node;
kono
parents:
diff changeset
201 Set_Next (HT.Nodes (Node), Next => HT.Buckets (Indx));
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 Inserted := True;
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 HT.Buckets (Indx) := Node;
kono
parents:
diff changeset
206 HT.Length := HT.Length + 1;
kono
parents:
diff changeset
207 end Generic_Conditional_Insert;
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 -----------------------------
kono
parents:
diff changeset
210 -- Generic_Replace_Element --
kono
parents:
diff changeset
211 -----------------------------
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 procedure Generic_Replace_Element
kono
parents:
diff changeset
214 (HT : in out Hash_Table_Type'Class;
kono
parents:
diff changeset
215 Node : Count_Type;
kono
parents:
diff changeset
216 Key : Key_Type)
kono
parents:
diff changeset
217 is
kono
parents:
diff changeset
218 pragma Assert (HT.Length > 0);
kono
parents:
diff changeset
219 pragma Assert (Node /= 0);
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 BB : Buckets_Type renames HT.Buckets;
kono
parents:
diff changeset
222 NN : Nodes_Type renames HT.Nodes;
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 Old_Indx : Hash_Type;
kono
parents:
diff changeset
225 New_Indx : constant Hash_Type := Checked_Index (HT, Key);
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 New_Bucket : Count_Type renames BB (New_Indx);
kono
parents:
diff changeset
228 N, M : Count_Type;
kono
parents:
diff changeset
229
kono
parents:
diff changeset
230 begin
kono
parents:
diff changeset
231 -- Per AI05-0022, the container implementation is required to detect
kono
parents:
diff changeset
232 -- element tampering by a generic actual subprogram.
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 -- The following block appears to be vestigial -- this should be done
kono
parents:
diff changeset
235 -- using Checked_Index instead. Also, we might have to move the actual
kono
parents:
diff changeset
236 -- tampering checks to the top of the subprogram, in order to prevent
kono
parents:
diff changeset
237 -- infinite recursion when calling Hash. (This is similar to how Insert
kono
parents:
diff changeset
238 -- and Delete are implemented.) This implies that we will have to defer
kono
parents:
diff changeset
239 -- the computation of New_Index until after the tampering check. ???
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 declare
kono
parents:
diff changeset
242 Lock : With_Lock (HT.TC'Unrestricted_Access);
kono
parents:
diff changeset
243 begin
kono
parents:
diff changeset
244 Old_Indx := HT.Buckets'First + Hash (NN (Node)) mod HT.Buckets'Length;
kono
parents:
diff changeset
245 end;
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 -- Replace_Element is allowed to change a node's key to Key
kono
parents:
diff changeset
248 -- (generic formal operation Assign provides the mechanism), but
kono
parents:
diff changeset
249 -- only if Key is not already in the hash table. (In a unique-key
kono
parents:
diff changeset
250 -- hash table as this one, a key is mapped to exactly one node.)
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 if Checked_Equivalent_Keys (HT, Key, Node) then
kono
parents:
diff changeset
253 TE_Check (HT.TC);
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 -- The new Key value is mapped to this same Node, so Node
kono
parents:
diff changeset
256 -- stays in the same bucket.
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 Assign (NN (Node), Key);
kono
parents:
diff changeset
259 return;
kono
parents:
diff changeset
260 end if;
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 -- Key is not equivalent to Node, so we now have to determine if it's
kono
parents:
diff changeset
263 -- equivalent to some other node in the hash table. This is the case
kono
parents:
diff changeset
264 -- irrespective of whether Key is in the same or a different bucket from
kono
parents:
diff changeset
265 -- Node.
kono
parents:
diff changeset
266
kono
parents:
diff changeset
267 N := New_Bucket;
kono
parents:
diff changeset
268 while N /= 0 loop
kono
parents:
diff changeset
269 if Checks and then Checked_Equivalent_Keys (HT, Key, N) then
kono
parents:
diff changeset
270 pragma Assert (N /= Node);
kono
parents:
diff changeset
271 raise Program_Error with
kono
parents:
diff changeset
272 "attempt to replace existing element";
kono
parents:
diff changeset
273 end if;
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 N := Next (NN (N));
kono
parents:
diff changeset
276 end loop;
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 -- We have determined that Key is not already in the hash table, so
kono
parents:
diff changeset
279 -- the change is tentatively allowed. We now perform the standard
kono
parents:
diff changeset
280 -- checks to determine whether the hash table is locked (because you
kono
parents:
diff changeset
281 -- cannot change an element while it's in use by Query_Element or
kono
parents:
diff changeset
282 -- Update_Element), or if the container is busy (because moving a
kono
parents:
diff changeset
283 -- node to a different bucket would interfere with iteration).
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 if Old_Indx = New_Indx then
kono
parents:
diff changeset
286 -- The node is already in the bucket implied by Key. In this case
kono
parents:
diff changeset
287 -- we merely change its value without moving it.
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 TE_Check (HT.TC);
kono
parents:
diff changeset
290
kono
parents:
diff changeset
291 Assign (NN (Node), Key);
kono
parents:
diff changeset
292 return;
kono
parents:
diff changeset
293 end if;
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 -- The node is a bucket different from the bucket implied by Key
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 TC_Check (HT.TC);
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 -- Do the assignment first, before moving the node, so that if Assign
kono
parents:
diff changeset
300 -- propagates an exception, then the hash table will not have been
kono
parents:
diff changeset
301 -- modified (except for any possible side-effect Assign had on Node).
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 Assign (NN (Node), Key);
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 -- Now we can safely remove the node from its current bucket
kono
parents:
diff changeset
306
kono
parents:
diff changeset
307 N := BB (Old_Indx); -- get value of first node in old bucket
kono
parents:
diff changeset
308 pragma Assert (N /= 0);
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 if N = Node then -- node is first node in its bucket
kono
parents:
diff changeset
311 BB (Old_Indx) := Next (NN (Node));
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 else
kono
parents:
diff changeset
314 pragma Assert (HT.Length > 1);
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 loop
kono
parents:
diff changeset
317 M := Next (NN (N));
kono
parents:
diff changeset
318 pragma Assert (M /= 0);
kono
parents:
diff changeset
319
kono
parents:
diff changeset
320 if M = Node then
kono
parents:
diff changeset
321 Set_Next (NN (N), Next => Next (NN (Node)));
kono
parents:
diff changeset
322 exit;
kono
parents:
diff changeset
323 end if;
kono
parents:
diff changeset
324
kono
parents:
diff changeset
325 N := M;
kono
parents:
diff changeset
326 end loop;
kono
parents:
diff changeset
327 end if;
kono
parents:
diff changeset
328
kono
parents:
diff changeset
329 -- Now we link the node into its new bucket (corresponding to Key)
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 Set_Next (NN (Node), Next => New_Bucket);
kono
parents:
diff changeset
332 New_Bucket := Node;
kono
parents:
diff changeset
333 end Generic_Replace_Element;
kono
parents:
diff changeset
334
kono
parents:
diff changeset
335 -----------
kono
parents:
diff changeset
336 -- Index --
kono
parents:
diff changeset
337 -----------
kono
parents:
diff changeset
338
kono
parents:
diff changeset
339 function Index
kono
parents:
diff changeset
340 (HT : Hash_Table_Type'Class;
kono
parents:
diff changeset
341 Key : Key_Type) return Hash_Type is
kono
parents:
diff changeset
342 begin
kono
parents:
diff changeset
343 return HT.Buckets'First + Hash (Key) mod HT.Buckets'Length;
kono
parents:
diff changeset
344 end Index;
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 end Ada.Containers.Hash_Tables.Generic_Bounded_Keys;