1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . D Y N A M I C _ H T A B L E S --
9 -- Copyright (C) 2002-2015, AdaCore --
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 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada
.Unchecked_Deallocation
;
34 package body GNAT
.Dynamic_HTables
is
40 package body Static_HTable
is
42 type Table_Type
is array (Header_Num
) of Elmt_Ptr
;
44 type Instance_Data
is record
46 Iterator_Index
: Header_Num
;
47 Iterator_Ptr
: Elmt_Ptr
;
48 Iterator_Started
: Boolean := False;
51 function Get_Non_Null
(T
: Instance
) return Elmt_Ptr
;
52 -- Returns Null_Ptr if Iterator_Started is False or if the Table is
53 -- empty. Returns Iterator_Ptr if non null, or the next non null
54 -- element in table if any.
60 function Get
(T
: Instance
; K
: Key
) return Elmt_Ptr
is
68 Elmt
:= T
.Table
(Hash
(K
));
71 if Elmt
= Null_Ptr
then
74 elsif Equal
(Get_Key
(Elmt
), K
) then
87 function Get_First
(T
: Instance
) return Elmt_Ptr
is
93 T
.Iterator_Started
:= True;
94 T
.Iterator_Index
:= T
.Table
'First;
95 T
.Iterator_Ptr
:= T
.Table
(T
.Iterator_Index
);
96 return Get_Non_Null
(T
);
103 function Get_Next
(T
: Instance
) return Elmt_Ptr
is
105 if T
= null or else not T
.Iterator_Started
then
109 T
.Iterator_Ptr
:= Next
(T
.Iterator_Ptr
);
110 return Get_Non_Null
(T
);
117 function Get_Non_Null
(T
: Instance
) return Elmt_Ptr
is
123 while T
.Iterator_Ptr
= Null_Ptr
loop
124 if T
.Iterator_Index
= T
.Table
'Last then
125 T
.Iterator_Started
:= False;
129 T
.Iterator_Index
:= T
.Iterator_Index
+ 1;
130 T
.Iterator_Ptr
:= T
.Table
(T
.Iterator_Index
);
133 return T
.Iterator_Ptr
;
140 procedure Remove
(T
: Instance
; K
: Key
) is
141 Index
: constant Header_Num
:= Hash
(K
);
143 Next_Elmt
: Elmt_Ptr
;
150 Elmt
:= T
.Table
(Index
);
152 if Elmt
= Null_Ptr
then
155 elsif Equal
(Get_Key
(Elmt
), K
) then
156 T
.Table
(Index
) := Next
(Elmt
);
160 Next_Elmt
:= Next
(Elmt
);
162 if Next_Elmt
= Null_Ptr
then
165 elsif Equal
(Get_Key
(Next_Elmt
), K
) then
166 Set_Next
(Elmt
, Next
(Next_Elmt
));
180 procedure Reset
(T
: in out Instance
) is
182 new Ada
.Unchecked_Deallocation
(Instance_Data
, Instance
);
189 for J
in T
.Table
'Range loop
190 T
.Table
(J
) := Null_Ptr
;
200 procedure Set
(T
: in out Instance
; E
: Elmt_Ptr
) is
205 T
:= new Instance_Data
;
208 Index
:= Hash
(Get_Key
(E
));
209 Set_Next
(E
, T
.Table
(Index
));
210 T
.Table
(Index
) := E
;
219 package body Simple_HTable
is
220 procedure Free
is new
221 Ada
.Unchecked_Deallocation
(Element_Wrapper
, Elmt_Ptr
);
227 function Get
(T
: Instance
; K
: Key
) return Element
is
235 Tmp
:= Tab
.Get
(Tab
.Instance
(T
), K
);
248 function Get_First
(T
: Instance
) return Element
is
249 Tmp
: constant Elmt_Ptr
:= Tab
.Get_First
(Tab
.Instance
(T
));
263 function Get_Key
(E
: Elmt_Ptr
) return Key
is
272 function Get_Next
(T
: Instance
) return Element
is
273 Tmp
: constant Elmt_Ptr
:= Tab
.Get_Next
(Tab
.Instance
(T
));
286 function Next
(E
: Elmt_Ptr
) return Elmt_Ptr
is
295 procedure Remove
(T
: Instance
; K
: Key
) is
299 Tmp
:= Tab
.Get
(Tab
.Instance
(T
), K
);
302 Tab
.Remove
(Tab
.Instance
(T
), K
);
311 procedure Reset
(T
: in out Instance
) is
315 E1
:= Tab
.Get_First
(Tab
.Instance
(T
));
316 while E1
/= null loop
317 E2
:= Tab
.Get_Next
(Tab
.Instance
(T
));
322 Tab
.Reset
(Tab
.Instance
(T
));
329 procedure Set
(T
: in out Instance
; K
: Key
; E
: Element
) is
330 Tmp
: constant Elmt_Ptr
:= Tab
.Get
(Tab
.Instance
(T
), K
);
333 Tab
.Set
(Tab
.Instance
(T
), new Element_Wrapper
'(K, E, null));
343 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
350 ------------------------
351 -- Load_Factor_HTable --
352 ------------------------
354 package body Load_Factor_HTable is
356 Min_Size_Increase : constant := 5;
357 -- The minimum increase expressed as number of buckets. This value is
358 -- used to determine the new size of small tables and/or small growth
362 (Elmt : not null Element_Ptr;
363 Chain : not null Element_Ptr);
364 -- Prepend an element to a bucket chain. Elmt is inserted after the
365 -- dummy head of Chain.
367 function Create_Buckets (Size : Positive) return Buckets_Array_Ptr;
368 -- Allocate and initialize a new set of buckets. The buckets are created
369 -- in the range Range_Type'First .. Range_Type'First + Size - 1.
371 procedure Detach (Elmt : not null Element_Ptr);
372 -- Remove an element from an arbitrary bucket chain
376 Chain : not null Element_Ptr) return Element_Ptr;
377 -- Try to locate the element which contains a particular key within a
378 -- bucket chain. If no such element exists, return No_Element.
381 new Ada.Unchecked_Deallocation (Buckets_Array, Buckets_Array_Ptr);
384 new Ada.Unchecked_Deallocation (Element, Element_Ptr);
386 function Is_Empty_Chain (Chain : not null Element_Ptr) return Boolean;
387 -- Determine whether a bucket chain contains only one element, namely
395 (Elmt : not null Element_Ptr;
396 Chain : not null Element_Ptr)
399 Chain.Next.Prev := Elmt;
400 Elmt.Next := Chain.Next;
409 function Create_Buckets (Size : Positive) return Buckets_Array_Ptr is
410 Low_Bound : constant Range_Type := Range_Type'First;
411 Buckets : Buckets_Array_Ptr;
415 new Buckets_Array (Low_Bound .. Low_Bound + Range_Type (Size) - 1);
417 -- Ensure that the dummy head of each bucket chain points to itself
418 -- in both directions.
420 for Index in Buckets'Range loop
422 Bucket : Element renames Buckets (Index);
425 Bucket.Prev := Bucket'Unchecked_Access;
426 Bucket.Next := Bucket'Unchecked_Access;
437 function Current_Size (T : Table) return Positive is
439 -- The table should have been properly initialized during object
442 if T.Buckets = null then
445 -- The size of the table is determined by the number of buckets
448 return T.Buckets'Length;
456 procedure Detach (Elmt : not null Element_Ptr) is
458 if Elmt.Prev /= null and Elmt.Next /= null then
459 Elmt.Prev.Next := Elmt.Next;
460 Elmt.Next.Prev := Elmt.Prev;
470 procedure Finalize (T : in out Table) is
471 Bucket : Element_Ptr;
475 -- Inspect the buckets and deallocate bucket chains
477 for Index in T.Buckets'Range loop
478 Bucket := T.Buckets (Index)'Unchecked_Access;
480 -- The current bucket chain contains an element other than the
483 while not Is_Empty_Chain (Bucket) loop
485 -- Skip the dummy head, remove and deallocate the element
493 -- Deallocate the buckets
504 Chain : not null Element_Ptr) return Element_Ptr
509 -- Skip the dummy head, inspect the bucket chain for an element whose
510 -- key matches the requested key. Since each bucket chain is circular
511 -- the search must stop once the dummy head is encountered.
514 while Elmt /= Chain loop
515 if Equal (Elmt.Key, Key) then
529 function Get (T : Table; Key : Key_Type) return Value_Type is
530 Bucket : Element_Ptr;
534 -- Obtain the bucket chain where the (key, value) pair should reside
535 -- by calculating the proper hash location.
537 Bucket := T.Buckets (Hash (Key, Current_Size (T)))'Unchecked_Access;
539 -- Try to find an element whose key matches the requested key
541 Elmt := Find (Key, Bucket);
543 -- The hash table does not contain a matching (key, value) pair
545 if Elmt = No_Element then
556 procedure Initialize (T : in out Table) is
558 pragma Assert (T.Buckets = null);
560 T.Buckets := Create_Buckets (Initial_Size);
561 T.Element_Count := 0;
568 function Is_Empty_Chain (Chain : not null Element_Ptr) return Boolean is
570 return Chain.Next = Chain and Chain.Prev = Chain;
577 procedure Remove (T : in out Table; Key : Key_Type) is
578 Bucket : Element_Ptr;
582 -- Obtain the bucket chain where the (key, value) pair should reside
583 -- by calculating the proper hash location.
585 Bucket := T.Buckets (Hash (Key, Current_Size (T)))'Unchecked_Access;
587 -- Try to find an element whose key matches the requested key
589 Elmt := Find (Key, Bucket);
591 -- Remove and deallocate the (key, value) pair
593 if Elmt /= No_Element then
608 Curr_Size : constant Positive := Current_Size (T);
611 -- Grow the table to a new size according to the desired percentage
612 -- and relocate all existing elements to the new buckets.
619 Buckets : Buckets_Array_Ptr;
621 Hash_Loc : Range_Type;
622 Old_Bucket : Element_Ptr;
623 Old_Buckets : Buckets_Array_Ptr := T.Buckets;
627 -- Calculate the new size and allocate a new set of buckets. Note
628 -- that a table with a small size or a small growth percentage may
629 -- not always grow (for example, 10 buckets and 3% increase). In
630 -- that case, enforce a minimum increase.
633 Positive'Max (Curr_Size * ((100 + Growth_Percentage) / 100),
635 Buckets := Create_Buckets (Size);
637 -- Inspect the old buckets and transfer all elements by rehashing
638 -- all (key, value) pairs in the new buckets.
640 for Index in Old_Buckets'Range loop
641 Old_Bucket := Old_Buckets (Index)'Unchecked_Access;
643 -- The current bucket chain contains an element other than the
646 while not Is_Empty_Chain (Old_Bucket) loop
648 -- Skip the dummy head and find the new hash location
650 Elmt := Old_Bucket.Next;
651 Hash_Loc := Hash (Elmt.Key, Size);
653 -- Remove the element from the old buckets and insert it
654 -- into the new buckets. Note that there is no need to check
655 -- for duplicates because the hash table did not have any to
661 Chain => Buckets (Hash_Loc)'Unchecked_Access);
665 -- Associate the new buckets with the table and reclaim the
666 -- storage occupied by the old buckets.
668 T.Buckets := Buckets;
675 subtype LLF is Long_Long_Float;
677 Count : Natural renames T.Element_Count;
678 Bucket : Element_Ptr;
679 Hash_Loc : Range_Type;
681 -- Start of processing for Set
684 -- Find the bucket where the (key, value) pair should be inserted by
685 -- computing the proper hash location.
687 Hash_Loc := Hash (Key, Curr_Size);
688 Bucket := T.Buckets (Hash_Loc)'Unchecked_Access;
690 -- Ensure that the key is not already present in the bucket in order
691 -- to avoid duplicates.
693 if Find (Key, Bucket) = No_Element then
695 (Elmt => new Element'(Key
, Val
, null, null),
699 -- Multiple insertions may cause long bucket chains and decrease
700 -- the performance of basic operations. If this is the case, grow
701 -- the table and rehash all existing elements.
703 if (LLF
(Count
) / LLF
(Curr_Size
)) > LLF
(Load_Factor
) then
708 end Load_Factor_HTable
;
710 end GNAT
.Dynamic_HTables
;