1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . H T A B L E --
9 -- Copyright (C) 1995-2024, 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 -- Hash table searching routines
34 -- This package contains two separate packages. The Simple_HTable package
35 -- provides a very simple abstraction that associates one element to one
36 -- key value and takes care of all allocations automatically using the heap.
37 -- The Static_HTable package provides a more complex interface that allows
38 -- complete control over allocation.
40 package System
.HTable
is
47 -- A simple hash table abstraction, easy to instantiate, easy to use.
48 -- The table associates one element to one key with the procedure Set.
49 -- Get retrieves the Element stored for a given Key. The efficiency of
50 -- retrieval is function of the size of the Table parameterized by
51 -- Header_Num and the hashing function Hash.
54 type Header_Num
is range <>;
55 -- An integer type indicating the number and range of hash headers
57 type Element
is private;
58 -- The type of element to be stored
61 -- The object that is returned by Get when no element has been set for
65 with function Hash
(F
: Key
) return Header_Num
;
66 with function Equal
(F1
, F2
: Key
) return Boolean;
68 package Simple_HTable
is
70 procedure Set
(K
: Key
; E
: Element
);
71 -- Associates an element with a given key. Overrides any previously
72 -- associated element.
75 -- Removes and frees all elements in the table
77 function Get
(K
: Key
) return Element
;
78 -- Returns the Element associated with a key or No_Element if the
79 -- given key has no associated element.
81 procedure Remove
(K
: Key
);
82 -- Removes the latest inserted element pointer associated with the
83 -- given key if any, does nothing if none.
85 function Get_First
return Element
;
86 -- Returns No_Element if the HTable is empty, otherwise returns one
87 -- non specified element. There is no guarantee that two calls to this
88 -- function will return the same element.
90 function Get_Next
return Element
;
91 -- Returns a non-specified element that has not been returned by the
92 -- same function since the last call to Get_First or No_Element if
93 -- there is no such element. If there is no call to Set in between
94 -- Get_Next calls, all the elements of the HTable will be traversed.
96 procedure Get_First
(K
: in out Key
; E
: out Element
);
97 -- This version of the iterator returns a key/element pair. A non-
98 -- specified entry is returned, and there is no guarantee that two
99 -- calls to this procedure will return the same element. If the table
100 -- is empty, E is set to No_Element, and K is unchanged, otherwise
101 -- K and E are set to the first returned entry.
103 procedure Get_Next
(K
: in out Key
; E
: out Element
);
104 -- This version of the iterator returns a key/element pair. It returns
105 -- a non-specified element that has not been returned since the last
106 -- call to Get_First. If there is no remaining element, then E is set
107 -- to No_Element, and the value in K is unchanged, otherwise K and E
108 -- are set to the next entry. If there is no call to Set in between
109 -- Get_Next calls, all the elements of the HTable will be traversed.
117 -- A low-level Hash-Table abstraction, not as easy to instantiate as
118 -- Simple_HTable but designed to allow complete control over the
119 -- allocation of necessary data structures. Particularly useful when
120 -- dynamic allocation is not desired. The model is that each Element
121 -- contains its own Key that can be retrieved by Get_Key. Furthermore,
122 -- Element provides a link that can be used by the HTable for linking
123 -- elements with same hash codes:
127 -- +-------------------+
129 -- +-------------------+
131 -- +-------------------+
133 -- +-------------------+
136 type Header_Num
is range <>;
137 -- An integer type indicating the number and range of hash headers
139 type Element
(<>) is limited private;
140 -- The type of element to be stored. This is historically part of the
141 -- interface, even though it is not used at all in the operations of
144 pragma Warnings
(Off
, Element
);
145 -- We have to kill warnings here, because Element is and always
146 -- has been unreferenced, but we cannot remove it at this stage,
147 -- since this unit is in wide use, and it certainly seems harmless.
149 type Elmt_Ptr
is private;
150 -- The type used to reference an element (will usually be an access
151 -- type, but could be some other form of type such as an integer type).
154 -- The null value of the Elmt_Ptr type
156 with procedure Set_Next
(E
: Elmt_Ptr
; Next
: Elmt_Ptr
);
157 with function Next
(E
: Elmt_Ptr
) return Elmt_Ptr
;
158 -- The type must provide an internal link for the sake of the
159 -- staticness of the HTable.
161 type Key
is limited private;
162 with function Get_Key
(E
: Elmt_Ptr
) return Key
;
163 with function Hash
(F
: Key
) return Header_Num
;
164 with function Equal
(F1
, F2
: Key
) return Boolean;
166 package Static_HTable
is
169 -- Resets the hash table by setting all its elements to Null_Ptr. The
170 -- effect is to clear the hash table so that it can be reused. For the
171 -- most common case where Elmt_Ptr is an access type, and Null_Ptr is
172 -- null, this is only needed if the same table is reused in a new
173 -- context. If Elmt_Ptr is other than an access type, or Null_Ptr is
174 -- other than null, then Reset must be called before the first use
175 -- of the hash table.
177 procedure Set
(E
: Elmt_Ptr
);
178 -- Insert the element pointer in the HTable
180 function Get
(K
: Key
) return Elmt_Ptr
;
181 -- Returns the latest inserted element pointer with the given Key
184 function Present
(K
: Key
) return Boolean;
185 -- True if an element whose Get_Key is K is in the table
187 function Set_If_Not_Present
(E
: Elmt_Ptr
) return Boolean;
188 -- If Present (Get_Key (E)), returns False. Otherwise, does Set (E), and
189 -- then returns True. Present (Get_Key (E)) is always True afterward,
190 -- and the result True indicates E is newly Set.
192 procedure Remove
(K
: Key
);
193 -- Removes the latest inserted element pointer associated with the
194 -- given key if any, does nothing if none.
196 function Get_First
return Elmt_Ptr
;
197 -- Returns Null_Ptr if the HTable is empty, otherwise returns one
198 -- non specified element. There is no guarantee that two calls to this
199 -- function will return the same element.
201 function Get_Next
return Elmt_Ptr
;
202 -- Returns a non-specified element that has not been returned by the
203 -- same function since the last call to Get_First or Null_Ptr if
204 -- there is no such element or Get_First has never been called. If
205 -- there is no call to 'Set' in between Get_Next calls, all the
206 -- elements of the HTable will be traversed.
214 -- A generic hashing function working on String keys
217 type Header_Num
is range <>;
218 function Hash
(Key
: String) return Header_Num
;