1 |
281 |
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-2009, 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 2, 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. See the GNU General Public License --
|
17 |
|
|
-- for more details. You should have received a copy of the GNU General --
|
18 |
|
|
-- Public License distributed with GNAT; see file COPYING. If not, write --
|
19 |
|
|
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
|
20 |
|
|
-- Boston, MA 02110-1301, USA. --
|
21 |
|
|
-- --
|
22 |
|
|
-- As a special exception, if other files instantiate generics from this --
|
23 |
|
|
-- unit, or you link this unit with other files to produce an executable, --
|
24 |
|
|
-- this unit does not by itself cause the resulting executable to be --
|
25 |
|
|
-- covered by the GNU General Public License. This exception does not --
|
26 |
|
|
-- however invalidate any other reasons why the executable file might be --
|
27 |
|
|
-- covered by the GNU Public License. --
|
28 |
|
|
-- --
|
29 |
|
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
30 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
31 |
|
|
-- --
|
32 |
|
|
------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
-- Hash table searching routines
|
35 |
|
|
|
36 |
|
|
-- This package contains two separate packages. The Simple_HTable package
|
37 |
|
|
-- provides a very simple abstraction that associates one element to one
|
38 |
|
|
-- key value and takes care of all allocations automatically using the heap.
|
39 |
|
|
-- The Static_HTable package provides a more complex interface that allows
|
40 |
|
|
-- complete control over allocation.
|
41 |
|
|
|
42 |
|
|
pragma Compiler_Unit;
|
43 |
|
|
|
44 |
|
|
package System.HTable is
|
45 |
|
|
pragma Preelaborate;
|
46 |
|
|
|
47 |
|
|
-------------------
|
48 |
|
|
-- Simple_HTable --
|
49 |
|
|
-------------------
|
50 |
|
|
|
51 |
|
|
-- A simple hash table abstraction, easy to instantiate, easy to use.
|
52 |
|
|
-- The table associates one element to one key with the procedure Set.
|
53 |
|
|
-- Get retrieves the Element stored for a given Key. The efficiency of
|
54 |
|
|
-- retrieval is function of the size of the Table parameterized by
|
55 |
|
|
-- Header_Num and the hashing function Hash.
|
56 |
|
|
|
57 |
|
|
generic
|
58 |
|
|
type Header_Num is range <>;
|
59 |
|
|
-- An integer type indicating the number and range of hash headers
|
60 |
|
|
|
61 |
|
|
type Element is private;
|
62 |
|
|
-- The type of element to be stored
|
63 |
|
|
|
64 |
|
|
No_Element : Element;
|
65 |
|
|
-- The object that is returned by Get when no element has been set for
|
66 |
|
|
-- a given key
|
67 |
|
|
|
68 |
|
|
type Key is private;
|
69 |
|
|
with function Hash (F : Key) return Header_Num;
|
70 |
|
|
with function Equal (F1, F2 : Key) return Boolean;
|
71 |
|
|
|
72 |
|
|
package Simple_HTable is
|
73 |
|
|
|
74 |
|
|
procedure Set (K : Key; E : Element);
|
75 |
|
|
-- Associates an element with a given key. Overrides any previously
|
76 |
|
|
-- associated element.
|
77 |
|
|
|
78 |
|
|
procedure Reset;
|
79 |
|
|
-- Removes and frees all elements in the table
|
80 |
|
|
|
81 |
|
|
function Get (K : Key) return Element;
|
82 |
|
|
-- Returns the Element associated with a key or No_Element if the
|
83 |
|
|
-- given key has not associated element
|
84 |
|
|
|
85 |
|
|
procedure Remove (K : Key);
|
86 |
|
|
-- Removes the latest inserted element pointer associated with the
|
87 |
|
|
-- given key if any, does nothing if none.
|
88 |
|
|
|
89 |
|
|
function Get_First return Element;
|
90 |
|
|
-- Returns No_Element if the HTable is empty, otherwise returns one
|
91 |
|
|
-- non specified element. There is no guarantee that two calls to this
|
92 |
|
|
-- function will return the same element.
|
93 |
|
|
|
94 |
|
|
function Get_Next return Element;
|
95 |
|
|
-- Returns a non-specified element that has not been returned by the
|
96 |
|
|
-- same function since the last call to Get_First or No_Element if
|
97 |
|
|
-- there is no such element. If there is no call to 'Set' in between
|
98 |
|
|
-- Get_Next calls, all the elements of the HTable will be traversed.
|
99 |
|
|
end Simple_HTable;
|
100 |
|
|
|
101 |
|
|
-------------------
|
102 |
|
|
-- Static_HTable --
|
103 |
|
|
-------------------
|
104 |
|
|
|
105 |
|
|
-- A low-level Hash-Table abstraction, not as easy to instantiate as
|
106 |
|
|
-- Simple_HTable but designed to allow complete control over the
|
107 |
|
|
-- allocation of necessary data structures. Particularly useful when
|
108 |
|
|
-- dynamic allocation is not desired. The model is that each Element
|
109 |
|
|
-- contains its own Key that can be retrieved by Get_Key. Furthermore,
|
110 |
|
|
-- Element provides a link that can be used by the HTable for linking
|
111 |
|
|
-- elements with same hash codes:
|
112 |
|
|
|
113 |
|
|
-- Element
|
114 |
|
|
|
115 |
|
|
-- +-------------------+
|
116 |
|
|
-- | Key |
|
117 |
|
|
-- +-------------------+
|
118 |
|
|
-- : other data :
|
119 |
|
|
-- +-------------------+
|
120 |
|
|
-- | Next Elmt |
|
121 |
|
|
-- +-------------------+
|
122 |
|
|
|
123 |
|
|
generic
|
124 |
|
|
type Header_Num is range <>;
|
125 |
|
|
-- An integer type indicating the number and range of hash headers
|
126 |
|
|
|
127 |
|
|
type Element (<>) is limited private;
|
128 |
|
|
-- The type of element to be stored. This is historically part of the
|
129 |
|
|
-- interface, even though it is not used at all in the operations of
|
130 |
|
|
-- the package.
|
131 |
|
|
|
132 |
|
|
pragma Warnings (Off, Element);
|
133 |
|
|
-- We have to kill warnings here, because Element is and always
|
134 |
|
|
-- has been unreferenced, but we cannot remove it at this stage,
|
135 |
|
|
-- since this unit is in wide use, and it certainly seems harmless.
|
136 |
|
|
|
137 |
|
|
type Elmt_Ptr is private;
|
138 |
|
|
-- The type used to reference an element (will usually be an access
|
139 |
|
|
-- type, but could be some other form of type such as an integer type).
|
140 |
|
|
|
141 |
|
|
Null_Ptr : Elmt_Ptr;
|
142 |
|
|
-- The null value of the Elmt_Ptr type
|
143 |
|
|
|
144 |
|
|
with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
|
145 |
|
|
with function Next (E : Elmt_Ptr) return Elmt_Ptr;
|
146 |
|
|
-- The type must provide an internal link for the sake of the
|
147 |
|
|
-- staticness of the HTable.
|
148 |
|
|
|
149 |
|
|
type Key is limited private;
|
150 |
|
|
with function Get_Key (E : Elmt_Ptr) return Key;
|
151 |
|
|
with function Hash (F : Key) return Header_Num;
|
152 |
|
|
with function Equal (F1, F2 : Key) return Boolean;
|
153 |
|
|
|
154 |
|
|
package Static_HTable is
|
155 |
|
|
|
156 |
|
|
procedure Reset;
|
157 |
|
|
-- Resets the hash table by setting all its elements to Null_Ptr. The
|
158 |
|
|
-- effect is to clear the hash table so that it can be reused. For the
|
159 |
|
|
-- most common case where Elmt_Ptr is an access type, and Null_Ptr is
|
160 |
|
|
-- null, this is only needed if the same table is reused in a new
|
161 |
|
|
-- context. If Elmt_Ptr is other than an access type, or Null_Ptr is
|
162 |
|
|
-- other than null, then Reset must be called before the first use
|
163 |
|
|
-- of the hash table.
|
164 |
|
|
|
165 |
|
|
procedure Set (E : Elmt_Ptr);
|
166 |
|
|
-- Insert the element pointer in the HTable
|
167 |
|
|
|
168 |
|
|
function Get (K : Key) return Elmt_Ptr;
|
169 |
|
|
-- Returns the latest inserted element pointer with the given Key
|
170 |
|
|
-- or null if none.
|
171 |
|
|
|
172 |
|
|
procedure Remove (K : Key);
|
173 |
|
|
-- Removes the latest inserted element pointer associated with the
|
174 |
|
|
-- given key if any, does nothing if none.
|
175 |
|
|
|
176 |
|
|
function Get_First return Elmt_Ptr;
|
177 |
|
|
-- Returns Null_Ptr if the HTable is empty, otherwise returns one
|
178 |
|
|
-- non specified element. There is no guarantee that two calls to this
|
179 |
|
|
-- function will return the same element.
|
180 |
|
|
|
181 |
|
|
function Get_Next return Elmt_Ptr;
|
182 |
|
|
-- Returns a non-specified element that has not been returned by the
|
183 |
|
|
-- same function since the last call to Get_First or Null_Ptr if
|
184 |
|
|
-- there is no such element or Get_First has never been called. If
|
185 |
|
|
-- there is no call to 'Set' in between Get_Next calls, all the
|
186 |
|
|
-- elements of the HTable will be traversed.
|
187 |
|
|
|
188 |
|
|
end Static_HTable;
|
189 |
|
|
|
190 |
|
|
----------
|
191 |
|
|
-- Hash --
|
192 |
|
|
----------
|
193 |
|
|
|
194 |
|
|
-- A generic hashing function working on String keys
|
195 |
|
|
|
196 |
|
|
generic
|
197 |
|
|
type Header_Num is range <>;
|
198 |
|
|
function Hash (Key : String) return Header_Num;
|
199 |
|
|
|
200 |
|
|
end System.HTable;
|