| 1 |
------------------------------------------------------------------------------ |
| 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-2025, 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 |
package System.HTable is |
| 41 |
pragma Preelaborate; |
| 42 |
|
| 43 |
------------------- |
| 44 |
-- Simple_HTable -- |
| 45 |
------------------- |
| 46 |
|
| 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. |
| 52 |
|
| 53 |
generic |
| 54 |
type Header_Num is range <>; |
| 55 |
-- An integer type indicating the number and range of hash headers |
| 56 |
|
| 57 |
type Element is private; |
| 58 |
-- The type of element to be stored |
| 59 |
|
| 60 |
No_Element : Element; |
| 61 |
-- The object that is returned by Get when no element has been set for |
| 62 |
-- a given key. |
| 63 |
|
| 64 |
type Key is private; |
| 65 |
with function Hash (F : Key) return Header_Num; |
| 66 |
with function Equal (F1, F2 : Key) return Boolean; |
| 67 |
|
| 68 |
package Simple_HTable is |
| 69 |
|
| 70 |
procedure Set (K : Key; E : Element); |
| 71 |
-- Associates an element with a given key. Overrides any previously |
| 72 |
-- associated element. |
| 73 |
|
| 74 |
procedure Reset; |
| 75 |
-- Removes and frees all elements in the table |
| 76 |
|
| 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. |
| 80 |
|
| 81 |
procedure Remove (K : Key); |
| 82 |
-- Removes the latest inserted element pointer associated with the |
| 83 |
-- given key if any, does nothing if none. |
| 84 |
|
| 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. |
| 89 |
|
| 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. |
| 95 |
|
| 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. |
| 102 |
|
| 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. |
| 110 |
|
| 111 |
end Simple_HTable; |
| 112 |
|
| 113 |
------------------- |
| 114 |
-- Static_HTable -- |
| 115 |
------------------- |
| 116 |
|
| 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: |
| 124 |
|
| 125 |
-- Element |
| 126 |
|
| 127 |
-- +-------------------+ |
| 128 |
-- | Key | |
| 129 |
-- +-------------------+ |
| 130 |
-- : other data : |
| 131 |
-- +-------------------+ |
| 132 |
-- | Next Elmt | |
| 133 |
-- +-------------------+ |
| 134 |
|
| 135 |
generic |
| 136 |
type Header_Num is range <>; |
| 137 |
-- An integer type indicating the number and range of hash headers |
| 138 |
|
| 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 |
| 142 |
-- the package. |
| 143 |
|
| 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. |
| 148 |
|
| 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). |
| 152 |
|
| 153 |
Null_Ptr : Elmt_Ptr; |
| 154 |
-- The null value of the Elmt_Ptr type |
| 155 |
|
| 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. |
| 160 |
|
| 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; |
| 165 |
|
| 166 |
package Static_HTable is |
| 167 |
|
| 168 |
procedure Reset; |
| 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. |
| 176 |
|
| 177 |
procedure Set (E : Elmt_Ptr); |
| 178 |
-- Insert the element pointer in the HTable |
| 179 |
|
| 180 |
function Get (K : Key) return Elmt_Ptr; |
| 181 |
-- Returns the latest inserted element pointer with the given Key |
| 182 |
-- or null if none. |
| 183 |
|
| 184 |
function Present (K : Key) return Boolean; |
| 185 |
-- True if an element whose Get_Key is K is in the table |
| 186 |
|
| 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. |
| 191 |
|
| 192 |
procedure Remove (K : Key); |
| 193 |
-- Removes the latest inserted element pointer associated with the |
| 194 |
-- given key if any, does nothing if none. |
| 195 |
|
| 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. |
| 200 |
|
| 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. |
| 207 |
|
| 208 |
end Static_HTable; |
| 209 |
|
| 210 |
---------- |
| 211 |
-- Hash -- |
| 212 |
---------- |
| 213 |
|
| 214 |
-- A generic hashing function working on String keys |
| 215 |
|
| 216 |
generic |
| 217 |
type Header_Num is range <>; |
| 218 |
function Hash (Key : String) return Header_Num; |
| 219 |
|
| 220 |
end System.HTable; |