| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . P E R F E C T _ H A S H _ G E N E R A T O R S -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2002-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 |
-- This package provides a generator of static minimal perfect hash functions. |
| 33 |
-- To understand what a perfect hash function is, we define several notions. |
| 34 |
-- These definitions are inspired from the following paper: |
| 35 |
|
| 36 |
-- Zbigniew J. Czech, George Havas, and Bohdan S. Majewski ``An Optimal |
| 37 |
-- Algorithm for Generating Minimal Perfect Hash Functions'', Information |
| 38 |
-- Processing Letters, 43(1992) pp.257-264, Oct.1992 |
| 39 |
|
| 40 |
-- Let W be a set of m words. A hash function h is a function that maps the |
| 41 |
-- set of words W into some given interval I of integers [0, k-1], where k is |
| 42 |
-- an integer, usually k >= m. h (w) where w is a word in W computes an |
| 43 |
-- address or an integer from I for the storage or the retrieval of that |
| 44 |
-- item. The storage area used to store items is known as a hash table. Words |
| 45 |
-- for which the same address is computed are called synonyms. Due to the |
| 46 |
-- existence of synonyms a situation called collision may arise in which two |
| 47 |
-- items w1 and w2 have the same address. Several schemes for resolving |
| 48 |
-- collisions are known. A perfect hash function is an injection from the word |
| 49 |
-- set W to the integer interval I with k >= m. If k = m, then h is a minimal |
| 50 |
-- perfect hash function. A hash function is order preserving if it puts |
| 51 |
-- entries into the hash table in a prespecified order. |
| 52 |
|
| 53 |
-- A minimal perfect hash function is defined by two properties: |
| 54 |
|
| 55 |
-- Since no collisions occur each item can be retrieved from the table in |
| 56 |
-- *one* probe. This represents the "perfect" property. |
| 57 |
|
| 58 |
-- The hash table size corresponds to the exact size of W and *no larger*. |
| 59 |
-- This represents the "minimal" property. |
| 60 |
|
| 61 |
-- The functions generated by this package require the words to be known in |
| 62 |
-- advance (they are "static" hash functions). The hash functions are also |
| 63 |
-- order preserving. If w2 is inserted after w1 in the generator, then h (w1) |
| 64 |
-- < h (w2). These hashing functions are convenient for use with realtime |
| 65 |
-- applications. |
| 66 |
|
| 67 |
package System.Perfect_Hash_Generators is |
| 68 |
|
| 69 |
type Optimization is (Memory_Space, CPU_Time); |
| 70 |
-- Optimize either the memory space or the execution time. Note: in |
| 71 |
-- practice, the optimization mode has little effect on speed. The tables |
| 72 |
-- are somewhat smaller with Memory_Space. |
| 73 |
|
| 74 |
Verbose : Boolean := False; |
| 75 |
-- Output the status of the algorithm. For instance, the tables, the random |
| 76 |
-- graph (edges, vertices) and selected char positions are output between |
| 77 |
-- two iterations. |
| 78 |
|
| 79 |
procedure Initialize |
| 80 |
(Seed : Natural; |
| 81 |
V : Positive; |
| 82 |
Optim : Optimization; |
| 83 |
Tries : Positive); |
| 84 |
-- Initialize the generator and its internal structures. Set the number of |
| 85 |
-- vertices in the random graphs. This value has to be greater than twice |
| 86 |
-- the number of keys in order for the algorithm to succeed. The word set |
| 87 |
-- is not modified (in particular when it is already set). For instance, it |
| 88 |
-- is possible to run several times the generator with different settings |
| 89 |
-- on the same words. |
| 90 |
-- |
| 91 |
-- A classical way of doing is to Insert all the words and then to invoke |
| 92 |
-- Initialize and Compute. If this fails to find a perfect hash function, |
| 93 |
-- invoke Initialize again with other configuration parameters (probably |
| 94 |
-- with a greater number of vertices). Once successful, invoke Define and |
| 95 |
-- Value, and then Finalize. |
| 96 |
|
| 97 |
procedure Finalize; |
| 98 |
-- Deallocate the internal structures and the words table |
| 99 |
|
| 100 |
procedure Insert (Value : String); |
| 101 |
-- Insert a new word into the table. ASCII.NUL characters are not allowed. |
| 102 |
|
| 103 |
Too_Many_Tries : exception; |
| 104 |
-- Raised after Tries unsuccessful runs |
| 105 |
|
| 106 |
procedure Compute (Position : String); |
| 107 |
-- Compute the hash function. Position allows the definition of selection |
| 108 |
-- of character positions used in the word hash function. Positions can be |
| 109 |
-- separated by commas and ranges like x-y may be used. Character '$' |
| 110 |
-- represents the final character of a word. With an empty position, the |
| 111 |
-- generator automatically produces positions to reduce the memory usage. |
| 112 |
-- Raise Too_Many_Tries if the algorithm does not succeed within Tries |
| 113 |
-- attempts (see Initialize). |
| 114 |
|
| 115 |
-- The procedure Define returns the lengths of an internal table and its |
| 116 |
-- item type size. The function Value returns the value of each item in |
| 117 |
-- the table. Together they can be used to retrieve the parameters of the |
| 118 |
-- hash function which has been computed by a call to Compute. |
| 119 |
|
| 120 |
-- The hash function has the following form: |
| 121 |
|
| 122 |
-- h (w) = (g (f1 (w)) + g (f2 (w))) mod m |
| 123 |
|
| 124 |
-- G is a function based on a graph table [0,n-1] -> [0,m-1]. m is the |
| 125 |
-- number of keys. n is an internally computed value and it can be obtained |
| 126 |
-- as the length of vector G. |
| 127 |
|
| 128 |
-- F1 and F2 are two functions based on two function tables T1 and T2. |
| 129 |
-- Their definition depends on the chosen optimization mode. |
| 130 |
|
| 131 |
-- Only some character positions are used in the words because they are |
| 132 |
-- significant. They are listed in a character position table (P in the |
| 133 |
-- pseudo-code below). For instance, in {"jan", "feb", "mar", "apr", "jun", |
| 134 |
-- "jul", "aug", "sep", "oct", "nov", "dec"}, only positions 2 and 3 are |
| 135 |
-- significant (the first character can be ignored). In this example, P = |
| 136 |
-- {2, 3} |
| 137 |
|
| 138 |
-- When Optimization is CPU_Time, the first dimension of T1 and T2 |
| 139 |
-- corresponds to the character position in the word and the second to the |
| 140 |
-- character set. As all the character set is not used, we define a used |
| 141 |
-- character table which associates a distinct index to each used character |
| 142 |
-- (unused characters are mapped to zero). In this case, the second |
| 143 |
-- dimension of T1 and T2 is reduced to the used character set (C in the |
| 144 |
-- pseudo-code below). Therefore, the hash function has the following: |
| 145 |
|
| 146 |
-- function Hash (S : String) return Natural is |
| 147 |
-- F : constant Natural := S'First - 1; |
| 148 |
-- L : constant Natural := S'Length; |
| 149 |
-- F1, F2 : Natural := 0; |
| 150 |
-- J : <t>; |
| 151 |
|
| 152 |
-- begin |
| 153 |
-- for K in P'Range loop |
| 154 |
-- exit when L < P (K); |
| 155 |
-- J := C (S (P (K) + F)); |
| 156 |
-- F1 := (F1 + Natural (T1 (K, J))) mod <n>; |
| 157 |
-- F2 := (F2 + Natural (T2 (K, J))) mod <n>; |
| 158 |
-- end loop; |
| 159 |
|
| 160 |
-- return (Natural (G (F1)) + Natural (G (F2))) mod <m>; |
| 161 |
-- end Hash; |
| 162 |
|
| 163 |
-- When Optimization is Memory_Space, the first dimension of T1 and T2 |
| 164 |
-- corresponds to the character position in the word and the second |
| 165 |
-- dimension is ignored. T1 and T2 are no longer matrices but vectors. |
| 166 |
-- Therefore, the used character table is not available. The hash function |
| 167 |
-- has the following form: |
| 168 |
|
| 169 |
-- function Hash (S : String) return Natural is |
| 170 |
-- F : constant Natural := S'First - 1; |
| 171 |
-- L : constant Natural := S'Length; |
| 172 |
-- F1, F2 : Natural := 0; |
| 173 |
-- J : <t>; |
| 174 |
|
| 175 |
-- begin |
| 176 |
-- for K in P'Range loop |
| 177 |
-- exit when L < P (K); |
| 178 |
-- J := Character'Pos (S (P (K) + F)); |
| 179 |
-- F1 := (F1 + Natural (T1 (K) * J)) mod <n>; |
| 180 |
-- F2 := (F2 + Natural (T2 (K) * J)) mod <n>; |
| 181 |
-- end loop; |
| 182 |
|
| 183 |
-- return (Natural (G (F1)) + Natural (G (F2))) mod <m>; |
| 184 |
-- end Hash; |
| 185 |
|
| 186 |
type Table_Name is |
| 187 |
(Character_Position, |
| 188 |
Used_Character_Set, |
| 189 |
Function_Table_1, |
| 190 |
Function_Table_2, |
| 191 |
Graph_Table); |
| 192 |
|
| 193 |
procedure Define |
| 194 |
(Name : Table_Name; |
| 195 |
Item_Size : out Natural; |
| 196 |
Length_1 : out Natural; |
| 197 |
Length_2 : out Natural); |
| 198 |
-- Return the definition of the table Name. This includes the length of |
| 199 |
-- dimensions 1 and 2 and the size of an unsigned integer item. When |
| 200 |
-- Length_2 is zero, the table has only one dimension. All the ranges |
| 201 |
-- start from zero. |
| 202 |
|
| 203 |
function Value |
| 204 |
(Name : Table_Name; |
| 205 |
J : Natural; |
| 206 |
K : Natural := 0) return Natural; |
| 207 |
-- Return the value of the component (J, K) of the table Name. When the |
| 208 |
-- table has only one dimension, K is ignored. |
| 209 |
|
| 210 |
end System.Perfect_Hash_Generators; |