1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.HASH_TABLES.GENERIC_BOUNDED_OPERATIONS --
9 -- Copyright (C) 2004-2011, 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 with System
; use type System
.Address
;
32 package body Ada
.Containers
.Hash_Tables
.Generic_Bounded_Operations
is
38 procedure Clear
(HT
: in out Hash_Table_Type
'Class) is
41 raise Program_Error
with
42 "attempt to tamper with cursors (container is busy)";
49 HT
.Buckets
:= (others => 0); -- optimize this somehow ???
52 ---------------------------
53 -- Delete_Node_Sans_Free --
54 ---------------------------
56 procedure Delete_Node_Sans_Free
57 (HT
: in out Hash_Table_Type
'Class;
60 pragma Assert
(X
/= 0);
68 raise Program_Error
with
69 "attempt to delete node from empty hashed container";
72 Indx
:= Index
(HT
, HT
.Nodes
(X
));
73 Prev
:= HT
.Buckets
(Indx
);
76 raise Program_Error
with
77 "attempt to delete node from empty hash bucket";
81 HT
.Buckets
(Indx
) := Next
(HT
.Nodes
(Prev
));
82 HT
.Length
:= HT
.Length
- 1;
87 raise Program_Error
with
88 "attempt to delete node not in its proper hash bucket";
92 Curr
:= Next
(HT
.Nodes
(Prev
));
95 raise Program_Error
with
96 "attempt to delete node not in its proper hash bucket";
100 Set_Next
(HT
.Nodes
(Prev
), Next
=> Next
(HT
.Nodes
(Curr
)));
101 HT
.Length
:= HT
.Length
- 1;
107 end Delete_Node_Sans_Free
;
113 function First
(HT
: Hash_Table_Type
'Class) return Count_Type
is
117 if HT
.Length
= 0 then
121 Indx
:= HT
.Buckets
'First;
123 if HT
.Buckets
(Indx
) /= 0 then
124 return HT
.Buckets
(Indx
);
136 (HT
: in out Hash_Table_Type
'Class;
139 N
: Nodes_Type
renames HT
.Nodes
;
142 -- This subprogram "deallocates" a node by relinking the node off of the
143 -- active list and onto the free list. Previously it would flag index
144 -- value 0 as an error. The precondition was weakened, so that index
145 -- value 0 is now allowed, and this value is interpreted to mean "do
146 -- nothing". This makes its behavior analogous to the behavior of
147 -- Ada.Unchecked_Deallocation, and allows callers to avoid having to add
148 -- special-case checks at the point of call.
154 pragma Assert
(X
<= HT
.Capacity
);
156 -- pragma Assert (N (X).Prev >= 0); -- node is active
157 -- Find a way to mark a node as active vs. inactive; we could
158 -- use a special value in Color_Type for this. ???
160 -- The hash table actually contains two data structures: a list for
161 -- the "active" nodes that contain elements that have been inserted
162 -- onto the container, and another for the "inactive" nodes of the free
165 -- We desire that merely declaring an object should have only minimal
166 -- cost; specially, we want to avoid having to initialize the free
167 -- store (to fill in the links), especially if the capacity is large.
169 -- The head of the free list is indicated by Container.Free. If its
170 -- value is non-negative, then the free store has been initialized
171 -- in the "normal" way: Container.Free points to the head of the list
172 -- of free (inactive) nodes, and the value 0 means the free list is
173 -- empty. Each node on the free list has been initialized to point
174 -- to the next free node (via its Parent component), and the value 0
175 -- means that this is the last free node.
177 -- If Container.Free is negative, then the links on the free store
178 -- have not been initialized. In this case the link values are
179 -- implied: the free store comprises the components of the node array
180 -- started with the absolute value of Container.Free, and continuing
181 -- until the end of the array (Nodes'Last).
184 -- It might be possible to perform an optimization here. Suppose that
185 -- the free store can be represented as having two parts: one
186 -- comprising the non-contiguous inactive nodes linked together
187 -- in the normal way, and the other comprising the contiguous
188 -- inactive nodes (that are not linked together, at the end of the
189 -- nodes array). This would allow us to never have to initialize
190 -- the free store, except in a lazy way as nodes become inactive.
192 -- When an element is deleted from the list container, its node
193 -- becomes inactive, and so we set its Next component to value of
194 -- the node's index (in the nodes array), to indicate that it is
195 -- now inactive. This provides a useful way to detect a dangling
196 -- cursor reference. ???
198 Set_Next
(N
(X
), Next
=> X
); -- Node is deallocated (not on active list)
201 -- The free store has previously been initialized. All we need to
202 -- do here is link the newly-free'd node onto the free list.
204 Set_Next
(N
(X
), HT
.Free
);
207 elsif X
+ 1 = abs HT
.Free
then
208 -- The free store has not been initialized, and the node becoming
209 -- inactive immediately precedes the start of the free store. All
210 -- we need to do is move the start of the free store back by one.
212 HT
.Free
:= HT
.Free
+ 1;
215 -- The free store has not been initialized, and the node becoming
216 -- inactive does not immediately precede the free store. Here we
217 -- first initialize the free store (meaning the links are given
218 -- values in the traditional way), and then link the newly-free'd
219 -- node onto the head of the free store.
222 -- See the comments above for an optimization opportunity. If
223 -- the next link for a node on the free store is negative, then
224 -- this means the remaining nodes on the free store are
225 -- physically contiguous, starting as the absolute value of
228 HT
.Free
:= abs HT
.Free
;
230 if HT
.Free
> HT
.Capacity
then
234 for I
in HT
.Free
.. HT
.Capacity
- 1 loop
235 Set_Next
(Node
=> N
(I
), Next
=> I
+ 1);
238 Set_Next
(Node
=> N
(HT
.Capacity
), Next
=> 0);
241 Set_Next
(Node
=> N
(X
), Next
=> HT
.Free
);
246 ----------------------
247 -- Generic_Allocate --
248 ----------------------
250 procedure Generic_Allocate
251 (HT
: in out Hash_Table_Type
'Class;
252 Node
: out Count_Type
)
254 N
: Nodes_Type
renames HT
.Nodes
;
260 -- We always perform the assignment first, before we
261 -- change container state, in order to defend against
262 -- exceptions duration assignment.
264 Set_Element
(N
(Node
));
265 HT
.Free
:= Next
(N
(Node
));
268 -- A negative free store value means that the links of the nodes
269 -- in the free store have not been initialized. In this case, the
270 -- nodes are physically contiguous in the array, starting at the
271 -- index that is the absolute value of the Container.Free, and
272 -- continuing until the end of the array (Nodes'Last).
276 -- As above, we perform this assignment first, before modifying
277 -- any container state.
279 Set_Element
(N
(Node
));
280 HT
.Free
:= HT
.Free
- 1;
282 end Generic_Allocate
;
288 function Generic_Equal
289 (L
, R
: Hash_Table_Type
'Class) return Boolean
297 if L
'Address = R
'Address then
301 if L
.Length
/= R
.Length
then
309 -- Find the first node of hash table L
311 L_Index
:= L
.Buckets
'First;
313 L_Node
:= L
.Buckets
(L_Index
);
314 exit when L_Node
/= 0;
315 L_Index
:= L_Index
+ 1;
318 -- For each node of hash table L, search for an equivalent node in hash
323 if not Find
(HT
=> R
, Key
=> L
.Nodes
(L_Node
)) then
329 L_Node
:= Next
(L
.Nodes
(L_Node
));
332 -- We have exhausted the nodes in this bucket
338 -- Find the next bucket
341 L_Index
:= L_Index
+ 1;
342 L_Node
:= L
.Buckets
(L_Index
);
343 exit when L_Node
/= 0;
349 -----------------------
350 -- Generic_Iteration --
351 -----------------------
353 procedure Generic_Iteration
(HT
: Hash_Table_Type
'Class) is
357 if HT
.Length
= 0 then
361 for Indx
in HT
.Buckets
'Range loop
362 Node
:= HT
.Buckets
(Indx
);
365 Node
:= Next
(HT
.Nodes
(Node
));
368 end Generic_Iteration
;
374 procedure Generic_Read
375 (Stream
: not null access Root_Stream_Type
'Class;
376 HT
: out Hash_Table_Type
'Class)
383 Count_Type
'Base'Read (Stream, N);
386 raise Program_Error with "stream appears to be corrupt";
393 if N > HT.Capacity then
394 raise Capacity_Error with "too many elements in stream";
399 Node : constant Count_Type := New_Node (Stream);
400 Indx : constant Hash_Type := Index (HT, HT.Nodes (Node));
401 B : Count_Type renames HT.Buckets (Indx);
403 Set_Next (HT.Nodes (Node), Next => B);
407 HT.Length := HT.Length + 1;
415 procedure Generic_Write
416 (Stream : not null access Root_Stream_Type'Class;
417 HT : Hash_Table_Type'Class)
419 procedure Write (Node : Count_Type);
420 pragma Inline (Write);
422 procedure Write is new Generic_Iteration (Write);
428 procedure Write (Node : Count_Type) is
430 Write (Stream, HT.Nodes (Node));
434 Count_Type'Base'Write
(Stream
, HT
.Length
);
443 (Buckets
: Buckets_Type
;
444 Node
: Node_Type
) return Hash_Type
is
446 return Buckets
'First + Hash_Node
(Node
) mod Buckets
'Length;
450 (HT
: Hash_Table_Type
'Class;
451 Node
: Node_Type
) return Hash_Type
is
453 return Index
(HT
.Buckets
, Node
);
461 (HT
: Hash_Table_Type
'Class;
462 Node
: Count_Type
) return Count_Type
464 Result
: Count_Type
:= Next
(HT
.Nodes
(Node
));
467 if Result
/= 0 then -- another node in same bucket
471 -- This was the last node in the bucket, so move to the next
472 -- bucket, and start searching for next node from there.
474 for Indx
in Index
(HT
, HT
.Nodes
(Node
)) + 1 .. HT
.Buckets
'Last loop
475 Result
:= HT
.Buckets
(Indx
);
477 if Result
/= 0 then -- bucket is not empty
485 end Ada
.Containers
.Hash_Tables
.Generic_Bounded_Operations
;