annotate gcc/ada/libgnat/g-pehage.ads @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . P E R F E C T _ H A S H _ G E N E R A T O R S --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 2002-2017, AdaCore --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This package provides a generator of static minimal perfect hash functions.
kono
parents:
diff changeset
33 -- To understand what a perfect hash function is, we define several notions.
kono
parents:
diff changeset
34 -- These definitions are inspired from the following paper:
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 -- Zbigniew J. Czech, George Havas, and Bohdan S. Majewski ``An Optimal
kono
parents:
diff changeset
37 -- Algorithm for Generating Minimal Perfect Hash Functions'', Information
kono
parents:
diff changeset
38 -- Processing Letters, 43(1992) pp.257-264, Oct.1992
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 -- Let W be a set of m words. A hash function h is a function that maps the
kono
parents:
diff changeset
41 -- set of words W into some given interval I of integers [0, k-1], where k is
kono
parents:
diff changeset
42 -- an integer, usually k >= m. h (w) where w is a word in W computes an
kono
parents:
diff changeset
43 -- address or an integer from I for the storage or the retrieval of that
kono
parents:
diff changeset
44 -- item. The storage area used to store items is known as a hash table. Words
kono
parents:
diff changeset
45 -- for which the same address is computed are called synonyms. Due to the
kono
parents:
diff changeset
46 -- existence of synonyms a situation called collision may arise in which two
kono
parents:
diff changeset
47 -- items w1 and w2 have the same address. Several schemes for resolving
kono
parents:
diff changeset
48 -- collisions are known. A perfect hash function is an injection from the word
kono
parents:
diff changeset
49 -- set W to the integer interval I with k >= m. If k = m, then h is a minimal
kono
parents:
diff changeset
50 -- perfect hash function. A hash function is order preserving if it puts
kono
parents:
diff changeset
51 -- entries into the hash table in a prespecified order.
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 -- A minimal perfect hash function is defined by two properties:
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 -- Since no collisions occur each item can be retrieved from the table in
kono
parents:
diff changeset
56 -- *one* probe. This represents the "perfect" property.
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 -- The hash table size corresponds to the exact size of W and *no larger*.
kono
parents:
diff changeset
59 -- This represents the "minimal" property.
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 -- The functions generated by this package require the words to be known in
kono
parents:
diff changeset
62 -- advance (they are "static" hash functions). The hash functions are also
kono
parents:
diff changeset
63 -- order preserving. If w2 is inserted after w1 in the generator, then h (w1)
kono
parents:
diff changeset
64 -- < h (w2). These hashing functions are convenient for use with realtime
kono
parents:
diff changeset
65 -- applications.
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 package GNAT.Perfect_Hash_Generators is
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 Default_K_To_V : constant Float := 2.05;
kono
parents:
diff changeset
70 -- Default ratio for the algorithm. When K is the number of keys, V =
kono
parents:
diff changeset
71 -- (K_To_V) * K is the size of the main table of the hash function. To
kono
parents:
diff changeset
72 -- converge, the algorithm requires K_To_V to be strictly greater than 2.0.
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 Default_Pkg_Name : constant String := "Perfect_Hash";
kono
parents:
diff changeset
75 -- Default package name in which the hash function is defined
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 Default_Position : constant String := "";
kono
parents:
diff changeset
78 -- The generator allows selection of the character positions used in the
kono
parents:
diff changeset
79 -- hash function. By default, all positions are selected.
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 Default_Tries : constant Positive := 20;
kono
parents:
diff changeset
82 -- This algorithm may not succeed to find a possible mapping on the first
kono
parents:
diff changeset
83 -- try and may have to iterate a number of times. This constant bounds the
kono
parents:
diff changeset
84 -- number of tries.
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 type Optimization is (Memory_Space, CPU_Time);
kono
parents:
diff changeset
87 -- Optimize either the memory space or the execution time. Note: in
kono
parents:
diff changeset
88 -- practice, the optimization mode has little effect on speed. The tables
kono
parents:
diff changeset
89 -- are somewhat smaller with Memory_Space.
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 Verbose : Boolean := False;
kono
parents:
diff changeset
92 -- Output the status of the algorithm. For instance, the tables, the random
kono
parents:
diff changeset
93 -- graph (edges, vertices) and selected char positions are output between
kono
parents:
diff changeset
94 -- two iterations.
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 procedure Initialize
kono
parents:
diff changeset
97 (Seed : Natural;
kono
parents:
diff changeset
98 K_To_V : Float := Default_K_To_V;
kono
parents:
diff changeset
99 Optim : Optimization := Memory_Space;
kono
parents:
diff changeset
100 Tries : Positive := Default_Tries);
kono
parents:
diff changeset
101 -- Initialize the generator and its internal structures. Set the ratio of
kono
parents:
diff changeset
102 -- vertices over keys in the random graphs. This value has to be greater
kono
parents:
diff changeset
103 -- than 2.0 in order for the algorithm to succeed. The word set is not
kono
parents:
diff changeset
104 -- modified (in particular when it is already set). For instance, it is
kono
parents:
diff changeset
105 -- possible to run several times the generator with different settings on
kono
parents:
diff changeset
106 -- the same words.
kono
parents:
diff changeset
107 --
kono
parents:
diff changeset
108 -- A classical way of doing is to Insert all the words and then to invoke
kono
parents:
diff changeset
109 -- Initialize and Compute. If Compute fails to find a perfect hash
kono
parents:
diff changeset
110 -- function, invoke Initialize another time with other configuration
kono
parents:
diff changeset
111 -- parameters (probably with a greater K_To_V ratio). Once successful,
kono
parents:
diff changeset
112 -- invoke Produce and Finalize.
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 procedure Finalize;
kono
parents:
diff changeset
115 -- Deallocate the internal structures and the words table
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 procedure Insert (Value : String);
kono
parents:
diff changeset
118 -- Insert a new word into the table. ASCII.NUL characters are not allowed.
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 Too_Many_Tries : exception;
kono
parents:
diff changeset
121 -- Raised after Tries unsuccessful runs
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 procedure Compute (Position : String := Default_Position);
kono
parents:
diff changeset
124 -- Compute the hash function. Position allows the definition of selection
kono
parents:
diff changeset
125 -- of character positions used in the word hash function. Positions can be
kono
parents:
diff changeset
126 -- separated by commas and ranges like x-y may be used. Character '$'
kono
parents:
diff changeset
127 -- represents the final character of a word. With an empty position, the
kono
parents:
diff changeset
128 -- generator automatically produces positions to reduce the memory usage.
kono
parents:
diff changeset
129 -- Raise Too_Many_Tries if the algorithm does not succeed within Tries
kono
parents:
diff changeset
130 -- attempts (see Initialize).
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 procedure Produce
kono
parents:
diff changeset
133 (Pkg_Name : String := Default_Pkg_Name;
kono
parents:
diff changeset
134 Use_Stdout : Boolean := False);
kono
parents:
diff changeset
135 -- Generate the hash function package Pkg_Name. This package includes the
kono
parents:
diff changeset
136 -- minimal perfect Hash function. The output is normally placed in the
kono
parents:
diff changeset
137 -- current directory, in files X.ads and X.adb, where X is the standard
kono
parents:
diff changeset
138 -- GNAT file name for a package named Pkg_Name. If Use_Stdout is True, the
kono
parents:
diff changeset
139 -- output goes to standard output, and no files are written.
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 ----------------------------------------------------------------
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 -- The routines and structures defined below allow producing the hash
kono
parents:
diff changeset
144 -- function using a different way from the procedure above. The procedure
kono
parents:
diff changeset
145 -- Define returns the lengths of an internal table and its item type size.
kono
parents:
diff changeset
146 -- The function Value returns the value of each item in the table.
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 -- The hash function has the following form:
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 -- h (w) = (g (f1 (w)) + g (f2 (w))) mod m
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 -- G is a function based on a graph table [0,n-1] -> [0,m-1]. m is the
kono
parents:
diff changeset
153 -- number of keys. n is an internally computed value and it can be obtained
kono
parents:
diff changeset
154 -- as the length of vector G.
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 -- F1 and F2 are two functions based on two function tables T1 and T2.
kono
parents:
diff changeset
157 -- Their definition depends on the chosen optimization mode.
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 -- Only some character positions are used in the words because they are
kono
parents:
diff changeset
160 -- significant. They are listed in a character position table (P in the
kono
parents:
diff changeset
161 -- pseudo-code below). For instance, in {"jan", "feb", "mar", "apr", "jun",
kono
parents:
diff changeset
162 -- "jul", "aug", "sep", "oct", "nov", "dec"}, only positions 2 and 3 are
kono
parents:
diff changeset
163 -- significant (the first character can be ignored). In this example, P =
kono
parents:
diff changeset
164 -- {2, 3}
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 -- When Optimization is CPU_Time, the first dimension of T1 and T2
kono
parents:
diff changeset
167 -- corresponds to the character position in the word and the second to the
kono
parents:
diff changeset
168 -- character set. As all the character set is not used, we define a used
kono
parents:
diff changeset
169 -- character table which associates a distinct index to each used character
kono
parents:
diff changeset
170 -- (unused characters are mapped to zero). In this case, the second
kono
parents:
diff changeset
171 -- dimension of T1 and T2 is reduced to the used character set (C in the
kono
parents:
diff changeset
172 -- pseudo-code below). Therefore, the hash function has the following:
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 -- function Hash (S : String) return Natural is
kono
parents:
diff changeset
175 -- F : constant Natural := S'First - 1;
kono
parents:
diff changeset
176 -- L : constant Natural := S'Length;
kono
parents:
diff changeset
177 -- F1, F2 : Natural := 0;
kono
parents:
diff changeset
178 -- J : <t>;
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 -- begin
kono
parents:
diff changeset
181 -- for K in P'Range loop
kono
parents:
diff changeset
182 -- exit when L < P (K);
kono
parents:
diff changeset
183 -- J := C (S (P (K) + F));
kono
parents:
diff changeset
184 -- F1 := (F1 + Natural (T1 (K, J))) mod <n>;
kono
parents:
diff changeset
185 -- F2 := (F2 + Natural (T2 (K, J))) mod <n>;
kono
parents:
diff changeset
186 -- end loop;
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 -- return (Natural (G (F1)) + Natural (G (F2))) mod <m>;
kono
parents:
diff changeset
189 -- end Hash;
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 -- When Optimization is Memory_Space, the first dimension of T1 and T2
kono
parents:
diff changeset
192 -- corresponds to the character position in the word and the second
kono
parents:
diff changeset
193 -- dimension is ignored. T1 and T2 are no longer matrices but vectors.
kono
parents:
diff changeset
194 -- Therefore, the used character table is not available. The hash function
kono
parents:
diff changeset
195 -- has the following form:
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 -- function Hash (S : String) return Natural is
kono
parents:
diff changeset
198 -- F : constant Natural := S'First - 1;
kono
parents:
diff changeset
199 -- L : constant Natural := S'Length;
kono
parents:
diff changeset
200 -- F1, F2 : Natural := 0;
kono
parents:
diff changeset
201 -- J : <t>;
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 -- begin
kono
parents:
diff changeset
204 -- for K in P'Range loop
kono
parents:
diff changeset
205 -- exit when L < P (K);
kono
parents:
diff changeset
206 -- J := Character'Pos (S (P (K) + F));
kono
parents:
diff changeset
207 -- F1 := (F1 + Natural (T1 (K) * J)) mod <n>;
kono
parents:
diff changeset
208 -- F2 := (F2 + Natural (T2 (K) * J)) mod <n>;
kono
parents:
diff changeset
209 -- end loop;
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 -- return (Natural (G (F1)) + Natural (G (F2))) mod <m>;
kono
parents:
diff changeset
212 -- end Hash;
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 type Table_Name is
kono
parents:
diff changeset
215 (Character_Position,
kono
parents:
diff changeset
216 Used_Character_Set,
kono
parents:
diff changeset
217 Function_Table_1,
kono
parents:
diff changeset
218 Function_Table_2,
kono
parents:
diff changeset
219 Graph_Table);
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 procedure Define
kono
parents:
diff changeset
222 (Name : Table_Name;
kono
parents:
diff changeset
223 Item_Size : out Natural;
kono
parents:
diff changeset
224 Length_1 : out Natural;
kono
parents:
diff changeset
225 Length_2 : out Natural);
kono
parents:
diff changeset
226 -- Return the definition of the table Name. This includes the length of
kono
parents:
diff changeset
227 -- dimensions 1 and 2 and the size of an unsigned integer item. When
kono
parents:
diff changeset
228 -- Length_2 is zero, the table has only one dimension. All the ranges
kono
parents:
diff changeset
229 -- start from zero.
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 function Value
kono
parents:
diff changeset
232 (Name : Table_Name;
kono
parents:
diff changeset
233 J : Natural;
kono
parents:
diff changeset
234 K : Natural := 0) return Natural;
kono
parents:
diff changeset
235 -- Return the value of the component (I, J) of the table Name. When the
kono
parents:
diff changeset
236 -- table has only one dimension, J is ignored.
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 end GNAT.Perfect_Hash_Generators;