1 |
706 |
jeremybenn |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT RUN-TIME COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- S Y S T E M . H T A B L E --
|
6 |
|
|
-- --
|
7 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 1995-2011, AdaCore --
|
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 |
|
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
28 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
29 |
|
|
-- --
|
30 |
|
|
------------------------------------------------------------------------------
|
31 |
|
|
|
32 |
|
|
-- Hash table searching routines
|
33 |
|
|
|
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.
|
39 |
|
|
|
40 |
|
|
pragma Compiler_Unit;
|
41 |
|
|
|
42 |
|
|
package System.HTable is
|
43 |
|
|
pragma Preelaborate;
|
44 |
|
|
|
45 |
|
|
-------------------
|
46 |
|
|
-- Simple_HTable --
|
47 |
|
|
-------------------
|
48 |
|
|
|
49 |
|
|
-- A simple hash table abstraction, easy to instantiate, easy to use.
|
50 |
|
|
-- The table associates one element to one key with the procedure Set.
|
51 |
|
|
-- Get retrieves the Element stored for a given Key. The efficiency of
|
52 |
|
|
-- retrieval is function of the size of the Table parameterized by
|
53 |
|
|
-- Header_Num and the hashing function Hash.
|
54 |
|
|
|
55 |
|
|
generic
|
56 |
|
|
type Header_Num is range <>;
|
57 |
|
|
-- An integer type indicating the number and range of hash headers
|
58 |
|
|
|
59 |
|
|
type Element is private;
|
60 |
|
|
-- The type of element to be stored
|
61 |
|
|
|
62 |
|
|
No_Element : Element;
|
63 |
|
|
-- The object that is returned by Get when no element has been set for
|
64 |
|
|
-- a given key
|
65 |
|
|
|
66 |
|
|
type Key is private;
|
67 |
|
|
with function Hash (F : Key) return Header_Num;
|
68 |
|
|
with function Equal (F1, F2 : Key) return Boolean;
|
69 |
|
|
|
70 |
|
|
package Simple_HTable is
|
71 |
|
|
|
72 |
|
|
procedure Set (K : Key; E : Element);
|
73 |
|
|
-- Associates an element with a given key. Overrides any previously
|
74 |
|
|
-- associated element.
|
75 |
|
|
|
76 |
|
|
procedure Reset;
|
77 |
|
|
-- Removes and frees all elements in the table
|
78 |
|
|
|
79 |
|
|
function Get (K : Key) return Element;
|
80 |
|
|
-- Returns the Element associated with a key or No_Element if the
|
81 |
|
|
-- given key has no associated element.
|
82 |
|
|
|
83 |
|
|
procedure Remove (K : Key);
|
84 |
|
|
-- Removes the latest inserted element pointer associated with the
|
85 |
|
|
-- given key if any, does nothing if none.
|
86 |
|
|
|
87 |
|
|
function Get_First return Element;
|
88 |
|
|
-- Returns No_Element if the HTable is empty, otherwise returns one
|
89 |
|
|
-- non specified element. There is no guarantee that two calls to this
|
90 |
|
|
-- function will return the same element.
|
91 |
|
|
|
92 |
|
|
function Get_Next return Element;
|
93 |
|
|
-- Returns a non-specified element that has not been returned by the
|
94 |
|
|
-- same function since the last call to Get_First or No_Element if
|
95 |
|
|
-- there is no such element. If there is no call to Set in between
|
96 |
|
|
-- Get_Next calls, all the elements of the HTable will be traversed.
|
97 |
|
|
|
98 |
|
|
procedure Get_First (K : in out Key; E : out Element);
|
99 |
|
|
-- This version of the iterator returns a key/element pair. A non-
|
100 |
|
|
-- specified entry is returned, and there is no guarantee that two
|
101 |
|
|
-- calls to this procedure will return the same element. If the table
|
102 |
|
|
-- is empty, E is set to No_Element, and K is unchanged, otherwise
|
103 |
|
|
-- K and E are set to the first returned entry.
|
104 |
|
|
|
105 |
|
|
procedure Get_Next (K : in out Key; E : out Element);
|
106 |
|
|
-- This version of the iterator returns a key/element pair. It returns
|
107 |
|
|
-- a non-specified element that has not been returned since the last
|
108 |
|
|
-- call to Get_First. If there is no remaining element, then E is set
|
109 |
|
|
-- to No_Element, and the value in K is unchanged, otherwise K and E
|
110 |
|
|
-- are set to the next entry. If there is no call to Set in between
|
111 |
|
|
-- Get_Next calls, all the elements of the HTable will be traversed.
|
112 |
|
|
|
113 |
|
|
end Simple_HTable;
|
114 |
|
|
|
115 |
|
|
-------------------
|
116 |
|
|
-- Static_HTable --
|
117 |
|
|
-------------------
|
118 |
|
|
|
119 |
|
|
-- A low-level Hash-Table abstraction, not as easy to instantiate as
|
120 |
|
|
-- Simple_HTable but designed to allow complete control over the
|
121 |
|
|
-- allocation of necessary data structures. Particularly useful when
|
122 |
|
|
-- dynamic allocation is not desired. The model is that each Element
|
123 |
|
|
-- contains its own Key that can be retrieved by Get_Key. Furthermore,
|
124 |
|
|
-- Element provides a link that can be used by the HTable for linking
|
125 |
|
|
-- elements with same hash codes:
|
126 |
|
|
|
127 |
|
|
-- Element
|
128 |
|
|
|
129 |
|
|
-- +-------------------+
|
130 |
|
|
-- | Key |
|
131 |
|
|
-- +-------------------+
|
132 |
|
|
-- : other data :
|
133 |
|
|
-- +-------------------+
|
134 |
|
|
-- | Next Elmt |
|
135 |
|
|
-- +-------------------+
|
136 |
|
|
|
137 |
|
|
generic
|
138 |
|
|
type Header_Num is range <>;
|
139 |
|
|
-- An integer type indicating the number and range of hash headers
|
140 |
|
|
|
141 |
|
|
type Element (<>) is limited private;
|
142 |
|
|
-- The type of element to be stored. This is historically part of the
|
143 |
|
|
-- interface, even though it is not used at all in the operations of
|
144 |
|
|
-- the package.
|
145 |
|
|
|
146 |
|
|
pragma Warnings (Off, Element);
|
147 |
|
|
-- We have to kill warnings here, because Element is and always
|
148 |
|
|
-- has been unreferenced, but we cannot remove it at this stage,
|
149 |
|
|
-- since this unit is in wide use, and it certainly seems harmless.
|
150 |
|
|
|
151 |
|
|
type Elmt_Ptr is private;
|
152 |
|
|
-- The type used to reference an element (will usually be an access
|
153 |
|
|
-- type, but could be some other form of type such as an integer type).
|
154 |
|
|
|
155 |
|
|
Null_Ptr : Elmt_Ptr;
|
156 |
|
|
-- The null value of the Elmt_Ptr type
|
157 |
|
|
|
158 |
|
|
with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
|
159 |
|
|
with function Next (E : Elmt_Ptr) return Elmt_Ptr;
|
160 |
|
|
-- The type must provide an internal link for the sake of the
|
161 |
|
|
-- staticness of the HTable.
|
162 |
|
|
|
163 |
|
|
type Key is limited private;
|
164 |
|
|
with function Get_Key (E : Elmt_Ptr) return Key;
|
165 |
|
|
with function Hash (F : Key) return Header_Num;
|
166 |
|
|
with function Equal (F1, F2 : Key) return Boolean;
|
167 |
|
|
|
168 |
|
|
package Static_HTable is
|
169 |
|
|
|
170 |
|
|
procedure Reset;
|
171 |
|
|
-- Resets the hash table by setting all its elements to Null_Ptr. The
|
172 |
|
|
-- effect is to clear the hash table so that it can be reused. For the
|
173 |
|
|
-- most common case where Elmt_Ptr is an access type, and Null_Ptr is
|
174 |
|
|
-- null, this is only needed if the same table is reused in a new
|
175 |
|
|
-- context. If Elmt_Ptr is other than an access type, or Null_Ptr is
|
176 |
|
|
-- other than null, then Reset must be called before the first use
|
177 |
|
|
-- of the hash table.
|
178 |
|
|
|
179 |
|
|
procedure Set (E : Elmt_Ptr);
|
180 |
|
|
-- Insert the element pointer in the HTable
|
181 |
|
|
|
182 |
|
|
function Get (K : Key) return Elmt_Ptr;
|
183 |
|
|
-- Returns the latest inserted element pointer with the given Key
|
184 |
|
|
-- or null if none.
|
185 |
|
|
|
186 |
|
|
function Present (K : Key) return Boolean;
|
187 |
|
|
-- True if an element whose Get_Key is K is in the table
|
188 |
|
|
|
189 |
|
|
function Set_If_Not_Present (E : Elmt_Ptr) return Boolean;
|
190 |
|
|
-- If Present (Get_Key (E)), returns False. Otherwise, does Set (E), and
|
191 |
|
|
-- then returns True. Present (Get_Key (E)) is always True afterward,
|
192 |
|
|
-- and the result True indicates E is newly Set.
|
193 |
|
|
|
194 |
|
|
procedure Remove (K : Key);
|
195 |
|
|
-- Removes the latest inserted element pointer associated with the
|
196 |
|
|
-- given key if any, does nothing if none.
|
197 |
|
|
|
198 |
|
|
function Get_First return Elmt_Ptr;
|
199 |
|
|
-- Returns Null_Ptr if the HTable is empty, otherwise returns one
|
200 |
|
|
-- non specified element. There is no guarantee that two calls to this
|
201 |
|
|
-- function will return the same element.
|
202 |
|
|
|
203 |
|
|
function Get_Next return Elmt_Ptr;
|
204 |
|
|
-- Returns a non-specified element that has not been returned by the
|
205 |
|
|
-- same function since the last call to Get_First or Null_Ptr if
|
206 |
|
|
-- there is no such element or Get_First has never been called. If
|
207 |
|
|
-- there is no call to 'Set' in between Get_Next calls, all the
|
208 |
|
|
-- elements of the HTable will be traversed.
|
209 |
|
|
|
210 |
|
|
end Static_HTable;
|
211 |
|
|
|
212 |
|
|
----------
|
213 |
|
|
-- Hash --
|
214 |
|
|
----------
|
215 |
|
|
|
216 |
|
|
-- A generic hashing function working on String keys
|
217 |
|
|
|
218 |
|
|
generic
|
219 |
|
|
type Header_Num is range <>;
|
220 |
|
|
function Hash (Key : String) return Header_Num;
|
221 |
|
|
|
222 |
|
|
end System.HTable;
|